]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_non_intrusive.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_non_intrursive.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
12 // a) non-intrusive method of implementing serialization
13 // b) usage of a non-default constructor
16 #include <cstdlib> // for rand()
17 #include <cstdio> // remove
18 #include <boost/config.hpp>
19 #include <boost/detail/workaround.hpp>
20 #include <boost/limits.hpp>
21 #include <boost/math/special_functions/next.hpp>
23 #if defined(BOOST_NO_STDC_NAMESPACE)
27 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(UNDER_CE)
28 using ::numeric_limits
;
33 #include <boost/archive/archive_exception.hpp>
34 #include "test_tools.hpp"
36 ///////////////////////////////////////////////////////
37 // simple class test - using non-intrusive syntax
38 // illustrates the usage of the non-intrusve syntax
49 bool operator==(const A
& rhs
) const;
50 bool operator<(const A
& rhs
) const;
54 s(static_cast<signed char>(0xff & std::rand())),
55 t(static_cast<signed char>(0xff & std::rand())),
58 w((float)std::rand() / std::rand()),
59 x((double)std::rand() / std::rand())
63 bool A::operator==(const A
&rhs
) const
70 && std::abs( boost::math::float_distance(w
, rhs
.w
)) < 2
71 && std::abs( boost::math::float_distance(x
, rhs
.x
)) < 2
75 bool A::operator<(const A
&rhs
) const
85 if(std::abs( boost::math::float_distance(w
, rhs
.w
)) > 1)
87 if(std::abs( boost::math::float_distance(x
, rhs
.x
)) > 1)
92 // note the following:
94 // function specializations must be defined in the appropriate
95 // namespace - boost::serialization
97 namespace serialization
{
99 // This first set of overrides should work with all compilers.
101 // The last argument is int while the default versions
102 // defined in serialization.hpp have long as the last argument.
103 // This is part of the work around for compilers that don't
104 // support correct function template ordering. These functions
105 // are always called with 0 (i.e. an int) as the last argument.
106 // Our specialized versions also have int as the last argument
107 // while the default versions have a long as the last argument.
108 // This makes our specialized versions a better match than the
109 // default ones as no argument conversion is required to make a match
110 template<class Archive
>
114 const unsigned int /* file_version */
116 ar
& boost::serialization::make_nvp("s", a
.s
);
117 ar
& boost::serialization::make_nvp("t", a
.t
);
118 ar
& boost::serialization::make_nvp("u", a
.u
);
119 ar
& boost::serialization::make_nvp("v", a
.v
);
120 ar
& boost::serialization::make_nvp("w", a
.w
);
121 ar
& boost::serialization::make_nvp("x", a
.x
);
127 void save(const char * testfile
){
128 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
129 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
132 oa
<< BOOST_SERIALIZATION_NVP(a
);
134 // save a copy pointer to this item
136 oa
<< BOOST_SERIALIZATION_NVP(pa1
);
138 // save pointer to a new object
140 oa
<< BOOST_SERIALIZATION_NVP(pa2
);
145 void load(const char * testfile
){
146 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
147 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
150 ia
>> BOOST_SERIALIZATION_NVP(a
);
153 ia
>> BOOST_SERIALIZATION_NVP(pa1
);
154 BOOST_CHECK_MESSAGE(pa1
== &a
, "Copy of pointer not correctly restored");
157 ia
>> BOOST_SERIALIZATION_NVP(pa2
);
158 BOOST_CHECK_MESSAGE(pa2
!= &a
, "Pointer not correctly restored");
164 test_main( int /* argc */, char* /* argv */[] )
166 const char * testfile
= boost::archive::tmpnam(NULL
);
167 BOOST_REQUIRE(NULL
!= testfile
);
170 std::remove(testfile
);