basic_binary_oprimitive.hpp 6.1 KB

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