basic_streambuf_locale_saver.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef BOOST_ARCHIVE_BASIC_STREAMBUF_LOCALE_SAVER_HPP
  2. #define BOOST_ARCHIVE_BASIC_STREAMBUF_LOCALE_SAVER_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_streambuf_locale_saver.hpp
  9. // (C) Copyright 2005 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. // note derived from boost/io/ios_state.hpp
  15. // Copyright 2002, 2005 Daryle Walker. Use, modification, and distribution
  16. // are subject to the Boost Software License, Version 1.0. (See accompanying
  17. // file LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
  18. // See <http://www.boost.org/libs/io/> for the library's home page.
  19. #ifndef BOOST_NO_STD_LOCALE
  20. #include <locale> // for std::locale
  21. #include <ios>
  22. #include <streambuf> // for std::basic_streambuf
  23. #include <boost/config.hpp>
  24. #include <boost/noncopyable.hpp>
  25. #ifdef BOOST_MSVC
  26. # pragma warning(push)
  27. # pragma warning(disable : 4511 4512)
  28. #endif
  29. namespace boost{
  30. namespace archive{
  31. template < typename Ch, class Tr >
  32. class basic_streambuf_locale_saver :
  33. private boost::noncopyable
  34. {
  35. public:
  36. explicit basic_streambuf_locale_saver(std::basic_streambuf<Ch, Tr> &s) :
  37. m_streambuf(s),
  38. m_locale(s.getloc())
  39. {}
  40. ~basic_streambuf_locale_saver(){
  41. m_streambuf.pubsync();
  42. m_streambuf.pubimbue(m_locale);
  43. }
  44. private:
  45. std::basic_streambuf<Ch, Tr> & m_streambuf;
  46. std::locale const m_locale;
  47. };
  48. template < typename Ch, class Tr >
  49. class basic_istream_locale_saver :
  50. private boost::noncopyable
  51. {
  52. public:
  53. explicit basic_istream_locale_saver(std::basic_istream<Ch, Tr> &s) :
  54. m_istream(s),
  55. m_locale(s.getloc())
  56. {}
  57. ~basic_istream_locale_saver(){
  58. // libstdc++ crashes without this
  59. m_istream.sync();
  60. m_istream.imbue(m_locale);
  61. }
  62. private:
  63. std::basic_istream<Ch, Tr> & m_istream;
  64. std::locale const m_locale;
  65. };
  66. template < typename Ch, class Tr >
  67. class basic_ostream_locale_saver :
  68. private boost::noncopyable
  69. {
  70. public:
  71. explicit basic_ostream_locale_saver(std::basic_ostream<Ch, Tr> &s) :
  72. m_ostream(s),
  73. m_locale(s.getloc())
  74. {}
  75. ~basic_ostream_locale_saver(){
  76. m_ostream.flush();
  77. m_ostream.imbue(m_locale);
  78. }
  79. private:
  80. std::basic_ostream<Ch, Tr> & m_ostream;
  81. std::locale const m_locale;
  82. };
  83. } // archive
  84. } // boost
  85. #ifdef BOOST_MSVC
  86. #pragma warning(pop)
  87. #endif
  88. #endif // BOOST_NO_STD_LOCALE
  89. #endif // BOOST_ARCHIVE_BASIC_STREAMBUF_LOCALE_SAVER_HPP