| 1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
|---|
| 2 | // test_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 | #include <fstream> |
|---|
| 12 | |
|---|
| 13 | #include <cstdio> // remove |
|---|
| 14 | #include <boost/config.hpp> |
|---|
| 15 | #if defined(BOOST_NO_STDC_NAMESPACE) |
|---|
| 16 | namespace std{ |
|---|
| 17 | using ::remove; |
|---|
| 18 | } |
|---|
| 19 | #endif |
|---|
| 20 | |
|---|
| 21 | #include <boost/archive/archive_exception.hpp> |
|---|
| 22 | #include "test_tools.hpp" |
|---|
| 23 | #include <boost/preprocessor/stringize.hpp> |
|---|
| 24 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
|---|
| 25 | |
|---|
| 26 | #include <boost/serialization/base_object.hpp> |
|---|
| 27 | #include <boost/serialization/export.hpp> |
|---|
| 28 | |
|---|
| 29 | class polymorphic_base |
|---|
| 30 | { |
|---|
| 31 | friend class boost::serialization::access; |
|---|
| 32 | template<class Archive> |
|---|
| 33 | void serialize(Archive & /* ar */, const unsigned int /* file_version */){ |
|---|
| 34 | } |
|---|
| 35 | public: |
|---|
| 36 | virtual ~polymorphic_base(){}; |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | BOOST_IS_ABSTRACT(polymorphic_base) |
|---|
| 40 | |
|---|
| 41 | class polymorphic_derived1 : public polymorphic_base |
|---|
| 42 | { |
|---|
| 43 | friend class boost::serialization::access; |
|---|
| 44 | template<class Archive> |
|---|
| 45 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
|---|
| 46 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base); |
|---|
| 47 | } |
|---|
| 48 | public: |
|---|
| 49 | virtual ~polymorphic_derived1(){} |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | BOOST_CLASS_EXPORT(polymorphic_derived1) |
|---|
| 53 | |
|---|
| 54 | class polymorphic_derived2 : public polymorphic_base |
|---|
| 55 | { |
|---|
| 56 | friend class boost::serialization::access; |
|---|
| 57 | template<class Archive> |
|---|
| 58 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
|---|
| 59 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base); |
|---|
| 60 | } |
|---|
| 61 | public: |
|---|
| 62 | virtual ~polymorphic_derived2(){} |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | BOOST_CLASS_EXPORT(polymorphic_derived2) |
|---|
| 66 | |
|---|
| 67 | // save exported polymorphic class |
|---|
| 68 | void save_exported(const char *testfile) |
|---|
| 69 | { |
|---|
| 70 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 71 | test_oarchive oa(os); |
|---|
| 72 | |
|---|
| 73 | polymorphic_base *rb1 = new polymorphic_derived1; |
|---|
| 74 | polymorphic_base *rb2 = new polymorphic_derived2; |
|---|
| 75 | |
|---|
| 76 | // export will permit correct serialization |
|---|
| 77 | // through a pointer to a base class |
|---|
| 78 | oa << BOOST_SERIALIZATION_NVP(rb1); |
|---|
| 79 | oa << BOOST_SERIALIZATION_NVP(rb2); |
|---|
| 80 | |
|---|
| 81 | delete rb1; |
|---|
| 82 | delete rb2; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | // save exported polymorphic class |
|---|
| 86 | void load_exported(const char *testfile) |
|---|
| 87 | { |
|---|
| 88 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 89 | test_iarchive ia(is); |
|---|
| 90 | |
|---|
| 91 | polymorphic_base *rb1 = NULL; |
|---|
| 92 | polymorphic_base *rb2 = NULL; |
|---|
| 93 | |
|---|
| 94 | // export will permit correct serialization |
|---|
| 95 | // through a pointer to a base class |
|---|
| 96 | ia >> BOOST_SERIALIZATION_NVP(rb1); |
|---|
| 97 | BOOST_CHECK_MESSAGE( |
|---|
| 98 | boost::serialization::type_info_implementation<polymorphic_derived1> |
|---|
| 99 | ::type::get_instance() |
|---|
| 100 | == boost::serialization::type_info_implementation<polymorphic_base> |
|---|
| 101 | ::type::get_derived_extended_type_info(*rb1), |
|---|
| 102 | "restored pointer b1 not of correct type" |
|---|
| 103 | ); |
|---|
| 104 | |
|---|
| 105 | ia >> BOOST_SERIALIZATION_NVP(rb2); |
|---|
| 106 | BOOST_CHECK_MESSAGE( |
|---|
| 107 | boost::serialization::type_info_implementation<polymorphic_derived2> |
|---|
| 108 | ::type::get_instance() |
|---|
| 109 | == boost::serialization::type_info_implementation<polymorphic_base> |
|---|
| 110 | ::type::get_derived_extended_type_info(*rb2), |
|---|
| 111 | "restored pointer b2 not of correct type" |
|---|
| 112 | ); |
|---|
| 113 | |
|---|
| 114 | delete rb1; |
|---|
| 115 | delete rb2; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | int |
|---|
| 119 | test_main( int /* argc */, char* /* argv */[] ) |
|---|
| 120 | { |
|---|
| 121 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 122 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 123 | |
|---|
| 124 | save_exported(testfile); |
|---|
| 125 | load_exported(testfile); |
|---|
| 126 | std::remove(testfile); |
|---|
| 127 | return EXIT_SUCCESS; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | // EOF |
|---|