]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_derived.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / serialization / test / test_derived.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_derived.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 <fstream>
13
14 #include <cstdio> // remove
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 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 ~base(){};
36 };
37
38 class derived1 : public 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(base);
44 }
45 };
46
47 class derived2 : public 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(base);
53 }
54 };
55
56 // save non-polymorphic classes through a base class pointer
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 // 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);
66
67 // upcasting non-polymorphic pointers may not lead to the expected
68 // result. In the current type id system
69 base *b1 = d1;
70 base *b2 = d2;
71
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);
81
82 BOOST_WARN_MESSAGE(
83 !(this_type == true_type),
84 "current type id system does not support non-polymorphic types"
85 );
86
87 oa << BOOST_SERIALIZATION_NVP(b1);
88 oa << BOOST_SERIALIZATION_NVP(b2);
89
90 delete d1;
91 delete d2;
92 }
93
94 // save non-polymorphic classes through a base class pointer
95 void load_derived(const char *testfile)
96 {
97 test_istream is(testfile, TEST_STREAM_FLAGS);
98 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
99
100 // registration not necessary when serializing the most derived pointer
101 derived1 *d1 = NULL;
102 derived2 *d2 = NULL;
103 ia >> BOOST_SERIALIZATION_NVP(d1) >> BOOST_SERIALIZATION_NVP(d2);
104
105 // upcasting non-polymorphic pointers may not lead to the expected
106 // result. In the current type id system
107 base *b1 = NULL;
108 base *b2 = NULL;
109
110 // note: this will produce incorrect results for non-polymorphic classes
111 ia >> BOOST_SERIALIZATION_NVP(b1);
112 ia >> BOOST_SERIALIZATION_NVP(b2);
113
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);
123
124 BOOST_WARN_MESSAGE(
125 ! (this_type == true_type),
126 "current type id system does fails for non-polymorphic types"
127 );
128
129 BOOST_CHECK(b1 == static_cast<base *>(d1));
130 BOOST_CHECK(b2 == static_cast<base *>(d2));
131
132 delete d1;
133 delete d2;
134 }
135
136 int
137 test_main( int /* argc */, char* /* argv */[] )
138 {
139 const char * testfile = boost::archive::tmpnam(NULL);
140 BOOST_REQUIRE(NULL != testfile);
141
142 save_derived(testfile);
143 load_derived(testfile);
144 std::remove(testfile);
145 return EXIT_SUCCESS;
146 }
147
148 // EOF