1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
2 | // test_split.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 | #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 | |
---|
21 | #include "test_tools.hpp" |
---|
22 | #include <boost/preprocessor/stringize.hpp> |
---|
23 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
---|
24 | |
---|
25 | #include <boost/serialization/split_member.hpp> |
---|
26 | #include <boost/serialization/split_free.hpp> |
---|
27 | |
---|
28 | class A |
---|
29 | { |
---|
30 | friend class boost::serialization::access; |
---|
31 | template<class Archive> |
---|
32 | void save( |
---|
33 | Archive & /* ar */, |
---|
34 | const unsigned int /* file_version */ |
---|
35 | ) const { |
---|
36 | ++(const_cast<A &>(*this).count); |
---|
37 | } |
---|
38 | template<class Archive> |
---|
39 | void load( |
---|
40 | Archive & /* ar */, |
---|
41 | const unsigned int /* file_version */ |
---|
42 | ){ |
---|
43 | --count; |
---|
44 | } |
---|
45 | BOOST_SERIALIZATION_SPLIT_MEMBER() |
---|
46 | int count; |
---|
47 | public: |
---|
48 | A() : count(0) {} |
---|
49 | ~A() { |
---|
50 | BOOST_CHECK(0 == count); |
---|
51 | } |
---|
52 | }; |
---|
53 | |
---|
54 | class B |
---|
55 | { |
---|
56 | friend class boost::serialization::access; |
---|
57 | template<class Archive> |
---|
58 | void save( |
---|
59 | Archive & /* ar */, |
---|
60 | const unsigned int /* file_version */ |
---|
61 | ) const { |
---|
62 | ++(const_cast<B &>(*this).count); |
---|
63 | } |
---|
64 | template<class Archive> |
---|
65 | void load( |
---|
66 | Archive & /* ar */, |
---|
67 | const unsigned int /* file_version */ |
---|
68 | ){ |
---|
69 | --count; |
---|
70 | } |
---|
71 | int count; |
---|
72 | public: |
---|
73 | B() : count(0) {} |
---|
74 | ~B() { |
---|
75 | BOOST_CHECK(0 == count); |
---|
76 | } |
---|
77 | }; |
---|
78 | |
---|
79 | // function specializations must be defined in the appropriate |
---|
80 | // namespace - boost::serialization |
---|
81 | namespace boost { |
---|
82 | namespace serialization { |
---|
83 | |
---|
84 | template<class Archive> |
---|
85 | void serialize( |
---|
86 | Archive & ar, |
---|
87 | B & b, |
---|
88 | const unsigned int file_version |
---|
89 | ){ |
---|
90 | boost::serialization::split_member(ar, b, file_version); |
---|
91 | } |
---|
92 | |
---|
93 | } // serialization |
---|
94 | } // namespace boost |
---|
95 | |
---|
96 | class C |
---|
97 | { |
---|
98 | public: |
---|
99 | int count; |
---|
100 | C() : count(0) {} |
---|
101 | ~C() { |
---|
102 | BOOST_CHECK(0 == count); |
---|
103 | } |
---|
104 | }; |
---|
105 | |
---|
106 | namespace boost { |
---|
107 | namespace serialization { |
---|
108 | |
---|
109 | template<class Archive> |
---|
110 | void save( |
---|
111 | Archive & /* ar */, |
---|
112 | const C & c, |
---|
113 | const unsigned int /* file_version */ |
---|
114 | ){ |
---|
115 | ++(const_cast<C &>(c).count); |
---|
116 | } |
---|
117 | |
---|
118 | template<class Archive> |
---|
119 | void load( |
---|
120 | Archive & /* ar */, |
---|
121 | C & c, |
---|
122 | const unsigned int /* file_version */ |
---|
123 | ){ |
---|
124 | --c.count; |
---|
125 | } |
---|
126 | |
---|
127 | } // serialization |
---|
128 | } // namespace boost |
---|
129 | |
---|
130 | BOOST_SERIALIZATION_SPLIT_FREE(C) |
---|
131 | |
---|
132 | void out(const char *testfile, A & a, B & b, C & c) |
---|
133 | { |
---|
134 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
135 | test_oarchive oa(os); |
---|
136 | oa << BOOST_SERIALIZATION_NVP(a); |
---|
137 | oa << BOOST_SERIALIZATION_NVP(b); |
---|
138 | oa << BOOST_SERIALIZATION_NVP(c); |
---|
139 | } |
---|
140 | |
---|
141 | void in(const char *testfile, A & a, B & b, C & c) |
---|
142 | { |
---|
143 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
144 | test_iarchive ia(is); |
---|
145 | ia >> BOOST_SERIALIZATION_NVP(a); |
---|
146 | ia >> BOOST_SERIALIZATION_NVP(b); |
---|
147 | ia >> BOOST_SERIALIZATION_NVP(c); |
---|
148 | } |
---|
149 | |
---|
150 | int |
---|
151 | test_main( int /* argc */, char* /* argv */[] ) |
---|
152 | { |
---|
153 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
154 | BOOST_REQUIRE(NULL != testfile); |
---|
155 | |
---|
156 | A a; |
---|
157 | B b; |
---|
158 | C c; |
---|
159 | |
---|
160 | out(testfile, a, b, c); |
---|
161 | in(testfile, a, b, c); |
---|
162 | std::remove(testfile); |
---|
163 | return EXIT_SUCCESS; |
---|
164 | } |
---|
165 | |
---|
166 | // EOF |
---|