test_private_base2.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_private_base.cpp
  3. // (C) Copyright 2009 Eric Moyer - 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. // should pass compilation and execution
  8. // invoke header for a custom archive test.
  9. #include <fstream>
  10. #include <boost/config.hpp>
  11. #if defined(BOOST_NO_STDC_NAMESPACE)
  12. namespace std{
  13. using ::remove;
  14. }
  15. #endif
  16. #include <boost/serialization/access.hpp>
  17. #include <boost/serialization/base_object.hpp>
  18. #include <boost/serialization/export.hpp>
  19. #include "test_tools.hpp"
  20. class Base {
  21. friend class boost::serialization::access;
  22. int m_i;
  23. template<class Archive>
  24. void serialize(Archive & ar, const unsigned int version){
  25. ar & BOOST_SERIALIZATION_NVP(m_i);
  26. }
  27. protected:
  28. bool equals(const Base &rhs) const {
  29. return m_i == rhs.m_i;
  30. }
  31. Base(int i = 0) :
  32. m_i(i)
  33. {}
  34. virtual ~Base(){};
  35. public:
  36. virtual bool operator==(const Base &rhs) const {
  37. return false;
  38. }// = 0;
  39. };
  40. class Derived : private Base {
  41. friend class boost::serialization::access;
  42. public:
  43. virtual bool operator==(const Derived &rhs) const {
  44. return Base::equals(static_cast<const Base &>(rhs));
  45. }
  46. Derived(int i = 0) :
  47. Base(i)
  48. {}
  49. };
  50. //BOOST_CLASS_EXPORT(Derived)
  51. int
  52. test_main( int /* argc */, char* /* argv */[] )
  53. {
  54. const char * testfile = boost::archive::tmpnam(NULL);
  55. BOOST_REQUIRE(NULL != testfile);
  56. Derived a(1), a1(2);
  57. {
  58. test_ostream os(testfile, TEST_STREAM_FLAGS);
  59. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  60. oa << boost::serialization::make_nvp("a", a);
  61. }
  62. {
  63. test_istream is(testfile, TEST_STREAM_FLAGS);
  64. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  65. ia >> boost::serialization::make_nvp("a", a1);
  66. }
  67. BOOST_CHECK_EQUAL(a, a1);
  68. std::remove(testfile);
  69. //Base *ta = static_cast<Base *>(&a);
  70. //Base *ta1 = NULL;
  71. Derived *ta = &a;
  72. Derived *ta1 = NULL;
  73. {
  74. test_ostream os(testfile, TEST_STREAM_FLAGS);
  75. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  76. oa << boost::serialization::make_nvp("ta", ta);
  77. }
  78. {
  79. test_istream is(testfile, TEST_STREAM_FLAGS);
  80. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  81. ia >> boost::serialization::make_nvp("ta", ta1);
  82. }
  83. BOOST_CHECK(ta != ta1);
  84. BOOST_CHECK(*ta == *ta1);
  85. //BOOST_CHECK(*static_cast<Derived *>(ta) == *static_cast<Derived *>(ta1));
  86. std::remove(testfile);
  87. return 0;
  88. }