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