1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 2 // test_multiple_inheritance.cpp 3 4 // (C) Copyright 2009 Robert Ramey. 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 library for multiple inheritence situations 10 11 #include <cassert> 12 #include <fstream> 13 14 #include <boost/config.hpp> 15 #include <cstdio> // remove 16 #if defined(BOOST_NO_STDC_NAMESPACE) 17 namespace std{ 18 using ::remove; 19 } 20 #endif 21 22 #include "test_tools.hpp" 23 24 #include <boost/serialization/access.hpp> 25 #include <boost/serialization/base_object.hpp> 26 #include <boost/serialization/nvp.hpp> 27 #include <boost/serialization/export.hpp> 28 29 struct Base1 { 30 int m_x; Base1Base131 Base1(){} Base1Base132 Base1(int x) : m_x(1 + x) {} ~Base1Base133 virtual ~Base1() {} operator ==Base134 bool operator==(Base1 & rhs) const { 35 return m_x == rhs.m_x; 36 } 37 // serialize 38 friend class boost::serialization::access; 39 template<class Archive> serializeBase140 void serialize(Archive & ar, const unsigned int /* file_version */) { 41 ar & BOOST_SERIALIZATION_NVP(m_x); 42 } 43 }; 44 45 //BOOST_CLASS_EXPORT(Base1) 46 47 struct Base2 { 48 int m_x; Base2Base249 Base2(){} Base2Base250 Base2(int x) : m_x(2 + x) {} ~Base2Base251 virtual ~Base2() {} operator ==Base252 bool operator==(Base2 & rhs) const { 53 return m_x == rhs.m_x; 54 } 55 // serialize 56 friend class boost::serialization::access; 57 template<class Archive> serializeBase258 void serialize(Archive & ar, const unsigned int /* file_version */) { 59 ar & BOOST_SERIALIZATION_NVP(m_x); 60 } 61 }; 62 63 //BOOST_CLASS_EXPORT(Base2) 64 65 struct Sub : 66 public Base1, 67 public Base2 68 { 69 int m_x; SubSub70 Sub(){} SubSub71 Sub(int x) : 72 Base1(x), 73 Base2(x), 74 m_x(x) 75 {} operator ==Sub76 bool operator==(Sub & rhs) const { 77 if(! Base2::operator==(rhs)) 78 return false; 79 if(! Base1::operator==(rhs)) 80 return false; 81 return m_x == rhs.m_x; 82 } ~SubSub83 virtual ~Sub() {} 84 85 // serialize 86 friend class boost::serialization::access; 87 template<class Archive> serializeSub88 void serialize(Archive &ar, const unsigned int /* file_version */) 89 { 90 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base1); 91 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base2); 92 ar & BOOST_SERIALIZATION_NVP(m_x); 93 } 94 }; 95 BOOST_CLASS_EXPORT(Sub)96 BOOST_CLASS_EXPORT(Sub) 97 98 int 99 test_main( int /* argc */, char* /* argv */[] ) 100 { 101 const char * testfile = boost::archive::tmpnam(NULL); 102 BOOST_REQUIRE(NULL != testfile); 103 Base2 * pb2; 104 { 105 // serialize 106 pb2 = new Sub(2); 107 108 test_ostream ofs(testfile); 109 test_oarchive oa(ofs); 110 oa << boost::serialization::make_nvp("Base2", pb2); 111 } 112 Base2 * pb2_1; 113 { 114 // de-serialize 115 test_istream ifs(testfile); 116 test_iarchive ia(ifs); 117 ia >> boost::serialization::make_nvp("Base2", pb2_1); 118 } 119 Sub *s1 = dynamic_cast<Sub *>(pb2); 120 BOOST_CHECK(0 != s1); 121 Sub *s2 = dynamic_cast<Sub *>(pb2_1); 122 BOOST_CHECK(0 != s2); 123 BOOST_CHECK(*s1 == *s2); 124 return EXIT_SUCCESS; 125 } 126