basic_binary_iarchive.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #ifndef BOOST_ARCHIVE_BASIC_BINARY_IARCHIVE_HPP
  2. #define BOOST_ARCHIVE_BASIC_BINARY_IARCHIVE_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // basic_binary_iarchive.hpp
  9. //
  10. // archives stored as native binary - this should be the fastest way
  11. // to archive the state of a group of obects. It makes no attempt to
  12. // convert to any canonical form.
  13. // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE
  14. // ON PLATFORM APART FROM THE ONE THEY ARE CREATED ON
  15. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  16. // Use, modification and distribution is subject to the Boost Software
  17. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  18. // http://www.boost.org/LICENSE_1_0.txt)
  19. // See http://www.boost.org for updates, documentation, and revision history.
  20. #include <boost/config.hpp>
  21. #include <boost/detail/workaround.hpp>
  22. #include <boost/archive/basic_archive.hpp>
  23. #include <boost/archive/detail/common_iarchive.hpp>
  24. #include <boost/serialization/collection_size_type.hpp>
  25. #include <boost/serialization/string.hpp>
  26. #include <boost/serialization/library_version_type.hpp>
  27. #include <boost/serialization/item_version_type.hpp>
  28. #include <boost/integer_traits.hpp>
  29. #ifdef BOOST_MSVC
  30. # pragma warning(push)
  31. # pragma warning(disable : 4511 4512)
  32. #endif
  33. #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
  34. namespace boost {
  35. namespace archive {
  36. namespace detail {
  37. template<class Archive> class interface_iarchive;
  38. } // namespace detail
  39. /////////////////////////////////////////////////////////////////////////
  40. // class basic_binary_iarchive - read serialized objects from a input binary stream
  41. template<class Archive>
  42. class BOOST_SYMBOL_VISIBLE basic_binary_iarchive :
  43. public detail::common_iarchive<Archive>
  44. {
  45. #ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  46. public:
  47. #else
  48. protected:
  49. #if BOOST_WORKAROUND(BOOST_MSVC, < 1500)
  50. // for some inexplicable reason insertion of "class" generates compile erro
  51. // on msvc 7.1
  52. friend detail::interface_iarchive<Archive>;
  53. #else
  54. friend class detail::interface_iarchive<Archive>;
  55. #endif
  56. #endif
  57. // intermediate level to support override of operators
  58. // fot templates in the absence of partial function
  59. // template ordering. If we get here pass to base class
  60. // note extra nonsense to sneak it pass the borland compiers
  61. typedef detail::common_iarchive<Archive> detail_common_iarchive;
  62. template<class T>
  63. void load_override(T & t){
  64. this->detail_common_iarchive::load_override(t);
  65. }
  66. // include these to trap a change in binary format which
  67. // isn't specifically handled
  68. // upto 32K classes
  69. BOOST_STATIC_ASSERT(sizeof(class_id_type) == sizeof(int_least16_t));
  70. BOOST_STATIC_ASSERT(sizeof(class_id_reference_type) == sizeof(int_least16_t));
  71. // upto 2G objects
  72. BOOST_STATIC_ASSERT(sizeof(object_id_type) == sizeof(uint_least32_t));
  73. BOOST_STATIC_ASSERT(sizeof(object_reference_type) == sizeof(uint_least32_t));
  74. // binary files don't include the optional information
  75. void load_override(class_id_optional_type & /* t */){}
  76. void load_override(tracking_type & t, int /*version*/){
  77. boost::serialization::library_version_type lv = this->get_library_version();
  78. if(boost::serialization::library_version_type(6) < lv){
  79. int_least8_t x=0;
  80. * this->This() >> x;
  81. t = boost::archive::tracking_type(x);
  82. }
  83. else{
  84. bool x=0;
  85. * this->This() >> x;
  86. t = boost::archive::tracking_type(x);
  87. }
  88. }
  89. void load_override(class_id_type & t){
  90. boost::serialization::library_version_type lv = this->get_library_version();
  91. /*
  92. * library versions:
  93. * boost 1.39 -> 5
  94. * boost 1.43 -> 7
  95. * boost 1.47 -> 9
  96. *
  97. *
  98. * 1) in boost 1.43 and inferior, class_id_type is always a 16bit value, with no check on the library version
  99. * --> this means all archives with version v <= 7 are written with a 16bit class_id_type
  100. * 2) in boost 1.44 this load_override has disappeared (and thus boost 1.44 is not backward compatible at all !!)
  101. * 3) recent boosts reintroduced load_override with a test on the version :
  102. * - v > 7 : this->detail_common_iarchive::load_override(t, version)
  103. * - v > 6 : 16bit
  104. * - other : 32bit
  105. * --> which is obviously incorrect, see point 1
  106. *
  107. * the fix here decodes class_id_type on 16bit for all v <= 7, which seems to be the correct behaviour ...
  108. */
  109. if(boost::serialization::library_version_type (7) < lv){
  110. this->detail_common_iarchive::load_override(t);
  111. }
  112. else{
  113. int_least16_t x=0;
  114. * this->This() >> x;
  115. t = boost::archive::class_id_type(x);
  116. }
  117. }
  118. void load_override(class_id_reference_type & t){
  119. load_override(static_cast<class_id_type &>(t));
  120. }
  121. void load_override(version_type & t){
  122. boost::serialization::library_version_type lv = this->get_library_version();
  123. if(boost::serialization::library_version_type(7) < lv){
  124. this->detail_common_iarchive::load_override(t);
  125. }
  126. else
  127. if(boost::serialization::library_version_type(6) < lv){
  128. uint_least8_t x=0;
  129. * this->This() >> x;
  130. t = boost::archive::version_type(x);
  131. }
  132. else
  133. if(boost::serialization::library_version_type(5) < lv){
  134. uint_least16_t x=0;
  135. * this->This() >> x;
  136. t = boost::archive::version_type(x);
  137. }
  138. else
  139. if(boost::serialization::library_version_type(2) < lv){
  140. // upto 255 versions
  141. unsigned char x=0;
  142. * this->This() >> x;
  143. t = version_type(x);
  144. }
  145. else{
  146. unsigned int x=0;
  147. * this->This() >> x;
  148. t = boost::archive::version_type(x);
  149. }
  150. }
  151. void load_override(boost::serialization::item_version_type & t){
  152. boost::serialization::library_version_type lv = this->get_library_version();
  153. // if(boost::serialization::library_version_type(7) < lvt){
  154. if(boost::serialization::library_version_type(6) < lv){
  155. this->detail_common_iarchive::load_override(t);
  156. }
  157. else
  158. if(boost::serialization::library_version_type(6) < lv){
  159. uint_least16_t x=0;
  160. * this->This() >> x;
  161. t = boost::serialization::item_version_type(x);
  162. }
  163. else{
  164. unsigned int x=0;
  165. * this->This() >> x;
  166. t = boost::serialization::item_version_type(x);
  167. }
  168. }
  169. void load_override(serialization::collection_size_type & t){
  170. if(boost::serialization::library_version_type(5) < this->get_library_version()){
  171. this->detail_common_iarchive::load_override(t);
  172. }
  173. else{
  174. unsigned int x=0;
  175. * this->This() >> x;
  176. t = serialization::collection_size_type(x);
  177. }
  178. }
  179. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  180. load_override(class_name_type & t);
  181. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  182. init();
  183. basic_binary_iarchive(unsigned int flags) :
  184. detail::common_iarchive<Archive>(flags)
  185. {}
  186. };
  187. } // namespace archive
  188. } // namespace boost
  189. #ifdef BOOST_MSVC
  190. #pragma warning(pop)
  191. #endif
  192. #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
  193. #endif // BOOST_ARCHIVE_BASIC_BINARY_IARCHIVE_HPP