basic_text_oprimitive.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP
  2. #define BOOST_ARCHIVE_BASIC_TEXT_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_text_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 text - note these ar templated on the basic
  15. // stream templates to accommodate wide (and other?) kind of characters
  16. //
  17. // note the fact that on libraries without wide characters, ostream is
  18. // is not a specialization of basic_ostream which in fact is not defined
  19. // in such cases. So we can't use basic_ostream<OStream::char_type> but rather
  20. // use two template parameters
  21. #include <iomanip>
  22. #include <locale>
  23. #include <cstddef> // size_t
  24. #include <boost/config.hpp>
  25. #include <boost/static_assert.hpp>
  26. #include <boost/io/ios_state.hpp>
  27. #include <boost/detail/workaround.hpp>
  28. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
  29. #include <boost/archive/dinkumware.hpp>
  30. #endif
  31. #if defined(BOOST_NO_STDC_NAMESPACE)
  32. namespace std{
  33. using ::size_t;
  34. #if ! defined(BOOST_DINKUMWARE_STDLIB) && ! defined(__SGI_STL_PORT)
  35. using ::locale;
  36. #endif
  37. } // namespace std
  38. #endif
  39. #include <boost/type_traits/is_floating_point.hpp>
  40. #include <boost/mpl/bool.hpp>
  41. #include <boost/limits.hpp>
  42. #include <boost/integer.hpp>
  43. #include <boost/io/ios_state.hpp>
  44. #include <boost/serialization/throw_exception.hpp>
  45. #include <boost/archive/basic_streambuf_locale_saver.hpp>
  46. #include <boost/archive/codecvt_null.hpp>
  47. #include <boost/archive/archive_exception.hpp>
  48. #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
  49. namespace boost {
  50. namespace archive {
  51. /////////////////////////////////////////////////////////////////////////
  52. // class basic_text_oprimitive - output of prmitives to stream
  53. template<class OStream>
  54. class BOOST_SYMBOL_VISIBLE basic_text_oprimitive
  55. {
  56. protected:
  57. OStream &os;
  58. io::ios_flags_saver flags_saver;
  59. io::ios_precision_saver precision_saver;
  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<typename OStream::char_type> codecvt_null_facet;
  69. std::locale archive_locale;
  70. basic_ostream_locale_saver<
  71. typename OStream::char_type,
  72. typename OStream::traits_type
  73. > locale_saver;
  74. #endif
  75. /////////////////////////////////////////////////////////
  76. // fundamental types that need special treatment
  77. void save(const bool t){
  78. // trap usage of invalid uninitialized boolean which would
  79. // otherwise crash on load.
  80. BOOST_ASSERT(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
  81. if(os.fail())
  82. boost::serialization::throw_exception(
  83. archive_exception(archive_exception::output_stream_error)
  84. );
  85. os << t;
  86. }
  87. void save(const signed char t)
  88. {
  89. save(static_cast<short int>(t));
  90. }
  91. void save(const unsigned char t)
  92. {
  93. save(static_cast<short unsigned int>(t));
  94. }
  95. void save(const char t)
  96. {
  97. save(static_cast<short int>(t));
  98. }
  99. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  100. void save(const wchar_t t)
  101. {
  102. BOOST_STATIC_ASSERT(sizeof(wchar_t) <= sizeof(int));
  103. save(static_cast<int>(t));
  104. }
  105. #endif
  106. /////////////////////////////////////////////////////////
  107. // saving of any types not listed above
  108. template<class T>
  109. void save_impl(const T &t, boost::mpl::bool_<false> &){
  110. if(os.fail())
  111. boost::serialization::throw_exception(
  112. archive_exception(archive_exception::output_stream_error)
  113. );
  114. os << t;
  115. }
  116. /////////////////////////////////////////////////////////
  117. // floating point types need even more special treatment
  118. // the following determines whether the type T is some sort
  119. // of floating point type. Note that we then assume that
  120. // the stream << operator is defined on that type - if not
  121. // we'll get a compile time error. This is meant to automatically
  122. // support synthesized types which support floating point
  123. // operations. Also it should handle compiler dependent types
  124. // such long double. Due to John Maddock.
  125. template<class T>
  126. struct is_float {
  127. typedef typename mpl::bool_<
  128. boost::is_floating_point<T>::value
  129. || (std::numeric_limits<T>::is_specialized
  130. && !std::numeric_limits<T>::is_integer
  131. && !std::numeric_limits<T>::is_exact
  132. && std::numeric_limits<T>::max_exponent)
  133. >::type type;
  134. };
  135. template<class T>
  136. void save_impl(const T &t, boost::mpl::bool_<true> &){
  137. // must be a user mistake - can't serialize un-initialized data
  138. if(os.fail())
  139. boost::serialization::throw_exception(
  140. archive_exception(archive_exception::output_stream_error)
  141. );
  142. // The formulae for the number of decimla digits required is given in
  143. // http://www2.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf
  144. // which is derived from Kahan's paper:
  145. // www.eecs.berkeley.edu/~wkahan/ieee754status/ieee754.ps
  146. // const unsigned int digits = (std::numeric_limits<T>::digits * 3010) / 10000;
  147. // note: I've commented out the above because I didn't get good results. e.g.
  148. // in one case I got a difference of 19 units.
  149. #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
  150. const unsigned int digits = std::numeric_limits<T>::max_digits10;
  151. #else
  152. const unsigned int digits = std::numeric_limits<T>::digits10 + 2;
  153. #endif
  154. os << std::setprecision(digits) << std::scientific << t;
  155. }
  156. template<class T>
  157. void save(const T & t){
  158. typename is_float<T>::type tf;
  159. save_impl(t, tf);
  160. }
  161. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  162. basic_text_oprimitive(OStream & os, bool no_codecvt);
  163. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  164. ~basic_text_oprimitive();
  165. public:
  166. // unformatted append of one character
  167. void put(typename OStream::char_type c){
  168. if(os.fail())
  169. boost::serialization::throw_exception(
  170. archive_exception(archive_exception::output_stream_error)
  171. );
  172. os.put(c);
  173. }
  174. // unformatted append of null terminated string
  175. void put(const char * s){
  176. while('\0' != *s)
  177. os.put(*s++);
  178. }
  179. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  180. save_binary(const void *address, std::size_t count);
  181. };
  182. } //namespace boost
  183. } //namespace archive
  184. #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
  185. #endif // BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP