| 1 | #include <cassert>
 | 
|---|
| 2 | #include <fstream>
 | 
|---|
| 3 | #include <iostream>
 | 
|---|
| 4 | 
 | 
|---|
| 5 | #include <boost/serialization/serialization.hpp>
 | 
|---|
| 6 | #include <boost/archive/xml_oarchive.hpp>
 | 
|---|
| 7 | #include <boost/archive/xml_iarchive.hpp>
 | 
|---|
| 8 | #include <boost/serialization/export.hpp>
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include <boost/serialization/nvp.hpp>
 | 
|---|
| 11 | 
 | 
|---|
| 12 | struct A {
 | 
|---|
| 13 |     virtual ~A() {}
 | 
|---|
| 14 |     virtual void foo() {}
 | 
|---|
| 15 |     friend class boost::serialization::access;
 | 
|---|
| 16 |     template<class Archive>
 | 
|---|
| 17 |     void serialize(Archive &/*ar*/, const unsigned int /*version*/) {
 | 
|---|
| 18 |     }
 | 
|---|
| 19 | };
 | 
|---|
| 20 | 
 | 
|---|
| 21 | struct B : virtual A {
 | 
|---|
| 22 |     friend class boost::serialization::access;
 | 
|---|
| 23 |     template<class Archive>
 | 
|---|
| 24 |     void serialize(Archive &ar, const unsigned int /*version*/) {
 | 
|---|
| 25 |         ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A);
 | 
|---|
| 26 |     }
 | 
|---|
| 27 | };
 | 
|---|
| 28 | 
 | 
|---|
| 29 | struct C : B {
 | 
|---|
| 30 |     virtual void foo() { std::cout << "C" << std::endl; }
 | 
|---|
| 31 |     friend class boost::serialization::access;
 | 
|---|
| 32 |     template<class Archive>
 | 
|---|
| 33 |     void serialize(Archive &ar, const unsigned int /*version*/) {
 | 
|---|
| 34 |         ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(B);
 | 
|---|
| 35 |     }
 | 
|---|
| 36 | };
 | 
|---|
| 37 | 
 | 
|---|
| 38 | BOOST_CLASS_EXPORT(C)
 | 
|---|
| 39 | 
 | 
|---|
| 40 | int main()
 | 
|---|
| 41 | {
 | 
|---|
| 42 |     {
 | 
|---|
| 43 |         std::ofstream ofs("outtarget.xml");
 | 
|---|
| 44 |         boost::archive::xml_oarchive oa(ofs);
 | 
|---|
| 45 |         assert(ofs);
 | 
|---|
| 46 |         A* pA = new C;
 | 
|---|
| 47 |         oa << boost::serialization::make_nvp("A", pA);
 | 
|---|
| 48 |     }
 | 
|---|
| 49 |     {
 | 
|---|
| 50 |         std::ifstream ifs("outtarget.xml");
 | 
|---|
| 51 |         assert(ifs);
 | 
|---|
| 52 |         boost::archive::xml_iarchive ia(ifs);
 | 
|---|
| 53 |         A* pA;
 | 
|---|
| 54 |         ia >> boost::serialization::make_nvp("A", pA);
 | 
|---|
| 55 |         pA->foo();
 | 
|---|
| 56 |     }
 | 
|---|
| 57 | }
 | 
|---|
| 58 | 
 | 
|---|