#include #include #include #include #include #include #include #include #include #include #include #include #include #include class base { protected : std::vector vec; protected : friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(vec); } public : void add(int i) { vec.push_back(i); } int get(int idx) { return vec[idx]; } }; class derived : public base { friend class boost::serialization::access; std::map hmap; template void serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::make_nvp("base", boost::serialization::base_object(*this)); //ar & BOOST_SERIALIZATION_NVP(boost::serialization::base_object(*this)); ar & BOOST_SERIALIZATION_NVP(hmap); } public : void add(std::string str, int i) { hmap[str] = i; base::add(i); } int get(std::string str) { return hmap[str]; } }; int main(int argc, char* argv[]) { try { derived A; A.add("First", 12); A.add("Second", 34); const derived AA = A; std::ofstream ofs("Test.xml"); assert(ofs.good()); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(AA); ofs.close(); derived B; std::ifstream ifs("Test.xml"); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(B); ifs.close(); std::cout << B.get("Second") << std::endl; } catch (boost::archive::xml_archive_exception ex) { std::cerr << " X " << ex.what() << std::endl; } catch (boost::archive::archive_exception ex) { std::cerr << " A " << ex.what() << std::endl; } catch (std::exception ex) { std::cerr << " E " << ex.what() << std::endl; } catch (...) { std::cerr << "Unknown Exception " << std::endl; } return 0; } /* // SerialTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Serializer.h" #include #include #include #include #include using namespace std; #include #include #include #include #include #include #include #include #include #include int _tmain(int argc, _TCHAR* argv[]) { try { derived A; A.add("First", 12); A.add("Second", 34); const derived B = A; Serializer< derived >::serialize_xml(A, "UT.xml"); derived C; Serializer< derived >::deserialize_xml(C, "UT.xml"); cout << C.get("First") << " - " << C.get("Second"); } catch (boost::archive::xml_archive_exception ex) { cerr << " X " << ex.what() << endl; } catch (boost::archive::archive_exception ex) { cerr << " A " << ex.what() << endl; } catch (exception ex) { cerr << " E " << ex.what() << endl; } catch (...) { cerr << "Unknown Exception " << endl; } return 0; } */