test_dll_exported.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_dll_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. // This is an overly complex test. The purpose of this test is to
  9. // demostrate and test the ability to serialize a hiarchy of class
  10. // through a base class pointer even though those class might be
  11. // implemente in different dlls and use different extended type info
  12. // systems.
  13. //
  14. // polymorphic_ base is locally declared and defined. It use the
  15. // "no_rtti" extended type info system.
  16. // polymorphic_derived1 is locally declared and defined. It uses
  17. // the default "type_id" extended type info system
  18. // polymorphic_derived2 is declared in polymorphic_derived.hpp
  19. // and defined in dll_polymorphic_derived2. It uses the typeid
  20. // system.
  21. #include <cstddef> // NULL
  22. #include <fstream>
  23. #include <cstdio> // remove
  24. #include <boost/config.hpp>
  25. #if defined(BOOST_NO_STDC_NAMESPACE)
  26. namespace std{
  27. using ::remove;
  28. }
  29. #endif
  30. #include <boost/archive/polymorphic_oarchive.hpp>
  31. #include <boost/archive/polymorphic_iarchive.hpp>
  32. #include "test_tools.hpp"
  33. #include <boost/archive/polymorphic_oarchive.hpp>
  34. #include <boost/archive/polymorphic_iarchive.hpp>
  35. #include <boost/serialization/base_object.hpp>
  36. #include <boost/serialization/export.hpp>
  37. #include <boost/serialization/access.hpp>
  38. #define POLYMORPHIC_BASE_IMPORT
  39. #include "polymorphic_base.hpp"
  40. #include "polymorphic_derived1.hpp"
  41. #define POLYMORPHIC_DERIVED2_IMPORT
  42. #include "polymorphic_derived2.hpp"
  43. // save exported polymorphic class
  44. void save_exported(const char *testfile)
  45. {
  46. test_ostream os(testfile, TEST_STREAM_FLAGS);
  47. test_oarchive oa_implementation(os, TEST_ARCHIVE_FLAGS);
  48. boost::archive::polymorphic_oarchive & oa_interface = oa_implementation;
  49. polymorphic_base *rb1 = new polymorphic_derived1;
  50. polymorphic_base *rb2 = new polymorphic_derived2;
  51. polymorphic_derived2 *rd21 = new polymorphic_derived2;
  52. // export will permit correct serialization
  53. // through a pointer to a base class
  54. oa_interface << BOOST_SERIALIZATION_NVP(rb1);
  55. oa_interface << BOOST_SERIALIZATION_NVP(rb2);
  56. oa_interface << BOOST_SERIALIZATION_NVP(rd21);
  57. delete rd21;
  58. delete rb2;
  59. delete rb1;
  60. }
  61. // save exported polymorphic class
  62. void load_exported(const char *testfile)
  63. {
  64. test_istream is(testfile, TEST_STREAM_FLAGS);
  65. test_iarchive ia_implementation(is, TEST_ARCHIVE_FLAGS);
  66. boost::archive::polymorphic_iarchive & ia_interface = ia_implementation;
  67. polymorphic_base *rb1 = NULL;
  68. polymorphic_base *rb2 = NULL;
  69. polymorphic_derived2 *rd21 = NULL;
  70. // export will permit correct serialization
  71. // through a pointer to a base class
  72. ia_interface >> BOOST_SERIALIZATION_NVP(rb1);
  73. BOOST_CHECK_MESSAGE(
  74. boost::serialization::type_info_implementation<polymorphic_derived1>
  75. ::type::get_const_instance()
  76. ==
  77. * boost::serialization::type_info_implementation<polymorphic_base>
  78. ::type::get_const_instance().get_derived_extended_type_info(*rb1),
  79. "restored pointer b1 not of correct type"
  80. );
  81. ia_interface >> BOOST_SERIALIZATION_NVP(rb2);
  82. BOOST_CHECK_MESSAGE(
  83. boost::serialization::type_info_implementation<polymorphic_derived2>
  84. ::type::get_const_instance()
  85. ==
  86. * boost::serialization::type_info_implementation<polymorphic_base>
  87. ::type::get_const_instance().get_derived_extended_type_info(*rb2),
  88. "restored pointer b2 not of correct type"
  89. );
  90. ia_interface >> BOOST_SERIALIZATION_NVP(rd21);
  91. BOOST_CHECK_MESSAGE(
  92. boost::serialization::type_info_implementation<polymorphic_derived2>
  93. ::type::get_const_instance()
  94. ==
  95. * boost::serialization::type_info_implementation<polymorphic_derived2>
  96. ::type::get_const_instance().get_derived_extended_type_info(*rd21),
  97. "restored pointer d2 not of correct type"
  98. );
  99. delete rd21;
  100. delete rb2;
  101. delete rb1;
  102. }
  103. int
  104. test_main( int /* argc */, char* /* argv */[] )
  105. {
  106. const char * testfile = boost::archive::tmpnam(NULL);
  107. BOOST_REQUIRE(NULL != testfile);
  108. save_exported(testfile);
  109. load_exported(testfile);
  110. std::remove(testfile);
  111. return EXIT_SUCCESS;
  112. }
  113. // EOF