Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/serialization/test/test_class_info_load.cpp @ 29

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

updated boost from 1_33_1 to 1_34_1

File size: 2.9 KB
Line 
1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// test_class_info_load.cpp: test implementation level trait
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// test implementation level "object_class_info"
10// should pass compilation and execution
11
12#include <string>
13#include <fstream>
14
15#include <boost/archive/tmpdir.hpp>
16#include <boost/preprocessor/stringize.hpp>
17#include "test_tools.hpp"
18#include <boost/preprocessor/stringize.hpp>
19#include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
20
21#include <boost/static_assert.hpp>
22#include <boost/serialization/level.hpp>
23#include <boost/serialization/tracking.hpp>
24#include <boost/serialization/version.hpp>
25#include <boost/serialization/nvp.hpp>
26
27class A
28{
29    friend class boost::serialization::access;
30    template<class Archive>
31    void serialize(Archive & /*ar*/, const unsigned int file_version){
32        // class that don't save class info always have a version number of 0
33        BOOST_CHECK(file_version == 0);
34        BOOST_STATIC_ASSERT(0 == ::boost::serialization::version<A>::value);
35        ++count;
36    }
37public:
38    unsigned int count;
39    A() : count(0) {}
40};
41
42BOOST_CLASS_IMPLEMENTATION(A, ::boost::serialization::object_serializable)
43BOOST_CLASS_TRACKING(A, ::boost::serialization::track_never)
44
45// second case : serialize WITH class information
46class B
47{
48    friend class boost::serialization::access;
49    template<class Archive>
50    void serialize(Archive & /*ar*/, const unsigned int file_version){
51        // verify at execution that the version number corresponds to the saved
52        // one
53        BOOST_CHECK(file_version == 2);
54        ++count;
55    }
56public:
57    unsigned int count;
58    B() : count(0) {}
59};
60
61BOOST_CLASS_IMPLEMENTATION(B, ::boost::serialization::object_class_info)
62BOOST_CLASS_TRACKING(B, ::boost::serialization::track_never)
63BOOST_CLASS_VERSION(B, 4)
64
65void in(const char *testfile, A & a, B & b)
66{
67    test_istream is(testfile, TEST_STREAM_FLAGS);
68    test_iarchive ia(is);
69    ia >> BOOST_SERIALIZATION_NVP(a);
70    ia >> BOOST_SERIALIZATION_NVP(a);
71    BOOST_CHECK(a.count == 2);  // no tracking => redundant loads
72    ia >> BOOST_SERIALIZATION_NVP(b);
73    ia >> BOOST_SERIALIZATION_NVP(b);
74    // note: archive was saved with tracking so that is what determines
75    // whether tracking is perform on load - regardless of the latest
76    // tracking setting.
77    BOOST_CHECK(b.count == 1);
78}
79
80int
81test_main( int /* argc */, char* /* argv */[] )
82{
83    A a;
84    B b;
85    std::string filename;
86    filename += boost::archive::tmpdir();
87    filename += '/';
88    filename += BOOST_PP_STRINGIZE(testfile_);
89    filename += BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST);
90    in(filename.c_str(), a, b);
91    return EXIT_SUCCESS;
92}
93
94// EOF
Note: See TracBrowser for help on using the repository browser.