1 | #include <fstream>
|
---|
2 | #include <string>
|
---|
3 | #include <iostream>
|
---|
4 | #include <sstream>
|
---|
5 |
|
---|
6 | #include <boost/archive/text_oarchive.hpp>
|
---|
7 | #include <boost/archive/text_iarchive.hpp>
|
---|
8 | #include <boost/serialization/serialization.hpp>
|
---|
9 | #include <boost/serialization/list.hpp>
|
---|
10 |
|
---|
11 | class A {
|
---|
12 | public:
|
---|
13 | A() {}
|
---|
14 |
|
---|
15 | virtual std::string toString(void) const =0;
|
---|
16 | private:
|
---|
17 | friend class boost::serialization::access;
|
---|
18 |
|
---|
19 | template <typename Archive>
|
---|
20 | void serialize(Archive& ar, const unsigned int version)
|
---|
21 | {
|
---|
22 | }
|
---|
23 | };
|
---|
24 |
|
---|
25 | class B : public A {
|
---|
26 | public:
|
---|
27 | B()
|
---|
28 | {
|
---|
29 | }
|
---|
30 |
|
---|
31 | explicit B(int i)
|
---|
32 | : m_i(i)
|
---|
33 | {
|
---|
34 | }
|
---|
35 |
|
---|
36 | std::string toString(void) const
|
---|
37 | {
|
---|
38 | std::ostringstream oss;
|
---|
39 | oss << "B = " << m_i;
|
---|
40 | return oss.str();
|
---|
41 | }
|
---|
42 |
|
---|
43 | private:
|
---|
44 | friend class boost::serialization::access;
|
---|
45 |
|
---|
46 | template <typename Archive>
|
---|
47 | void serialize(Archive& ar, const unsigned int version)
|
---|
48 | {
|
---|
49 | ar & boost::serialization::base_object<A>(*this);
|
---|
50 | ar & m_i;
|
---|
51 | }
|
---|
52 |
|
---|
53 | int m_i;
|
---|
54 | };
|
---|
55 |
|
---|
56 | class C : public A {
|
---|
57 | public:
|
---|
58 | C()
|
---|
59 | {
|
---|
60 | }
|
---|
61 |
|
---|
62 | explicit C(double d)
|
---|
63 | : m_d(d)
|
---|
64 | {
|
---|
65 | }
|
---|
66 |
|
---|
67 | std::string toString(void) const
|
---|
68 | {
|
---|
69 | std::ostringstream oss;
|
---|
70 | oss << "C = " << m_d;
|
---|
71 | return oss.str();
|
---|
72 | }
|
---|
73 |
|
---|
74 | private:
|
---|
75 | friend class boost::serialization::access;
|
---|
76 |
|
---|
77 | template <typename Archive>
|
---|
78 | void serialize(Archive& ar, const unsigned int version)
|
---|
79 | {
|
---|
80 | ar & boost::serialization::base_object<A>(*this);
|
---|
81 | ar & m_d;
|
---|
82 | }
|
---|
83 |
|
---|
84 | double m_d;
|
---|
85 | };
|
---|
86 |
|
---|
87 | int main() {
|
---|
88 | boost::serialization::void_cast_register(
|
---|
89 | static_cast<B *>(NULL),
|
---|
90 | static_cast<A *>(NULL)
|
---|
91 | );
|
---|
92 | boost::serialization::void_cast_register(
|
---|
93 | static_cast<C *>(NULL),
|
---|
94 | static_cast<A *>(NULL)
|
---|
95 | );
|
---|
96 |
|
---|
97 | std::list<A*> l;
|
---|
98 | {
|
---|
99 | std::ifstream ifs("filename");
|
---|
100 | boost::archive::text_iarchive ia(ifs);
|
---|
101 | ia.register_type(static_cast<B*>(NULL));
|
---|
102 | ia.register_type(static_cast<C*>(NULL));
|
---|
103 |
|
---|
104 | try
|
---|
105 | {
|
---|
106 | ia >> l;
|
---|
107 | }
|
---|
108 | catch(std::exception& ex)
|
---|
109 | {
|
---|
110 | std::cout << ex.what() << std::endl;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | return 0;
|
---|
115 | }
|
---|