• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_class_info_save.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 <fstream>
13 #include <string>
14 
15 #include <boost/static_assert.hpp>
16 #include <boost/archive/tmpdir.hpp>
17 #include <boost/preprocessor/stringize.hpp>
18 #include "test_tools.hpp"
19 
20 #include <boost/serialization/level.hpp>
21 #include <boost/serialization/tracking.hpp>
22 #include <boost/serialization/nvp.hpp>
23 
24 // first case : serialize WITHOUT class information
25 class A
26 {
27     friend class boost::serialization::access;
28     template<class Archive>
serialize(Archive &,const unsigned int file_version)29     void serialize(Archive & /*ar*/, const unsigned int file_version){
30         // class that don't save class info always have a version number of 0
31         BOOST_CHECK(file_version == 0);
32         BOOST_STATIC_ASSERT(0 == ::boost::serialization::version<A>::value);
33         ++count;
34     }
35 public:
36     unsigned int count;
A()37     A() : count(0) {}
38 };
39 
40 BOOST_CLASS_IMPLEMENTATION(A, ::boost::serialization::object_serializable)
41 BOOST_CLASS_TRACKING(A, ::boost::serialization::track_never)
42 
43 // second case : serialize WITH class information
44 // note: GCC compile fails if this is after the class declaration
45 class B;
46 BOOST_CLASS_VERSION(B, 2)
47 
48 class B
49 {
50     friend class boost::serialization::access;
51     template<class Archive>
serialize(Archive &,const unsigned int file_version)52     void serialize(Archive & /*ar*/, const unsigned int file_version){
53         // verify at execution that correct version number is passed on save
54         BOOST_CHECK(
55             static_cast<const int>(file_version)
56             == ::boost::serialization::version<B>::value
57         );
58         ++count;
59     }
60 public:
61     unsigned int count;
B()62     B() : count(0) {}
63 };
64 
BOOST_CLASS_IMPLEMENTATION(B,::boost::serialization::object_class_info)65 BOOST_CLASS_IMPLEMENTATION(B, ::boost::serialization::object_class_info)
66 BOOST_CLASS_TRACKING(B, boost::serialization::track_always)
67 
68 #include <iostream>
69 
70 void out(const char *testfile, const A & a, const B & b)
71 {
72     test_ostream os(testfile, TEST_STREAM_FLAGS);
73     test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
74     // write object twice to check tracking
75     oa << BOOST_SERIALIZATION_NVP(a);
76     oa << BOOST_SERIALIZATION_NVP(a);
77     BOOST_CHECK(a.count == 2);  // no tracking => redundant saves
78     std::cout << "a.count=" << a.count << '\n' ;
79     oa << BOOST_SERIALIZATION_NVP(b);
80     oa << BOOST_SERIALIZATION_NVP(b);
81     BOOST_CHECK(b.count == 1);  // tracking => no redundant saves
82     std::cout << "b.count=" << b.count << '\n' ;
83 }
84 
85 int
test_main(int,char * [])86 test_main( int /* argc */, char* /* argv */[] )
87 {
88     A a;
89     B b;
90     std::string filename;
91     filename += boost::archive::tmpdir();
92     filename += '/';
93     filename += BOOST_PP_STRINGIZE(testfile_);
94     filename += BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST);
95     out(filename.c_str(), a, b);
96     return EXIT_SUCCESS;
97 }
98 
99 // EOF
100