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 <cstddef> // NULL |
12 | #include <cstdio> // remove |
13 | #include <fstream> |
14 | |
15 | #include <boost/config.hpp> |
16 | #if defined(BOOST_NO_STDC_NAMESPACE) |
17 | namespace std{ |
18 | using ::remove; |
19 | } |
20 | #endif |
21 | |
22 | #include <boost/archive/archive_exception.hpp> |
23 | #include "test_tools.hpp" |
24 | |
25 | #include <boost/serialization/base_object.hpp> |
26 | #include <boost/serialization/type_info_implementation.hpp> |
27 | |
28 | class polymorphic_base |
29 | { |
30 | friend class boost::serialization::access; |
31 | template<class Archive> |
32 | void serialize(Archive & /* ar */, const unsigned int /* file_version */){ |
33 | } |
34 | public: |
35 | virtual ~polymorphic_base(){}; |
36 | }; |
37 | |
38 | class polymorphic_derived1 : public polymorphic_base |
39 | { |
40 | friend class boost::serialization::access; |
41 | template<class Archive> |
42 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
43 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base); |
44 | } |
45 | }; |
46 | |
47 | class polymorphic_derived2 : public polymorphic_base |
48 | { |
49 | friend class boost::serialization::access; |
50 | template<class Archive> |
51 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
52 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base); |
53 | } |
54 | }; |
55 | |
56 | // save derived polymorphic class |
57 | void save_derived(const char *testfile) |
58 | { |
59 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
60 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
61 | |
62 | polymorphic_derived1 *rd1 = new polymorphic_derived1; |
63 | polymorphic_derived2 *rd2 = new polymorphic_derived2; |
64 | |
65 | // registration IS necessary when serializing pointers of |
66 | // polymorphic classes |
67 | oa.register_type(static_cast<polymorphic_derived1 *>(NULL)); |
68 | oa.register_type(static_cast<polymorphic_derived2 *>(NULL)); |
69 | oa << BOOST_SERIALIZATION_NVP(rd1); |
70 | oa << BOOST_SERIALIZATION_NVP(rd2); |
71 | |
72 | // the above operation registers the derived classes as a side |
73 | // effect. Hence, instances can now be correctly serialized through |
74 | // a base class pointer. |
75 | polymorphic_base *rb1 = rd1; |
76 | polymorphic_base *rb2 = rd2; |
77 | oa << BOOST_SERIALIZATION_NVP(rb1); |
78 | oa << BOOST_SERIALIZATION_NVP(rb2); |
79 | |
80 | delete rd1; |
81 | delete rd2; |
82 | } |
83 | |
84 | // save derived polymorphic class |
85 | void load_derived(const char *testfile) |
86 | { |
87 | test_istream is(testfile, TEST_STREAM_FLAGS); |
88 | test_iarchive ia(is, TEST_ARCHIVE_FLAGS); |
89 | |
90 | polymorphic_derived1 *rd1 = NULL; |
91 | polymorphic_derived2 *rd2 = NULL; |
92 | |
93 | // registration IS necessary when serializing pointers of |
94 | // polymorphic classes |
95 | ia.register_type(static_cast<polymorphic_derived1 *>(NULL)); |
96 | ia.register_type(static_cast<polymorphic_derived2 *>(NULL)); |
97 | |
98 | ia >> BOOST_SERIALIZATION_NVP(rd1); |
99 | |
100 | const boost::serialization::extended_type_info * p1; |
101 | p1 = & boost::serialization::type_info_implementation<polymorphic_derived1> |
102 | ::type::get_const_instance(); |
103 | |
104 | BOOST_CHECK(NULL != p1); |
105 | |
106 | const boost::serialization::extended_type_info * p2; |
107 | p2 = boost::serialization::type_info_implementation<polymorphic_derived1> |
108 | ::type::get_const_instance().get_derived_extended_type_info(t: *rd1); |
109 | |
110 | BOOST_CHECK(NULL != p2); |
111 | |
112 | BOOST_CHECK_MESSAGE(p1 == p2, "restored pointer d1 not of correct type" ); |
113 | |
114 | ia >> BOOST_SERIALIZATION_NVP(rd2); |
115 | |
116 | BOOST_CHECK_MESSAGE( |
117 | & boost::serialization::type_info_implementation<polymorphic_derived2> |
118 | ::type::get_const_instance() |
119 | == |
120 | boost::serialization::type_info_implementation<polymorphic_derived2> |
121 | ::type::get_const_instance().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 operation 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_const_instance(); |
140 | |
141 | BOOST_CHECK(NULL != p1); |
142 | |
143 | p2 = boost::serialization::type_info_implementation<polymorphic_base> |
144 | ::type::get_const_instance().get_derived_extended_type_info(t: *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_const_instance() |
160 | == boost::serialization::type_info_implementation<polymorphic_base> |
161 | ::type::get_const_instance().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, TEST_ARCHIVE_FLAGS); |
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, TEST_ARCHIVE_FLAGS); |
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_const_instance() |
206 | == boost::serialization::type_info_implementation<polymorphic_base> |
207 | ::type::get_const_instance().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_const_instance() |
214 | == boost::serialization::type_info_implementation<polymorphic_base> |
215 | ::type::get_const_instance().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(filename: testfile); |
236 | return EXIT_SUCCESS; |
237 | } |
238 | |
239 | // EOF |
240 | |