
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