test_derived.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_derived.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> // NULL
  9. #include <fstream>
  10. #include <cstdio> // remove
  11. #include <boost/config.hpp>
  12. #if defined(BOOST_NO_STDC_NAMESPACE)
  13. namespace std{
  14. using ::remove;
  15. }
  16. #endif
  17. #include <boost/archive/archive_exception.hpp>
  18. #include "test_tools.hpp"
  19. #include <boost/serialization/base_object.hpp>
  20. #include <boost/serialization/type_info_implementation.hpp>
  21. class base
  22. {
  23. friend class boost::serialization::access;
  24. template<class Archive>
  25. void serialize(Archive & /* ar */, const unsigned int /* file_version */){
  26. }
  27. public:
  28. virtual ~base(){};
  29. };
  30. class derived1 : public base
  31. {
  32. friend class boost::serialization::access;
  33. template<class Archive>
  34. void serialize(Archive &ar, const unsigned int /* file_version */){
  35. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base);
  36. }
  37. };
  38. class derived2 : public base
  39. {
  40. friend class boost::serialization::access;
  41. template<class Archive>
  42. void serialize(Archive &ar, const unsigned int /* file_version */){
  43. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base);
  44. }
  45. };
  46. // save non-polymorphic classes through a base class pointer
  47. void save_derived(const char *testfile)
  48. {
  49. test_ostream os(testfile, TEST_STREAM_FLAGS);
  50. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  51. // registration not necessary when serializing the most derived pointer
  52. derived1 *d1 = new derived1;
  53. derived2 *d2 = new derived2;
  54. oa << BOOST_SERIALIZATION_NVP(d1) << BOOST_SERIALIZATION_NVP(d2);
  55. // upcasting non-polymorphic pointers may not lead to the expected
  56. // result. In the current type id system
  57. base *b1 = d1;
  58. base *b2 = d2;
  59. // Warning, the current type id system does not yield true
  60. // type id for non-polymorphic types
  61. const boost::serialization::extended_type_info & this_type
  62. = boost::serialization::type_info_implementation<base>::type
  63. ::get_const_instance();
  64. // retrieve the true type of the object pointed to
  65. const boost::serialization::extended_type_info & true_type
  66. = * boost::serialization::type_info_implementation<base>::type
  67. ::get_const_instance().get_derived_extended_type_info(*b1);
  68. BOOST_WARN_MESSAGE(
  69. !(this_type == true_type),
  70. "current type id system does not support non-polymorphic types"
  71. );
  72. oa << BOOST_SERIALIZATION_NVP(b1);
  73. oa << BOOST_SERIALIZATION_NVP(b2);
  74. delete d1;
  75. delete d2;
  76. }
  77. // save non-polymorphic classes through a base class pointer
  78. void load_derived(const char *testfile)
  79. {
  80. test_istream is(testfile, TEST_STREAM_FLAGS);
  81. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  82. // registration not necessary when serializing the most derived pointer
  83. derived1 *d1 = NULL;
  84. derived2 *d2 = NULL;
  85. ia >> BOOST_SERIALIZATION_NVP(d1) >> BOOST_SERIALIZATION_NVP(d2);
  86. // upcasting non-polymorphic pointers may not lead to the expected
  87. // result. In the current type id system
  88. base *b1 = NULL;
  89. base *b2 = NULL;
  90. // note: this will produce incorrect results for non-polymorphic classes
  91. ia >> BOOST_SERIALIZATION_NVP(b1);
  92. ia >> BOOST_SERIALIZATION_NVP(b2);
  93. // Warning, the current type id system does not yield true
  94. // type id for non-polymorphic types
  95. const boost::serialization::extended_type_info & this_type
  96. = boost::serialization::type_info_implementation<base>::type
  97. ::get_const_instance();
  98. // retrieve the true type of the object pointed to
  99. const boost::serialization::extended_type_info & true_type
  100. = * boost::serialization::type_info_implementation<base>::type
  101. ::get_const_instance().get_derived_extended_type_info(*b1);
  102. BOOST_WARN_MESSAGE(
  103. ! (this_type == true_type),
  104. "current type id system does fails for non-polymorphic types"
  105. );
  106. BOOST_CHECK(b1 == static_cast<base *>(d1));
  107. BOOST_CHECK(b2 == static_cast<base *>(d2));
  108. delete d1;
  109. delete d2;
  110. }
  111. int
  112. test_main( int /* argc */, char* /* argv */[] )
  113. {
  114. const char * testfile = boost::archive::tmpnam(NULL);
  115. BOOST_REQUIRE(NULL != testfile);
  116. save_derived(testfile);
  117. load_derived(testfile);
  118. std::remove(testfile);
  119. return EXIT_SUCCESS;
  120. }
  121. // EOF