]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_cyclic_ptrs.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_cyclic_ptrs.cpp
4 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
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 // should pass compilation and execution
11 #include <cstddef> // NULL
14 #include <cstdio> // remove
15 #include <boost/config.hpp>
16 #if defined(BOOST_NO_STDC_NAMESPACE)
22 #include "test_tools.hpp"
23 #include <boost/core/no_exceptions_support.hpp>
25 #include <boost/serialization/nvp.hpp>
26 #include <boost/serialization/version.hpp>
27 #include <boost/serialization/base_object.hpp>
32 ///////////////////////////////////////////////////////
33 // class with a member which refers to itself
37 friend class boost::serialization::access
;
38 template<class Archive
>
39 void serialize(Archive
&ar
, const unsigned int /* file_version */){
40 ar
& BOOST_SERIALIZATION_BASE_OBJECT_NVP(A
);
41 ar
& BOOST_SERIALIZATION_NVP(j
);
44 bool operator==(const J
&rhs
) const;
50 BOOST_CLASS_VERSION(J
, 6)
52 bool J::operator==(const J
&rhs
) const
54 return static_cast<const A
&>(*this) == static_cast<const A
&>(rhs
);
57 ///////////////////////////////////////////////////////
58 // class with members that refer to each other
59 // this is an example of a class that, as written, cannot
60 // be serialized with this system. The problem is that the
61 // serialization of the first member - j1 , provokes serialization
62 // of those objects which it points to either directly or indirectly.
63 // When those objects are subsequently serialized, it is discovered
64 // that have already been serialized through pointers. This is
65 // detected by the system and an exception - pointer_conflict -
66 // is thrown. Permiting this to go undetected would result in the
67 // creation of multiple equal objects rather than the original
74 friend class boost::serialization::access
;
75 template<class Archive
>
78 const unsigned int /* file_version */
80 ar
& BOOST_SERIALIZATION_NVP(j1
);
81 ar
& BOOST_SERIALIZATION_NVP(j2
);
82 ar
& BOOST_SERIALIZATION_NVP(j3
);
85 bool operator==(const K
&rhs
) const;
90 : j1(&j2
), j2(&j3
), j3(&j1
)
94 bool K::operator==(const K
&rhs
) const
107 const char * testfile
= boost::archive::tmpnam(NULL
);
108 BOOST_REQUIRE(NULL
!= testfile
);
112 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
113 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
114 oa
<< BOOST_SERIALIZATION_NVP(j1
);
117 // try to read the archive
118 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
119 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
120 ia
>> BOOST_SERIALIZATION_NVP(j2
);
122 BOOST_CHECK(j1
== j2
);
123 std::remove(testfile
);
128 const char * testfile
= boost::archive::tmpnam(NULL
);
129 BOOST_REQUIRE(NULL
!= testfile
);
133 J
*j2
= reinterpret_cast<J
*>(0xBAADF00D);
135 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
136 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
137 oa
<< BOOST_SERIALIZATION_NVP(j1
);
140 // try to read the archive
141 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
142 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
143 ia
>> BOOST_SERIALIZATION_NVP(j2
);
145 BOOST_CHECK(*j1
== *j2
);
146 BOOST_CHECK(j2
== j2
->j
);
147 std::remove(testfile
);
152 const char * testfile
= boost::archive::tmpnam(NULL
);
153 BOOST_REQUIRE(NULL
!= testfile
);
156 boost::archive::archive_exception
exception(
157 boost::archive::archive_exception::no_exception
160 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
161 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
163 oa
<< BOOST_SERIALIZATION_NVP(k
);
165 BOOST_CATCH (boost::archive::archive_exception ae
){
170 exception
.code
== boost::archive::archive_exception::pointer_conflict
173 // if exception wasn't invoked
174 if(exception
.code
== boost::archive::archive_exception::no_exception
){
175 // try to read the archive
176 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
177 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
178 exception
= boost::archive::archive_exception(
179 boost::archive::archive_exception::no_exception
182 ia
>> BOOST_SERIALIZATION_NVP(k
);
184 BOOST_CATCH (boost::archive::archive_exception ae
){
189 exception
.code
== boost::archive::archive_exception::pointer_conflict
192 std::remove(testfile
);
196 int test_main( int /* argc */, char* /* argv */[] ){