]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_shared_ptr.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
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)
9 // See http://www.boost.org for updates, documentation, and revision history.
11 #include <cstddef> // NULL
12 #include <cstdio> // remove
15 #include <boost/config.hpp>
16 #if defined(BOOST_NO_STDC_NAMESPACE)
22 #include <boost/serialization/nvp.hpp>
23 #include <boost/serialization/export.hpp>
24 #include <boost/serialization/shared_ptr.hpp>
25 #include <boost/serialization/weak_ptr.hpp>
27 #include "test_tools.hpp"
29 // This is a simple class. It contains a counter of the number
30 // of objects of this class which have been instantiated.
34 friend class boost::serialization::access
;
36 template<class Archive
>
37 void serialize(Archive
& ar
, const unsigned int /* file_version */){
38 ar
& BOOST_SERIALIZATION_NVP(x
);
41 A
& operator=(A
const & rhs
);
44 bool operator==(const A
& rhs
) const {
47 A(){++count
;} // default constructor
48 virtual ~A(){--count
;} // default destructor
51 BOOST_SERIALIZATION_SHARED_PTR(A
)
55 // B is a subclass of A
59 friend class boost::serialization::access
;
60 template<class Archive
>
61 void serialize(Archive
& ar
, const unsigned int /* file_version */){
62 ar
& BOOST_SERIALIZATION_BASE_OBJECT_NVP(A
);
70 // B needs to be exported because its serialized via a base class pointer
72 BOOST_SERIALIZATION_SHARED_PTR(B
)
74 // test a non-polymorphic class
78 friend class boost::serialization::access
;
80 template<class Archive
>
81 void serialize(Archive
& ar
, const unsigned int /* file_version */){
82 ar
& BOOST_SERIALIZATION_NVP(z
);
86 bool operator==(const C
& rhs
) const {
90 z(++count
) // default constructor
92 virtual ~C(){--count
;} // default destructor
98 void save(const char * testfile
, const SP
& spa
)
100 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
101 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
102 oa
<< BOOST_SERIALIZATION_NVP(spa
);
106 void load(const char * testfile
, SP
& spa
)
108 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
109 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
110 ia
>> BOOST_SERIALIZATION_NVP(spa
);
115 void save_and_load(SP
& spa
)
117 const char * testfile
= boost::archive::tmpnam(NULL
);
118 BOOST_REQUIRE(NULL
!= testfile
);
121 load(testfile
, spa1
);
124 (spa
.get() == NULL
&& spa1
.get() == NULL
)
127 std::remove(testfile
);
132 const char * testfile
,
136 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
137 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
138 oa
<< BOOST_SERIALIZATION_NVP(first
);
139 oa
<< BOOST_SERIALIZATION_NVP(second
);
144 const char * testfile
,
148 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
149 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
150 ia
>> BOOST_SERIALIZATION_NVP(first
);
151 ia
>> BOOST_SERIALIZATION_NVP(second
);
154 // Run tests by serializing two shared_ptrs into an archive,
155 // clearing them (deleting the objects) and then reloading the
156 // objects back from an archive.
158 void save_and_load2(SP
& first
, SP
& second
)
160 const char * testfile
= boost::archive::tmpnam(NULL
);
161 BOOST_REQUIRE(NULL
!= testfile
);
163 save2(testfile
, first
, second
);
165 // Clear the pointers, thereby destroying the objects they contain
169 load2(testfile
, first
, second
);
171 BOOST_CHECK(first
== second
);
172 std::remove(testfile
);
175 template<class SP
, class WP
>
177 const char * testfile
,
182 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
183 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
184 oa
<< BOOST_SERIALIZATION_NVP(third
);
185 oa
<< BOOST_SERIALIZATION_NVP(first
);
186 oa
<< BOOST_SERIALIZATION_NVP(second
);
189 template<class SP
, class WP
>
191 const char * testfile
,
196 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
197 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
198 // note that we serialize the weak pointer first
199 ia
>> BOOST_SERIALIZATION_NVP(third
);
200 // inorder to test that a temporarily solitary weak pointer
201 // correctly restored.
202 ia
>> BOOST_SERIALIZATION_NVP(first
);
203 ia
>> BOOST_SERIALIZATION_NVP(second
);
206 template<class SP
, class WP
>
212 const char * testfile
= boost::archive::tmpnam(NULL
);
213 BOOST_REQUIRE(NULL
!= testfile
);
215 save3(testfile
, first
, second
, third
);
217 // Clear the pointers, thereby destroying the objects they contain
222 load3(testfile
, first
, second
, third
);
224 BOOST_CHECK(first
== second
);
225 BOOST_CHECK(first
== third
.lock());
226 std::remove(testfile
);
230 void save4(const char * testfile
, const SP
& spc
)
232 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
233 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
234 oa
<< BOOST_SERIALIZATION_NVP(spc
);
238 void load4(const char * testfile
, SP
& spc
)
240 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
241 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
242 ia
>> BOOST_SERIALIZATION_NVP(spc
);
247 void save_and_load4(SP
& spc
)
249 const char * testfile
= boost::archive::tmpnam(NULL
);
250 BOOST_REQUIRE(NULL
!= testfile
);
251 save4(testfile
, spc
);
253 load4(testfile
, spc1
);
256 (spc
.get() == NULL
&& spc1
.get() == NULL
)
259 std::remove(testfile
);
262 // This does the tests
263 template<template<class T
> class SPT
, template<class T
> class WPT
>
267 // These are our shared_ptrs
273 // These are our shared_ptrs
283 // Try to save and load pointers to As
286 save_and_load2(spa
, spa1
);
288 // Try to save and load pointers to Bs
291 save_and_load2(spa
, spa1
);
293 // test a weak pointer
297 save_and_load3(spa
, spa1
, wp
);
299 // obj of type B gets destroyed
300 // as smart_ptr goes out of scope
302 BOOST_CHECK(A::count
== 0);
304 // Try to save and load pointers to Cs
309 BOOST_CHECK(C::count
== 0);
312 // This does the tests
313 int test_main(int /* argc */, char * /* argv */[])
316 result
&= test
<boost::shared_ptr
, boost::weak_ptr
>();
317 #ifndef BOOST_NO_CXX11_SMART_PTR
318 result
&= test
<std::shared_ptr
, std::weak_ptr
>();
320 return result
? EXIT_SUCCESS
: EXIT_FAILURE
;