basic_text_oprimitive.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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) && (_MSC_VER >= 1020)
  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 <boost/config/no_tr1/cmath.hpp> // isnan
  24. #include <boost/assert.hpp>
  25. #include <cstddef> // size_t
  26. #include <boost/config.hpp>
  27. #include <boost/static_assert.hpp>
  28. #include <boost/detail/workaround.hpp>
  29. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
  30. #include <boost/archive/dinkumware.hpp>
  31. #endif
  32. #if defined(BOOST_NO_STDC_NAMESPACE)
  33. namespace std{
  34. using ::size_t;
  35. #if ! defined(BOOST_DINKUMWARE_STDLIB) && ! defined(__SGI_STL_PORT)
  36. using ::locale;
  37. #endif
  38. } // namespace std
  39. #endif
  40. #include <boost/limits.hpp>
  41. #include <boost/integer.hpp>
  42. #include <boost/io/ios_state.hpp>
  43. #include <boost/scoped_ptr.hpp>
  44. #include <boost/serialization/throw_exception.hpp>
  45. #include <boost/archive/archive_exception.hpp>
  46. #include <boost/archive/basic_streambuf_locale_saver.hpp>
  47. #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
  48. namespace boost {
  49. namespace archive {
  50. class save_access;
  51. /////////////////////////////////////////////////////////////////////////
  52. // class basic_text_oprimitive - output of prmitives to stream
  53. template<class OStream>
  54. class basic_text_oprimitive
  55. {
  56. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  57. protected:
  58. #else
  59. public:
  60. #endif
  61. OStream &os;
  62. io::ios_flags_saver flags_saver;
  63. io::ios_precision_saver precision_saver;
  64. #ifndef BOOST_NO_STD_LOCALE
  65. boost::scoped_ptr<std::locale> archive_locale;
  66. basic_streambuf_locale_saver<
  67. BOOST_DEDUCED_TYPENAME OStream::char_type,
  68. BOOST_DEDUCED_TYPENAME OStream::traits_type
  69. > locale_saver;
  70. #endif
  71. // default saving of primitives.
  72. template<class T>
  73. void save(const T &t){
  74. if(os.fail())
  75. boost::serialization::throw_exception(
  76. archive_exception(archive_exception::output_stream_error)
  77. );
  78. os << t;
  79. }
  80. /////////////////////////////////////////////////////////
  81. // fundamental types that need special treatment
  82. void save(const bool t){
  83. // trap usage of invalid uninitialized boolean which would
  84. // otherwise crash on load.
  85. BOOST_ASSERT(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
  86. if(os.fail())
  87. boost::serialization::throw_exception(
  88. archive_exception(archive_exception::output_stream_error)
  89. );
  90. os << t;
  91. }
  92. void save(const signed char t)
  93. {
  94. save(static_cast<short int>(t));
  95. }
  96. void save(const unsigned char t)
  97. {
  98. save(static_cast<short unsigned int>(t));
  99. }
  100. void save(const char t)
  101. {
  102. save(static_cast<short int>(t));
  103. }
  104. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  105. void save(const wchar_t t)
  106. {
  107. BOOST_STATIC_ASSERT(sizeof(wchar_t) <= sizeof(int));
  108. save(static_cast<int>(t));
  109. }
  110. #endif
  111. void save(const float t)
  112. {
  113. // must be a user mistake - can't serialize un-initialized data
  114. if(os.fail())
  115. boost::serialization::throw_exception(
  116. archive_exception(archive_exception::output_stream_error)
  117. );
  118. os << std::setprecision(std::numeric_limits<float>::digits10 + 2);
  119. os << t;
  120. }
  121. void save(const double t)
  122. {
  123. // must be a user mistake - can't serialize un-initialized data
  124. if(os.fail())
  125. boost::serialization::throw_exception(
  126. archive_exception(archive_exception::output_stream_error)
  127. );
  128. os << std::setprecision(std::numeric_limits<double>::digits10 + 2);
  129. os << t;
  130. }
  131. BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
  132. basic_text_oprimitive(OStream & os, bool no_codecvt);
  133. BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
  134. ~basic_text_oprimitive();
  135. public:
  136. // unformatted append of one character
  137. void put(BOOST_DEDUCED_TYPENAME OStream::char_type c){
  138. if(os.fail())
  139. boost::serialization::throw_exception(
  140. archive_exception(archive_exception::output_stream_error)
  141. );
  142. os.put(c);
  143. }
  144. // unformatted append of null terminated string
  145. void put(const char * s){
  146. while('\0' != *s)
  147. os.put(*s++);
  148. }
  149. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  150. save_binary(const void *address, std::size_t count);
  151. };
  152. } //namespace boost
  153. } //namespace archive
  154. #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
  155. #endif // BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP