Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] object tracking for shared strings
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-12-16 19:55:50


Nils Krumnack wrote:
> Hi all,
> At first glance, object tracking seems to be the way to go. If my
> strings were tracked, there would automatically be only one copy of
> each string. However I can't quite figure out how. I don't really
> want to change the global tracking behavior for std::string, since
> that might end up affecting code all over the place.

> I was looking for something like that in the documentation, but wasn't
> very successful. Any ideas?

your instinct is correct. It's just that std::string is special - (its the
only "special" type with respect to boost serialization). It is
marked "primitive" so it will never be tracked.

For you're situation the solution is to:

a) make your own string type:

struct my_string : public std::string {
    template<class Archive>
    void serialization(Archive & ar, const unsigned int version){
        ar & base_object<std::string>(*this);
    }
};

BOOST_SERIALIZATION_TRACKING(my_string, track_always).

Now you have as special string that wil be the same is std::string
except that this one will be tracked during serialization. Note that
that keeps normal strings separate from these "special" strings
so you don't have to worry about having wierd surprises in
other parts of your executable.

If a lot of your strings are duplicated, I think boot::flyweight
might bear looking into.

Robert Ramey


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net