| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // test_cyclic_ptrs_trac9601.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 | |
| 18 | #include <cstdio> // remove |
| 19 | #include <fstream> |
| 20 | #include <memory> // auto_ptr |
| 21 | |
| 22 | #if defined(BOOST_NO_STDC_NAMESPACE) |
| 23 | namespace std { |
| 24 | using ::remove; |
| 25 | } |
| 26 | #endif |
| 27 | |
| 28 | struct pointer_holder |
| 29 | { |
| 30 | pointer_holder(pointer_holder* ptr = 0) |
| 31 | : ptr(ptr) |
| 32 | {} |
| 33 | |
| 34 | pointer_holder* ptr; |
| 35 | |
| 36 | template <typename Archive> |
| 37 | void serialize(Archive& ar, const unsigned int /*v*/) |
| 38 | { |
| 39 | ar & BOOST_SERIALIZATION_NVP(ptr); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | BOOST_CLASS_VERSION(pointer_holder,1) |
| 44 | |
| 45 | int test_main( int /* argc */, char* /* argv */[] ) |
| 46 | { |
| 47 | const char * testfile = boost::archive::tmpnam(0); |
| 48 | BOOST_REQUIRE(0 != testfile); |
| 49 | |
| 50 | //! Write it. |
| 51 | { |
| 52 | std::auto_ptr<pointer_holder> store(new pointer_holder()); |
| 53 | store->ptr = store.get(); |
| 54 | |
| 55 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
| 56 | test_oarchive ar(os, TEST_ARCHIVE_FLAGS); |
| 57 | |
| 58 | pointer_holder* pPtrHldr = store.get(); |
| 59 | ar << BOOST_SERIALIZATION_NVP(pPtrHldr); |
| 60 | } |
| 61 | |
| 62 | //! Read it. |
| 63 | { |
| 64 | pointer_holder* pPtrHldr = reinterpret_cast<pointer_holder*>(0xBAADF00D); |
| 65 | |
| 66 | test_istream is(testfile, TEST_STREAM_FLAGS); |
| 67 | test_iarchive ar(is, TEST_ARCHIVE_FLAGS); |
| 68 | |
| 69 | ar >> BOOST_SERIALIZATION_NVP(pPtrHldr); |
| 70 | |
| 71 | BOOST_CHECK(0 != pPtrHldr); |
| 72 | if (0 != pPtrHldr) |
| 73 | { |
| 74 | BOOST_CHECK(pPtrHldr->ptr == pPtrHldr); |
| 75 | } |
| 76 | |
| 77 | delete pPtrHldr; |
| 78 | } |
| 79 | |
| 80 | std::remove(testfile); |
| 81 | |
| 82 | return EXIT_SUCCESS; |
| 83 | } |