| | 1 | /////////////////////////////////////////////////////////////////////////////// |
| | 2 | // test_cyclic_ptrs_multiple_inheritance.cpp |
| | 3 | // |
| | 4 | // (C) Copyright 2014 |
| | 5 | // Brandon Kohn |
| | 6 | // |
| | 7 | // Distributed under the Boost Software License, Version 1.0. (See |
| | 8 | // accompanying file LICENSE_1_0.txt or copy at |
| | 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| | 10 | // |
| | 11 | #include <boost/config.hpp> |
| | 12 | |
| | 13 | #include "test_tools.hpp" |
| | 14 | |
| | 15 | #include <boost/serialization/nvp.hpp> |
| | 16 | #include <boost/serialization/version.hpp> |
| | 17 | #include <boost/serialization/base_object.hpp> |
| | 18 | #include <boost/serialization/export.hpp> |
| | 19 | |
| | 20 | #include <cstdio> // remove |
| | 21 | #include <fstream> |
| | 22 | #include <memory> // auto_ptr |
| | 23 | |
| | 24 | #if defined(BOOST_NO_STDC_NAMESPACE) |
| | 25 | namespace std { |
| | 26 | using ::remove; |
| | 27 | } |
| | 28 | #endif |
| | 29 | |
| | 30 | struct base_a; |
| | 31 | struct base_b; |
| | 32 | struct common_ab; |
| | 33 | |
| | 34 | struct base_a |
| | 35 | { |
| | 36 | base_a(base_b * ptr = reinterpret_cast<base_b*>(0xDEADBEEF)) |
| | 37 | : ptr_a(ptr) |
| | 38 | {} |
| | 39 | |
| | 40 | virtual ~base_a(){} |
| | 41 | |
| | 42 | base_b * ptr_a; |
| | 43 | |
| | 44 | template <typename Archive> |
| | 45 | void serialize(Archive& ar, const unsigned int /*v*/) |
| | 46 | { |
| | 47 | ar & BOOST_SERIALIZATION_NVP(ptr_a); |
| | 48 | } |
| | 49 | }; |
| | 50 | |
| | 51 | BOOST_CLASS_VERSION(base_a,1) |
| | 52 | |
| | 53 | struct base_b |
| | 54 | { |
| | 55 | base_b(base_a * ptr = reinterpret_cast<base_a*>(0xDEADDEAD)) |
| | 56 | : ptr_b(ptr) |
| | 57 | {} |
| | 58 | |
| | 59 | virtual ~base_b(){} |
| | 60 | |
| | 61 | base_a * ptr_b; |
| | 62 | |
| | 63 | template <typename Archive> |
| | 64 | void serialize(Archive& ar, const unsigned int /*v*/) |
| | 65 | { |
| | 66 | ar & BOOST_SERIALIZATION_NVP(ptr_b); |
| | 67 | } |
| | 68 | }; |
| | 69 | |
| | 70 | BOOST_CLASS_VERSION(base_b,1) |
| | 71 | |
| | 72 | struct common_ab : base_a, base_b |
| | 73 | { |
| | 74 | common_ab() |
| | 75 | : base_a() |
| | 76 | , base_b() |
| | 77 | , ptr(reinterpret_cast<common_ab*>(0xBAADF00D)) |
| | 78 | {} |
| | 79 | |
| | 80 | virtual ~common_ab(){} |
| | 81 | |
| | 82 | common_ab * ptr; |
| | 83 | |
| | 84 | template <typename Archive> |
| | 85 | void serialize(Archive& ar, const unsigned int /*v*/) |
| | 86 | { |
| | 87 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_a); |
| | 88 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_b); |
| | 89 | ar & BOOST_SERIALIZATION_NVP(ptr); |
| | 90 | } |
| | 91 | }; |
| | 92 | |
| | 93 | BOOST_CLASS_VERSION(common_ab,1) |
| | 94 | BOOST_CLASS_EXPORT(common_ab) |
| | 95 | |
| | 96 | //! Create an instance of common_ab where all ptr member variables point to the single instance. |
| | 97 | std::auto_ptr<common_ab> make_common() |
| | 98 | { |
| | 99 | std::auto_ptr<common_ab> p(new common_ab()); |
| | 100 | p->ptr = p.get(); |
| | 101 | p->ptr_a = static_cast<base_b*>(p.get()); |
| | 102 | p->ptr_b = static_cast<base_a*>(p.get()); |
| | 103 | return p; |
| | 104 | } |
| | 105 | |
| | 106 | int test_main( int /* argc */, char* /* argv */[] ) |
| | 107 | { |
| | 108 | const char * testfile = boost::archive::tmpnam(0); |
| | 109 | BOOST_REQUIRE(0 != testfile); |
| | 110 | |
| | 111 | //! Write it. |
| | 112 | { |
| | 113 | std::auto_ptr<common_ab> store(make_common()); |
| | 114 | |
| | 115 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
| | 116 | test_oarchive ar(os, TEST_ARCHIVE_FLAGS); |
| | 117 | |
| | 118 | common_ab* pPtrHldr = store.get(); |
| | 119 | ar << BOOST_SERIALIZATION_NVP(pPtrHldr); |
| | 120 | } |
| | 121 | |
| | 122 | //! Read it. |
| | 123 | { |
| | 124 | common_ab* pPtrHldr = reinterpret_cast<common_ab*>(0xBAADBAAD); |
| | 125 | |
| | 126 | test_istream is(testfile, TEST_STREAM_FLAGS); |
| | 127 | test_iarchive ar(is, TEST_ARCHIVE_FLAGS); |
| | 128 | |
| | 129 | ar >> BOOST_SERIALIZATION_NVP(pPtrHldr); |
| | 130 | |
| | 131 | BOOST_CHECK(0 != pPtrHldr); |
| | 132 | if (0 != pPtrHldr) |
| | 133 | { |
| | 134 | //! Check that all the ptrs are the same object. |
| | 135 | BOOST_CHECK(pPtrHldr->ptr == pPtrHldr); |
| | 136 | BOOST_CHECK(pPtrHldr->ptr_a == static_cast<base_b*>(pPtrHldr)); |
| | 137 | BOOST_CHECK(pPtrHldr->ptr_b == static_cast<base_a*>(pPtrHldr)); |
| | 138 | } |
| | 139 | |
| | 140 | delete pPtrHldr; |
| | 141 | } |
| | 142 | |
| | 143 | std::remove(testfile); |
| | 144 | |
| | 145 | return EXIT_SUCCESS; |
| | 146 | } |