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