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:13:28


Joaquin M Lopez Munoz wrote:
>
>
> The following might be one possible approach to your envisioned
> scenario using the technique proposed by Scott. There is one
> significant difference with the way TextureFactory is defined,
> namely that construct is required to return a Texture (your
> version does the construction using placement new).

I only chose the placement new because flyweight already has an
allocator template parameter. If allocation became part of the factory
then I think more cool option occur like polymorphic flyweights where a
key constructs a derived type via the factory but the flyweight and
calling code only knows about the base type.

> Complete sample follows:
>
>
> typedef flyweight<key_value<KeyAndFactory,LoadedTexture> >
> TextureFlyweight;
>
> int main()
> {
> Log log1,log2;
> TextureFactory factory1(log1),factory2(log2);
>
> TextureFlyweight fw1("hello",factory1);
> TextureFlyweight fw2("hello",factory2);
> TextureFlyweight fw3("bye",factory1);
> const Texture& t1=fw1;
>
> assert(fw1==fw2);
> assert(fw1!=fw3);
> }
>
>
> Is this more similar to what you have in mind?
>

That is certainly a lot closer. I didn't know that flyweights could have
multiple construction parameters. I didn't see any examples of that in
the docs, it seems like a pretty cool feature you might show off somewhere.

-- 
Michael Marcin

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