#include #include #include #include #include #include #include #include #include #include #include class Left; typedef boost::shared_ptr LeftSp; class Right; typedef boost::shared_ptr RightSp; class Bottom; typedef boost::shared_ptr BottomSp; typedef boost::weak_ptr BottomWp; class Left { public: Left() {} virtual ~Left() {} private: // serialize friend class boost::serialization::access; template void serialize(Archive & /*ar*/, const unsigned int /* file_version */) {} }; BOOST_CLASS_EXPORT(Left) class Right { public: Right() {} virtual ~Right() {} private: // serialize friend class boost::serialization::access; template void serialize(Archive & /*ar*/, const unsigned int /* file_version */) {} }; BOOST_CLASS_EXPORT(Right) class Bottom:public Left, public Right { public: virtual ~Bottom() {} static BottomSp create() { BottomSp sp(new Bottom); sp->setWp(sp); return sp; } private: BottomWp wp_; Bottom() {} void setWp(BottomSp sp) { wp_ = sp; } // serialize friend class boost::serialization::access; template void save(Archive &ar, const unsigned int /* file_version */) const { ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(Left); ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(Right); ar << BOOST_SERIALIZATION_NVP(wp_); // *1 } template void load(Archive & ar, const unsigned int /* file_version */) { ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(Left); ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(Right); ar >> BOOST_SERIALIZATION_NVP(wp_); // *2 } BOOST_SERIALIZATION_SPLIT_MEMBER() }; BOOST_CLASS_EXPORT(Bottom) class Holder { public: LeftSp mbl_; // shared_ptr RightSp mbr_; // shared_ptr Holder() {} // for serialize Holder(BottomSp l, BottomSp r):mbl_(l) ,mbr_(r) {} // for appli construct // serialize friend class boost::serialization::access; template void save(Archive &ar, const unsigned int /* file_version */) const { // polymophic serialize via shared_ptr ar << BOOST_SERIALIZATION_NVP(mbl_); // polymophic serialize via shared_ptr ar << BOOST_SERIALIZATION_NVP(mbr_); } template void load(Archive & ar, const unsigned int /* file_version */) { // polymophic de-serialize via shared_ptr ar >> BOOST_SERIALIZATION_NVP(mbl_); // polymophic de-serialize via shared_ptr ar >> BOOST_SERIALIZATION_NVP(mbr_); } BOOST_SERIALIZATION_SPLIT_MEMBER() }; BOOST_CLASS_EXPORT(Holder) int main() { { // serialize Holder h1(Bottom::create(), Bottom::create()); std::ofstream ofs("output.xml"); assert(ofs); boost::archive::xml_oarchive oa(ofs); oa << boost::serialization::make_nvp("Holder", h1); } { // de-serialize Holder h2; std::ifstream ifs("output.xml"); assert(ifs); boost::archive::xml_iarchive ia(ifs); ia >> boost::serialization::make_nvp("Holder", h2); // check Left *pLeft = h2.mbl_.get(); Bottom *pBottomFromLeft = dynamic_cast(pLeft); Right *pRight = h2.mbr_.get(); Bottom *pBottomFromRight = dynamic_cast(pRight); assert(pBottomFromLeft); // Success assert(pBottomFromRight); // Fail. Comment out *1 and *2, it works corrctly. } } // output.xml // // // // // // // // // // // // // // // // // // // // // // // // // // // // //