portable_binary_iarchive.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #ifndef PORTABLE_BINARY_IARCHIVE_HPP
  2. #define PORTABLE_BINARY_IARCHIVE_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. #if defined(_MSC_VER)
  8. #pragma warning( push )
  9. #pragma warning( disable : 4244 )
  10. #endif
  11. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  12. // portable_binary_iarchive.hpp
  13. // (C) Copyright 2002-7 Robert Ramey - http://www.rrsd.com .
  14. // Use, modification and distribution is subject to the Boost Software
  15. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  16. // http://www.boost.org/LICENSE_1_0.txt)
  17. // See http://www.boost.org for updates, documentation, and revision history.
  18. #include <istream>
  19. #include <boost/serialization/string.hpp>
  20. #include <boost/serialization/item_version_type.hpp>
  21. #include <boost/archive/archive_exception.hpp>
  22. #include <boost/archive/basic_binary_iprimitive.hpp>
  23. #include <boost/archive/detail/common_iarchive.hpp>
  24. #include <boost/archive/detail/register_archive.hpp>
  25. #include "portable_binary_archive.hpp"
  26. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  27. // exception to be thrown if integer read from archive doesn't fit
  28. // variable being loaded
  29. class portable_binary_iarchive_exception :
  30. public boost::archive::archive_exception
  31. {
  32. public:
  33. enum exception_code {
  34. incompatible_integer_size
  35. } m_exception_code ;
  36. portable_binary_iarchive_exception(exception_code c = incompatible_integer_size ) :
  37. boost::archive::archive_exception(boost::archive::archive_exception::other_exception),
  38. m_exception_code(c)
  39. {}
  40. virtual const char *what( ) const throw( )
  41. {
  42. const char *msg = "programmer error";
  43. switch(m_exception_code){
  44. case incompatible_integer_size:
  45. msg = "integer cannot be represented";
  46. break;
  47. default:
  48. msg = boost::archive::archive_exception::what();
  49. assert(false);
  50. break;
  51. }
  52. return msg;
  53. }
  54. };
  55. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  56. // "Portable" input binary archive. It addresses integer size and endienness so
  57. // that binary archives can be passed across systems. Note:floating point types
  58. // not addressed here
  59. class portable_binary_iarchive :
  60. public boost::archive::basic_binary_iprimitive<
  61. portable_binary_iarchive,
  62. std::istream::char_type,
  63. std::istream::traits_type
  64. >,
  65. public boost::archive::detail::common_iarchive<
  66. portable_binary_iarchive
  67. >
  68. {
  69. typedef boost::archive::basic_binary_iprimitive<
  70. portable_binary_iarchive,
  71. std::istream::char_type,
  72. std::istream::traits_type
  73. > primitive_base_t;
  74. typedef boost::archive::detail::common_iarchive<
  75. portable_binary_iarchive
  76. > archive_base_t;
  77. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  78. public:
  79. #else
  80. friend archive_base_t;
  81. friend primitive_base_t; // since with override load below
  82. friend class boost::archive::detail::interface_iarchive<
  83. portable_binary_iarchive
  84. >;
  85. friend class boost::archive::load_access;
  86. protected:
  87. #endif
  88. unsigned int m_flags;
  89. void load_impl(boost::intmax_t & l, char maxsize);
  90. // default fall through for any types not specified here
  91. template<class T>
  92. void load(T & t){
  93. boost::intmax_t l;
  94. load_impl(l, sizeof(T));
  95. // use cast to avoid compile time warning
  96. //t = static_cast< T >(l);
  97. t = T(l);
  98. }
  99. void load(boost::serialization::item_version_type & t){
  100. boost::intmax_t l;
  101. load_impl(l, sizeof(boost::serialization::item_version_type));
  102. // use cast to avoid compile time warning
  103. t = boost::serialization::item_version_type(l);
  104. }
  105. void load(boost::archive::version_type & t){
  106. boost::intmax_t l;
  107. load_impl(l, sizeof(boost::archive::version_type));
  108. // use cast to avoid compile time warning
  109. t = boost::archive::version_type(l);
  110. }
  111. void load(boost::archive::class_id_type & t){
  112. boost::intmax_t l;
  113. load_impl(l, sizeof(boost::archive::class_id_type));
  114. // use cast to avoid compile time warning
  115. t = boost::archive::class_id_type(static_cast<int>(l));
  116. }
  117. void load(std::string & t){
  118. this->primitive_base_t::load(t);
  119. }
  120. #ifndef BOOST_NO_STD_WSTRING
  121. void load(std::wstring & t){
  122. this->primitive_base_t::load(t);
  123. }
  124. #endif
  125. void load(float & t){
  126. this->primitive_base_t::load(t);
  127. // floats not supported
  128. //BOOST_STATIC_ASSERT(false);
  129. }
  130. void load(double & t){
  131. this->primitive_base_t::load(t);
  132. // doubles not supported
  133. //BOOST_STATIC_ASSERT(false);
  134. }
  135. void load(char & t){
  136. this->primitive_base_t::load(t);
  137. }
  138. void load(unsigned char & t){
  139. this->primitive_base_t::load(t);
  140. }
  141. typedef boost::archive::detail::common_iarchive<portable_binary_iarchive>
  142. detail_common_iarchive;
  143. template<class T>
  144. void load_override(T & t){
  145. this->detail_common_iarchive::load_override(t);
  146. }
  147. void load_override(boost::archive::class_name_type & t);
  148. // binary files don't include the optional information
  149. void load_override(boost::archive::class_id_optional_type &){}
  150. void init(unsigned int flags);
  151. public:
  152. portable_binary_iarchive(std::istream & is, unsigned flags = 0) :
  153. primitive_base_t(
  154. * is.rdbuf(),
  155. 0 != (flags & boost::archive::no_codecvt)
  156. ),
  157. archive_base_t(flags),
  158. m_flags(0)
  159. {
  160. init(flags);
  161. }
  162. portable_binary_iarchive(
  163. std::basic_streambuf<
  164. std::istream::char_type,
  165. std::istream::traits_type
  166. > & bsb,
  167. unsigned int flags
  168. ) :
  169. primitive_base_t(
  170. bsb,
  171. 0 != (flags & boost::archive::no_codecvt)
  172. ),
  173. archive_base_t(flags),
  174. m_flags(0)
  175. {
  176. init(flags);
  177. }
  178. };
  179. // required by export in boost version > 1.34
  180. #ifdef BOOST_SERIALIZATION_REGISTER_ARCHIVE
  181. BOOST_SERIALIZATION_REGISTER_ARCHIVE(portable_binary_iarchive)
  182. #endif
  183. // required by export in boost <= 1.34
  184. #define BOOST_ARCHIVE_CUSTOM_IARCHIVE_TYPES portable_binary_iarchive
  185. #if defined(_MSC_VER)
  186. #pragma warning( pop )
  187. #endif
  188. #endif // PORTABLE_BINARY_IARCHIVE_HPP