1 | #include <fstream>
|
---|
2 |
|
---|
3 | #include <boost/archive/binary_iarchive.hpp>
|
---|
4 | #include <boost/archive/binary_oarchive.hpp>
|
---|
5 | #include <boost/serialization/vector.hpp>
|
---|
6 | #include <boost/serialization/shared_ptr.hpp>
|
---|
7 | #include <boost/serialization/nvp.hpp>
|
---|
8 |
|
---|
9 | typedef std::vector<int> B;
|
---|
10 |
|
---|
11 | class A;
|
---|
12 |
|
---|
13 | typedef boost::shared_ptr<A> a_ptr;
|
---|
14 | typedef boost::shared_ptr<B> b_ptr;
|
---|
15 |
|
---|
16 | class A{
|
---|
17 | public:
|
---|
18 | A(){}
|
---|
19 | A(b_ptr b) : b_(b){}
|
---|
20 | virtual ~A(){}
|
---|
21 | b_ptr b_;
|
---|
22 |
|
---|
23 | friend class boost::serialization::access;
|
---|
24 | template<class Archive>
|
---|
25 | void serialize(Archive & ar, const unsigned int version){
|
---|
26 | ar & BOOST_SERIALIZATION_NVP(b_);
|
---|
27 | }
|
---|
28 | };
|
---|
29 |
|
---|
30 | class DA : public A{
|
---|
31 | public:
|
---|
32 | DA(){}
|
---|
33 | DA(b_ptr b) : A(b){}
|
---|
34 | template<class Archive>
|
---|
35 | void serialize(Archive & ar, const unsigned int version){
|
---|
36 | ar & boost::serialization::make_nvp("base", boost::serialization::base_object<A>(*this));
|
---|
37 | }
|
---|
38 | };
|
---|
39 |
|
---|
40 |
|
---|
41 | typedef std::vector<a_ptr> VA;
|
---|
42 | typedef boost::shared_ptr<VA> va_ptr;
|
---|
43 |
|
---|
44 | typedef std::vector<va_ptr> VVA;
|
---|
45 | typedef boost::shared_ptr<VVA> vva_ptr;
|
---|
46 |
|
---|
47 | template <typename Archive>
|
---|
48 | void register_types(Archive& ar){
|
---|
49 | ar.template register_type<DA>();
|
---|
50 | }
|
---|
51 |
|
---|
52 | b_ptr make_b(){
|
---|
53 | return b_ptr(new B);
|
---|
54 | }
|
---|
55 |
|
---|
56 | a_ptr make_a(){
|
---|
57 | return a_ptr(new DA(make_b()));
|
---|
58 | }
|
---|
59 |
|
---|
60 | va_ptr make_va(){
|
---|
61 | VA va;
|
---|
62 | va.push_back(make_a());
|
---|
63 | va.push_back(make_a());
|
---|
64 | return va_ptr(new VA(va));
|
---|
65 | }
|
---|
66 |
|
---|
67 | vva_ptr make_vva(){
|
---|
68 | VVA vva;
|
---|
69 | vva.push_back(make_va());
|
---|
70 | vva.push_back(make_va());
|
---|
71 | return vva_ptr(new VVA(vva));
|
---|
72 | }
|
---|
73 |
|
---|
74 | int main(){
|
---|
75 | std::string filename = "tmp";
|
---|
76 | std::vector<vva_ptr> thing;
|
---|
77 | thing.push_back(make_vva());
|
---|
78 | thing.push_back(make_vva());
|
---|
79 | {
|
---|
80 | std::ofstream ofs(filename.c_str());
|
---|
81 | boost::archive::binary_oarchive oa(ofs);
|
---|
82 | register_types(oa);
|
---|
83 | oa << BOOST_SERIALIZATION_NVP(thing);
|
---|
84 | }
|
---|
85 | {
|
---|
86 | std::ifstream ifs(filename.c_str());
|
---|
87 | boost::archive::binary_iarchive ia(ifs);
|
---|
88 | register_types(ia);
|
---|
89 | ia >> BOOST_SERIALIZATION_NVP(thing);
|
---|
90 | }
|
---|
91 | return 0;
|
---|
92 | }
|
---|