1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
2 | // test_registered.cpp |
---|
3 | |
---|
4 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
---|
5 | // Use, modification and distribution is subject to the Boost Software |
---|
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
7 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | |
---|
9 | // should pass compilation and execution |
---|
10 | |
---|
11 | #include <fstream> |
---|
12 | |
---|
13 | #include <cstdio> // remove |
---|
14 | #include <boost/config.hpp> |
---|
15 | #if defined(BOOST_NO_STDC_NAMESPACE) |
---|
16 | namespace std{ |
---|
17 | using ::remove; |
---|
18 | } |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <boost/archive/archive_exception.hpp> |
---|
22 | #include "test_tools.hpp" |
---|
23 | #include <boost/preprocessor/stringize.hpp> |
---|
24 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
---|
25 | |
---|
26 | #include <boost/serialization/base_object.hpp> |
---|
27 | #include <boost/serialization/type_info_implementation.hpp> |
---|
28 | |
---|
29 | class polymorphic_base |
---|
30 | { |
---|
31 | friend class boost::serialization::access; |
---|
32 | template<class Archive> |
---|
33 | void serialize(Archive & /* ar */, const unsigned int /* file_version */){ |
---|
34 | } |
---|
35 | public: |
---|
36 | virtual ~polymorphic_base(){}; |
---|
37 | }; |
---|
38 | |
---|
39 | class polymorphic_derived1 : public polymorphic_base |
---|
40 | { |
---|
41 | friend class boost::serialization::access; |
---|
42 | template<class Archive> |
---|
43 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
---|
44 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base); |
---|
45 | } |
---|
46 | }; |
---|
47 | |
---|
48 | class polymorphic_derived2 : public polymorphic_base |
---|
49 | { |
---|
50 | friend class boost::serialization::access; |
---|
51 | template<class Archive> |
---|
52 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
---|
53 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base); |
---|
54 | } |
---|
55 | }; |
---|
56 | |
---|
57 | // save derived polymorphic class |
---|
58 | void save_derived(const char *testfile) |
---|
59 | { |
---|
60 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
61 | test_oarchive oa(os); |
---|
62 | |
---|
63 | polymorphic_derived1 *rd1 = new polymorphic_derived1; |
---|
64 | polymorphic_derived2 *rd2 = new polymorphic_derived2; |
---|
65 | |
---|
66 | // registration IS necessary when serializing pointers of |
---|
67 | // polymorphic classes |
---|
68 | oa.register_type(static_cast<polymorphic_derived1 *>(NULL)); |
---|
69 | oa.register_type(static_cast<polymorphic_derived2 *>(NULL)); |
---|
70 | oa << BOOST_SERIALIZATION_NVP(rd1); |
---|
71 | oa << BOOST_SERIALIZATION_NVP(rd2); |
---|
72 | |
---|
73 | // the above opereration registers the derived classes as a side |
---|
74 | // effect. Hence, instances can now be correctly serialized through |
---|
75 | // a base class pointer. |
---|
76 | polymorphic_base *rb1 = rd1; |
---|
77 | polymorphic_base *rb2 = rd2; |
---|
78 | oa << BOOST_SERIALIZATION_NVP(rb1); |
---|
79 | oa << BOOST_SERIALIZATION_NVP(rb2); |
---|
80 | |
---|
81 | delete rd1; |
---|
82 | delete rd2; |
---|
83 | } |
---|
84 | |
---|
85 | // save derived polymorphic class |
---|
86 | void load_derived(const char *testfile) |
---|
87 | { |
---|
88 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
89 | test_iarchive ia(is); |
---|
90 | |
---|
91 | polymorphic_derived1 *rd1 = NULL; |
---|
92 | polymorphic_derived2 *rd2 = NULL; |
---|
93 | |
---|
94 | // registration IS necessary when serializing pointers of |
---|
95 | // polymorphic classes |
---|
96 | ia.register_type(static_cast<polymorphic_derived1 *>(NULL)); |
---|
97 | ia.register_type(static_cast<polymorphic_derived2 *>(NULL)); |
---|
98 | |
---|
99 | ia >> BOOST_SERIALIZATION_NVP(rd1); |
---|
100 | |
---|
101 | const boost::serialization::extended_type_info * p1; |
---|
102 | p1 = boost::serialization::type_info_implementation<polymorphic_derived1> |
---|
103 | ::type::get_instance(); |
---|
104 | |
---|
105 | BOOST_CHECK(NULL != p1); |
---|
106 | |
---|
107 | const boost::serialization::extended_type_info * p2; |
---|
108 | p2 = boost::serialization::type_info_implementation<polymorphic_derived1> |
---|
109 | ::type::get_derived_extended_type_info(*rd1); |
---|
110 | |
---|
111 | BOOST_CHECK(NULL != p2); |
---|
112 | |
---|
113 | BOOST_CHECK_MESSAGE(p1 == p2, "restored pointer d1 not of correct type"); |
---|
114 | |
---|
115 | ia >> BOOST_SERIALIZATION_NVP(rd2); |
---|
116 | |
---|
117 | BOOST_CHECK_MESSAGE( |
---|
118 | boost::serialization::type_info_implementation<polymorphic_derived2> |
---|
119 | ::type::get_instance() |
---|
120 | == boost::serialization::type_info_implementation<polymorphic_derived2> |
---|
121 | ::type::get_derived_extended_type_info(*rd2), |
---|
122 | "restored pointer d2 not of correct type" |
---|
123 | ); |
---|
124 | |
---|
125 | polymorphic_base *rb1 = NULL; |
---|
126 | polymorphic_base *rb2 = NULL; |
---|
127 | |
---|
128 | // the above opereration registers the derived classes as a side |
---|
129 | // effect. Hence, instances can now be correctly serialized through |
---|
130 | // a base class pointer. |
---|
131 | ia >> BOOST_SERIALIZATION_NVP(rb1); |
---|
132 | |
---|
133 | BOOST_CHECK_MESSAGE( |
---|
134 | rb1 == dynamic_cast<polymorphic_base *>(rd1), |
---|
135 | "serialized pointers not correctly restored" |
---|
136 | ); |
---|
137 | |
---|
138 | p1 = boost::serialization::type_info_implementation<polymorphic_derived1> |
---|
139 | ::type::get_instance(); |
---|
140 | |
---|
141 | BOOST_CHECK(NULL != p1); |
---|
142 | |
---|
143 | p2 = boost::serialization::type_info_implementation<polymorphic_base> |
---|
144 | ::type::get_derived_extended_type_info(*rb1); |
---|
145 | |
---|
146 | BOOST_CHECK(NULL != p2); |
---|
147 | |
---|
148 | BOOST_CHECK_MESSAGE(p1 == p2, "restored pointer b1 not of correct type"); |
---|
149 | |
---|
150 | ia >> BOOST_SERIALIZATION_NVP(rb2); |
---|
151 | |
---|
152 | BOOST_CHECK_MESSAGE( |
---|
153 | rb2 == dynamic_cast<polymorphic_base *>(rd2), |
---|
154 | "serialized pointers not correctly restored" |
---|
155 | ); |
---|
156 | |
---|
157 | BOOST_CHECK_MESSAGE( |
---|
158 | boost::serialization::type_info_implementation<polymorphic_derived2> |
---|
159 | ::type::get_instance() |
---|
160 | == boost::serialization::type_info_implementation<polymorphic_base> |
---|
161 | ::type::get_derived_extended_type_info(*rb2), |
---|
162 | "restored pointer b2 not of correct type" |
---|
163 | ); |
---|
164 | |
---|
165 | delete rb1; |
---|
166 | delete rb2; |
---|
167 | } |
---|
168 | |
---|
169 | // save registered polymorphic class |
---|
170 | void save_registered(const char *testfile) |
---|
171 | { |
---|
172 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
173 | test_oarchive oa(os); |
---|
174 | |
---|
175 | polymorphic_base *rb1 = new polymorphic_derived1; |
---|
176 | polymorphic_base *rb2 = new polymorphic_derived2; |
---|
177 | |
---|
178 | // registration (forward declaration) will permit correct serialization |
---|
179 | // through a pointer to a base class |
---|
180 | oa.register_type(static_cast<polymorphic_derived1 *>(NULL)); |
---|
181 | oa.register_type(static_cast<polymorphic_derived2 *>(NULL)); |
---|
182 | oa << BOOST_SERIALIZATION_NVP(rb1) << BOOST_SERIALIZATION_NVP(rb2); |
---|
183 | |
---|
184 | delete rb1; |
---|
185 | delete rb2; |
---|
186 | } |
---|
187 | |
---|
188 | // save registered polymorphic class |
---|
189 | void load_registered(const char *testfile) |
---|
190 | { |
---|
191 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
192 | test_iarchive ia(is); |
---|
193 | |
---|
194 | polymorphic_base *rb1 = NULL; |
---|
195 | polymorphic_base *rb2 = NULL; |
---|
196 | |
---|
197 | // registration (forward declaration) will permit correct serialization |
---|
198 | // through a pointer to a base class |
---|
199 | ia.register_type(static_cast<polymorphic_derived1 *>(NULL)); |
---|
200 | ia.register_type(static_cast<polymorphic_derived2 *>(NULL)); |
---|
201 | ia >> BOOST_SERIALIZATION_NVP(rb1) >> BOOST_SERIALIZATION_NVP(rb2); |
---|
202 | |
---|
203 | BOOST_CHECK_MESSAGE( |
---|
204 | boost::serialization::type_info_implementation<polymorphic_derived1> |
---|
205 | ::type::get_instance() |
---|
206 | == boost::serialization::type_info_implementation<polymorphic_base> |
---|
207 | ::type::get_derived_extended_type_info(*rb1), |
---|
208 | "restored pointer b1 not of correct type" |
---|
209 | ); |
---|
210 | |
---|
211 | BOOST_CHECK_MESSAGE( |
---|
212 | boost::serialization::type_info_implementation<polymorphic_derived2> |
---|
213 | ::type::get_instance() |
---|
214 | == boost::serialization::type_info_implementation<polymorphic_base> |
---|
215 | ::type::get_derived_extended_type_info(*rb2), |
---|
216 | "restored pointer b2 not of correct type" |
---|
217 | ); |
---|
218 | |
---|
219 | delete rb1; |
---|
220 | delete rb2; |
---|
221 | } |
---|
222 | |
---|
223 | int |
---|
224 | test_main( int /* argc */, char* /* argv */[] ) |
---|
225 | { |
---|
226 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
227 | |
---|
228 | BOOST_REQUIRE(NULL != testfile); |
---|
229 | |
---|
230 | save_derived(testfile); |
---|
231 | load_derived(testfile); |
---|
232 | save_registered(testfile); |
---|
233 | load_registered(testfile); |
---|
234 | |
---|
235 | std::remove(testfile); |
---|
236 | return EXIT_SUCCESS; |
---|
237 | } |
---|
238 | |
---|
239 | // EOF |
---|