tags:

views:

321

answers:

2

A lot of different screens in my app refer to the same entity/business objects over and over again.
Currently, each screen refers to their own copy of each object.
Also, entity objects may themselves expose access to other entity objects, again new copies of objects are created.
I'm trying to find a caching solution.

I'm looking for something similar to boost::flyweight.
However, based on immutable key/mutable value and reference counted.

boost::flyweight<key_value<long, SomeObject>, tag<SomeObject> > object;

The above is almost perfect. I'm looking for a similar container that will give mutable access to SomeObject

Edit: I like the flyweight's syntax and semantics. However, flyweight only allows const SomeObject& access, no chance to modify the object.

Edit2: Code has to compile on MSVC++6

Any ideas?

A: 

As long as you are happy affecting intrinsic state, then from the internals in boost/flyweight/key_value.hpp it looks like you can get away with a const_cast. If you have your own key extractor you should ensure it doesn't vary with the operations that making x mutable will expose it to.

flyweight<key_value<long, SomeObject> > kvfw(2);
SomeObject &x = const_cast<SomeObject &>(static_cast<const SomeObject&>(kvfw));
p00ya
I have to compile on MSVC++6, so hit a snag with boost. I should've put that as a requirement.
Chris Bednarski
This works quite well. I hid the casts in operator-> and oprator*
Chris Bednarski
A: 

I think if you make flyweights mutable, then they cannot be legally called flyweights. Imagine a situation where glyphs are represented as flyweights. What will happen if one function changes the codepoint of the glyph that represents the letter 'A'? Another function which render the glyphs on screen, will try to draw 'A' and the user might end up seeing 'B' or something else! I think you need immutable keys referring to mutable objects. Then, think of using a hash table coupled with some reference counting mechanism.

Vijay Mathew
I do get your point. Although, isn't that what flyweights do? Provide a static hash table of reference counted objects? I like the syntax of flyweights so I would like to find something that is similar.
Chris Bednarski