Boost logo

Boost :

From: Christian Larsen (contact_at_[hidden])
Date: 2008-09-02 15:55:56


JOAQUIN M. LOPEZ MUÑOZ skrev:
> I've tried to address most of the suggestions and comments
> raised during the review. The two most important changes
> are:
> 1. Alberto Barbati's insightful review has led to the
> introduction of so-called key-value flyweights
> (http://tinyurl.com/5mnwjp ). This type of flyweights is
> meant for the situations where constructing the underlying
> type is expensive and must be avoided except if absolutely
> necessary. I think the design is robust enough and covers
> the variants (inner/outer key) that arose during the
> discussion with Alberto.

I just read the start of the tutorial, and I like what I see. I haven't
tried using the library yet, though. But I have a question regarding the
last assignment in the "Key extractors" section. Maybe I just missed
something, so please enlighten me.

It's nice that the following construct will avoid creating a texture if
it already exists:

   flyweight<key_value<std::string,texture> > fw("grass.texture");

But in the example with the texture_filename_extractor, it seems to me,
that a texture is still fully constructed before the assignment. From
the documentation
(http://svn.boost.org/svn/boost/sandbox/flyweight/libs/flyweight/doc/tutorial/key_value.html):

   struct texture_filename_extractor {...};
   flyweight<key_value<std::string,texture,
             texture_filename_extractor> > fw;
   ...
   fw=texture("sand.texture"); // OK now

I get it that this is to show the need for key extractors, but isn't it
possible to also assign without creating already existing textures? I
got a little confused, because the previous section dealt with improving
performance by avoiding unnecessary construction, and here it occurs to
me that there is one.

Wouldn't the optimal way to perform this assignment be something like:

   typedef flyweight<key_value<std::string,texture,
                     texture_filename_extractor> > texture_fw;
   texture_fw fw;
   ...
   fw=texture_fw("sand.texture"); // No unnecessary construction?

If this is the case, I think it would improve the documentation if a
note was added after the example that this is the best way to do an
assignment from a "new" texture. Please correct me if I'm wrong.

Best regards,
Christian


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