#include #include #include #include #include #include #define SAVING 1 #define EXPORT_ALIASES 0 #if SAVING namespace NS { #endif struct Base { virtual ~Base() {} template void serialize (Archive &ar, const unsigned int) {} }; struct Derived0 : Base { template void serialize (Archive &ar, const unsigned int) { ar & boost::serialization::base_object(*this) & m_data_0 & m_data_1; } int m_data_0; double m_data_1; }; struct Derived1 : Base { std::vector m_data; template void serialize (Archive &ar, const unsigned int) { ar & boost::serialization::base_object(*this) & m_data; } }; #if SAVING } #endif #if SAVING BOOST_CLASS_EXPORT(NS::Derived0) BOOST_CLASS_EXPORT(NS::Derived1) #else BOOST_CLASS_EXPORT(Derived0) BOOST_CLASS_EXPORT(Derived1) #if EXPORT_ALIASES BOOST_CLASS_EXPORT_ALIAS(Derived0, "NS::Derived0"); BOOST_CLASS_EXPORT_ALIAS(Derived1, "NS::Derived1"); #endif #endif int main() { #if SAVING using namespace NS; #endif Derived0 *original_derived_0 = new Derived0; Derived1 *original_derived_1 = new Derived1; original_derived_0->m_data_0 = 1; original_derived_0->m_data_1 = 2.0; original_derived_1->m_data.push_back("foo"); original_derived_1->m_data.push_back("bar"); Base * const base_ptr_to_original_derived_0 = original_derived_0; Base * const base_ptr_to_original_derived_1 = original_derived_1; #if SAVING { std::ofstream ofs("test_archive.bin", std::ios_base::out | std::ios_base::binary); boost::archive::binary_oarchive oa(ofs); oa << base_ptr_to_original_derived_0 << base_ptr_to_original_derived_1; } #else Base *base_ptr_to_new_derived_0 = 0; Base *base_ptr_to_new_derived_1 = 0; { std::ifstream ifs("test_archive.bin", std::ios_base::in | std::ios_base::binary); boost::archive::binary_iarchive ia(ifs); ia >> base_ptr_to_new_derived_0 >> base_ptr_to_new_derived_1; } assert(base_ptr_to_new_derived_0); assert(base_ptr_to_new_derived_1); Derived0 *new_derived_0 = dynamic_cast(base_ptr_to_new_derived_0); Derived1 *new_derived_1 = dynamic_cast(base_ptr_to_new_derived_1); assert(new_derived_0); assert(new_derived_1); assert(new_derived_0->m_data_0 == original_derived_0->m_data_0); assert(new_derived_0->m_data_1 == original_derived_0->m_data_1); assert(std::equal(new_derived_1->m_data.begin(), new_derived_1->m_data.end(), original_derived_1->m_data.begin())); std::cout << "Success." << std::endl; #endif return 0; }