test_no_rtti.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_no_rtti.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. // note: this program tests the inter-operability of different
  8. // extended typeinfo systems. In this example, one class is
  9. // identified using the default RTTI while the other uses a custom
  10. // system based on the export key.
  11. //
  12. // As this program uses RTTI for one of the types, the test will fail
  13. // on a system for which RTTI is not enabled or not existent.
  14. #include <cstddef>
  15. #include <fstream>
  16. #include <iostream>
  17. #include <boost/config.hpp>
  18. #include <cstdio> // remove
  19. #if defined(BOOST_NO_STDC_NAMESPACE)
  20. namespace std{
  21. using ::remove;
  22. }
  23. #endif
  24. #include <boost/serialization/type_info_implementation.hpp>
  25. #include <boost/serialization/export.hpp>
  26. #include <boost/serialization/nvp.hpp>
  27. #include "test_tools.hpp"
  28. #include <boost/archive/polymorphic_oarchive.hpp>
  29. #include <boost/archive/polymorphic_iarchive.hpp>
  30. #include "polymorphic_base.hpp"
  31. #include "polymorphic_derived1.hpp"
  32. #include "polymorphic_derived2.hpp"
  33. // save derived polymorphic class
  34. void save_derived(const char *testfile)
  35. {
  36. test_ostream os(testfile, TEST_STREAM_FLAGS);
  37. test_oarchive oa_implementation(os, TEST_ARCHIVE_FLAGS);
  38. boost::archive::polymorphic_oarchive & oa_interface = oa_implementation;
  39. polymorphic_derived1 *rd1 = new polymorphic_derived1;
  40. polymorphic_derived2 *rd2 = new polymorphic_derived2;
  41. std::cout << "saving polymorphic_derived1 (no_rtti)\n";
  42. oa_interface << BOOST_SERIALIZATION_NVP(rd1);
  43. std::cout << "saving polymorphic_derived2\n";
  44. oa_interface << BOOST_SERIALIZATION_NVP(rd2);
  45. const polymorphic_base *rb1 = rd1;
  46. polymorphic_base *rb2 = rd2;
  47. std::cout << "saving polymorphic_derived1 (no_rtti) through base (rtti)\n";
  48. oa_interface << BOOST_SERIALIZATION_NVP(rb1);
  49. std::cout << "saving polymorphic_derived2 through base\n";
  50. oa_interface << BOOST_SERIALIZATION_NVP(rb2);
  51. delete rd1;
  52. delete rd2;
  53. }
  54. void load_derived(const char *testfile)
  55. {
  56. test_istream is(testfile, TEST_STREAM_FLAGS);
  57. test_iarchive ia_implementation(is, TEST_ARCHIVE_FLAGS);
  58. boost::archive::polymorphic_iarchive & ia_interface = ia_implementation;
  59. polymorphic_derived1 *rd1 = NULL;
  60. polymorphic_derived2 *rd2 = NULL;
  61. std::cout << "loading polymorphic_derived1 (no_rtti)\n";
  62. ia_interface >> BOOST_SERIALIZATION_NVP(rd1);
  63. BOOST_CHECK_MESSAGE(
  64. boost::serialization::type_info_implementation<
  65. polymorphic_derived1
  66. >::type::get_const_instance()
  67. ==
  68. * boost::serialization::type_info_implementation<
  69. polymorphic_derived1
  70. >::type::get_const_instance().get_derived_extended_type_info(*rd1)
  71. ,
  72. "restored pointer d1 not of correct type"
  73. );
  74. std::cout << "loading polymorphic_derived2\n";
  75. ia_interface >> BOOST_SERIALIZATION_NVP(rd2);
  76. BOOST_CHECK_MESSAGE(
  77. boost::serialization::type_info_implementation<
  78. polymorphic_derived2
  79. >::type::get_const_instance()
  80. ==
  81. * boost::serialization::type_info_implementation<
  82. polymorphic_derived2
  83. >::type::get_const_instance().get_derived_extended_type_info(*rd2)
  84. ,
  85. "restored pointer d2 not of correct type"
  86. );
  87. polymorphic_base *rb1 = NULL;
  88. polymorphic_base *rb2 = NULL;
  89. // the above opereration registers the derived classes as a side
  90. // effect. Hence, instances can now be correctly serialized through
  91. // a base class pointer.
  92. std::cout << "loading polymorphic_derived1 (no_rtti) through base (no_rtti)\n";
  93. ia_interface >> BOOST_SERIALIZATION_NVP(rb1);
  94. BOOST_CHECK_MESSAGE(
  95. rb1 == dynamic_cast<polymorphic_base *>(rd1),
  96. "serialized pointers not correctly restored"
  97. );
  98. BOOST_CHECK_MESSAGE(
  99. boost::serialization::type_info_implementation<
  100. polymorphic_derived1
  101. >::type::get_const_instance()
  102. ==
  103. * boost::serialization::type_info_implementation<
  104. polymorphic_base
  105. >::type::get_const_instance().get_derived_extended_type_info(*rb1)
  106. ,
  107. "restored pointer b1 not of correct type"
  108. );
  109. std::cout << "loading polymorphic_derived2 through base (no_rtti)\n";
  110. ia_interface >> BOOST_SERIALIZATION_NVP(rb2);
  111. BOOST_CHECK_MESSAGE(
  112. rb2 == dynamic_cast<polymorphic_base *>(rd2),
  113. "serialized pointers not correctly restored"
  114. );
  115. BOOST_CHECK_MESSAGE(
  116. boost::serialization::type_info_implementation<
  117. polymorphic_derived2
  118. >::type::get_const_instance()
  119. ==
  120. * boost::serialization::type_info_implementation<
  121. polymorphic_base
  122. >::type::get_const_instance().get_derived_extended_type_info(*rb2)
  123. ,
  124. "restored pointer b2 not of correct type"
  125. );
  126. delete rb1;
  127. delete rb2;
  128. }
  129. int
  130. test_main( int /* argc */, char* /* argv */[] )
  131. {
  132. const char * testfile = boost::archive::tmpnam(NULL);
  133. BOOST_REQUIRE(NULL != testfile);
  134. save_derived(testfile);
  135. load_derived(testfile);
  136. std::remove(testfile);
  137. return EXIT_SUCCESS;
  138. }
  139. // EOF