test_exported.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_exported.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>
  9. #include <fstream>
  10. #include <boost/config.hpp>
  11. #include <cstdio> // remove
  12. #if defined(BOOST_NO_STDC_NAMESPACE)
  13. namespace std{
  14. using ::remove;
  15. }
  16. #endif
  17. #include <boost/serialization/type_info_implementation.hpp>
  18. #include <boost/serialization/export.hpp>
  19. #include <boost/serialization/base_object.hpp>
  20. #include "test_tools.hpp"
  21. #include <boost/archive/polymorphic_oarchive.hpp>
  22. #include <boost/archive/polymorphic_iarchive.hpp>
  23. #include "polymorphic_derived1.hpp"
  24. #include "polymorphic_derived2.hpp"
  25. // save exported polymorphic class
  26. void save_exported(const char *testfile)
  27. {
  28. test_ostream os(testfile, TEST_STREAM_FLAGS);
  29. test_oarchive oa_implementation(os, TEST_ARCHIVE_FLAGS);
  30. boost::archive::polymorphic_oarchive & oa_interface = oa_implementation;
  31. const polymorphic_base *rb1 = new polymorphic_derived1;
  32. const polymorphic_base *rb2 = new polymorphic_derived2;
  33. // export will permit correct serialization
  34. // through a pointer to a base class
  35. std::cout << "saving polymorphic_derived1 (no_rtti)\n";
  36. oa_interface << BOOST_SERIALIZATION_NVP(rb1);
  37. std::cout << "saving polymorphic_derived2\n";
  38. oa_interface << BOOST_SERIALIZATION_NVP(rb2);
  39. delete rb1;
  40. delete rb2;
  41. }
  42. // save exported polymorphic class
  43. void load_exported(const char *testfile)
  44. {
  45. test_istream is(testfile, TEST_STREAM_FLAGS);
  46. test_iarchive ia_implementation(is, TEST_ARCHIVE_FLAGS);
  47. boost::archive::polymorphic_iarchive & ia_interface = ia_implementation;
  48. polymorphic_base *rb1 = NULL;
  49. polymorphic_base *rb2 = NULL;
  50. // export will permit correct serialization
  51. // through a pointer to a base class
  52. ia_interface >> BOOST_SERIALIZATION_NVP(rb1);
  53. BOOST_CHECK_MESSAGE(
  54. boost::serialization::type_info_implementation<polymorphic_derived1>
  55. ::type::get_const_instance()
  56. ==
  57. * boost::serialization::type_info_implementation<polymorphic_base>
  58. ::type::get_const_instance().get_derived_extended_type_info(*rb1),
  59. "restored pointer b1 not of correct type"
  60. );
  61. ia_interface >> BOOST_SERIALIZATION_NVP(rb2);
  62. BOOST_CHECK_MESSAGE(
  63. boost::serialization::type_info_implementation<polymorphic_derived2>
  64. ::type::get_const_instance()
  65. ==
  66. * boost::serialization::type_info_implementation<polymorphic_base>
  67. ::type::get_const_instance().get_derived_extended_type_info(*rb2),
  68. "restored pointer b2 not of correct type"
  69. );
  70. delete rb1;
  71. delete rb2;
  72. }
  73. int
  74. test_main( int /* argc */, char* /* argv */[] )
  75. {
  76. const char * testfile = boost::archive::tmpnam(NULL);
  77. BOOST_REQUIRE(NULL != testfile);
  78. save_exported(testfile);
  79. load_exported(testfile);
  80. std::remove(testfile);
  81. return EXIT_SUCCESS;
  82. }
  83. // EOF