1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
2 | // test_mi.cpp |
---|
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 of serialization of base classes with multiple inheritance |
---|
10 | // contributed by Peter Dimov |
---|
11 | |
---|
12 | #include <iostream> |
---|
13 | #include <fstream> |
---|
14 | |
---|
15 | #include <cstdio> // remove |
---|
16 | #include <boost/config.hpp> |
---|
17 | #if defined(BOOST_NO_STDC_NAMESPACE) |
---|
18 | namespace std{ |
---|
19 | using ::remove; |
---|
20 | } |
---|
21 | #endif |
---|
22 | |
---|
23 | #include "test_tools.hpp" |
---|
24 | #include <boost/preprocessor/stringize.hpp> |
---|
25 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
---|
26 | |
---|
27 | #include <boost/serialization/nvp.hpp> |
---|
28 | #include <boost/serialization/base_object.hpp> |
---|
29 | |
---|
30 | class A |
---|
31 | { |
---|
32 | private: |
---|
33 | friend class boost::serialization::access; |
---|
34 | int x; |
---|
35 | template<class Archive> |
---|
36 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
---|
37 | ar & BOOST_SERIALIZATION_NVP(x); |
---|
38 | } |
---|
39 | public: |
---|
40 | explicit A(int x = 0): x(x) {} |
---|
41 | virtual ~A(); // = 0; |
---|
42 | int get_x() const |
---|
43 | { |
---|
44 | return x; |
---|
45 | } |
---|
46 | }; |
---|
47 | |
---|
48 | inline A::~A() |
---|
49 | { |
---|
50 | } |
---|
51 | |
---|
52 | class B |
---|
53 | { |
---|
54 | private: |
---|
55 | int y; |
---|
56 | friend class boost::serialization::access; |
---|
57 | template<class Archive> |
---|
58 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
---|
59 | ar & BOOST_SERIALIZATION_NVP(y); |
---|
60 | } |
---|
61 | public: |
---|
62 | explicit B(int y = 0): y(y) {} |
---|
63 | virtual ~B(){} |
---|
64 | int get_y() const |
---|
65 | { |
---|
66 | return y; |
---|
67 | } |
---|
68 | }; |
---|
69 | |
---|
70 | class C: public A, public B |
---|
71 | { |
---|
72 | private: |
---|
73 | friend class boost::serialization::access; |
---|
74 | template<class Archive> |
---|
75 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
---|
76 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); |
---|
77 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(B); |
---|
78 | } |
---|
79 | public: |
---|
80 | C(){} |
---|
81 | C(int x, int y): A(x), B(y){} |
---|
82 | }; |
---|
83 | |
---|
84 | int |
---|
85 | test_main( int /* argc */, char* /* argv */[] ) |
---|
86 | { |
---|
87 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
88 | BOOST_REQUIRE(NULL != testfile); |
---|
89 | |
---|
90 | C * pc = new C(1, 2); |
---|
91 | A * pa = pc; |
---|
92 | B * pb = pc; |
---|
93 | |
---|
94 | BOOST_CHECK(pa == pc); |
---|
95 | BOOST_CHECK(pb == pc); |
---|
96 | |
---|
97 | int x = pa->get_x(); |
---|
98 | int y = pb->get_y(); |
---|
99 | |
---|
100 | std::cout << "pa->get_x(): " << pa->get_x() << std::endl; |
---|
101 | std::cout << "pb->get_y(): " << pb->get_y() << std::endl; |
---|
102 | |
---|
103 | { |
---|
104 | test_ostream ofs(testfile, TEST_STREAM_FLAGS); |
---|
105 | test_oarchive oa(ofs); |
---|
106 | oa << BOOST_SERIALIZATION_NVP(pc); |
---|
107 | oa << BOOST_SERIALIZATION_NVP(pa); |
---|
108 | oa << BOOST_SERIALIZATION_NVP(pb); |
---|
109 | } |
---|
110 | |
---|
111 | delete pc; |
---|
112 | pc = NULL; |
---|
113 | pa = NULL; |
---|
114 | pb = NULL; |
---|
115 | |
---|
116 | { |
---|
117 | test_istream ifs(testfile, TEST_STREAM_FLAGS); |
---|
118 | test_iarchive ia(ifs); |
---|
119 | ia >> BOOST_SERIALIZATION_NVP(pc); |
---|
120 | ia >> BOOST_SERIALIZATION_NVP(pa); |
---|
121 | ia >> BOOST_SERIALIZATION_NVP(pb); |
---|
122 | } |
---|
123 | |
---|
124 | BOOST_CHECK(pa == pc); |
---|
125 | BOOST_CHECK(pb == pc); |
---|
126 | |
---|
127 | BOOST_CHECK(x == pa->get_x()); |
---|
128 | BOOST_CHECK(y == pb->get_y()); |
---|
129 | |
---|
130 | std::cout << "pa->get_x(): " << pa->get_x() << std::endl; |
---|
131 | std::cout << "pb->get_y(): " << pb->get_y() << std::endl; |
---|
132 | |
---|
133 | delete pc; |
---|
134 | std::remove(testfile); |
---|
135 | return EXIT_SUCCESS; |
---|
136 | } |
---|