basic_binary_oprimitive.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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)
  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 objects. 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/serialization/is_bitwise_serializable.hpp>
  37. #include <boost/serialization/array_wrapper.hpp>
  38. #include <boost/archive/basic_streambuf_locale_saver.hpp>
  39. #include <boost/archive/codecvt_null.hpp>
  40. #include <boost/archive/archive_exception.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 primitives
  47. template<class Archive, class Elem, class Tr>
  48. class BOOST_SYMBOL_VISIBLE basic_binary_oprimitive {
  49. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  50. friend class save_access;
  51. protected:
  52. #else
  53. public:
  54. #endif
  55. std::basic_streambuf<Elem, Tr> & m_sb;
  56. // return a pointer to the most derived class
  57. Archive * This(){
  58. return static_cast<Archive *>(this);
  59. }
  60. #ifndef BOOST_NO_STD_LOCALE
  61. // note order! - if you change this, libstd++ will fail!
  62. // a) create new locale with new codecvt facet
  63. // b) save current locale
  64. // c) change locale to new one
  65. // d) use stream buffer
  66. // e) change locale back to original
  67. // f) destroy new codecvt facet
  68. boost::archive::codecvt_null<Elem> codecvt_null_facet;
  69. basic_streambuf_locale_saver<Elem, Tr> locale_saver;
  70. std::locale archive_locale;
  71. #endif
  72. // default saving of primitives.
  73. template<class T>
  74. void save(const T & t)
  75. {
  76. save_binary(& t, sizeof(T));
  77. }
  78. /////////////////////////////////////////////////////////
  79. // fundamental types that need special treatment
  80. // trap usage of invalid uninitialized boolean which would
  81. // otherwise crash on load.
  82. void save(const bool t){
  83. BOOST_ASSERT(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
  84. save_binary(& t, sizeof(t));
  85. }
  86. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  87. save(const std::string &s);
  88. #ifndef BOOST_NO_STD_WSTRING
  89. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  90. save(const std::wstring &ws);
  91. #endif
  92. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  93. save(const char * t);
  94. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  95. save(const wchar_t * t);
  96. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  97. init();
  98. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  99. basic_binary_oprimitive(
  100. std::basic_streambuf<Elem, Tr> & sb,
  101. bool no_codecvt
  102. );
  103. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  104. ~basic_binary_oprimitive();
  105. public:
  106. // we provide an optimized save for all fundamental types
  107. // typedef serialization::is_bitwise_serializable<mpl::_1>
  108. // use_array_optimization;
  109. // workaround without using mpl lambdas
  110. struct use_array_optimization {
  111. template <class T>
  112. #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
  113. struct apply {
  114. typedef typename boost::serialization::is_bitwise_serializable< T >::type type;
  115. };
  116. #else
  117. struct apply : public boost::serialization::is_bitwise_serializable< T > {};
  118. #endif
  119. };
  120. // the optimized save_array dispatches to save_binary
  121. template <class ValueType>
  122. void save_array(boost::serialization::array_wrapper<ValueType> const& a, unsigned int)
  123. {
  124. save_binary(a.address(),a.count()*sizeof(ValueType));
  125. }
  126. void save_binary(const void *address, std::size_t count);
  127. };
  128. template<class Archive, class Elem, class Tr>
  129. inline void
  130. basic_binary_oprimitive<Archive, Elem, Tr>::save_binary(
  131. const void *address,
  132. std::size_t count
  133. ){
  134. // BOOST_ASSERT(count <= std::size_t(boost::integer_traits<std::streamsize>::const_max));
  135. // note: if the following assertions fail
  136. // a likely cause is that the output stream is set to "text"
  137. // mode where by cr characters receive special treatment.
  138. // be sure that the output stream is opened with ios::binary
  139. //if(os.fail())
  140. // boost::serialization::throw_exception(
  141. // archive_exception(archive_exception::output_stream_error)
  142. // );
  143. // figure number of elements to output - round up
  144. count = ( count + sizeof(Elem) - 1) / sizeof(Elem);
  145. std::streamsize scount = m_sb.sputn(
  146. static_cast<const Elem *>(address),
  147. static_cast<std::streamsize>(count)
  148. );
  149. if(count != static_cast<std::size_t>(scount))
  150. boost::serialization::throw_exception(
  151. archive_exception(archive_exception::output_stream_error)
  152. );
  153. //os.write(
  154. // static_cast<const typename OStream::char_type *>(address),
  155. // count
  156. //);
  157. //BOOST_ASSERT(os.good());
  158. }
  159. } //namespace boost
  160. } //namespace archive
  161. #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
  162. #endif // BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP