#include "stdafx.h" #include #include #include #include #include #include #include #include #include #include class TestBase { public: TestBase() : m_baseInt(111) {} virtual ~TestBase() {} virtual const char* get_key() const = 0; private: friend class boost::serialization::access; template void serialize(Archive& ar, const unsigned int) { ar & boost::serialization::make_nvp("BaseInt", m_baseInt); } int m_baseInt; }; BOOST_SERIALIZATION_ASSUME_ABSTRACT(TestBase) BOOST_CLASS_TYPE_INFO(TestBase, extended_type_info_no_rtti) BOOST_CLASS_EXPORT(TestBase) class TestType1 : public TestBase { public: TestType1() : m_int(666), m_float(777.f) {} virtual const char* get_key() const; private: friend class boost::serialization::access; template void serialize(Archive& ar, const unsigned int version) { ar & boost::serialization::make_nvp("TestBase", boost::serialization::base_object(*this)); ar & boost::serialization::make_nvp("Type1Int", m_int); ar & boost::serialization::make_nvp("Type1Float", m_float); } int m_int; float m_float; }; BOOST_CLASS_TYPE_INFO(TestType1, extended_type_info_no_rtti) BOOST_CLASS_EXPORT(TestType1) const char* TestType1::get_key() const { return boost::serialization::type_info_implementation::type::get_const_instance().get_key(); } class TestType2 : public TestBase { public: TestType2() : m_int(222), m_float(333.f) {} virtual const char* get_key() const; private: friend class boost::serialization::access; template void serialize(Archive& ar, const unsigned int version) { ar & boost::serialization::make_nvp("TestBase", boost::serialization::base_object(*this)); ar & boost::serialization::make_nvp("Type2Int", m_int); ar & boost::serialization::make_nvp("Type2Float", m_float); } int m_int; float m_float; }; BOOST_CLASS_TYPE_INFO(TestType2, extended_type_info_no_rtti) BOOST_CLASS_EXPORT(TestType2) const char* TestType2::get_key() const { return boost::serialization::type_info_implementation::type::get_const_instance().get_key(); } int _tmain(int argc, _TCHAR* argv[]) { //Create new file std::ofstream testTypeOFS("TestType.xml"); boost::archive::xml_oarchive testOutXML(testTypeOFS); TestType1* pT1 = new TestType1; TestType2* pT2 = new TestType2; testOutXML << boost::serialization::make_nvp("TestType1", pT1); testOutXML << boost::serialization::make_nvp("TestType2", pT2); testTypeOFS.close(); delete pT1; delete pT2; //Load the file back in to extract a derived type. std::ifstream testTypeIFS("TestType.xml"); boost::archive::xml_iarchive testInXML(testTypeIFS); TestBase* pTB = null; try { testInXML >> boost::serialization::make_nvp("TestBase", pTB); } catch(boost::archive::archive_exception& ex) { std::cout << "Archive Exception: " << ex.code << "-" << ex.what() << std::endl; char exitNotice; std::cin >> exitNotice; } testTypeIFS.close(); return 0; }