]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_derived.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
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
11 #include <cstddef> // NULL
14 #include <cstdio> // remove
15 #include <boost/config.hpp>
16 #if defined(BOOST_NO_STDC_NAMESPACE)
22 #include <boost/archive/archive_exception.hpp>
23 #include "test_tools.hpp"
25 #include <boost/serialization/base_object.hpp>
26 #include <boost/serialization/type_info_implementation.hpp>
30 friend class boost::serialization::access
;
31 template<class Archive
>
32 void serialize(Archive
& /* ar */, const unsigned int /* file_version */){
38 class derived1
: public base
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(base
);
47 class derived2
: public base
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(base
);
56 // save non-polymorphic classes through a base class pointer
57 void save_derived(const char *testfile
)
59 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
60 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
62 // registration not necessary when serializing the most derived pointer
63 derived1
*d1
= new derived1
;
64 derived2
*d2
= new derived2
;
65 oa
<< BOOST_SERIALIZATION_NVP(d1
) << BOOST_SERIALIZATION_NVP(d2
);
67 // upcasting non-polymorphic pointers may not lead to the expected
68 // result. In the current type id system
72 // Warning, the current type id system does not yield true
73 // type id for non-polymorphic types
74 const boost::serialization::extended_type_info
& this_type
75 = boost::serialization::type_info_implementation
<base
>::type
76 ::get_const_instance();
77 // retrieve the true type of the object pointed to
78 const boost::serialization::extended_type_info
& true_type
79 = * boost::serialization::type_info_implementation
<base
>::type
80 ::get_const_instance().get_derived_extended_type_info(*b1
);
83 !(this_type
== true_type
),
84 "current type id system does not support non-polymorphic types"
87 oa
<< BOOST_SERIALIZATION_NVP(b1
);
88 oa
<< BOOST_SERIALIZATION_NVP(b2
);
94 // save non-polymorphic classes through a base class pointer
95 void load_derived(const char *testfile
)
97 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
98 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
100 // registration not necessary when serializing the most derived pointer
103 ia
>> BOOST_SERIALIZATION_NVP(d1
) >> BOOST_SERIALIZATION_NVP(d2
);
105 // upcasting non-polymorphic pointers may not lead to the expected
106 // result. In the current type id system
110 // note: this will produce incorrect results for non-polymorphic classes
111 ia
>> BOOST_SERIALIZATION_NVP(b1
);
112 ia
>> BOOST_SERIALIZATION_NVP(b2
);
114 // Warning, the current type id system does not yield true
115 // type id for non-polymorphic types
116 const boost::serialization::extended_type_info
& this_type
117 = boost::serialization::type_info_implementation
<base
>::type
118 ::get_const_instance();
119 // retrieve the true type of the object pointed to
120 const boost::serialization::extended_type_info
& true_type
121 = * boost::serialization::type_info_implementation
<base
>::type
122 ::get_const_instance().get_derived_extended_type_info(*b1
);
125 ! (this_type
== true_type
),
126 "current type id system does fails for non-polymorphic types"
129 BOOST_CHECK(b1
== static_cast<base
*>(d1
));
130 BOOST_CHECK(b2
== static_cast<base
*>(d2
));
137 test_main( int /* argc */, char* /* argv */[] )
139 const char * testfile
= boost::archive::tmpnam(NULL
);
140 BOOST_REQUIRE(NULL
!= testfile
);
142 save_derived(testfile
);
143 load_derived(testfile
);
144 std::remove(testfile
);