| 1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 2 | // test_dll_exported.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 | // This is an overly complex test. The purpose of this test is to |
| 12 | // demonstrate and test the ability to serialize a hierarchy of class |
| 13 | // through a base class pointer even though those class might be |
| 14 | // implemented in different dlls and use different extended type info |
| 15 | // systems. |
| 16 | // |
| 17 | // polymorphic_ base is locally declared and defined. It use the |
| 18 | // "no_rtti" extended type info system. |
| 19 | |
| 20 | // polymorphic_derived1 is locally declared and defined. It uses |
| 21 | // the default "type_id" extended type info system |
| 22 | |
| 23 | // polymorphic_derived2 is declared in polymorphic_derived.hpp |
| 24 | // and defined in dll_polymorphic_derived2. It uses the typeid |
| 25 | // system. |
| 26 | |
| 27 | #include <cstddef> // NULL |
| 28 | #include <fstream> |
| 29 | |
| 30 | #include <cstdio> // remove |
| 31 | #include <boost/config.hpp> |
| 32 | #if defined(BOOST_NO_STDC_NAMESPACE) |
| 33 | namespace std{ |
| 34 | using ::remove; |
| 35 | } |
| 36 | #endif |
| 37 | |
| 38 | #include <boost/archive/polymorphic_oarchive.hpp> |
| 39 | #include <boost/archive/polymorphic_iarchive.hpp> |
| 40 | |
| 41 | #include "test_tools.hpp" |
| 42 | |
| 43 | #include <boost/archive/polymorphic_oarchive.hpp> |
| 44 | #include <boost/archive/polymorphic_iarchive.hpp> |
| 45 | |
| 46 | #include <boost/serialization/base_object.hpp> |
| 47 | #include <boost/serialization/export.hpp> |
| 48 | #include <boost/serialization/access.hpp> |
| 49 | |
| 50 | #define POLYMORPHIC_BASE_IMPORT |
| 51 | #include "polymorphic_base.hpp" |
| 52 | |
| 53 | #include "polymorphic_derived1.hpp" |
| 54 | |
| 55 | #define POLYMORPHIC_DERIVED2_IMPORT |
| 56 | #include "polymorphic_derived2.hpp" |
| 57 | |
| 58 | // save exported polymorphic class |
| 59 | void save_exported(const char *testfile) |
| 60 | { |
| 61 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
| 62 | test_oarchive oa_implementation(os, TEST_ARCHIVE_FLAGS); |
| 63 | boost::archive::polymorphic_oarchive & oa_interface = oa_implementation; |
| 64 | |
| 65 | polymorphic_base *rb1 = new polymorphic_derived1; |
| 66 | polymorphic_base *rb2 = new polymorphic_derived2; |
| 67 | polymorphic_derived2 *rd21 = new polymorphic_derived2; |
| 68 | |
| 69 | // export will permit correct serialization |
| 70 | // through a pointer to a base class |
| 71 | oa_interface << BOOST_SERIALIZATION_NVP(rb1); |
| 72 | oa_interface << BOOST_SERIALIZATION_NVP(rb2); |
| 73 | oa_interface << BOOST_SERIALIZATION_NVP(rd21); |
| 74 | |
| 75 | delete rd21; |
| 76 | delete rb2; |
| 77 | delete rb1; |
| 78 | } |
| 79 | |
| 80 | // save exported polymorphic class |
| 81 | void load_exported(const char *testfile) |
| 82 | { |
| 83 | test_istream is(testfile, TEST_STREAM_FLAGS); |
| 84 | test_iarchive ia_implementation(is, TEST_ARCHIVE_FLAGS); |
| 85 | boost::archive::polymorphic_iarchive & ia_interface = ia_implementation; |
| 86 | |
| 87 | polymorphic_base *rb1 = NULL; |
| 88 | polymorphic_base *rb2 = NULL; |
| 89 | polymorphic_derived2 *rd21 = NULL; |
| 90 | |
| 91 | // export will permit correct serialization |
| 92 | // through a pointer to a base class |
| 93 | ia_interface >> BOOST_SERIALIZATION_NVP(rb1); |
| 94 | BOOST_CHECK_MESSAGE( |
| 95 | boost::serialization::type_info_implementation<polymorphic_derived1> |
| 96 | ::type::get_const_instance() |
| 97 | == |
| 98 | * boost::serialization::type_info_implementation<polymorphic_base> |
| 99 | ::type::get_const_instance().get_derived_extended_type_info(*rb1), |
| 100 | "restored pointer b1 not of correct type" |
| 101 | ); |
| 102 | ia_interface >> BOOST_SERIALIZATION_NVP(rb2); |
| 103 | BOOST_CHECK_MESSAGE( |
| 104 | boost::serialization::type_info_implementation<polymorphic_derived2> |
| 105 | ::type::get_const_instance() |
| 106 | == |
| 107 | * boost::serialization::type_info_implementation<polymorphic_base> |
| 108 | ::type::get_const_instance().get_derived_extended_type_info(*rb2), |
| 109 | "restored pointer b2 not of correct type" |
| 110 | ); |
| 111 | ia_interface >> BOOST_SERIALIZATION_NVP(rd21); |
| 112 | BOOST_CHECK_MESSAGE( |
| 113 | boost::serialization::type_info_implementation<polymorphic_derived2> |
| 114 | ::type::get_const_instance() |
| 115 | == |
| 116 | * boost::serialization::type_info_implementation<polymorphic_derived2> |
| 117 | ::type::get_const_instance().get_derived_extended_type_info(*rd21), |
| 118 | "restored pointer d2 not of correct type" |
| 119 | ); |
| 120 | delete rd21; |
| 121 | delete rb2; |
| 122 | delete rb1; |
| 123 | } |
| 124 | |
| 125 | int |
| 126 | test_main( int /* argc */, char* /* argv */[] ) |
| 127 | { |
| 128 | const char * testfile = boost::archive::tmpnam(NULL); |
| 129 | BOOST_REQUIRE(NULL != testfile); |
| 130 | |
| 131 | save_exported(testfile); |
| 132 | load_exported(testfile); |
| 133 | std::remove(filename: testfile); |
| 134 | return EXIT_SUCCESS; |
| 135 | } |
| 136 | |
| 137 | // EOF |
| 138 | |