1 | #include <boost/archive/binary_oarchive.hpp>
|
---|
2 | #include <boost/archive/binary_iarchive.hpp>
|
---|
3 | #include <boost/serialization/split_member.hpp>
|
---|
4 | #include <boost/serialization/serialization.hpp>
|
---|
5 | #include <cstdio>
|
---|
6 | #include <fstream>
|
---|
7 |
|
---|
8 | struct I {
|
---|
9 | virtual void m() = 0;
|
---|
10 | template<class Archive> void serialize(Archive& ar, int) { }
|
---|
11 | };
|
---|
12 |
|
---|
13 | struct J {
|
---|
14 | virtual void m() = 0;
|
---|
15 | template<class Archive> void serialize(Archive& ar, int) { }
|
---|
16 | };
|
---|
17 |
|
---|
18 | struct D;
|
---|
19 |
|
---|
20 | struct C {
|
---|
21 | int v;
|
---|
22 | I* i;
|
---|
23 | J* j;
|
---|
24 | D* d;
|
---|
25 | template<class Archive> void serialize(Archive& ar, int) { ar & v
|
---|
26 | & i
|
---|
27 | & j
|
---|
28 | & d; }
|
---|
29 | };
|
---|
30 |
|
---|
31 | struct D {
|
---|
32 | int v;
|
---|
33 | J* j;
|
---|
34 | C* c;
|
---|
35 | template<class Archive> void serialize(Archive& ar, int) { ar & v
|
---|
36 | & j
|
---|
37 | & c; }
|
---|
38 | };
|
---|
39 |
|
---|
40 | struct E : C, I {
|
---|
41 | E(int) { i = this; }
|
---|
42 | void m() { }
|
---|
43 | template<class Archive> void serialize(Archive& ar, int)
|
---|
44 | { ar & boost::serialization::base_object<C>(*this)
|
---|
45 | & boost::serialization::base_object<I>(*this); }
|
---|
46 | };
|
---|
47 |
|
---|
48 | struct A {
|
---|
49 | A(int foo) : e(foo) { }
|
---|
50 | void m();
|
---|
51 | int v;
|
---|
52 | E e;
|
---|
53 | template<class Archive> void serialize(Archive& ar, int) { ar & v
|
---|
54 | & e; }
|
---|
55 | };
|
---|
56 |
|
---|
57 | struct B : J {
|
---|
58 | B(int) { d.j = this; }
|
---|
59 | void m() { }
|
---|
60 | int v;
|
---|
61 | D d;
|
---|
62 | template<class Archive> void serialize(Archive& ar, int)
|
---|
63 | { ar & boost::serialization::base_object<J>(*this)
|
---|
64 | & v
|
---|
65 | & d; }
|
---|
66 | };
|
---|
67 |
|
---|
68 | struct T {
|
---|
69 | T(int foo) : a(foo), b(foo) {
|
---|
70 | a.e.j = &b;
|
---|
71 | a.e.d = &b.d;
|
---|
72 | b.d.c = &a.e;
|
---|
73 | }
|
---|
74 | A a;
|
---|
75 | B b;
|
---|
76 | template<class Archive>
|
---|
77 | void serialize(Archive& ar, int) { ar & a & b; }
|
---|
78 | };
|
---|
79 |
|
---|
80 | template<class T>
|
---|
81 | void store(const T& v) {
|
---|
82 | std::ofstream ofs("filename");
|
---|
83 | boost::archive::binary_oarchive oa(ofs);
|
---|
84 | oa << v;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int main(int, char**)
|
---|
88 | {
|
---|
89 | T t(0);
|
---|
90 | store(t);
|
---|
91 | return 0;
|
---|
92 | }
|
---|
93 |
|
---|