1
0

basic_binary_iprimitive.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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) && (_MSC_VER >= 1020)
  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/scoped_ptr.hpp>
  39. #include <boost/serialization/throw_exception.hpp>
  40. #include <boost/integer.hpp>
  41. #include <boost/integer_traits.hpp>
  42. #include <boost/archive/basic_streambuf_locale_saver.hpp>
  43. #include <boost/archive/archive_exception.hpp>
  44. #include <boost/mpl/placeholders.hpp>
  45. #include <boost/serialization/is_bitwise_serializable.hpp>
  46. #include <boost/serialization/array.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 basic_binary_iprimitive
  55. {
  56. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  57. friend class load_access;
  58. protected:
  59. #else
  60. public:
  61. #endif
  62. std::basic_streambuf<Elem, Tr> & m_sb;
  63. // return a pointer to the most derived class
  64. Archive * This(){
  65. return static_cast<Archive *>(this);
  66. }
  67. #ifndef BOOST_NO_STD_LOCALE
  68. boost::scoped_ptr<std::locale> archive_locale;
  69. basic_streambuf_locale_saver<Elem, Tr> locale_saver;
  70. #endif
  71. // main template for serilization of primitive types
  72. template<class T>
  73. void load(T & t){
  74. load_binary(& t, sizeof(T));
  75. }
  76. /////////////////////////////////////////////////////////
  77. // fundamental types that need special treatment
  78. // trap usage of invalid uninitialized boolean
  79. void load(bool & t){
  80. load_binary(& t, sizeof(t));
  81. int i = t;
  82. BOOST_ASSERT(0 == i || 1 == i);
  83. (void)i; // warning suppression for release builds.
  84. }
  85. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  86. load(std::string &s);
  87. #ifndef BOOST_NO_STD_WSTRING
  88. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  89. load(std::wstring &ws);
  90. #endif
  91. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  92. load(char * t);
  93. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  94. load(wchar_t * t);
  95. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  96. init();
  97. BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
  98. basic_binary_iprimitive(
  99. std::basic_streambuf<Elem, Tr> & sb,
  100. bool no_codecvt
  101. );
  102. BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
  103. ~basic_binary_iprimitive();
  104. public:
  105. // we provide an optimized load for all fundamental types
  106. // typedef serialization::is_bitwise_serializable<mpl::_1>
  107. // use_array_optimization;
  108. struct use_array_optimization {
  109. template <class T>
  110. #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
  111. struct apply {
  112. typedef BOOST_DEDUCED_TYPENAME boost::serialization::is_bitwise_serializable< T >::type type;
  113. };
  114. #else
  115. struct apply : public boost::serialization::is_bitwise_serializable< T > {};
  116. #endif
  117. };
  118. // the optimized load_array dispatches to load_binary
  119. template <class ValueType>
  120. void load_array(serialization::array<ValueType>& a, unsigned int)
  121. {
  122. load_binary(a.address(),a.count()*sizeof(ValueType));
  123. }
  124. void
  125. load_binary(void *address, std::size_t count);
  126. };
  127. template<class Archive, class Elem, class Tr>
  128. inline void
  129. basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
  130. void *address,
  131. std::size_t count
  132. ){
  133. // note: an optimizer should eliminate the following for char files
  134. BOOST_ASSERT(
  135. static_cast<std::streamsize>(count / sizeof(Elem))
  136. <= boost::integer_traits<std::streamsize>::const_max
  137. );
  138. std::streamsize s = static_cast<std::streamsize>(count / sizeof(Elem));
  139. std::streamsize scount = m_sb.sgetn(
  140. static_cast<Elem *>(address),
  141. s
  142. );
  143. if(scount != s)
  144. boost::serialization::throw_exception(
  145. archive_exception(archive_exception::input_stream_error)
  146. );
  147. // note: an optimizer should eliminate the following for char files
  148. BOOST_ASSERT(count % sizeof(Elem) <= boost::integer_traits<std::streamsize>::const_max);
  149. s = static_cast<std::streamsize>(count % sizeof(Elem));
  150. if(0 < s){
  151. // if(is.fail())
  152. // boost::serialization::throw_exception(
  153. // archive_exception(archive_exception::stream_error)
  154. // );
  155. Elem t;
  156. scount = m_sb.sgetn(& t, 1);
  157. if(scount != 1)
  158. boost::serialization::throw_exception(
  159. archive_exception(archive_exception::input_stream_error)
  160. );
  161. std::memcpy(static_cast<char*>(address) + (count - s), &t, s);
  162. }
  163. }
  164. } // namespace archive
  165. } // namespace boost
  166. #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
  167. #endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP