B.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef BOOST_SERIALIZATION_TEST_B_HPP
  2. #define BOOST_SERIALIZATION_TEST_B_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // B.hpp
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. #include <cstdlib> // for rand()
  15. #include <cmath>
  16. #include <boost/math/special_functions/next.hpp>
  17. #include <boost/config.hpp>
  18. #if defined(BOOST_NO_STDC_NAMESPACE)
  19. namespace std{
  20. using ::rand;
  21. }
  22. #endif
  23. #include <boost/serialization/version.hpp>
  24. #include <boost/serialization/split_member.hpp>
  25. #include <boost/serialization/base_object.hpp>
  26. #include "A.hpp"
  27. ///////////////////////////////////////////////////////
  28. // Derived class test
  29. class B : public A
  30. {
  31. private:
  32. friend class boost::serialization::access;
  33. template<class Archive>
  34. void save(Archive &ar, const unsigned int /* file_version */) const
  35. {
  36. // write any base class info to the archive
  37. ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(A);
  38. // write out members
  39. ar << BOOST_SERIALIZATION_NVP(s);
  40. ar << BOOST_SERIALIZATION_NVP(t);
  41. ar << BOOST_SERIALIZATION_NVP(u);
  42. ar << BOOST_SERIALIZATION_NVP(v);
  43. ar << BOOST_SERIALIZATION_NVP(w);
  44. ar << BOOST_SERIALIZATION_NVP(x);
  45. }
  46. template<class Archive>
  47. void load(Archive & ar, const unsigned int file_version)
  48. {
  49. // read any base class info to the archive
  50. ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(A);
  51. switch(file_version){
  52. case 1:
  53. case 2:
  54. ar >> BOOST_SERIALIZATION_NVP(s);
  55. ar >> BOOST_SERIALIZATION_NVP(t);
  56. ar >> BOOST_SERIALIZATION_NVP(u);
  57. ar >> BOOST_SERIALIZATION_NVP(v);
  58. ar >> BOOST_SERIALIZATION_NVP(w);
  59. ar >> BOOST_SERIALIZATION_NVP(x);
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. BOOST_SERIALIZATION_SPLIT_MEMBER()
  66. signed char s;
  67. unsigned char t;
  68. signed int u;
  69. unsigned int v;
  70. float w;
  71. double x;
  72. public:
  73. B();
  74. virtual ~B(){};
  75. bool operator==(const B &rhs) const;
  76. };
  77. B::B() :
  78. s(static_cast<signed char>(std::rand())),
  79. t(static_cast<unsigned char>(std::rand())),
  80. u(std::rand()),
  81. v(std::rand()),
  82. w((float)std::rand() / std::rand()),
  83. x((double)std::rand() / std::rand())
  84. {
  85. }
  86. BOOST_CLASS_VERSION(B, 2)
  87. inline bool B::operator==(const B &rhs) const
  88. {
  89. return
  90. A::operator==(rhs)
  91. && s == rhs.s
  92. && t == rhs.t
  93. && u == rhs.u
  94. && v == rhs.v
  95. && std::abs( boost::math::float_distance(w, rhs.w)) < 2
  96. && std::abs( boost::math::float_distance(x, rhs.x)) < 2
  97. ;
  98. }
  99. #endif // BOOST_SERIALIZATION_TEST_B_HPP