basic_binary_iprimitive.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 obects. 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/mpl/placeholders.hpp>
  42. #include <boost/serialization/is_bitwise_serializable.hpp>
  43. #include <boost/serialization/array_wrapper.hpp>
  44. #include <boost/archive/basic_streambuf_locale_saver.hpp>
  45. #include <boost/archive/codecvt_null.hpp>
  46. #include <boost/archive/archive_exception.hpp>
  47. #include <boost/archive/detail/auto_link_archive.hpp>
  48. #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
  49. namespace boost {
  50. namespace archive {
  51. /////////////////////////////////////////////////////////////////////////////
  52. // class binary_iarchive - read serialized objects from a input binary stream
  53. template<class Archive, class Elem, class Tr>
  54. class BOOST_SYMBOL_VISIBLE basic_binary_iprimitive {
  55. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  56. friend class load_access;
  57. protected:
  58. #else
  59. public:
  60. #endif
  61. std::basic_streambuf<Elem, Tr> & m_sb;
  62. // return a pointer to the most derived class
  63. Archive * This(){
  64. return static_cast<Archive *>(this);
  65. }
  66. #ifndef BOOST_NO_STD_LOCALE
  67. // note order! - if you change this, libstd++ will fail!
  68. // a) create new locale with new codecvt facet
  69. // b) save current locale
  70. // c) change locale to new one
  71. // d) use stream buffer
  72. // e) change locale back to original
  73. // f) destroy new codecvt facet
  74. boost::archive::codecvt_null<Elem> codecvt_null_facet;
  75. basic_streambuf_locale_saver<Elem, Tr> locale_saver;
  76. std::locale archive_locale;
  77. #endif
  78. // main template for serilization of primitive types
  79. template<class T>
  80. void load(T & t){
  81. load_binary(& t, sizeof(T));
  82. }
  83. /////////////////////////////////////////////////////////
  84. // fundamental types that need special treatment
  85. // trap usage of invalid uninitialized boolean
  86. void load(bool & t){
  87. load_binary(& t, sizeof(t));
  88. int i = t;
  89. BOOST_ASSERT(0 == i || 1 == i);
  90. (void)i; // warning suppression for release builds.
  91. }
  92. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  93. load(std::string &s);
  94. #ifndef BOOST_NO_STD_WSTRING
  95. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  96. load(std::wstring &ws);
  97. #endif
  98. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  99. load(char * t);
  100. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  101. load(wchar_t * t);
  102. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  103. init();
  104. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  105. basic_binary_iprimitive(
  106. std::basic_streambuf<Elem, Tr> & sb,
  107. bool no_codecvt
  108. );
  109. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  110. ~basic_binary_iprimitive();
  111. public:
  112. // we provide an optimized load for all fundamental types
  113. // typedef serialization::is_bitwise_serializable<mpl::_1>
  114. // use_array_optimization;
  115. struct use_array_optimization {
  116. template <class T>
  117. #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
  118. struct apply {
  119. typedef typename boost::serialization::is_bitwise_serializable< T >::type type;
  120. };
  121. #else
  122. struct apply : public boost::serialization::is_bitwise_serializable< T > {};
  123. #endif
  124. };
  125. // the optimized load_array dispatches to load_binary
  126. template <class ValueType>
  127. void load_array(serialization::array_wrapper<ValueType>& a, unsigned int)
  128. {
  129. load_binary(a.address(),a.count()*sizeof(ValueType));
  130. }
  131. void
  132. load_binary(void *address, std::size_t count);
  133. };
  134. template<class Archive, class Elem, class Tr>
  135. inline void
  136. basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
  137. void *address,
  138. std::size_t count
  139. ){
  140. // note: an optimizer should eliminate the following for char files
  141. BOOST_ASSERT(
  142. static_cast<std::streamsize>(count / sizeof(Elem))
  143. <= boost::integer_traits<std::streamsize>::const_max
  144. );
  145. std::streamsize s = static_cast<std::streamsize>(count / sizeof(Elem));
  146. std::streamsize scount = m_sb.sgetn(
  147. static_cast<Elem *>(address),
  148. s
  149. );
  150. if(scount != s)
  151. boost::serialization::throw_exception(
  152. archive_exception(archive_exception::input_stream_error)
  153. );
  154. // note: an optimizer should eliminate the following for char files
  155. BOOST_ASSERT(count % sizeof(Elem) <= boost::integer_traits<std::streamsize>::const_max);
  156. s = static_cast<std::streamsize>(count % sizeof(Elem));
  157. if(0 < s){
  158. // if(is.fail())
  159. // boost::serialization::throw_exception(
  160. // archive_exception(archive_exception::stream_error)
  161. // );
  162. Elem t;
  163. scount = m_sb.sgetn(& t, 1);
  164. if(scount != 1)
  165. boost::serialization::throw_exception(
  166. archive_exception(archive_exception::input_stream_error)
  167. );
  168. std::memcpy(static_cast<char*>(address) + (count - s), &t, static_cast<std::size_t>(s));
  169. }
  170. }
  171. } // namespace archive
  172. } // namespace boost
  173. #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
  174. #endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP