Boost logo

Boost :

From: martin.ecker_at_[hidden]
Date: 2004-05-21 05:03:08

Gennadiy Rozental wrote:

> You could use simple map<void*,void*> that mapr raw pointer to an
> address of shred_ptr

We're using a similar approach in our current version of
boost::serialization. Our solution currently stores a
raw_ptr-to-shared_ptr<void> map in the input archive object.

With this shared_ptr registry in the input archive our non-intrusive
serialization code for shared_ptr looks like this (the code for
weak_ptr is more or less the same):

template<class Archive, class T>
inline void save(
    Archive & ar,
    const boost::shared_ptr<T> &t,
    const unsigned int /* file_version */
        ar << boost::serialization::make_nvp("px", t.get());

template<class Archive, class T>
inline void load(
    Archive & ar,
    boost::shared_ptr<T> &t,
    const unsigned int /* file_version */
        T* r;
        ar >> boost::serialization::make_nvp("px", r);
        t = ar.get_shared_ptr_registry().lookup(r);
        if (!t)

template<class Archive, class T>
inline void serialize(
    Archive & ar,
    boost::shared_ptr<T> &t,
    const unsigned int file_version
    boost::serialization::split_free(ar, t, file_version);

So, upon deserialization, when an object pointed to by a shared_ptr is
loaded for the first time the shared_ptr instance is registered in the
shared_ptr registry of the archive (where it is stored as
shared_ptr<void> to allow shared_ptrs that only point to the base
class to be retrieved correctly from the registry). In all other
cases, the previously deserialized shared_ptr instance is used to
create the shared_ptr instance that is currently being deserialized.

Note that the lookup function uses a (hackky) reinterpret_cast to
convert from the shared_ptr<void> which is stored in the registry to
the actually requested shared_ptr<T> type. In practice, this seems to
work and the reference count is maintained correctly, but there
might be a better solution.

Also note that the situation posted by Peter Dimov with multiple
inheritance does not work with this solution. Single inheritance
should work fine, though.

Martin Ecker

TAB Austria
Industrie- und Unterhaltungselektronik GmbH & CoKG

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