Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / serialization / test / test_no_rtti.cpp
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 <cstddef>
18 #include <fstream>
19 #include <iostream>
20
21 #include <boost/config.hpp>
22 #include <cstdio> // remove
23 #if defined(BOOST_NO_STDC_NAMESPACE)
24 namespace std{ 
25     using ::remove;
26 }
27 #endif
28
29 #include <boost/serialization/type_info_implementation.hpp>
30 #include <boost/serialization/export.hpp>
31 #include <boost/serialization/nvp.hpp>
32
33 #include "test_tools.hpp"
34
35 #include <boost/archive/polymorphic_oarchive.hpp>
36 #include <boost/archive/polymorphic_iarchive.hpp>
37
38 #include "polymorphic_base.hpp"
39
40 #include "polymorphic_derived1.hpp"
41
42 #include "polymorphic_derived2.hpp"
43
44 // save derived polymorphic class
45 void save_derived(const char *testfile)
46 {
47     test_ostream os(testfile, TEST_STREAM_FLAGS);
48     test_oarchive oa_implementation(os, TEST_ARCHIVE_FLAGS);
49     boost::archive::polymorphic_oarchive & oa_interface = oa_implementation;
50
51     polymorphic_derived1 *rd1 = new polymorphic_derived1;
52     polymorphic_derived2 *rd2 = new polymorphic_derived2;
53
54     std::cout << "saving polymorphic_derived1 (no_rtti)\n";
55     oa_interface << BOOST_SERIALIZATION_NVP(rd1);
56
57     std::cout << "saving polymorphic_derived2\n";
58     oa_interface << BOOST_SERIALIZATION_NVP(rd2);
59
60     const polymorphic_base *rb1 =  rd1;
61     polymorphic_base *rb2 = rd2;
62     std::cout << "saving polymorphic_derived1 (no_rtti) through base (rtti)\n";
63     oa_interface << BOOST_SERIALIZATION_NVP(rb1);
64     std::cout << "saving polymorphic_derived2 through base\n";
65     oa_interface << BOOST_SERIALIZATION_NVP(rb2);
66
67     delete rd1;
68     delete rd2;
69 }
70
71 void load_derived(const char *testfile)
72 {
73     test_istream is(testfile, TEST_STREAM_FLAGS);
74     test_iarchive  ia_implementation(is, TEST_ARCHIVE_FLAGS);
75     boost::archive::polymorphic_iarchive & ia_interface = ia_implementation;
76
77     polymorphic_derived1 *rd1 = NULL;
78     polymorphic_derived2 *rd2 = NULL;
79
80     std::cout << "loading polymorphic_derived1 (no_rtti)\n";
81     ia_interface >> BOOST_SERIALIZATION_NVP(rd1);
82     BOOST_CHECK_MESSAGE(
83         boost::serialization::type_info_implementation<
84             polymorphic_derived1
85         >::type::get_const_instance()
86         == 
87         * boost::serialization::type_info_implementation<
88             polymorphic_derived1
89         >::type::get_const_instance().get_derived_extended_type_info(*rd1)
90         ,
91         "restored pointer d1 not of correct type"
92     );
93
94     std::cout << "loading polymorphic_derived2\n";
95     ia_interface >> BOOST_SERIALIZATION_NVP(rd2);
96     BOOST_CHECK_MESSAGE(
97         boost::serialization::type_info_implementation<
98             polymorphic_derived2
99         >::type::get_const_instance()
100         == 
101         * boost::serialization::type_info_implementation<
102             polymorphic_derived2
103         >::type::get_const_instance().get_derived_extended_type_info(*rd2)
104         ,
105         "restored pointer d2 not of correct type"
106     );
107     polymorphic_base *rb1 = NULL;
108     polymorphic_base *rb2 = NULL;
109
110     // the above opereration registers the derived classes as a side
111     // effect.  Hence, instances can now be correctly serialized through
112     // a base class pointer.
113     std::cout << "loading polymorphic_derived1 (no_rtti) through base (no_rtti)\n";
114     ia_interface >> BOOST_SERIALIZATION_NVP(rb1);
115
116     BOOST_CHECK_MESSAGE(
117         rb1 == dynamic_cast<polymorphic_base *>(rd1),
118         "serialized pointers not correctly restored"
119     );
120
121     BOOST_CHECK_MESSAGE(
122         boost::serialization::type_info_implementation<
123             polymorphic_derived1
124         >::type::get_const_instance()
125         == 
126         * boost::serialization::type_info_implementation<
127             polymorphic_base
128         >::type::get_const_instance().get_derived_extended_type_info(*rb1)
129         ,
130         "restored pointer b1 not of correct type"
131     );
132     std::cout << "loading polymorphic_derived2 through base (no_rtti)\n";
133     ia_interface >> BOOST_SERIALIZATION_NVP(rb2);
134
135     BOOST_CHECK_MESSAGE(
136         rb2 ==  dynamic_cast<polymorphic_base *>(rd2),
137         "serialized pointers not correctly restored"
138     );
139     BOOST_CHECK_MESSAGE(
140         boost::serialization::type_info_implementation<
141             polymorphic_derived2
142         >::type::get_const_instance()
143         == 
144         * boost::serialization::type_info_implementation<
145             polymorphic_base
146         >::type::get_const_instance().get_derived_extended_type_info(*rb2)
147         ,
148         "restored pointer b2 not of correct type"
149     );
150     delete rb1;
151     delete rb2;
152 }
153
154 int
155 test_main( int /* argc */, char* /* argv */[] )
156 {
157     const char * testfile = boost::archive::tmpnam(NULL);
158     BOOST_REQUIRE(NULL != testfile);
159
160     save_derived(testfile);
161     load_derived(testfile);
162     std::remove(testfile);
163     return EXIT_SUCCESS;
164 }
165
166 // EOF