test_split.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_split.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. // should pass compilation and execution
  8. #include <cstddef> // NULL
  9. #include <cstdio> // remove
  10. #include <fstream>
  11. #include <boost/config.hpp>
  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/split_member.hpp>
  19. #include <boost/serialization/split_free.hpp>
  20. class A
  21. {
  22. friend class boost::serialization::access;
  23. template<class Archive>
  24. void save(
  25. Archive & /* ar */,
  26. const unsigned int /* file_version */
  27. ) const {
  28. ++(const_cast<A &>(*this).count);
  29. }
  30. template<class Archive>
  31. void load(
  32. Archive & /* ar */,
  33. const unsigned int /* file_version */
  34. ){
  35. --count;
  36. }
  37. BOOST_SERIALIZATION_SPLIT_MEMBER()
  38. int count;
  39. public:
  40. A() : count(0) {}
  41. ~A() {
  42. BOOST_CHECK(0 == count);
  43. }
  44. };
  45. class B
  46. {
  47. friend class boost::serialization::access;
  48. template<class Archive>
  49. void save(
  50. Archive & /* ar */,
  51. const unsigned int /* file_version */
  52. ) const {
  53. ++(const_cast<B &>(*this).count);
  54. }
  55. template<class Archive>
  56. void load(
  57. Archive & /* ar */,
  58. const unsigned int /* file_version */
  59. ){
  60. --count;
  61. }
  62. int count;
  63. public:
  64. B() : count(0) {}
  65. ~B() {
  66. BOOST_CHECK(0 == count);
  67. }
  68. };
  69. // function specializations must be defined in the appropriate
  70. // namespace - boost::serialization
  71. namespace boost {
  72. namespace serialization {
  73. template<class Archive>
  74. void serialize(
  75. Archive & ar,
  76. B & b,
  77. const unsigned int file_version
  78. ){
  79. boost::serialization::split_member(ar, b, file_version);
  80. }
  81. } // serialization
  82. } // namespace boost
  83. class C
  84. {
  85. public:
  86. int count;
  87. C() : count(0) {}
  88. ~C() {
  89. BOOST_CHECK(0 == count);
  90. }
  91. };
  92. namespace boost {
  93. namespace serialization {
  94. template<class Archive>
  95. void save(
  96. Archive & /* ar */,
  97. const C & c,
  98. const unsigned int /* file_version */
  99. ){
  100. ++(const_cast<C &>(c).count);
  101. }
  102. template<class Archive>
  103. void load(
  104. Archive & /* ar */,
  105. C & c,
  106. const unsigned int /* file_version */
  107. ){
  108. --c.count;
  109. }
  110. } // serialization
  111. } // namespace boost
  112. BOOST_SERIALIZATION_SPLIT_FREE(C)
  113. void out(const char *testfile, A & a, B & b, C & c)
  114. {
  115. test_ostream os(testfile, TEST_STREAM_FLAGS);
  116. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  117. oa << BOOST_SERIALIZATION_NVP(a);
  118. oa << BOOST_SERIALIZATION_NVP(b);
  119. oa << BOOST_SERIALIZATION_NVP(c);
  120. }
  121. void in(const char *testfile, A & a, B & b, C & c)
  122. {
  123. test_istream is(testfile, TEST_STREAM_FLAGS);
  124. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  125. ia >> BOOST_SERIALIZATION_NVP(a);
  126. ia >> BOOST_SERIALIZATION_NVP(b);
  127. ia >> BOOST_SERIALIZATION_NVP(c);
  128. }
  129. int
  130. test_main( int /* argc */, char* /* argv */[] )
  131. {
  132. const char * testfile = boost::archive::tmpnam(NULL);
  133. BOOST_REQUIRE(NULL != testfile);
  134. A a;
  135. B b;
  136. C c;
  137. out(testfile, a, b, c);
  138. in(testfile, a, b, c);
  139. std::remove(testfile);
  140. return EXIT_SUCCESS;
  141. }
  142. // EOF