test_multiple_inheritance.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_multiple_inheritance.cpp
  3. // (C) Copyright 2009 Robert Ramey.
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // test of serialization library for multiple inheritence situations
  8. #include <cassert>
  9. #include <fstream>
  10. #include <boost/config.hpp>
  11. #include <cstdio> // remove
  12. #if defined(BOOST_NO_STDC_NAMESPACE)
  13. namespace std{
  14. using ::remove;
  15. }
  16. #endif
  17. #include "test_tools.hpp"
  18. #include <boost/serialization/access.hpp>
  19. #include <boost/serialization/base_object.hpp>
  20. #include <boost/serialization/nvp.hpp>
  21. #include <boost/serialization/export.hpp>
  22. struct Base1 {
  23. int m_x;
  24. Base1(){}
  25. Base1(int x) : m_x(1 + x) {}
  26. virtual ~Base1() {}
  27. bool operator==(Base1 & rhs) const {
  28. return m_x == rhs.m_x;
  29. }
  30. // serialize
  31. friend class boost::serialization::access;
  32. template<class Archive>
  33. void serialize(Archive & ar, const unsigned int /* file_version */) {
  34. ar & BOOST_SERIALIZATION_NVP(m_x);
  35. }
  36. };
  37. //BOOST_CLASS_EXPORT(Base1)
  38. struct Base2 {
  39. int m_x;
  40. Base2(){}
  41. Base2(int x) : m_x(2 + x) {}
  42. virtual ~Base2() {}
  43. bool operator==(Base2 & rhs) const {
  44. return m_x == rhs.m_x;
  45. }
  46. // serialize
  47. friend class boost::serialization::access;
  48. template<class Archive>
  49. void serialize(Archive & ar, const unsigned int /* file_version */) {
  50. ar & BOOST_SERIALIZATION_NVP(m_x);
  51. }
  52. };
  53. //BOOST_CLASS_EXPORT(Base2)
  54. struct Sub :
  55. public Base1,
  56. public Base2
  57. {
  58. int m_x;
  59. Sub(){}
  60. Sub(int x) :
  61. Base1(x),
  62. Base2(x),
  63. m_x(x)
  64. {}
  65. bool operator==(Sub & rhs) const {
  66. if(! Base2::operator==(rhs))
  67. return false;
  68. if(! Base1::operator==(rhs))
  69. return false;
  70. return m_x == rhs.m_x;
  71. }
  72. virtual ~Sub() {}
  73. // serialize
  74. friend class boost::serialization::access;
  75. template<class Archive>
  76. void serialize(Archive &ar, const unsigned int /* file_version */)
  77. {
  78. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base1);
  79. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base2);
  80. ar & BOOST_SERIALIZATION_NVP(m_x);
  81. }
  82. };
  83. BOOST_CLASS_EXPORT(Sub)
  84. int
  85. test_main( int /* argc */, char* /* argv */[] )
  86. {
  87. const char * testfile = boost::archive::tmpnam(NULL);
  88. BOOST_REQUIRE(NULL != testfile);
  89. Base2 * pb2;
  90. {
  91. // serialize
  92. pb2 = new Sub(2);
  93. test_ostream ofs(testfile);
  94. test_oarchive oa(ofs);
  95. oa << boost::serialization::make_nvp("Base2", pb2);
  96. }
  97. Base2 * pb2_1;
  98. {
  99. // de-serialize
  100. test_istream ifs(testfile);
  101. test_iarchive ia(ifs);
  102. ia >> boost::serialization::make_nvp("Base2", pb2_1);
  103. }
  104. Sub *s1 = dynamic_cast<Sub *>(pb2);
  105. BOOST_CHECK(0 != s1);
  106. Sub *s2 = dynamic_cast<Sub *>(pb2_1);
  107. BOOST_CHECK(0 != s2);
  108. BOOST_CHECK(*s1 == *s2);
  109. return EXIT_SUCCESS;
  110. }