• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_no_rtti.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 // note: this program tests the inter-operability of different
10 // extended typeinfo systems.  In this example, one class is
11 // identified using the default RTTI while the other uses a custom
12 // system based on the export key.
13 //
14 // As this program uses RTTI for one of the types, the test will fail
15 // on a system for which RTTI is not enabled or not existent.
16 
17 #include <fstream>
18 
19 #include <cstdio> // remove
20 #include <boost/config.hpp>
21 #if defined(BOOST_NO_STDC_NAMESPACE)
22 namespace std{
23     using ::remove;
24 }
25 #endif
26 
27 #include <boost/static_assert.hpp>
28 #include <boost/type_traits/is_same.hpp>
29 
30 #include <boost/archive/archive_exception.hpp>
31 #include "../test/test_tools.hpp"
32 #include <boost/preprocessor/stringize.hpp>
33 // #include <boost/preprocessor/cat.hpp>
34 // the following fails with (only!) gcc 3.4
35 // #include BOOST_PP_STRINGIZE(BOOST_PP_CAT(../test/,BOOST_ARCHIVE_TEST))
36 // just copy over the files from the test directory
37 #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
38 
39 #include <boost/serialization/nvp.hpp>
40 #include <boost/serialization/base_object.hpp>
41 #include <boost/serialization/export.hpp>
42 #include <boost/serialization/type_info_implementation.hpp>
43 #include <boost/serialization/extended_type_info_no_rtti.hpp>
44 
45 class polymorphic_base
46 {
47     friend class boost::serialization::access;
48     template<class Archive>
serialize(Archive &,const unsigned int)49     void serialize(Archive & /* ar */, const unsigned int /* file_version */){
50     }
51 public:
52     virtual const char * get_key() const = 0;
~polymorphic_base()53     virtual ~polymorphic_base(){};
54 };
55 
56 BOOST_IS_ABSTRACT(polymorphic_base)
57 BOOST_CLASS_TYPE_INFO(
58     polymorphic_base,
59     extended_type_info_no_rtti<polymorphic_base>
60 )
61 // note: types which use ...no_rtti MUST be exported
62 BOOST_CLASS_EXPORT(polymorphic_base)
63 
64 class polymorphic_derived1 : public polymorphic_base
65 {
66     friend class boost::serialization::access;
67     template<class Archive>
serialize(Archive & ar,const unsigned int)68     void serialize(Archive &ar, const unsigned int  /* file_version */){
69         ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base);
70     }
71 public:
72     virtual const char * get_key() const ;
73 };
74 
BOOST_CLASS_TYPE_INFO(polymorphic_derived1,extended_type_info_no_rtti<polymorphic_derived1>)75 BOOST_CLASS_TYPE_INFO(
76     polymorphic_derived1,
77     extended_type_info_no_rtti<polymorphic_derived1>
78 )
79 BOOST_CLASS_EXPORT(polymorphic_derived1)
80 
81 const char * polymorphic_derived1::get_key() const {
82     const boost::serialization::extended_type_info *eti
83         = boost::serialization::type_info_implementation<polymorphic_derived1>
84             ::type::get_instance();
85     return eti->get_key();
86 }
87 
88 class polymorphic_derived2 : public polymorphic_base
89 {
90     friend class boost::serialization::access;
91     template<class Archive>
serialize(Archive & ar,const unsigned int)92     void serialize(Archive &ar, const unsigned int /* file_version */){
93         ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base);
94     }
95 public:
96     virtual const char * get_key() const ;
97 };
98 
99 // note the mixing of type_info systems is supported.
BOOST_CLASS_TYPE_INFO(polymorphic_derived2,boost::serialization::extended_type_info_typeid<polymorphic_derived2>)100 BOOST_CLASS_TYPE_INFO(
101     polymorphic_derived2,
102     boost::serialization::extended_type_info_typeid<polymorphic_derived2>
103 )
104 
105 BOOST_CLASS_EXPORT(polymorphic_derived2)
106 
107 const char * polymorphic_derived2::get_key() const {
108     // use the exported key as the identifier
109     const boost::serialization::extended_type_info *eti
110         = boost::serialization::type_info_implementation<polymorphic_derived2>
111         ::type::get_instance();
112     return eti->get_key();
113 }
114 
115 // save derived polymorphic class
save_derived(const char * testfile)116 void save_derived(const char *testfile)
117 {
118     test_ostream os(testfile, TEST_STREAM_FLAGS);
119     test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
120 
121     polymorphic_derived1 *rd1 = new polymorphic_derived1;
122     polymorphic_derived2 *rd2 = new polymorphic_derived2;
123 
124     oa << BOOST_SERIALIZATION_NVP(rd1);
125     oa << BOOST_SERIALIZATION_NVP(rd2);
126 
127     // the above opereration registers the derived classes as a side
128     // effect.  Hence, instances can now be correctly serialized through
129     // a base class pointer.
130     polymorphic_base *rb1 =  rd1;
131     polymorphic_base *rb2 =  rd2;
132     oa << BOOST_SERIALIZATION_NVP(rb1);
133     oa << BOOST_SERIALIZATION_NVP(rb2);
134 
135     delete rd1;
136     delete rd2;
137 }
138 
139 // save derived polymorphic class
load_derived(const char * testfile)140 void load_derived(const char *testfile)
141 {
142     test_istream is(testfile, TEST_STREAM_FLAGS);
143     test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
144 
145     polymorphic_derived1 *rd1 = NULL;
146     polymorphic_derived2 *rd2 = NULL;
147 
148     ia >> BOOST_SERIALIZATION_NVP(rd1);
149 
150     BOOST_CHECK_MESSAGE(
151         boost::serialization::type_info_implementation<polymorphic_derived1>
152             ::type::get_instance()
153         == boost::serialization::type_info_implementation<polymorphic_derived1>
154             ::type::get_derived_extended_type_info(*rd1),
155         "restored pointer d1 not of correct type"
156     );
157 
158     ia >> BOOST_SERIALIZATION_NVP(rd2);
159 
160     BOOST_CHECK_MESSAGE(
161         boost::serialization::type_info_implementation<polymorphic_derived2>
162             ::type::get_instance()
163         == boost::serialization::type_info_implementation<polymorphic_derived2>
164             ::type::get_derived_extended_type_info(*rd2),
165         "restored pointer d2 not of correct type"
166     );
167 
168     polymorphic_base *rb1 = NULL;
169     polymorphic_base *rb2 = NULL;
170 
171     // the above opereration registers the derived classes as a side
172     // effect.  Hence, instances can now be correctly serialized through
173     // a base class pointer.
174     ia >> BOOST_SERIALIZATION_NVP(rb1);
175 
176     BOOST_CHECK_MESSAGE(
177         rb1 == dynamic_cast<polymorphic_base *>(rd1),
178         "serialized pointers not correctly restored"
179     );
180 
181     BOOST_CHECK_MESSAGE(
182         boost::serialization::type_info_implementation<polymorphic_derived1>
183             ::type::get_instance()
184         == boost::serialization::type_info_implementation<polymorphic_base>
185             ::type::get_derived_extended_type_info(*rb1),
186         "restored pointer b1 not of correct type"
187     );
188 
189     ia >> BOOST_SERIALIZATION_NVP(rb2);
190 
191     BOOST_CHECK_MESSAGE(
192         rb2 ==  dynamic_cast<polymorphic_base *>(rd2),
193         "serialized pointers not correctly restored"
194     );
195 
196     BOOST_CHECK_MESSAGE(
197         boost::serialization::type_info_implementation<polymorphic_derived2>
198             ::type::get_instance()
199         == boost::serialization::type_info_implementation<polymorphic_base>
200             ::type::get_derived_extended_type_info(*rb2),
201         "restored pointer b2 not of correct type"
202     );
203 
204     delete rb1;
205     delete rb2;
206 }
207 
208 int
test_main(int,char * [])209 test_main( int /* argc */, char* /* argv */[] )
210 {
211     const char * testfile = boost::archive::tmpnam(NULL);
212 
213     BOOST_REQUIRE(NULL != testfile);
214 
215     save_derived(testfile);
216     load_derived(testfile);
217 
218     std::remove(testfile);
219     return EXIT_SUCCESS;
220 }
221 
222 // EOF
223