1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
2 | // test_non_intrursive.cpp |
---|
3 | |
---|
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) |
---|
8 | |
---|
9 | // should pass compilation and execution |
---|
10 | |
---|
11 | // this tests: |
---|
12 | // a) non-intrusive method of implementing serialization |
---|
13 | // b) usage of a non-default constructor |
---|
14 | |
---|
15 | #include <fstream> |
---|
16 | #include <cstdlib> // for rand() |
---|
17 | #include <cstdio> // remove |
---|
18 | #include <cmath> // for fabs() |
---|
19 | |
---|
20 | #include <boost/config.hpp> |
---|
21 | #if defined(BOOST_NO_STDC_NAMESPACE) |
---|
22 | namespace std{ |
---|
23 | using ::rand; |
---|
24 | using ::fabs; |
---|
25 | using ::remove; |
---|
26 | } |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <boost/archive/archive_exception.hpp> |
---|
30 | #include "test_tools.hpp" |
---|
31 | #include <boost/preprocessor/stringize.hpp> |
---|
32 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
---|
33 | |
---|
34 | /////////////////////////////////////////////////////// |
---|
35 | // simple class test - using non-intrusive syntax |
---|
36 | // illustrates the usage of the non-intrusve syntax |
---|
37 | class A |
---|
38 | { |
---|
39 | public: |
---|
40 | signed char s; |
---|
41 | unsigned char t; |
---|
42 | signed int u; |
---|
43 | unsigned int v; |
---|
44 | float w; |
---|
45 | double x; |
---|
46 | A(); |
---|
47 | bool operator==(const A & rhs) const; |
---|
48 | bool operator<(const A & rhs) const; |
---|
49 | }; |
---|
50 | |
---|
51 | A::A() : |
---|
52 | s(std::rand()), |
---|
53 | t(std::rand()), |
---|
54 | u(std::rand()), |
---|
55 | v(std::rand()), |
---|
56 | w((float)std::rand() / std::rand()), |
---|
57 | x((double)std::rand() / std::rand()) |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | bool A::operator==(const A &rhs) const |
---|
62 | { |
---|
63 | return |
---|
64 | s == rhs.s |
---|
65 | && t == rhs.t |
---|
66 | && u == rhs.u |
---|
67 | && v == rhs.v |
---|
68 | && std::fabs(w - rhs.w) <= std::numeric_limits<float>::round_error() |
---|
69 | && std::fabs(x - rhs.x) <= std::numeric_limits<float>::round_error() |
---|
70 | ; |
---|
71 | } |
---|
72 | |
---|
73 | bool A::operator<(const A &rhs) const |
---|
74 | { |
---|
75 | if(! s == rhs.s ) |
---|
76 | return s < rhs.s; |
---|
77 | if(! t == rhs.t ) |
---|
78 | return t < rhs.t; |
---|
79 | if(! u == rhs.u ) |
---|
80 | return t < rhs.u; |
---|
81 | if(! v == rhs.v ) |
---|
82 | return t < rhs.v; |
---|
83 | if(! (std::fabs(w - rhs.w) < std::numeric_limits<float>::round_error() ) ) |
---|
84 | return t < rhs.w; |
---|
85 | if(! (std::fabs(x - rhs.x) < std::numeric_limits<float>::round_error() ) ) |
---|
86 | return t < rhs.x; |
---|
87 | return false; |
---|
88 | } |
---|
89 | |
---|
90 | // note the following: |
---|
91 | |
---|
92 | // function specializations must be defined in the appropriate |
---|
93 | // namespace - boost::serialization |
---|
94 | namespace boost { |
---|
95 | namespace serialization { |
---|
96 | |
---|
97 | // This first set of overrides should work with all compilers. |
---|
98 | |
---|
99 | // The last argument is int while the default versions |
---|
100 | // defined in serialization.hpp have long as the last argument. |
---|
101 | // This is part of the work around for compilers that don't |
---|
102 | // support correct function template ordering. These functions |
---|
103 | // are always called with 0 (i.e. an int) as the last argument. |
---|
104 | // Our specialized versions also have int as the last argument |
---|
105 | // while the default versions have a long as the last argument. |
---|
106 | // This makes our specialized versions a better match than the |
---|
107 | // default ones as no argument conversion is required to make a match |
---|
108 | template<class Archive> |
---|
109 | void serialize( |
---|
110 | Archive & ar, |
---|
111 | A & a, |
---|
112 | const unsigned int /* file_version */ |
---|
113 | ){ |
---|
114 | ar & boost::serialization::make_nvp("s", a.s); |
---|
115 | ar & boost::serialization::make_nvp("t", a.t); |
---|
116 | ar & boost::serialization::make_nvp("u", a.u); |
---|
117 | ar & boost::serialization::make_nvp("v", a.v); |
---|
118 | ar & boost::serialization::make_nvp("w", a.w); |
---|
119 | ar & boost::serialization::make_nvp("x", a.x); |
---|
120 | } |
---|
121 | |
---|
122 | } // serialization |
---|
123 | } // namespace boost |
---|
124 | |
---|
125 | void save(const char * testfile){ |
---|
126 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
127 | test_oarchive oa(os); |
---|
128 | A a; |
---|
129 | |
---|
130 | oa << BOOST_SERIALIZATION_NVP(a); |
---|
131 | |
---|
132 | // save a copy pointer to this item |
---|
133 | A *pa1 = &a; |
---|
134 | oa << BOOST_SERIALIZATION_NVP(pa1); |
---|
135 | |
---|
136 | // save pointer to a new object |
---|
137 | A *pa2 = new A(); |
---|
138 | oa << BOOST_SERIALIZATION_NVP(pa2); |
---|
139 | |
---|
140 | delete pa2; |
---|
141 | } |
---|
142 | |
---|
143 | void load(const char * testfile){ |
---|
144 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
145 | test_iarchive ia(is); |
---|
146 | |
---|
147 | A a; |
---|
148 | ia >> BOOST_SERIALIZATION_NVP(a); |
---|
149 | |
---|
150 | A *pa1; |
---|
151 | ia >> BOOST_SERIALIZATION_NVP(pa1); |
---|
152 | BOOST_CHECK_MESSAGE(pa1 == &a, "Copy of pointer not correctly restored"); |
---|
153 | |
---|
154 | A *pa2; |
---|
155 | ia >> BOOST_SERIALIZATION_NVP(pa2); |
---|
156 | BOOST_CHECK_MESSAGE(pa2 != &a, "Pointer not correctly restored"); |
---|
157 | |
---|
158 | delete pa2; |
---|
159 | } |
---|
160 | |
---|
161 | int |
---|
162 | test_main( int /* argc */, char* /* argv */[] ) |
---|
163 | { |
---|
164 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
165 | BOOST_REQUIRE(NULL != testfile); |
---|
166 | save(testfile); |
---|
167 | load(testfile); |
---|
168 | std::remove(testfile); |
---|
169 | return EXIT_SUCCESS; |
---|
170 | } |
---|
171 | |
---|
172 | // EOF |
---|