test_mi.cpp 3.0 KB

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