| 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 that 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 <boost/config.hpp> |
| 31 |
|
| 32 |
#include <boost/archive/archive_exception.hpp> |
| 33 |
|
| 34 |
#include <boost/serialization/base_object.hpp> |
| 35 |
#include <boost/serialization/export.hpp> |
| 36 |
|
| 37 |
#include <boost/archive/text_oarchive.hpp> |
| 38 |
#include <boost/archive/text_iarchive.hpp> |
| 39 |
|
| 40 |
#include "polymorphic_base.hpp" |
| 41 |
|
| 42 |
#include "polymorphic_derived2.hpp" |
| 43 |
|
| 44 |
// save exported polymorphic class |
| 45 |
void save_exported(const char *testfile) |
| 46 |
{ |
| 47 |
std::ofstream os(testfile); |
| 48 |
boost::archive::text_oarchive oa(os); |
| 49 |
|
| 50 |
polymorphic_base *rb2 = new polymorphic_derived2; |
| 51 |
polymorphic_derived2 *rd21 = new polymorphic_derived2; |
| 52 |
|
| 53 |
// export will permit correct serialization |
| 54 |
// through a pointer to a base class |
| 55 |
oa << BOOST_SERIALIZATION_NVP(rb2); |
| 56 |
oa << BOOST_SERIALIZATION_NVP(rd21); |
| 57 |
|
| 58 |
delete rb2; |
| 59 |
delete rd21; |
| 60 |
} |
| 61 |
|
| 62 |
// save exported polymorphic class |
| 63 |
void load_exported(const char *testfile) |
| 64 |
{ |
| 65 |
std::ifstream is(testfile); |
| 66 |
boost::archive::text_iarchive ia(is); |
| 67 |
|
| 68 |
polymorphic_base *rb2 = NULL; |
| 69 |
polymorphic_derived2 *rd21 = NULL; |
| 70 |
|
| 71 |
// export will permit correct serialization |
| 72 |
// through a pointer to a base class |
| 73 |
ia >> BOOST_SERIALIZATION_NVP(rb2); |
| 74 |
assert( |
| 75 |
boost::serialization::type_info_implementation<polymorphic_derived2> |
| 76 |
::type::get_const_instance() |
| 77 |
== |
| 78 |
* boost::serialization::type_info_implementation<polymorphic_base> |
| 79 |
::type::get_const_instance().get_derived_extended_type_info(*rb2)); |
| 80 |
ia >> BOOST_SERIALIZATION_NVP(rd21); |
| 81 |
assert( |
| 82 |
boost::serialization::type_info_implementation<polymorphic_derived2> |
| 83 |
::type::get_const_instance() |
| 84 |
== |
| 85 |
* boost::serialization::type_info_implementation<polymorphic_derived2> |
| 86 |
::type::get_const_instance().get_derived_extended_type_info(*rd21)); |
| 87 |
delete rb2; |
| 88 |
delete rd21; |
| 89 |
} |
| 90 |
|
| 91 |
int main( int /* argc */, char* /* argv */[] ) |
| 92 |
{ |
| 93 |
save_exported("testfile"); |
| 94 |
load_exported("testfile"); |
| 95 |
return EXIT_SUCCESS; |
| 96 |
} |
| 97 |
|
| 98 |
// EOF |