|
Serialization
|
shared_ptr
illustrates the straightforward way of serializing a moderately complicated class structure.
Unfortunately, this way of doing it suffered from some undesirable features
shared_ptr
.
The shared_ptr
interface has been included
in std::tr1
and may someday be included in the standard
C++ library. An implementation which depends only on the public interface can be guaranteed to
function with any other future implementation of shared_ptr
.
template<class Archive, class T>
inline void save(
Archive & ar,
const boost::shared_ptr<T> &t,
const unsigned int /* file_version */
){
const T * t_ptr = t.get();
// just serialize the underlying raw pointer
ar <<: boost::serialization::make_nvp("px", t_ptr);
}
template<class Archive, class T>
inline void load(
Archive & ar,
boost::shared_ptr<T> &t,
const unsigned int file_version
){
T* r;
// recover the underlying raw pointer
ar >> boost::serialization::make_nvp("px", r);
// To Do - match up with other shared pointers which
// use this same raw pointer.
...
}
In principle, this is very much simpler than the original implementation. Completion of
this code requires:
shared_ptr
instances.
weak_ptr
.
boost::serialization::shared_ptr.hpp
Note that if your code needs to read archives created under boost version 1.32, you will have to include the following
#include <boost/serialization/shared_ptr_132.hpp>
#include <boost/serialization/shared_ptr.hpp>
rather than just
#include <boost/serialization/shared_ptr.hpp>
© Copyright Robert Ramey 2002-2004. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)