test_class_info_save.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_class_info_save.cpp: test implementation level trait
  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 implementation level "object_class_info"
  8. // should pass compilation and execution
  9. #include <fstream>
  10. #include <string>
  11. #include <boost/static_assert.hpp>
  12. #include <boost/archive/tmpdir.hpp>
  13. #include <boost/preprocessor/stringize.hpp>
  14. #include "test_tools.hpp"
  15. #include <boost/serialization/level.hpp>
  16. #include <boost/serialization/version.hpp>
  17. #include <boost/serialization/tracking.hpp>
  18. #include <boost/serialization/nvp.hpp>
  19. // first case : serialize WITHOUT class information
  20. class A
  21. {
  22. friend class boost::serialization::access;
  23. template<class Archive>
  24. void serialize(Archive & /*ar*/, const unsigned int file_version){
  25. // class that don't save class info always have a version number of 0
  26. BOOST_CHECK(file_version == 0);
  27. BOOST_STATIC_ASSERT(0 == ::boost::serialization::version<A>::value);
  28. ++count;
  29. }
  30. public:
  31. unsigned int count;
  32. A() : count(0) {}
  33. };
  34. BOOST_CLASS_IMPLEMENTATION(A, ::boost::serialization::object_serializable)
  35. BOOST_CLASS_TRACKING(A, ::boost::serialization::track_never)
  36. // second case : serialize WITH class information
  37. // note: GCC compile fails if this is after the class declaration
  38. class B;
  39. BOOST_CLASS_VERSION(B, 2)
  40. class B
  41. {
  42. friend class boost::serialization::access;
  43. template<class Archive>
  44. void serialize(Archive & /*ar*/, const unsigned int file_version){
  45. // verify at execution that correct version number is passed on save
  46. BOOST_CHECK(
  47. static_cast<const int>(file_version)
  48. == ::boost::serialization::version<B>::value
  49. );
  50. ++count;
  51. }
  52. public:
  53. unsigned int count;
  54. B() : count(0) {}
  55. };
  56. BOOST_CLASS_IMPLEMENTATION(B, ::boost::serialization::object_class_info)
  57. BOOST_CLASS_TRACKING(B, boost::serialization::track_always)
  58. #include <iostream>
  59. void out(const char *testfile, const A & a, const B & b)
  60. {
  61. test_ostream os(testfile, TEST_STREAM_FLAGS);
  62. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  63. // write object twice to check tracking
  64. oa << BOOST_SERIALIZATION_NVP(a);
  65. oa << BOOST_SERIALIZATION_NVP(a);
  66. BOOST_CHECK(a.count == 2); // no tracking => redundant saves
  67. std::cout << "a.count=" << a.count << '\n' ;
  68. oa << BOOST_SERIALIZATION_NVP(b);
  69. oa << BOOST_SERIALIZATION_NVP(b);
  70. BOOST_CHECK(b.count == 1); // tracking => no redundant saves
  71. std::cout << "b.count=" << b.count << '\n' ;
  72. }
  73. int
  74. test_main( int /* argc */, char* /* argv */[] )
  75. {
  76. A a;
  77. B b;
  78. std::string filename;
  79. filename += boost::archive::tmpdir();
  80. filename += '/';
  81. filename += BOOST_PP_STRINGIZE(testfile_);
  82. filename += BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST);
  83. out(filename.c_str(), a, b);
  84. return EXIT_SUCCESS;
  85. }
  86. // EOF