test_class_info_load.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_class_info_load.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 <string>
  10. #include <fstream>
  11. #include <boost/archive/tmpdir.hpp>
  12. #include <boost/preprocessor/stringize.hpp>
  13. #include "test_tools.hpp"
  14. #include <boost/static_assert.hpp>
  15. #include <boost/serialization/level.hpp>
  16. #include <boost/serialization/tracking.hpp>
  17. #include <boost/serialization/version.hpp>
  18. #include <boost/serialization/nvp.hpp>
  19. class A
  20. {
  21. friend class boost::serialization::access;
  22. template<class Archive>
  23. void serialize(Archive & /*ar*/, const unsigned int file_version){
  24. // class that don't save class info always have a version number of 0
  25. BOOST_CHECK(file_version == 0);
  26. BOOST_STATIC_ASSERT(0 == ::boost::serialization::version<A>::value);
  27. ++count;
  28. }
  29. public:
  30. unsigned int count;
  31. A() : count(0) {}
  32. };
  33. BOOST_CLASS_IMPLEMENTATION(A, ::boost::serialization::object_serializable)
  34. BOOST_CLASS_TRACKING(A, ::boost::serialization::track_never)
  35. // second case : serialize WITH class information
  36. class B
  37. {
  38. friend class boost::serialization::access;
  39. template<class Archive>
  40. void serialize(Archive & /*ar*/, const unsigned int file_version){
  41. // verify at execution that the version number corresponds to the saved
  42. // one
  43. BOOST_CHECK(file_version == 2);
  44. ++count;
  45. }
  46. public:
  47. unsigned int count;
  48. B() : count(0) {}
  49. };
  50. BOOST_CLASS_IMPLEMENTATION(B, ::boost::serialization::object_class_info)
  51. BOOST_CLASS_TRACKING(B, ::boost::serialization::track_never)
  52. BOOST_CLASS_VERSION(B, 4)
  53. void in(const char *testfile, A & a, B & b)
  54. {
  55. test_istream is(testfile, TEST_STREAM_FLAGS);
  56. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  57. ia >> BOOST_SERIALIZATION_NVP(a);
  58. ia >> BOOST_SERIALIZATION_NVP(a);
  59. BOOST_CHECK(a.count == 2); // no tracking => redundant loads
  60. ia >> BOOST_SERIALIZATION_NVP(b);
  61. ia >> BOOST_SERIALIZATION_NVP(b);
  62. // note: archive was saved with tracking so that is what determines
  63. // whether tracking is perform on load - regardless of the latest
  64. // tracking setting.
  65. BOOST_CHECK(b.count == 1);
  66. }
  67. int
  68. test_main( int /* argc */, char* /* argv */[] )
  69. {
  70. A a;
  71. B b;
  72. std::string filename;
  73. filename += boost::archive::tmpdir();
  74. filename += '/';
  75. filename += BOOST_PP_STRINGIZE(testfile_);
  76. filename += BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST);
  77. in(filename.c_str(), a, b);
  78. return EXIT_SUCCESS;
  79. }
  80. // EOF