Boost logo

Boost :

Subject: Re: [boost] [flyweight] key value with objects not constructible from a key
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2008-11-12 17:16:03


Michael Marcin <mike.marcin <at> gmail.com> writes:

> Joaquin M Lopez Munoz wrote:
> > OK, please help me make out a more complete picture of
> > the use case you've got in mind. Imagine you have a flyweight
> > type that behaves the way you need/want, let's call it
> > texture_flyweight. The associated key is std::string and the
> > associated value is texture. Now, you write the following:
> >
> > texture_flyweight fw("wood.texture");
> >
> > If an equivalent flyweight already exists then there is no
> > problem, fw will be assigned the same texture object, this is
> > how Boost.Flyweight currently works. Now, imagine that
> > no equivalent texture exists, and yet you can't construct
> > a texture object from "wood.texture" alone. How would you
> > have then the above statement behave?
> >
>
> It would go to the factory's construct method which might look like:
>
> void TextureFactory::construct( void* address, const std::string& filename )
> {
> // complex work involving lots of 3rd party libraries
> // happens here
> GLuint glTextureName = LoadImage( filename );
>
> // texture stays simple
> new(address) Texture( glTextureName, filename );
> }

This can be covered in a manner similar to the one described
in my reply to Scott's post:

  struct Texture
  {
    Texture(GLuint,const std::string&);
    ...
  };

  struct LoadedTexture:Texture
  {
    LoadedTexture(const std::string& filename):
      Texture(LoadImage(filename),filename)
    {}
  };

  typedef flyweight<key_value<std::string,LoadedTexture> >
  TextureFlyweight;

Does this address your needs?

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk