performance_no_rtti.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <fstream>
  15. #include <cstdio> // remove
  16. #include <boost/config.hpp>
  17. #if defined(BOOST_NO_STDC_NAMESPACE)
  18. namespace std{
  19. using ::remove;
  20. }
  21. #endif
  22. #include <boost/static_assert.hpp>
  23. #include <boost/type_traits/is_same.hpp>
  24. #include <boost/archive/archive_exception.hpp>
  25. #include "../test/test_tools.hpp"
  26. #include <boost/preprocessor/stringize.hpp>
  27. // #include <boost/preprocessor/cat.hpp>
  28. // the following fails with (only!) gcc 3.4
  29. // #include BOOST_PP_STRINGIZE(BOOST_PP_CAT(../test/,BOOST_ARCHIVE_TEST))
  30. // just copy over the files from the test directory
  31. #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
  32. #include <boost/serialization/nvp.hpp>
  33. #include <boost/serialization/base_object.hpp>
  34. #include <boost/serialization/export.hpp>
  35. #include <boost/serialization/type_info_implementation.hpp>
  36. #include <boost/serialization/extended_type_info_no_rtti.hpp>
  37. class polymorphic_base
  38. {
  39. friend class boost::serialization::access;
  40. template<class Archive>
  41. void serialize(Archive & /* ar */, const unsigned int /* file_version */){
  42. }
  43. public:
  44. virtual const char * get_key() const = 0;
  45. virtual ~polymorphic_base(){};
  46. };
  47. BOOST_IS_ABSTRACT(polymorphic_base)
  48. BOOST_CLASS_TYPE_INFO(
  49. polymorphic_base,
  50. extended_type_info_no_rtti<polymorphic_base>
  51. )
  52. // note: types which use ...no_rtti MUST be exported
  53. BOOST_CLASS_EXPORT(polymorphic_base)
  54. class polymorphic_derived1 : public polymorphic_base
  55. {
  56. friend class boost::serialization::access;
  57. template<class Archive>
  58. void serialize(Archive &ar, const unsigned int /* file_version */){
  59. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base);
  60. }
  61. public:
  62. virtual const char * get_key() const ;
  63. };
  64. BOOST_CLASS_TYPE_INFO(
  65. polymorphic_derived1,
  66. extended_type_info_no_rtti<polymorphic_derived1>
  67. )
  68. BOOST_CLASS_EXPORT(polymorphic_derived1)
  69. const char * polymorphic_derived1::get_key() const {
  70. const boost::serialization::extended_type_info *eti
  71. = boost::serialization::type_info_implementation<polymorphic_derived1>
  72. ::type::get_instance();
  73. return eti->get_key();
  74. }
  75. class polymorphic_derived2 : public polymorphic_base
  76. {
  77. friend class boost::serialization::access;
  78. template<class Archive>
  79. void serialize(Archive &ar, const unsigned int /* file_version */){
  80. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base);
  81. }
  82. public:
  83. virtual const char * get_key() const ;
  84. };
  85. // note the mixing of type_info systems is supported.
  86. BOOST_CLASS_TYPE_INFO(
  87. polymorphic_derived2,
  88. boost::serialization::extended_type_info_typeid<polymorphic_derived2>
  89. )
  90. BOOST_CLASS_EXPORT(polymorphic_derived2)
  91. const char * polymorphic_derived2::get_key() const {
  92. // use the exported key as the identifier
  93. const boost::serialization::extended_type_info *eti
  94. = boost::serialization::type_info_implementation<polymorphic_derived2>
  95. ::type::get_instance();
  96. return eti->get_key();
  97. }
  98. // save derived polymorphic class
  99. void save_derived(const char *testfile)
  100. {
  101. test_ostream os(testfile, TEST_STREAM_FLAGS);
  102. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  103. polymorphic_derived1 *rd1 = new polymorphic_derived1;
  104. polymorphic_derived2 *rd2 = new polymorphic_derived2;
  105. oa << BOOST_SERIALIZATION_NVP(rd1);
  106. oa << BOOST_SERIALIZATION_NVP(rd2);
  107. // the above opereration registers the derived classes as a side
  108. // effect. Hence, instances can now be correctly serialized through
  109. // a base class pointer.
  110. polymorphic_base *rb1 = rd1;
  111. polymorphic_base *rb2 = rd2;
  112. oa << BOOST_SERIALIZATION_NVP(rb1);
  113. oa << BOOST_SERIALIZATION_NVP(rb2);
  114. delete rd1;
  115. delete rd2;
  116. }
  117. // save derived polymorphic class
  118. void load_derived(const char *testfile)
  119. {
  120. test_istream is(testfile, TEST_STREAM_FLAGS);
  121. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  122. polymorphic_derived1 *rd1 = NULL;
  123. polymorphic_derived2 *rd2 = NULL;
  124. ia >> BOOST_SERIALIZATION_NVP(rd1);
  125. BOOST_CHECK_MESSAGE(
  126. boost::serialization::type_info_implementation<polymorphic_derived1>
  127. ::type::get_instance()
  128. == boost::serialization::type_info_implementation<polymorphic_derived1>
  129. ::type::get_derived_extended_type_info(*rd1),
  130. "restored pointer d1 not of correct type"
  131. );
  132. ia >> BOOST_SERIALIZATION_NVP(rd2);
  133. BOOST_CHECK_MESSAGE(
  134. boost::serialization::type_info_implementation<polymorphic_derived2>
  135. ::type::get_instance()
  136. == boost::serialization::type_info_implementation<polymorphic_derived2>
  137. ::type::get_derived_extended_type_info(*rd2),
  138. "restored pointer d2 not of correct type"
  139. );
  140. polymorphic_base *rb1 = NULL;
  141. polymorphic_base *rb2 = NULL;
  142. // the above opereration registers the derived classes as a side
  143. // effect. Hence, instances can now be correctly serialized through
  144. // a base class pointer.
  145. ia >> BOOST_SERIALIZATION_NVP(rb1);
  146. BOOST_CHECK_MESSAGE(
  147. rb1 == dynamic_cast<polymorphic_base *>(rd1),
  148. "serialized pointers not correctly restored"
  149. );
  150. BOOST_CHECK_MESSAGE(
  151. boost::serialization::type_info_implementation<polymorphic_derived1>
  152. ::type::get_instance()
  153. == boost::serialization::type_info_implementation<polymorphic_base>
  154. ::type::get_derived_extended_type_info(*rb1),
  155. "restored pointer b1 not of correct type"
  156. );
  157. ia >> BOOST_SERIALIZATION_NVP(rb2);
  158. BOOST_CHECK_MESSAGE(
  159. rb2 == dynamic_cast<polymorphic_base *>(rd2),
  160. "serialized pointers not correctly restored"
  161. );
  162. BOOST_CHECK_MESSAGE(
  163. boost::serialization::type_info_implementation<polymorphic_derived2>
  164. ::type::get_instance()
  165. == boost::serialization::type_info_implementation<polymorphic_base>
  166. ::type::get_derived_extended_type_info(*rb2),
  167. "restored pointer b2 not of correct type"
  168. );
  169. delete rb1;
  170. delete rb2;
  171. }
  172. int
  173. test_main( int /* argc */, char* /* argv */[] )
  174. {
  175. const char * testfile = boost::archive::tmpnam(NULL);
  176. BOOST_REQUIRE(NULL != testfile);
  177. save_derived(testfile);
  178. load_derived(testfile);
  179. std::remove(testfile);
  180. return EXIT_SUCCESS;
  181. }
  182. // EOF