Boost logo

Boost :

From: Michael Marcin (mmarcin_at_[hidden])
Date: 2008-02-12 12:36:23

Unfortunately I didn't have time to participate in the review of
flyweight but I did follow it. Now that it has been accepted I'm looking
into using it as a replacement for my homebrew mechanism.

We currently have a Factory which we pass a hash id (which happens to be
the hash of the lowercase filename using Bob Jenkin's lookup3 hash). The
Factory then looks to the Cache to see if the object is already cached
(i.e. exists in its collection which happens to be a boost::ptr_map of
hash ids to the shared Resource). If its not in the Cache we pass the
hash to the Repository which uses it to lookup and load the resource
from an archive on disk. We then return the reference to the Cache's
resource as a boost::cref<Resource>.

Memory is very constrained and performance is important. We don't use
reference counting but have designed the application so that there are
places we can safely clear all the caches with no dangling references. I
see that there is a no_tracking policy which allows us to skip reference
counting. Is there a way to clear all Flyweight factories when using the
no_tracking policy (the equivalent of our Caches IIUC)? A bonus would be
to easily switch to the refcounted tracking policy and assert or throw
when we try to clear all the factories if there are any outstanding
references as a debugging aid.

I tried to replace a simple part of the application that is using the
homebrew mechanism with Boost.Flyweight and I ran into some problems.

The current code looks like:

std::auto_ptr<SoundInstance> CreateSound( unsigned int soundId )
    const Sound& sound =
    return std::auto_ptr<SoundInstance>( new SoundInstance(sound) );

I would think the new code would look like:

std::auto_ptr<SoundInstance> CreateSound( unsigned int soundId )
    boost::flyweight<Sound> sound( soundId );
    return std::auto_ptr<SoundInstance>( new SoundInstance(sound) );

I need to create a factory that takes Resource hashes and loads
resources which it caches in an unordered_ptr_set and then returns a
reference to that cached resource. I then need to be able to clear the
cached resources of the factory explicitly.

I would like to store boost::flyweight<Resource> instead of const
Resource& but I need to make sure that boost::flyweight<Resource> is the
same size as a pointer to the Resource given proper policies (since the
memory requirements are stringent).

Is this possible with the current version? Is it a reasonable to apply
flyweight in this manner?


Michael Marcin

Boost list run by bdawes at, gregod at, cpdaniel at, john at