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