#include #include #include #include #include #include #include #include struct Base1 { int m_x; Base1(){} Base1(int x) : m_x(1 + x) {} virtual ~Base1() {} bool operator==(Base1 & rhs) const { return m_x == rhs.m_x; } // serialize friend class boost::serialization::access; template void serialize(Archive & /*ar*/, const unsigned int /* file_version */) {} }; //BOOST_CLASS_EXPORT(Base1) struct Base2 { int m_x; Base2(){} Base2(int x) : m_x(2 + x) {} virtual ~Base2() {} bool operator==(Base2 & rhs) const { return m_x == rhs.m_x; } // serialize friend class boost::serialization::access; template void serialize(Archive & /*ar*/, const unsigned int /* file_version */) {} }; //BOOST_CLASS_EXPORT(Base2) struct Sub:public Base1, public Base2 { int m_x; boost::weak_ptr m_wp; Sub(){} Sub(int x) : Base1(x), Base2(x), m_x(x) {} bool operator==(Sub & rhs) const { if(! Base2::operator==(rhs)) return false; if(! Base1::operator==(rhs)) return false; return m_x == rhs.m_x; } virtual ~Sub() {} // serialize friend class boost::serialization::access; template void serialize(Archive &ar, const unsigned int /* file_version */) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base1); ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base2); ar & BOOST_SERIALIZATION_NVP(m_x); ar & BOOST_SERIALIZATION_NVP(m_wp); // *1 } }; BOOST_CLASS_EXPORT(Sub) int main() { boost::shared_ptr pb2; { // serialize boost::shared_ptr s(new Sub(2)); s->m_wp = s; // set weak_ptr. If not set, deserialize success. pb2 = boost::shared_ptr(s); std::ofstream ofs("output.xml"); assert(ofs); boost::archive::xml_oarchive oa(ofs); oa << boost::serialization::make_nvp("Base2", pb2); } boost::shared_ptr pb2_1; { // de-serialize std::ifstream ifs("output.xml"); assert(ifs); boost::archive::xml_iarchive ia(ifs); ia >> boost::serialization::make_nvp("Base2", pb2_1); } assert(*dynamic_cast(pb2.get()) == *dynamic_cast(pb2_1.get())); }