Boost logo

Boost :

Subject: Re: [boost] [flyweight] key value with objects not constructible from a key
From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2008-11-12 18:01:53


Joaquin M Lopez Munoz wrote:
> Michael Marcin <mike.marcin <at> gmail.com> writes:
>> But loaded_texture's can't share any state or be initialized with state
>> other than the key.
> [...]
>
> OK, then we're back to my original question: how do yo expect
> the following statement to behave
>
> texture_flyweight fw("wood.texture");
>
> when no previous equivalent flyweight object exists? Please
> note that I'm not fooling around your problem, I understand
> it but we have the crucial point that *all* the information
> necessary to construct the value should be available at
> the point of flyweight construction. This is not AFAICS a
> particular limitation of Boost.Flyweight, but just the way
> things are. This is why I'm inviting you to come up with
> a suitable creation process (regardless of whether it is
> implementable with B.F or not) so that I can see how you
> expect the value construction information to be provided.
> Just sketch how your ideal flyweight library should behave
> under these circumstances and we'll see whether B.F is up to
> the task or not.

The information does exist, in the custom factory.

I'm going to move closer to real code but still try to keep all of our
project's nasty details out of the picture.

int main()
{
    // we use a lot of logging, its a complex project
    Log logger( "log.txt" );

    // all the game's assets are actually stored in an archive
    // direct file access is not enough to load images.
    ZipArchive zip( "game.zip" );

    typedef std::auto_ptr<TextureFactory> TextureFactoryPtr;
    TextureFactoryPtr textureFactory( new TextureFactory(zip,logger) );

    // we don't want to have to recompile all code that uses textures
    // just to to add or remove support for an image format
    textureFactory->AddImageLoader( ".png", new PngLoader );
    textureFactory->AddImageLoader( ".jpeg", new JpegLoader );

    // convenient access function to custom holder which holds a pointer
    // to a TextureFactory
    get_holder<texture_flyweight>()->Initialize(textureFactory.get());

    {
       // calls into TextureFactory to see if the object already exists
       // this is what flyweight's factory concept currently supports
       // TextureFactory models the current factory as well as providing
       // construction
       //
       // it doesnt exist so it calls into TextureFactory construct
       // which uses PngLoader to get a GLuint
       // it uses the GLuint and the filename (to support key extraction)
       // to construct a Texture
       texture_flyweight fw("wood.png");
    }

    // holder's TextureFactory pointer set to null, not strictly
    // necessary but allows us to flag erroneous usage of
    // texture_flyweights during shutdown
    get_holder<texture_flyweight>()->Destroy();

    // all texture_flyweights should be destroyed by this point,
    // TextureFactory's destructor can enforce this
    // with an assertion perhaps
    textureFactory.reset();
}

-- 
Michael Marcin

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