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