Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/serialization/test/test_split.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.4 KB
Line 
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)
16namespace 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
28class 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;
47public:
48    A() : count(0) {}
49    ~A() {
50        BOOST_CHECK(0 == count);
51    }
52};
53
54class 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;
72public:
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
81namespace boost { 
82namespace serialization {
83
84template<class Archive>
85void 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
96class C
97{
98public:
99    int count;
100    C() : count(0) {}
101    ~C() {
102        BOOST_CHECK(0 == count);
103    }
104};
105
106namespace boost { 
107namespace serialization {
108
109template<class Archive>
110void save(
111    Archive & /* ar */, 
112    const C & c,
113    const unsigned int /* file_version */
114){
115    ++(const_cast<C &>(c).count);
116}
117
118template<class Archive>
119void 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
130BOOST_SERIALIZATION_SPLIT_FREE(C)
131
132void 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
141void 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
150int
151test_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
Note: See TracBrowser for help on using the repository browser.