codecvt_holder.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2003-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. // Contains machinery for performing code conversion.
  7. #ifndef BOOST_IOSTREAMS_DETAIL_CODECVT_HOLDER_HPP_INCLUDED
  8. #define BOOST_IOSTREAMS_DETAIL_CODECVT_HOLDER_HPP_INCLUDED
  9. #if defined(_MSC_VER)
  10. # pragma once
  11. #endif
  12. #include <cwchar> // mbstate_t.
  13. #include <locale> // codecvt, locale.
  14. #include <boost/config.hpp> // HAS_MACRO_USE_FACET.
  15. #include <boost/iostreams/detail/config/codecvt.hpp>
  16. namespace boost { namespace iostreams { namespace detail {
  17. struct default_codecvt {
  18. typedef wchar_t intern_type, from_type;
  19. typedef char extern_type, to_type;
  20. typedef std::mbstate_t state_type;
  21. };
  22. template<typename Codecvt>
  23. struct codecvt_holder {
  24. typedef Codecvt codecvt_type;
  25. const codecvt_type& get() const { return codecvt_; }
  26. void imbue(const std::locale&) { }
  27. Codecvt codecvt_;
  28. };
  29. template<>
  30. struct codecvt_holder<default_codecvt> {
  31. typedef std::codecvt<wchar_t, char, std::mbstate_t> codecvt_type;
  32. codecvt_holder() { reset_codecvt(); }
  33. const codecvt_type& get() const { return *codecvt_; }
  34. void imbue(const std::locale& loc)
  35. {
  36. loc_ = loc;
  37. reset_codecvt();
  38. }
  39. void reset_codecvt()
  40. {
  41. using namespace std;
  42. #ifndef BOOST_HAS_MACRO_USE_FACET
  43. codecvt_ = & use_facet< codecvt_type >(loc_);
  44. #else
  45. codecvt_ = & _USE(loc_, codecvt_type);
  46. #endif
  47. }
  48. std::locale loc_; // Prevent codecvt_ from being freed.
  49. const codecvt_type* codecvt_;
  50. };
  51. } } } // End namespaces detail, iostreams, boost.
  52. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CODECVT_HOLDER_HPP_INCLUDED