I have a complex network of objects connected with QSharedPointers and QWeakPointers. Is there a simple way to save/load them with Boost.Serialization? So far I have this:
namespace boost {
namespace serialization {
template<class Archive, class T>
void save(Archive& ar, QSharedPointer<T> const& ptr, unsigned version) {
T* sharedPointer = ptr.data();
ar & BOOST_SERIALIZATION_NVP(sharedPointer);
}
template<class Archive, class T>
void load(Archive& ar, QSharedPointer<T>& ptr, unsigned version) {
T* sharedPointer = 0;
ar & BOOST_SERIALIZATION_NVP(sharedPointer);
ptr = QSharedPointer<T>(sharedPointer);
}
template<class Archive, class T>
void save(Archive& ar, QWeakPointer<T> const& ptr, unsigned version) {
T* weakPointer = ptr.toStrongRef().data();
ar & BOOST_SERIALIZATION_NVP(weakPointer);
}
template<class Archive, class T>
void load(Archive& ar, QWeakPointer<T>& ptr, unsigned version) {
T* weakPointer = 0;
ar & BOOST_SERIALIZATION_NVP(weakPointer);
ptr = QSharedPointer<T>(weakPointer);
}
}
}
This is not working because the shared pointers are always constructed from raw pointers so they all think the reference count is 1. It also immediately frees weak pointers.
With some effort I can convert the classes to use boost::shared_ptr
and boost::weak_ptr
. Will that do any good?