test_private_base.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. public:
  35. };
  36. class Derived : private Base {
  37. friend class boost::serialization::access;
  38. private:
  39. Base & base_cast(){
  40. return static_cast<Base &>(*this);
  41. }
  42. template<class Archive>
  43. void serialize(Archive & ar, const unsigned int version){
  44. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
  45. }
  46. public:
  47. bool operator==(const Derived &rhs) const {
  48. return Base::equals(static_cast<const Base &>(rhs));
  49. }
  50. Derived(int i = 0) :
  51. Base(i)
  52. {}
  53. };
  54. int
  55. test_main( int /* argc */, char* /* argv */[] )
  56. {
  57. const char * testfile = boost::archive::tmpnam(NULL);
  58. BOOST_REQUIRE(NULL != testfile);
  59. Derived a(1), a1(2);
  60. {
  61. test_ostream os(testfile, TEST_STREAM_FLAGS);
  62. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  63. oa << boost::serialization::make_nvp("a", a);
  64. }
  65. {
  66. test_istream is(testfile, TEST_STREAM_FLAGS);
  67. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  68. ia >> boost::serialization::make_nvp("a", a1);
  69. }
  70. BOOST_CHECK_EQUAL(a, a1);
  71. std::remove(testfile);
  72. return 0;
  73. }