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