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