1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
2 | // test_unregistered.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 "test_tools.hpp" |
---|
22 | #include <boost/preprocessor/stringize.hpp> |
---|
23 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
---|
24 | |
---|
25 | #include <boost/archive/archive_exception.hpp> |
---|
26 | #include <boost/serialization/base_object.hpp> |
---|
27 | #include <boost/detail/no_exceptions_support.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 unregistered polymorphic classes |
---|
58 | void save_unregistered1(const char *testfile) |
---|
59 | { |
---|
60 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
61 | test_oarchive oa(os); |
---|
62 | |
---|
63 | polymorphic_base *rb1 = new polymorphic_derived1; |
---|
64 | |
---|
65 | // registration IS necessary when serializing a polymorphic class |
---|
66 | // through pointer to the base class |
---|
67 | bool except = false; |
---|
68 | BOOST_TRY { |
---|
69 | oa << BOOST_SERIALIZATION_NVP(rb1); |
---|
70 | } |
---|
71 | BOOST_CATCH(boost::archive::archive_exception aex){ |
---|
72 | except = true; |
---|
73 | } |
---|
74 | BOOST_CATCH_END |
---|
75 | BOOST_CHECK_MESSAGE(except, "lack of registration not detected !"); |
---|
76 | |
---|
77 | delete rb1; |
---|
78 | } |
---|
79 | |
---|
80 | // note: the corresponding save function above will not result in |
---|
81 | // valid archive - hence, the following code which attempts to load |
---|
82 | // and archive will fail. Leave this as a reminder not to do this |
---|
83 | #if 0 |
---|
84 | // load unregistered polymorphic classes |
---|
85 | void load_unregistered1(const char *testfile) |
---|
86 | { |
---|
87 | std::ifstream is(testfile); |
---|
88 | boost::archive::iarchive ia(is); |
---|
89 | |
---|
90 | polymorphic_base *rb1(NULL); |
---|
91 | |
---|
92 | // registration IS necessary when serializing a polymorphic class |
---|
93 | // through pointer to the base class |
---|
94 | bool except = false; |
---|
95 | BOOST_TRY { |
---|
96 | ia >> BOOST_SERIALIZATION_NVP(rb1); |
---|
97 | } |
---|
98 | BOOST_CATCH(boost::archive::archive_exception aex){ |
---|
99 | except = true; |
---|
100 | BOOST_CHECK_MESSAGE( |
---|
101 | NULL == rb1, |
---|
102 | "failed load resulted in a non-null pointer" |
---|
103 | ); |
---|
104 | } |
---|
105 | BOOST_CATCH_END |
---|
106 | BOOST_CHECK_MESSAGE(except, "lack of registration not detected !"); |
---|
107 | |
---|
108 | delete rb1; |
---|
109 | } |
---|
110 | #endif |
---|
111 | |
---|
112 | // save unregistered polymorphic classes |
---|
113 | void save_unregistered2(const char *testfile) |
---|
114 | { |
---|
115 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
116 | test_oarchive oa(os); |
---|
117 | |
---|
118 | polymorphic_derived1 *rd1 = new polymorphic_derived1; |
---|
119 | |
---|
120 | // registration is NOT necessary when serializing a polymorphic class |
---|
121 | // through pointer to a derived class |
---|
122 | bool except = false; |
---|
123 | BOOST_TRY { |
---|
124 | oa << BOOST_SERIALIZATION_NVP(rd1); |
---|
125 | } |
---|
126 | BOOST_CATCH(boost::archive::archive_exception aex){ |
---|
127 | except = true; |
---|
128 | } |
---|
129 | BOOST_CATCH_END |
---|
130 | BOOST_CHECK_MESSAGE(! except, "registration not detected !"); |
---|
131 | |
---|
132 | delete rd1; |
---|
133 | } |
---|
134 | |
---|
135 | // note: the corresponding save function above will not result in |
---|
136 | // valid archive - hence, the following code which attempts to load |
---|
137 | // and archive will fail. Leave this as a reminder not to do this |
---|
138 | // load unregistered polymorphic classes |
---|
139 | void load_unregistered2(const char *testfile) |
---|
140 | { |
---|
141 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
142 | test_iarchive ia(is); |
---|
143 | |
---|
144 | polymorphic_derived1 *rd1 = NULL; |
---|
145 | |
---|
146 | // registration is NOT necessary when serializing a polymorphic class |
---|
147 | // or through pointer to a derived class |
---|
148 | bool except = false; |
---|
149 | BOOST_TRY { |
---|
150 | ia >> BOOST_SERIALIZATION_NVP(rd1); |
---|
151 | } |
---|
152 | BOOST_CATCH(boost::archive::archive_exception aex){ |
---|
153 | except = true; |
---|
154 | BOOST_CHECK_MESSAGE( |
---|
155 | NULL == rd1, |
---|
156 | "failed load resulted in a non-null pointer" |
---|
157 | ); |
---|
158 | } |
---|
159 | BOOST_CATCH_END |
---|
160 | BOOST_CHECK_MESSAGE(! except, "registration not detected !"); |
---|
161 | |
---|
162 | delete rd1; |
---|
163 | } |
---|
164 | |
---|
165 | // save registered polymorphic class |
---|
166 | void save_registered(const char *testfile) |
---|
167 | { |
---|
168 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
169 | test_oarchive oa(os); |
---|
170 | |
---|
171 | polymorphic_base *rb1 = new polymorphic_derived1; |
---|
172 | polymorphic_base *rb2 = new polymorphic_derived2; |
---|
173 | |
---|
174 | // registration (forward declaration) will permit correct serialization |
---|
175 | // through a pointer to a base class |
---|
176 | oa.register_type(static_cast<polymorphic_derived1 *>(NULL)); |
---|
177 | oa.register_type(static_cast<polymorphic_derived2 *>(NULL)); |
---|
178 | oa << BOOST_SERIALIZATION_NVP(rb1); |
---|
179 | oa << BOOST_SERIALIZATION_NVP(rb2); |
---|
180 | |
---|
181 | delete rb1; |
---|
182 | delete rb2; |
---|
183 | } |
---|
184 | |
---|
185 | // load registered polymorphic class |
---|
186 | void load_registered(const char *testfile) |
---|
187 | { |
---|
188 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
189 | test_iarchive ia(is); |
---|
190 | |
---|
191 | polymorphic_base *rb1 = NULL; |
---|
192 | polymorphic_base *rb2 = NULL; |
---|
193 | |
---|
194 | // registration (forward declaration) will permit correct serialization |
---|
195 | // through a pointer to a base class |
---|
196 | ia.register_type(static_cast<polymorphic_derived1 *>(NULL)); |
---|
197 | ia.register_type(static_cast<polymorphic_derived2 *>(NULL)); |
---|
198 | |
---|
199 | ia >> BOOST_SERIALIZATION_NVP(rb1); |
---|
200 | |
---|
201 | BOOST_CHECK_MESSAGE(NULL != rb1, "Load resulted in NULL pointer"); |
---|
202 | BOOST_CHECK_MESSAGE( |
---|
203 | boost::serialization::type_info_implementation<polymorphic_derived1> |
---|
204 | ::type::get_instance() |
---|
205 | == boost::serialization::type_info_implementation<polymorphic_base> |
---|
206 | ::type::get_derived_extended_type_info(*rb1), |
---|
207 | "restored pointer b1 not of correct type" |
---|
208 | ); |
---|
209 | |
---|
210 | ia >> BOOST_SERIALIZATION_NVP(rb2); |
---|
211 | BOOST_CHECK_MESSAGE(NULL != rb2, "Load resulted in NULL pointer"); |
---|
212 | BOOST_CHECK_MESSAGE( |
---|
213 | boost::serialization::type_info_implementation<polymorphic_derived2> |
---|
214 | ::type::get_instance() |
---|
215 | == boost::serialization::type_info_implementation<polymorphic_base> |
---|
216 | ::type::get_derived_extended_type_info(*rb2), |
---|
217 | "restored pointer b2 not of correct type" |
---|
218 | ); |
---|
219 | |
---|
220 | delete rb1; |
---|
221 | delete rb2; |
---|
222 | } |
---|
223 | |
---|
224 | int |
---|
225 | test_main( int /* argc */, char* /* argv */[] ) |
---|
226 | { |
---|
227 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
228 | BOOST_REQUIRE(NULL != testfile); |
---|
229 | save_unregistered1(testfile); |
---|
230 | // load_unregistered1(testfile); |
---|
231 | save_unregistered2(testfile); |
---|
232 | load_unregistered2(testfile); |
---|
233 | save_registered(testfile); |
---|
234 | load_registered(testfile); |
---|
235 | std::remove(testfile); |
---|
236 | return EXIT_SUCCESS; |
---|
237 | } |
---|
238 | |
---|
239 | // EOF |
---|