basic_text_iprimitive.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef BOOST_ARCHIVE_BASIC_TEXT_IPRIMITIVE_HPP
  2. #define BOOST_ARCHIVE_BASIC_TEXT_IPRIMITIVE_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_iprimitive.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 are 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. // not a specialization of basic_ostream which in fact is not defined
  19. // in such cases. So we can't use basic_ostream<IStream::char_type> but rather
  20. // use two template parameters
  21. #include <locale>
  22. #include <cstddef> // size_t
  23. #include <boost/config.hpp>
  24. #if defined(BOOST_NO_STDC_NAMESPACE)
  25. namespace std{
  26. using ::size_t;
  27. #if ! defined(BOOST_DINKUMWARE_STDLIB) && ! defined(__SGI_STL_PORT)
  28. using ::locale;
  29. #endif
  30. } // namespace std
  31. #endif
  32. #include <boost/io/ios_state.hpp>
  33. #include <boost/static_assert.hpp>
  34. #include <boost/detail/workaround.hpp>
  35. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
  36. #include <boost/archive/dinkumware.hpp>
  37. #endif
  38. #include <boost/serialization/throw_exception.hpp>
  39. #include <boost/archive/codecvt_null.hpp>
  40. #include <boost/archive/archive_exception.hpp>
  41. #include <boost/archive/basic_streambuf_locale_saver.hpp>
  42. #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
  43. namespace boost {
  44. namespace archive {
  45. /////////////////////////////////////////////////////////////////////////
  46. // class basic_text_iarchive - load serialized objects from a input text stream
  47. #if defined(_MSC_VER)
  48. #pragma warning( push )
  49. #pragma warning( disable : 4244 4267 )
  50. #endif
  51. template<class IStream>
  52. class BOOST_SYMBOL_VISIBLE basic_text_iprimitive {
  53. protected:
  54. IStream &is;
  55. io::ios_flags_saver flags_saver;
  56. io::ios_precision_saver precision_saver;
  57. #ifndef BOOST_NO_STD_LOCALE
  58. // note order! - if you change this, libstd++ will fail!
  59. // a) create new locale with new codecvt facet
  60. // b) save current locale
  61. // c) change locale to new one
  62. // d) use stream buffer
  63. // e) change locale back to original
  64. // f) destroy new codecvt facet
  65. boost::archive::codecvt_null<typename IStream::char_type> codecvt_null_facet;
  66. std::locale archive_locale;
  67. basic_istream_locale_saver<
  68. typename IStream::char_type,
  69. typename IStream::traits_type
  70. > locale_saver;
  71. #endif
  72. template<class T>
  73. void load(T & t)
  74. {
  75. if(is >> t)
  76. return;
  77. boost::serialization::throw_exception(
  78. archive_exception(archive_exception::input_stream_error)
  79. );
  80. }
  81. void load(char & t)
  82. {
  83. short int i;
  84. load(i);
  85. t = i;
  86. }
  87. void load(signed char & t)
  88. {
  89. short int i;
  90. load(i);
  91. t = i;
  92. }
  93. void load(unsigned char & t)
  94. {
  95. unsigned short int i;
  96. load(i);
  97. t = i;
  98. }
  99. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  100. void load(wchar_t & t)
  101. {
  102. BOOST_STATIC_ASSERT(sizeof(wchar_t) <= sizeof(int));
  103. int i;
  104. load(i);
  105. t = i;
  106. }
  107. #endif
  108. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  109. basic_text_iprimitive(IStream &is, bool no_codecvt);
  110. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  111. ~basic_text_iprimitive();
  112. public:
  113. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  114. load_binary(void *address, std::size_t count);
  115. };
  116. #if defined(_MSC_VER)
  117. #pragma warning( pop )
  118. #endif
  119. } // namespace archive
  120. } // namespace boost
  121. #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
  122. #endif // BOOST_ARCHIVE_BASIC_TEXT_IPRIMITIVE_HPP