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 // 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.
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.
21 #include <boost/config.hpp>
22 #include <cstdio> // remove
23 #if defined(BOOST_NO_STDC_NAMESPACE)
29 #include <boost/serialization/type_info_implementation.hpp>
30 #include <boost/serialization/export.hpp>
31 #include <boost/serialization/nvp.hpp>
33 #include "test_tools.hpp"
35 #include <boost/archive/polymorphic_oarchive.hpp>
36 #include <boost/archive/polymorphic_iarchive.hpp>
38 #include "polymorphic_base.hpp"
40 #include "polymorphic_derived1.hpp"
42 #include "polymorphic_derived2.hpp"
44 // save derived polymorphic class
45 void save_derived(const char *testfile)
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;
51 polymorphic_derived1 *rd1 = new polymorphic_derived1;
52 polymorphic_derived2 *rd2 = new polymorphic_derived2;
54 std::cout << "saving polymorphic_derived1 (no_rtti)\n";
55 oa_interface << BOOST_SERIALIZATION_NVP(rd1);
57 std::cout << "saving polymorphic_derived2\n";
58 oa_interface << BOOST_SERIALIZATION_NVP(rd2);
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);
71 void load_derived(const char *testfile)
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;
77 polymorphic_derived1 *rd1 = NULL;
78 polymorphic_derived2 *rd2 = NULL;
80 std::cout << "loading polymorphic_derived1 (no_rtti)\n";
81 ia_interface >> BOOST_SERIALIZATION_NVP(rd1);
83 boost::serialization::type_info_implementation<
85 >::type::get_const_instance()
87 * boost::serialization::type_info_implementation<
89 >::type::get_const_instance().get_derived_extended_type_info(*rd1)
91 "restored pointer d1 not of correct type"
94 std::cout << "loading polymorphic_derived2\n";
95 ia_interface >> BOOST_SERIALIZATION_NVP(rd2);
97 boost::serialization::type_info_implementation<
99 >::type::get_const_instance()
101 * boost::serialization::type_info_implementation<
103 >::type::get_const_instance().get_derived_extended_type_info(*rd2)
105 "restored pointer d2 not of correct type"
107 polymorphic_base *rb1 = NULL;
108 polymorphic_base *rb2 = NULL;
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);
117 rb1 == dynamic_cast<polymorphic_base *>(rd1),
118 "serialized pointers not correctly restored"
122 boost::serialization::type_info_implementation<
124 >::type::get_const_instance()
126 * boost::serialization::type_info_implementation<
128 >::type::get_const_instance().get_derived_extended_type_info(*rb1)
130 "restored pointer b1 not of correct type"
132 std::cout << "loading polymorphic_derived2 through base (no_rtti)\n";
133 ia_interface >> BOOST_SERIALIZATION_NVP(rb2);
136 rb2 == dynamic_cast<polymorphic_base *>(rd2),
137 "serialized pointers not correctly restored"
140 boost::serialization::type_info_implementation<
142 >::type::get_const_instance()
144 * boost::serialization::type_info_implementation<
146 >::type::get_const_instance().get_derived_extended_type_info(*rb2)
148 "restored pointer b2 not of correct type"
155 test_main( int /* argc */, char* /* argv */[] )
157 const char * testfile = boost::archive::tmpnam(NULL);
158 BOOST_REQUIRE(NULL != testfile);
160 save_derived(testfile);
161 load_derived(testfile);
162 std::remove(testfile);