]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_unregistered.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_unregistered.cpp
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)
9 // should pass compilation and execution
13 #include <cstddef> // NULL
14 #include <cstdio> // remove
15 #include <cstring> // strcmp
16 #include <boost/config.hpp>
17 #if defined(BOOST_NO_STDC_NAMESPACE)
23 #include "test_tools.hpp"
25 #include <boost/archive/archive_exception.hpp>
26 #include <boost/serialization/base_object.hpp>
27 #include <boost/serialization/type_info_implementation.hpp>
28 #include <boost/core/no_exceptions_support.hpp>
30 class polymorphic_base
32 friend class boost::serialization::access
;
33 template<class Archive
>
34 void serialize(Archive
& /* ar */, const unsigned int /* file_version */){
37 virtual ~polymorphic_base(){};
40 class polymorphic_derived1
: public polymorphic_base
42 friend class boost::serialization::access
;
43 template<class Archive
>
44 void serialize(Archive
&ar
, const unsigned int /* file_version */){
45 ar
& BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base
);
49 class polymorphic_derived2
: public polymorphic_base
51 friend class boost::serialization::access
;
52 template<class Archive
>
53 void serialize(Archive
&ar
, const unsigned int /* file_version */){
54 ar
& BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base
);
59 template<typename Archive
>
60 void serialize(Archive
&, unsigned int ver
) {
61 BOOST_CHECK(ver
== 1);
65 template<typename Archive
>
66 void serialize(Archive
&, unsigned int ver
) {
67 BOOST_CHECK(ver
== 2);
71 BOOST_CLASS_VERSION(type1
, 1);
72 BOOST_CLASS_VERSION(type2
, 2);
74 // save unregistered polymorphic classes
75 void save_unregistered1(const char *testfile
)
77 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
78 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
80 polymorphic_base
*rb1
= new polymorphic_derived1
;
82 // registration IS necessary when serializing a polymorphic class
83 // through pointer to the base class
86 oa
<< BOOST_SERIALIZATION_NVP(rb1
);
88 BOOST_CATCH(boost::archive::archive_exception aex
){
92 BOOST_CHECK_MESSAGE(except
, "lack of registration not detected !");
97 // note: the corresponding save function above will not result in
98 // valid archive - hence, the following code which attempts to load
99 // and archive will fail. Leave this as a reminder not to do this
101 // load unregistered polymorphic classes
102 void load_unregistered1(const char *testfile
)
104 std::ifstream
is(testfile
);
105 boost::archive::iarchive
ia(is
);
107 polymorphic_base
*rb1(NULL
);
109 // registration IS necessary when serializing a polymorphic class
110 // through pointer to the base class
113 ia
>> BOOST_SERIALIZATION_NVP(rb1
);
115 BOOST_CATCH(boost::archive::archive_exception aex
){
119 "failed load resulted in a non-null pointer"
123 BOOST_CHECK_MESSAGE(except
, "lack of registration not detected !");
129 // save unregistered polymorphic classes
130 void save_unregistered2(const char *testfile
)
132 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
133 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
135 polymorphic_derived1
*rd1
= new polymorphic_derived1
;
137 // registration is NOT necessary when serializing a polymorphic class
138 // through pointer to a derived class
141 oa
<< BOOST_SERIALIZATION_NVP(rd1
);
143 BOOST_CATCH(boost::archive::archive_exception aex
){
147 BOOST_CHECK_MESSAGE(! except
, "registration not detected !");
152 // note: the corresponding save function above will not result in
153 // valid archive - hence, the following code which attempts to load
154 // and archive will fail. Leave this as a reminder not to do this
155 // load unregistered polymorphic classes
156 void load_unregistered2(const char *testfile
)
158 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
159 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
161 polymorphic_derived1
*rd1
= NULL
;
163 // registration is NOT necessary when serializing a polymorphic class
164 // or through pointer to a derived class
167 ia
>> BOOST_SERIALIZATION_NVP(rd1
);
169 BOOST_CATCH(boost::archive::archive_exception aex
){
173 "failed load resulted in a non-null pointer"
177 BOOST_CHECK_MESSAGE(! except
, "registration not detected !");
182 // save registered polymorphic class
183 void save_registered(const char *testfile
)
185 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
186 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
188 polymorphic_base
*rb1
= new polymorphic_derived1
;
189 polymorphic_base
*rb2
= new polymorphic_derived2
;
191 // registration (forward declaration) will permit correct serialization
192 // through a pointer to a base class
193 oa
.register_type(static_cast<polymorphic_derived1
*>(NULL
));
194 oa
.register_type(static_cast<polymorphic_derived2
*>(NULL
));
195 oa
<< BOOST_SERIALIZATION_NVP(rb1
);
196 oa
<< BOOST_SERIALIZATION_NVP(rb2
);
202 // load registered polymorphic class
203 void load_registered(const char *testfile
)
205 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
206 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
208 polymorphic_base
*rb1
= NULL
;
209 polymorphic_base
*rb2
= NULL
;
211 // registration (forward declaration) will permit correct serialization
212 // through a pointer to a base class
213 ia
.register_type(static_cast<polymorphic_derived1
*>(NULL
));
214 ia
.register_type(static_cast<polymorphic_derived2
*>(NULL
));
216 ia
>> BOOST_SERIALIZATION_NVP(rb1
);
218 BOOST_CHECK_MESSAGE(NULL
!= rb1
, "Load resulted in NULL pointer");
220 boost::serialization::type_info_implementation
<
222 >::type::get_const_instance()
224 * boost::serialization::type_info_implementation
<
226 >::type::get_const_instance().get_derived_extended_type_info(*rb1
),
227 "restored pointer b1 not of correct type"
230 ia
>> BOOST_SERIALIZATION_NVP(rb2
);
231 BOOST_CHECK_MESSAGE(NULL
!= rb2
, "Load resulted in NULL pointer");
233 boost::serialization::type_info_implementation
<
235 >::type::get_const_instance()
237 * boost::serialization::type_info_implementation
<
239 >::type::get_const_instance().get_derived_extended_type_info(*rb2
),
240 "restored pointer b2 not of correct type"
247 // store a pointer from slot0
248 void save_unregistered_pointer(const char *testfile
)
250 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
251 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
253 oa
.register_type(static_cast<type2
*>(NULL
));
256 type2
*pointer2
= new type2
;
259 oa
& BOOST_SERIALIZATION_NVP(instance1
) & BOOST_SERIALIZATION_NVP(pointer2
);
262 BOOST_CHECK_MESSAGE(false, "unexpected exception");
269 // load a pointer from slot0 which has no pointer serializer
270 void load_unregistered_pointer(const char *testfile
)
272 test_istream
is(testfile
);
273 test_iarchive
ia(is
);
276 type2
*pointer2(NULL
);
280 ia
& BOOST_SERIALIZATION_NVP(instance1
) & BOOST_SERIALIZATION_NVP(pointer2
);
282 BOOST_CATCH(boost::archive::archive_exception aex
){
285 std::strcmp(aex
.what(), "unregistered class") == 0,
286 "incorrect exception"
290 BOOST_CHECK_MESSAGE(except
, "lack of registration not detected !");
291 BOOST_CHECK_MESSAGE(NULL
== pointer2
, "expected failed load");
297 test_main( int /* argc */, char* /* argv */[] )
299 const char * testfile
= boost::archive::tmpnam(NULL
);
300 BOOST_REQUIRE(NULL
!= testfile
);
301 save_unregistered1(testfile
);
302 // load_unregistered1(testfile);
303 save_unregistered2(testfile
);
304 load_unregistered2(testfile
);
305 save_registered(testfile
);
306 load_registered(testfile
);
307 save_unregistered_pointer(testfile
);
308 load_unregistered_pointer(testfile
);
309 std::remove(testfile
);