How can boost::serialization be used with std::shared_ptr from C++11?
I finally found a solution on how to serialize the std::shared_ptr using boost serialization. All you need is the following piece of code (explanation follows):
#include <boost/serialization/split_free.hpp>
#include <boost/unordered_map.hpp>
//---/ Wrapper for std::shared_ptr<> /------------------------------------------
namespace boost { namespace serialization {
template<class Archive, class Type>
void save(Archive & archive, const std::shared_ptr<Type> & value, const unsigned int /*version*/)
{
Type *data = value.get();
archive << data;
}
template<class Archive, class Type>
void load(Archive & archive, std::shared_ptr<Type> & value, const unsigned int /*version*/)
{
Type *data;
archive >> data;
typedef std::weak_ptr<Type> WeakPtr;
static boost::unordered_map<void*, WeakPtr> hash;
if (hash[data].expired())
{
value = std::shared_ptr<Type>(data);
hash[data] = value;
}
else value = hash[data].lock();
}
template<class Archive, class Type>
inline void serialize(Archive & archive, std::shared_ptr<Type> & value, const unsigned int version)
{
split_free(archive, value, version);
}
}}
This code simply serializes the object managed by the std::shared_ptr in the function save(). If multiple std::shared_ptr instances point to same object boost serialization will take automatically care to store it only once. The magic happens in load() where boost serialization returns a raw pointer to the object (data). This raw pointer is looked up in a hash that holds a weak_ptr for each raw pointer. In case that the weak_ptr in the hash is expired we can safely create a new shared_ptr instance, let it manage the raw pointer and store a weak_ptr in the hash. In case that the weak_ptr is not expired we simply lock it to return a shared_ptr. This way the reference counting is correct.
As of Boost 1.56, the serialization library has built-in support for std::shared_ptr. You do not need to implement your own serialization helper functions if you can use a more recent version of the library.
Serialisation is provided by boost and not by the standard library and although shared_ptr
is included in the standard it is part of TR1 (technical report 1).
TR1 as of now does not have serialization. So I would recommend that you use boost's shared pointer.