1 | #include <cassert>
|
---|
2 | #include <fstream>
|
---|
3 |
|
---|
4 | #include <boost/serialization/serialization.hpp>
|
---|
5 | #include <boost/archive/xml_oarchive.hpp>
|
---|
6 | #include <boost/archive/xml_iarchive.hpp>
|
---|
7 | #include <boost/serialization/export.hpp>
|
---|
8 |
|
---|
9 | #include <boost/serialization/nvp.hpp>
|
---|
10 |
|
---|
11 | struct VBase;
|
---|
12 |
|
---|
13 | struct VBase {
|
---|
14 | virtual ~VBase() {}
|
---|
15 | friend class boost::serialization::access;
|
---|
16 | template<class Archive>
|
---|
17 | void serialize(Archive &/*ar*/, const unsigned int /*version*/) {
|
---|
18 | }
|
---|
19 | };
|
---|
20 |
|
---|
21 | BOOST_CLASS_TRACKING(VBase, boost::serialization::track_always)
|
---|
22 |
|
---|
23 |
|
---|
24 | struct Mid1 : virtual VBase {
|
---|
25 | friend class boost::serialization::access;
|
---|
26 | template<class Archive>
|
---|
27 | void serialize(Archive &ar, const unsigned int /*version*/) {
|
---|
28 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(VBase);
|
---|
29 | }
|
---|
30 | };
|
---|
31 |
|
---|
32 | struct Mid2 : virtual VBase {
|
---|
33 | friend class boost::serialization::access;
|
---|
34 | template<class Archive>
|
---|
35 | void serialize(Archive &ar, const unsigned int /*version*/) {
|
---|
36 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(VBase);
|
---|
37 | }
|
---|
38 | };
|
---|
39 |
|
---|
40 | struct Sub1 : Mid1, Mid2 {
|
---|
41 | friend class boost::serialization::access;
|
---|
42 | template<class Archive>
|
---|
43 | void serialize(Archive &ar, const unsigned int /*version*/) {
|
---|
44 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Mid1);
|
---|
45 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Mid2);
|
---|
46 | }
|
---|
47 | };
|
---|
48 |
|
---|
49 | struct Sub2 : Mid1, Mid2 {
|
---|
50 | friend class boost::serialization::access;
|
---|
51 | template<class Archive>
|
---|
52 | void serialize(Archive &ar, const unsigned int /*version*/) {
|
---|
53 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Mid1);
|
---|
54 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Mid2);
|
---|
55 | }
|
---|
56 | };
|
---|
57 |
|
---|
58 | struct Target : Sub2 {
|
---|
59 | friend class boost::serialization::access;
|
---|
60 | template<class Archive>
|
---|
61 | void serialize(Archive &ar, const unsigned int /*version*/) {
|
---|
62 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Sub2);
|
---|
63 | }
|
---|
64 | };
|
---|
65 |
|
---|
66 | #if 0
|
---|
67 | // OK
|
---|
68 | BOOST_CLASS_EXPORT(Target)
|
---|
69 | BOOST_CLASS_EXPORT(Sub1)
|
---|
70 | #else
|
---|
71 | // NG
|
---|
72 | BOOST_CLASS_EXPORT(Sub1)
|
---|
73 | BOOST_CLASS_EXPORT(Target)
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | int main()
|
---|
77 | {
|
---|
78 | // Serialize pTarget via VBase pointer.
|
---|
79 | {
|
---|
80 | std::ofstream ofs("outtarget.xml");
|
---|
81 | boost::archive::xml_oarchive oa(ofs);
|
---|
82 | assert(ofs);
|
---|
83 | Target* pTarget = new Target;
|
---|
84 | VBase * pVBase = pTarget;
|
---|
85 | oa << boost::serialization::make_nvp("pVBase", pVBase);
|
---|
86 | }
|
---|
87 | {
|
---|
88 | std::ifstream ifs("outtarget.xml");
|
---|
89 | assert(ifs);
|
---|
90 | boost::archive::xml_iarchive ia(ifs);
|
---|
91 | VBase * pVBase;
|
---|
92 | ia >> boost::serialization::make_nvp("pVBase", pVBase);
|
---|
93 | }
|
---|
94 | // Serialize pSub1 via VBase pointer.
|
---|
95 | {
|
---|
96 | std::ofstream ofs("outsub1.xml");
|
---|
97 | boost::archive::xml_oarchive oa(ofs);
|
---|
98 | assert(ofs);
|
---|
99 | Sub1* pSub1 = new Sub1;
|
---|
100 | VBase * pVBase = pSub1;
|
---|
101 | oa << boost::serialization::make_nvp("pVBase", pVBase);
|
---|
102 | }
|
---|
103 | {
|
---|
104 | std::ifstream ifs("outsub1.xml");
|
---|
105 | assert(ifs);
|
---|
106 | boost::archive::xml_iarchive ia(ifs);
|
---|
107 | VBase * pVBase;
|
---|
108 | ia >> boost::serialization::make_nvp("pVBase", pVBase);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|