test_mult_archive_types.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_mult_archive_types.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. #include <cstddef>
  8. #include <fstream>
  9. #include <boost/config.hpp>
  10. #include <cstdio> // remove
  11. #if defined(BOOST_NO_STDC_NAMESPACE)
  12. namespace std{
  13. using ::remove;
  14. }
  15. #endif
  16. #include <boost/archive/text_oarchive.hpp>
  17. #include <boost/archive/text_iarchive.hpp>
  18. #include <boost/archive/xml_oarchive.hpp>
  19. #include <boost/archive/xml_iarchive.hpp>
  20. #include "test_tools.hpp"
  21. #include <boost/serialization/export.hpp>
  22. #include <boost/serialization/nvp.hpp>
  23. // This is a simple class. It contains a counter of the number
  24. // of objects of this class which have been instantiated.
  25. class A
  26. {
  27. private:
  28. friend class boost::serialization::access;
  29. int x;
  30. template<class Archive>
  31. void serialize(Archive & ar, const unsigned int /* file_version */){
  32. ar & BOOST_SERIALIZATION_NVP(x);
  33. }
  34. public:
  35. static int count;
  36. A(){++count;} // default constructor
  37. virtual ~A(){--count;} // default destructor
  38. };
  39. BOOST_CLASS_EXPORT(A)
  40. // B is a subclass of A
  41. class B : public A
  42. {
  43. private:
  44. friend class boost::serialization::access;
  45. template<class Archive>
  46. void serialize(Archive & ar, const unsigned int /* file_version */){
  47. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A);
  48. }
  49. public:
  50. static int count;
  51. B() : A() {};
  52. virtual ~B() {};
  53. };
  54. BOOST_CLASS_EXPORT(B)
  55. int A::count = 0;
  56. // Run tests by serializing two shared_ptrs into an archive of type
  57. // OARCH, clearing them (deleting the objects) and then reloading the
  58. // objects back from an archive of type OARCH.
  59. template<class OA, class IA>
  60. void test_save_and_load(A * first, A * second)
  61. {
  62. const char * testfile = boost::archive::tmpnam(NULL);
  63. BOOST_REQUIRE(NULL != testfile);
  64. // Save
  65. {
  66. std::ofstream os(testfile);
  67. OA oa(os);
  68. oa << BOOST_SERIALIZATION_NVP(first);
  69. oa << BOOST_SERIALIZATION_NVP(second);
  70. }
  71. // Clear the pointers, thereby destroying the objects they contain
  72. first = NULL;
  73. second = NULL;
  74. // Load
  75. {
  76. std::ifstream is(testfile);
  77. IA ia(is);
  78. ia >> BOOST_SERIALIZATION_NVP(first);
  79. ia >> BOOST_SERIALIZATION_NVP(second);
  80. }
  81. BOOST_CHECK(first == second);
  82. std::remove(testfile);
  83. }
  84. using namespace boost::archive;
  85. // This does the tests
  86. int test_main(int /* argc */, char * /* argv */[])
  87. {
  88. // Try to save and load pointers to As, to a text archive
  89. A * a = new A;
  90. A * a1 = a;
  91. test_save_and_load<text_oarchive, text_iarchive>(a, a1);
  92. // Try to save and load pointers to Bs, to a text archive
  93. B * b = new B;
  94. B * b1 = b;
  95. test_save_and_load<text_oarchive, text_iarchive>(b, b1);
  96. // Try to save and load pointers to As, to an xml archive
  97. test_save_and_load<xml_oarchive, xml_iarchive>(a, a1);
  98. // Try to save and load pointers to Bs, to an xml archive
  99. test_save_and_load<xml_oarchive, xml_iarchive>(b, b1);
  100. delete a;
  101. delete b;
  102. return EXIT_SUCCESS;
  103. }