basic_binary_iprimitive.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP
  2. #define BOOST_ARCHIVE_BINARY_IPRIMITIVE_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( disable : 4800 )
  9. #endif
  10. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  11. // basic_binary_iprimitive.hpp
  12. //
  13. // archives stored as native binary - this should be the fastest way
  14. // to archive the state of a group of objects. It makes no attempt to
  15. // convert to any canonical form.
  16. // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE
  17. // ON PLATFORM APART FROM THE ONE THEY ARE CREATED ON
  18. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  19. // Use, modification and distribution is subject to the Boost Software
  20. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  21. // http://www.boost.org/LICENSE_1_0.txt)
  22. // See http://www.boost.org for updates, documentation, and revision history.
  23. #include <iosfwd>
  24. #include <boost/assert.hpp>
  25. #include <locale>
  26. #include <cstring> // std::memcpy
  27. #include <cstddef> // std::size_t
  28. #include <streambuf> // basic_streambuf
  29. #include <string>
  30. #include <boost/config.hpp>
  31. #if defined(BOOST_NO_STDC_NAMESPACE)
  32. namespace std{
  33. using ::memcpy;
  34. using ::size_t;
  35. } // namespace std
  36. #endif
  37. #include <boost/cstdint.hpp>
  38. #include <boost/serialization/throw_exception.hpp>
  39. #include <boost/integer.hpp>
  40. #include <boost/integer_traits.hpp>
  41. #include <boost/serialization/is_bitwise_serializable.hpp>
  42. #include <boost/serialization/array_wrapper.hpp>
  43. #include <boost/archive/basic_streambuf_locale_saver.hpp>
  44. #include <boost/archive/codecvt_null.hpp>
  45. #include <boost/archive/archive_exception.hpp>
  46. #include <boost/archive/detail/auto_link_archive.hpp>
  47. #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
  48. namespace boost {
  49. namespace archive {
  50. /////////////////////////////////////////////////////////////////////////////
  51. // class binary_iarchive - read serialized objects from a input binary stream
  52. template<class Archive, class Elem, class Tr>
  53. class BOOST_SYMBOL_VISIBLE basic_binary_iprimitive {
  54. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  55. friend class load_access;
  56. protected:
  57. #else
  58. public:
  59. #endif
  60. std::basic_streambuf<Elem, Tr> & m_sb;
  61. // return a pointer to the most derived class
  62. Archive * This(){
  63. return static_cast<Archive *>(this);
  64. }
  65. #ifndef BOOST_NO_STD_LOCALE
  66. // note order! - if you change this, libstd++ will fail!
  67. // a) create new locale with new codecvt facet
  68. // b) save current locale
  69. // c) change locale to new one
  70. // d) use stream buffer
  71. // e) change locale back to original
  72. // f) destroy new codecvt facet
  73. boost::archive::codecvt_null<Elem> codecvt_null_facet;
  74. basic_streambuf_locale_saver<Elem, Tr> locale_saver;
  75. std::locale archive_locale;
  76. #endif
  77. // main template for serialization of primitive types
  78. template<class T>
  79. void load(T & t){
  80. load_binary(& t, sizeof(T));
  81. }
  82. /////////////////////////////////////////////////////////
  83. // fundamental types that need special treatment
  84. // trap usage of invalid uninitialized boolean
  85. void load(bool & t){
  86. load_binary(& t, sizeof(t));
  87. int i = t;
  88. BOOST_ASSERT(0 == i || 1 == i);
  89. (void)i; // warning suppression for release builds.
  90. }
  91. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  92. load(std::string &s);
  93. #ifndef BOOST_NO_STD_WSTRING
  94. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  95. load(std::wstring &ws);
  96. #endif
  97. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  98. load(char * t);
  99. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  100. load(wchar_t * t);
  101. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  102. init();
  103. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  104. basic_binary_iprimitive(
  105. std::basic_streambuf<Elem, Tr> & sb,
  106. bool no_codecvt
  107. );
  108. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  109. ~basic_binary_iprimitive();
  110. public:
  111. // we provide an optimized load for all fundamental types
  112. // typedef serialization::is_bitwise_serializable<mpl::_1>
  113. // use_array_optimization;
  114. struct use_array_optimization {
  115. template <class T>
  116. #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
  117. struct apply {
  118. typedef typename boost::serialization::is_bitwise_serializable< T >::type type;
  119. };
  120. #else
  121. struct apply : public boost::serialization::is_bitwise_serializable< T > {};
  122. #endif
  123. };
  124. // the optimized load_array dispatches to load_binary
  125. template <class ValueType>
  126. void load_array(serialization::array_wrapper<ValueType>& a, unsigned int)
  127. {
  128. load_binary(a.address(),a.count()*sizeof(ValueType));
  129. }
  130. void
  131. load_binary(void *address, std::size_t count);
  132. };
  133. template<class Archive, class Elem, class Tr>
  134. inline void
  135. basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
  136. void *address,
  137. std::size_t count
  138. ){
  139. // note: an optimizer should eliminate the following for char files
  140. BOOST_ASSERT(
  141. static_cast<std::streamsize>(count / sizeof(Elem))
  142. <= boost::integer_traits<std::streamsize>::const_max
  143. );
  144. std::streamsize s = static_cast<std::streamsize>(count / sizeof(Elem));
  145. std::streamsize scount = m_sb.sgetn(
  146. static_cast<Elem *>(address),
  147. s
  148. );
  149. if(scount != s)
  150. boost::serialization::throw_exception(
  151. archive_exception(archive_exception::input_stream_error)
  152. );
  153. // note: an optimizer should eliminate the following for char files
  154. BOOST_ASSERT(count % sizeof(Elem) <= boost::integer_traits<std::streamsize>::const_max);
  155. s = static_cast<std::streamsize>(count % sizeof(Elem));
  156. if(0 < s){
  157. // if(is.fail())
  158. // boost::serialization::throw_exception(
  159. // archive_exception(archive_exception::stream_error)
  160. // );
  161. Elem t;
  162. scount = m_sb.sgetn(& t, 1);
  163. if(scount != 1)
  164. boost::serialization::throw_exception(
  165. archive_exception(archive_exception::input_stream_error)
  166. );
  167. std::memcpy(static_cast<char*>(address) + (count - s), &t, static_cast<std::size_t>(s));
  168. }
  169. }
  170. } // namespace archive
  171. } // namespace boost
  172. #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
  173. #endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP