[29] | 1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
| 2 | // test_diamond.cpp |
---|
| 3 | |
---|
| 4 | // (C) Copyright 2002 Vladimir Prus. |
---|
| 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 | // test of serialization library for diamond intheritence situations |
---|
| 10 | |
---|
| 11 | #include <fstream> |
---|
| 12 | #include <iostream> |
---|
| 13 | |
---|
| 14 | #include <cstdio> // remove |
---|
| 15 | #include <boost/config.hpp> |
---|
| 16 | #if defined(BOOST_NO_STDC_NAMESPACE) |
---|
| 17 | namespace std{ |
---|
| 18 | using ::remove; |
---|
| 19 | } |
---|
| 20 | #endif |
---|
| 21 | |
---|
| 22 | #include "test_tools.hpp" |
---|
| 23 | #include <boost/preprocessor/stringize.hpp> |
---|
| 24 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
---|
| 25 | |
---|
| 26 | #include <boost/serialization/map.hpp> |
---|
| 27 | #include <boost/serialization/utility.hpp> |
---|
| 28 | #include <boost/serialization/split_member.hpp> |
---|
| 29 | #include <boost/serialization/tracking.hpp> |
---|
| 30 | #include <boost/serialization/base_object.hpp> |
---|
| 31 | #include <boost/serialization/nvp.hpp> |
---|
| 32 | #include <boost/serialization/export.hpp> |
---|
| 33 | |
---|
| 34 | using namespace boost; |
---|
| 35 | |
---|
| 36 | int save_count = 0; // used to detect when base class is saved multiple times |
---|
| 37 | int load_count = 0; // used to detect when base class is loaded multiple times |
---|
| 38 | |
---|
| 39 | class base { |
---|
| 40 | public: |
---|
| 41 | base() : i(0) {} |
---|
| 42 | base(int i) : i(i) |
---|
| 43 | { |
---|
| 44 | m[i] = "text"; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | template<class Archive> |
---|
| 48 | void save(Archive &ar, const unsigned int /* file_version */) const |
---|
| 49 | { |
---|
| 50 | std::cout << "Saving base\n"; |
---|
| 51 | ar << BOOST_SERIALIZATION_NVP(i) << BOOST_SERIALIZATION_NVP(m); |
---|
| 52 | ++save_count; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | template<class Archive> |
---|
| 56 | void load(Archive & ar, const unsigned int /* file_version */) |
---|
| 57 | { |
---|
| 58 | std::cout << "Restoring base\n"; |
---|
| 59 | ar >> BOOST_SERIALIZATION_NVP(i) >> BOOST_SERIALIZATION_NVP(m); |
---|
| 60 | ++load_count; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | BOOST_SERIALIZATION_SPLIT_MEMBER() |
---|
| 64 | |
---|
| 65 | bool operator==(const base& another) const |
---|
| 66 | { |
---|
| 67 | return i == another.i && m == another.m; |
---|
| 68 | } |
---|
| 69 | // make virtual to evade gcc quirk |
---|
| 70 | virtual ~base() {}; |
---|
| 71 | private: |
---|
| 72 | int i; |
---|
| 73 | std::map<int, std::string> m; |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | // note: the default is for object tracking to be performed if and only |
---|
| 77 | // if and object of the corresponding class is anywhere serialized |
---|
| 78 | // through a pointer. In this example, that doesn't occur so |
---|
| 79 | // by default, the shared base object wouldn't normally be tracked. |
---|
| 80 | // This would leave to multiple save/load operation of the data in |
---|
| 81 | // this shared base class. This wouldn't cause an error, but it would |
---|
| 82 | // be a waste of time. So set the tracking behavior trait of the base |
---|
| 83 | // class to always track serialized objects of that class. This permits |
---|
| 84 | // the system to detect and elminate redundent save/load operations. |
---|
| 85 | // (It is concievable that this might someday be detected automatically |
---|
| 86 | // but for now, this is not done so we have to rely on the programmer |
---|
| 87 | // to specify this trait) |
---|
| 88 | BOOST_CLASS_TRACKING(base, track_always) |
---|
| 89 | |
---|
| 90 | class derived1 : virtual public base { |
---|
| 91 | public: |
---|
| 92 | template<class Archive> |
---|
| 93 | void save(Archive &ar, const unsigned int /* file_version */) const |
---|
| 94 | { |
---|
| 95 | std::cout << "Saving derived1\n"; |
---|
| 96 | ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(base); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | template<class Archive> |
---|
| 100 | void load(Archive & ar, const unsigned int /* file_version */) |
---|
| 101 | { |
---|
| 102 | std::cout << "Restoring derived1\n"; |
---|
| 103 | ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(base); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | BOOST_SERIALIZATION_SPLIT_MEMBER() |
---|
| 107 | }; |
---|
| 108 | |
---|
| 109 | class derived2 : virtual public base { |
---|
| 110 | public: |
---|
| 111 | template<class Archive> |
---|
| 112 | void save(Archive &ar, const unsigned int /* file_version */) const |
---|
| 113 | { |
---|
| 114 | std::cout << "Saving derived2\n"; |
---|
| 115 | ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(base); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | template<class Archive> |
---|
| 119 | void load(Archive & ar, const unsigned int /* file_version */) |
---|
| 120 | { |
---|
| 121 | std::cout << "Restoring derived2\n"; |
---|
| 122 | ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(base); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | BOOST_SERIALIZATION_SPLIT_MEMBER() |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | class final : public derived1, public derived2 { |
---|
| 129 | public: |
---|
| 130 | final() {} |
---|
| 131 | final(int i) : base(i) {} |
---|
| 132 | |
---|
| 133 | template<class Archive> |
---|
| 134 | void save(Archive &ar, const unsigned int /* file_version */) const |
---|
| 135 | { |
---|
| 136 | std::cout << "Saving final\n"; |
---|
| 137 | ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(derived1); |
---|
| 138 | ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(derived2); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | template<class Archive> |
---|
| 142 | void load(Archive & ar, const unsigned int /* file_version */) |
---|
| 143 | { |
---|
| 144 | std::cout << "Restoring final\n"; |
---|
| 145 | ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(derived1); |
---|
| 146 | ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(derived2); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | BOOST_SERIALIZATION_SPLIT_MEMBER() |
---|
| 150 | }; |
---|
| 151 | |
---|
| 152 | BOOST_CLASS_EXPORT(final) |
---|
| 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 | const final b(3); |
---|
| 161 | { |
---|
| 162 | test_ostream ofs(testfile, TEST_STREAM_FLAGS); |
---|
| 163 | test_oarchive oa(ofs); |
---|
| 164 | oa << boost::serialization::make_nvp("b", b); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | final b2; |
---|
| 168 | { |
---|
| 169 | test_istream ifs(testfile, TEST_STREAM_FLAGS); |
---|
| 170 | test_iarchive ia(ifs); |
---|
| 171 | ia >> boost::serialization::make_nvp("b2", b2); |
---|
| 172 | } |
---|
| 173 | BOOST_CHECK(1 == save_count); |
---|
| 174 | BOOST_CHECK(1 == load_count); |
---|
| 175 | BOOST_CHECK(b2 == b); |
---|
| 176 | std::remove(testfile); |
---|
| 177 | |
---|
| 178 | // do the same test with pointers |
---|
| 179 | testfile = boost::archive::tmpnam(NULL); |
---|
| 180 | BOOST_REQUIRE(NULL != testfile); |
---|
| 181 | |
---|
| 182 | save_count = 0; |
---|
| 183 | load_count = 0; |
---|
| 184 | |
---|
| 185 | const base* bp = new final( 3 ); |
---|
| 186 | { |
---|
| 187 | test_ostream ofs(testfile); |
---|
| 188 | test_oarchive oa(ofs); |
---|
| 189 | oa << BOOST_SERIALIZATION_NVP(bp); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | base* bp2; |
---|
| 193 | { |
---|
| 194 | test_istream ifs(testfile); |
---|
| 195 | test_iarchive ia(ifs); |
---|
| 196 | ia >> BOOST_SERIALIZATION_NVP(bp2); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | BOOST_CHECK(1 == save_count); |
---|
| 200 | BOOST_CHECK(1 == load_count); |
---|
| 201 | BOOST_CHECK(*bp2 == *bp); |
---|
| 202 | std::remove(testfile); |
---|
| 203 | |
---|
| 204 | return EXIT_SUCCESS; |
---|
| 205 | } |
---|