basic_binary_oprimitive.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP
  2. #define BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // basic_binary_oprimitive.hpp
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. // archives stored as native binary - this should be the fastest way
  15. // to archive the state of a group of obects. It makes no attempt to
  16. // convert to any canonical form.
  17. // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE
  18. // ON PLATFORM APART FROM THE ONE THEY ARE CREATE ON
  19. #include <iosfwd>
  20. #include <boost/assert.hpp>
  21. #include <locale>
  22. #include <streambuf> // basic_streambuf
  23. #include <string>
  24. #include <cstddef> // size_t
  25. #include <boost/config.hpp>
  26. #if defined(BOOST_NO_STDC_NAMESPACE)
  27. namespace std{
  28. using ::size_t;
  29. } // namespace std
  30. #endif
  31. #include <boost/cstdint.hpp>
  32. #include <boost/integer.hpp>
  33. #include <boost/integer_traits.hpp>
  34. #include <boost/scoped_ptr.hpp>
  35. #include <boost/serialization/throw_exception.hpp>
  36. #include <boost/archive/basic_streambuf_locale_saver.hpp>
  37. #include <boost/archive/archive_exception.hpp>
  38. #include <boost/serialization/is_bitwise_serializable.hpp>
  39. #include <boost/mpl/placeholders.hpp>
  40. #include <boost/serialization/array.hpp>
  41. #include <boost/archive/detail/auto_link_archive.hpp>
  42. #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
  43. namespace boost {
  44. namespace archive {
  45. /////////////////////////////////////////////////////////////////////////
  46. // class basic_binary_oprimitive - binary output of prmitives
  47. template<class Archive, class Elem, class Tr>
  48. class basic_binary_oprimitive
  49. {
  50. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  51. friend class save_access;
  52. protected:
  53. #else
  54. public:
  55. #endif
  56. std::basic_streambuf<Elem, Tr> & m_sb;
  57. // return a pointer to the most derived class
  58. Archive * This(){
  59. return static_cast<Archive *>(this);
  60. }
  61. #ifndef BOOST_NO_STD_LOCALE
  62. boost::scoped_ptr<std::locale> archive_locale;
  63. basic_streambuf_locale_saver<Elem, Tr> locale_saver;
  64. #endif
  65. // default saving of primitives.
  66. template<class T>
  67. void save(const T & t)
  68. {
  69. save_binary(& t, sizeof(T));
  70. }
  71. /////////////////////////////////////////////////////////
  72. // fundamental types that need special treatment
  73. // trap usage of invalid uninitialized boolean which would
  74. // otherwise crash on load.
  75. void save(const bool t){
  76. BOOST_ASSERT(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
  77. save_binary(& t, sizeof(t));
  78. }
  79. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  80. save(const std::string &s);
  81. #ifndef BOOST_NO_STD_WSTRING
  82. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  83. save(const std::wstring &ws);
  84. #endif
  85. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  86. save(const char * t);
  87. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  88. save(const wchar_t * t);
  89. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  90. init();
  91. BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
  92. basic_binary_oprimitive(
  93. std::basic_streambuf<Elem, Tr> & sb,
  94. bool no_codecvt
  95. );
  96. BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
  97. ~basic_binary_oprimitive();
  98. public:
  99. // we provide an optimized save for all fundamental types
  100. // typedef serialization::is_bitwise_serializable<mpl::_1>
  101. // use_array_optimization;
  102. // workaround without using mpl lambdas
  103. struct use_array_optimization {
  104. template <class T>
  105. #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
  106. struct apply {
  107. typedef BOOST_DEDUCED_TYPENAME boost::serialization::is_bitwise_serializable< T >::type type;
  108. };
  109. #else
  110. struct apply : public boost::serialization::is_bitwise_serializable< T > {};
  111. #endif
  112. };
  113. // the optimized save_array dispatches to save_binary
  114. template <class ValueType>
  115. void save_array(boost::serialization::array<ValueType> const& a, unsigned int)
  116. {
  117. save_binary(a.address(),a.count()*sizeof(ValueType));
  118. }
  119. void save_binary(const void *address, std::size_t count);
  120. };
  121. template<class Archive, class Elem, class Tr>
  122. inline void
  123. basic_binary_oprimitive<Archive, Elem, Tr>::save_binary(
  124. const void *address,
  125. std::size_t count
  126. ){
  127. //BOOST_ASSERT(
  128. // static_cast<std::size_t>((std::numeric_limits<std::streamsize>::max)()) >= count
  129. //);
  130. // note: if the following assertions fail
  131. // a likely cause is that the output stream is set to "text"
  132. // mode where by cr characters recieve special treatment.
  133. // be sure that the output stream is opened with ios::binary
  134. //if(os.fail())
  135. // boost::serialization::throw_exception(
  136. // archive_exception(archive_exception::output_stream_error)
  137. // );
  138. // figure number of elements to output - round up
  139. count = ( count + sizeof(Elem) - 1)
  140. / sizeof(Elem);
  141. BOOST_ASSERT(count <= std::size_t(boost::integer_traits<std::streamsize>::const_max));
  142. std::streamsize scount = m_sb.sputn(
  143. static_cast<const Elem *>(address),
  144. static_cast<std::streamsize>(count)
  145. );
  146. if(count != static_cast<std::size_t>(scount))
  147. boost::serialization::throw_exception(
  148. archive_exception(archive_exception::output_stream_error)
  149. );
  150. //os.write(
  151. // static_cast<const BOOST_DEDUCED_TYPENAME OStream::char_type *>(address),
  152. // count
  153. //);
  154. //BOOST_ASSERT(os.good());
  155. }
  156. } //namespace boost
  157. } //namespace archive
  158. #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
  159. #endif // BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP