| 1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
|---|
| 2 | // test_shared_ptr.cpp |
|---|
| 3 | |
|---|
| 4 | // (C) Copyright 2002 Robert Ramey- http://www.rrsd.com - David Tonge . |
|---|
| 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 | // See http://www.boost.org for updates, documentation, and revision history. |
|---|
| 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 | #include <boost/type_traits/broken_compiler_spec.hpp> |
|---|
| 21 | |
|---|
| 22 | #include <boost/serialization/shared_ptr.hpp> |
|---|
| 23 | #include <boost/serialization/weak_ptr.hpp> |
|---|
| 24 | #include <boost/serialization/nvp.hpp> |
|---|
| 25 | |
|---|
| 26 | #include "test_tools.hpp" |
|---|
| 27 | #include <boost/preprocessor/stringize.hpp> |
|---|
| 28 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
|---|
| 29 | |
|---|
| 30 | #include <boost/serialization/export.hpp> |
|---|
| 31 | |
|---|
| 32 | // This is a simple class. It contains a counter of the number |
|---|
| 33 | // of objects of this class which have been instantiated. |
|---|
| 34 | class A |
|---|
| 35 | { |
|---|
| 36 | private: |
|---|
| 37 | friend class boost::serialization::access; |
|---|
| 38 | int x; |
|---|
| 39 | template<class Archive> |
|---|
| 40 | void serialize(Archive & ar, const unsigned int /* file_version */){ |
|---|
| 41 | ar & BOOST_SERIALIZATION_NVP(x); |
|---|
| 42 | } |
|---|
| 43 | public: |
|---|
| 44 | static int count; |
|---|
| 45 | bool operator==(const A & rhs) const { |
|---|
| 46 | return x == rhs.x; |
|---|
| 47 | } |
|---|
| 48 | A(){++count;} // default constructor |
|---|
| 49 | virtual ~A(){--count;} // default destructor |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | //BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(A) |
|---|
| 53 | BOOST_SERIALIZATION_SHARED_PTR(A) |
|---|
| 54 | |
|---|
| 55 | // B is a subclass of A |
|---|
| 56 | class B : public A |
|---|
| 57 | { |
|---|
| 58 | private: |
|---|
| 59 | friend class boost::serialization::access; |
|---|
| 60 | int y; |
|---|
| 61 | template<class Archive> |
|---|
| 62 | void serialize(Archive & ar, const unsigned int /* file_version */){ |
|---|
| 63 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); |
|---|
| 64 | } |
|---|
| 65 | public: |
|---|
| 66 | static int count; |
|---|
| 67 | B() : A() {}; |
|---|
| 68 | virtual ~B() {}; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | // B needs to be exported because its serialized via a base class pointer |
|---|
| 72 | BOOST_CLASS_EXPORT(B) |
|---|
| 73 | BOOST_SERIALIZATION_SHARED_PTR(B) |
|---|
| 74 | |
|---|
| 75 | int A::count = 0; |
|---|
| 76 | |
|---|
| 77 | void save(const char * testfile, const boost::shared_ptr<A>& spa) |
|---|
| 78 | { |
|---|
| 79 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 80 | test_oarchive oa(os); |
|---|
| 81 | oa << BOOST_SERIALIZATION_NVP(spa); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void load(const char * testfile, boost::shared_ptr<A>& spa) |
|---|
| 85 | { |
|---|
| 86 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 87 | test_iarchive ia(is); |
|---|
| 88 | ia >> BOOST_SERIALIZATION_NVP(spa); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | // trivial test |
|---|
| 92 | void save_and_load(boost::shared_ptr<A>& spa) |
|---|
| 93 | { |
|---|
| 94 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 95 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 96 | save(testfile, spa); |
|---|
| 97 | boost::shared_ptr<A> spa1; |
|---|
| 98 | load(testfile, spa1); |
|---|
| 99 | |
|---|
| 100 | BOOST_CHECK( |
|---|
| 101 | spa.get() == NULL && spa1.get() == NULL |
|---|
| 102 | || * spa == * spa1 |
|---|
| 103 | ); |
|---|
| 104 | std::remove(testfile); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | void save2( |
|---|
| 108 | const char * testfile, |
|---|
| 109 | const boost::shared_ptr<A>& first, |
|---|
| 110 | const boost::shared_ptr<A>& second |
|---|
| 111 | ){ |
|---|
| 112 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 113 | test_oarchive oa(os); |
|---|
| 114 | oa << BOOST_SERIALIZATION_NVP(first); |
|---|
| 115 | oa << BOOST_SERIALIZATION_NVP(second); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void load2( |
|---|
| 119 | const char * testfile, |
|---|
| 120 | boost::shared_ptr<A>& first, |
|---|
| 121 | boost::shared_ptr<A>& second) |
|---|
| 122 | { |
|---|
| 123 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 124 | test_iarchive ia(is); |
|---|
| 125 | ia >> BOOST_SERIALIZATION_NVP(first); |
|---|
| 126 | ia >> BOOST_SERIALIZATION_NVP(second); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | // Run tests by serializing two shared_ptrs into an archive, |
|---|
| 130 | // clearing them (deleting the objects) and then reloading the |
|---|
| 131 | // objects back from an archive. |
|---|
| 132 | void save_and_load2(boost::shared_ptr<A>& first, boost::shared_ptr<A>& second) |
|---|
| 133 | { |
|---|
| 134 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 135 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 136 | |
|---|
| 137 | save2(testfile, first, second); |
|---|
| 138 | |
|---|
| 139 | // Clear the pointers, thereby destroying the objects they contain |
|---|
| 140 | first.reset(); |
|---|
| 141 | second.reset(); |
|---|
| 142 | |
|---|
| 143 | load2(testfile, first, second); |
|---|
| 144 | |
|---|
| 145 | BOOST_CHECK(first == second); |
|---|
| 146 | std::remove(testfile); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | void save3( |
|---|
| 150 | const char * testfile, |
|---|
| 151 | boost::shared_ptr<A>& first, |
|---|
| 152 | boost::shared_ptr<A>& second, |
|---|
| 153 | boost::weak_ptr<A>& third |
|---|
| 154 | ){ |
|---|
| 155 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 156 | test_oarchive oa(os); |
|---|
| 157 | oa << BOOST_SERIALIZATION_NVP(third); |
|---|
| 158 | oa << BOOST_SERIALIZATION_NVP(first); |
|---|
| 159 | oa << BOOST_SERIALIZATION_NVP(second); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | void load3( |
|---|
| 163 | const char * testfile, |
|---|
| 164 | boost::shared_ptr<A>& first, |
|---|
| 165 | boost::shared_ptr<A>& second, |
|---|
| 166 | boost::weak_ptr<A>& third |
|---|
| 167 | ){ |
|---|
| 168 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 169 | test_iarchive ia(is); |
|---|
| 170 | // note that we serialize the weak pointer first |
|---|
| 171 | ia >> BOOST_SERIALIZATION_NVP(third); |
|---|
| 172 | // inorder to test that a temporarily solitary weak pointer |
|---|
| 173 | // correcttly restored. |
|---|
| 174 | ia >> BOOST_SERIALIZATION_NVP(first); |
|---|
| 175 | ia >> BOOST_SERIALIZATION_NVP(second); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | void save_and_load3( |
|---|
| 179 | boost::shared_ptr<A>& first, |
|---|
| 180 | boost::shared_ptr<A>& second, |
|---|
| 181 | boost::weak_ptr<A>& third |
|---|
| 182 | ){ |
|---|
| 183 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 184 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 185 | |
|---|
| 186 | save3(testfile, first, second, third); |
|---|
| 187 | |
|---|
| 188 | // Clear the pointers, thereby destroying the objects they contain |
|---|
| 189 | first.reset(); |
|---|
| 190 | second.reset(); |
|---|
| 191 | third.reset(); |
|---|
| 192 | |
|---|
| 193 | load3(testfile, first, second, third); |
|---|
| 194 | |
|---|
| 195 | BOOST_CHECK(first == second); |
|---|
| 196 | BOOST_CHECK(first == third.lock()); |
|---|
| 197 | std::remove(testfile); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | // This does the tests |
|---|
| 201 | int test_main(int /* argc */, char * /* argv */[]) |
|---|
| 202 | { |
|---|
| 203 | // These are our shared_ptrs |
|---|
| 204 | boost::shared_ptr<A> spa; |
|---|
| 205 | |
|---|
| 206 | // trivial test 1 |
|---|
| 207 | save_and_load(spa); |
|---|
| 208 | |
|---|
| 209 | //trivival test 2 |
|---|
| 210 | spa = boost::shared_ptr<A>(new A); |
|---|
| 211 | save_and_load(spa); |
|---|
| 212 | |
|---|
| 213 | // Try to save and load pointers to As, to a text archive |
|---|
| 214 | spa = boost::shared_ptr<A>(new A); |
|---|
| 215 | boost::shared_ptr<A> spa1 = spa; |
|---|
| 216 | save_and_load2(spa, spa1); |
|---|
| 217 | |
|---|
| 218 | // test a weak pointer |
|---|
| 219 | spa = boost::shared_ptr<A>(new A); |
|---|
| 220 | spa1 = spa; |
|---|
| 221 | boost::weak_ptr<A> wp = spa; |
|---|
| 222 | save_and_load3(spa, spa1, wp); |
|---|
| 223 | |
|---|
| 224 | // Try to save and load pointers to Bs, to a text archive |
|---|
| 225 | spa = boost::shared_ptr<A>(new B); |
|---|
| 226 | spa1 = spa; |
|---|
| 227 | save_and_load2(spa, spa1); |
|---|
| 228 | |
|---|
| 229 | // obj of type B gets destroyed |
|---|
| 230 | // as smart_ptr goes out of scope |
|---|
| 231 | return EXIT_SUCCESS; |
|---|
| 232 | } |
|---|