codecvt_helper.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 the definition of the template codecvt_helper, useful for
  7. // defining specializations of std::codecvt where state_type != mbstate_t.
  8. // Compensates for the fact that some standard library implementations
  9. // do not derive the primiary codecvt template from locale::facet or
  10. // provide the correct member types and functions.
  11. // Usage:
  12. //
  13. // // In global namespace:
  14. // BOOST_IOSTREAMS_CODECVT_SPEC(mystate)
  15. //
  16. // // In user namespace:
  17. // template<typename Intern, typename Extern>
  18. // struct mycodecvt : codecvt_helper<Intern, Extern, State> { ... };
  19. //
  20. // // Or:
  21. // struct mycodecvt : codecvt_helper<wchar_t, char, State> { ... };
  22. //
  23. // Etc.
  24. #ifndef BOOST_IOSTREAMS_DETAIL_CODECVT_HELPER_HPP_INCLUDED
  25. #define BOOST_IOSTREAMS_DETAIL_CODECVT_HELPER_HPP_INCLUDED
  26. #if defined(_MSC_VER)
  27. # pragma once
  28. #endif
  29. #include <boost/config.hpp> // Put size_t in std, BOOST_MSVC, Dinkum.
  30. #include <boost/detail/workaround.hpp>
  31. #include <algorithm> // min.
  32. #include <cstddef> // size_t.
  33. #include <locale> // locale, codecvt_base, codecvt.
  34. #include <boost/iostreams/detail/config/codecvt.hpp>
  35. //------------------Definition of traits--------------------------------------//
  36. namespace boost { namespace iostreams { namespace detail {
  37. #if !BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) //-----------------------//
  38. template<typename T>
  39. struct codecvt_intern { typedef typename T::intern_type type; };
  40. template<typename T>
  41. struct codecvt_extern { typedef typename T::extern_type type; };
  42. #else // #if !BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) //--------------//
  43. template<typename T>
  44. struct codecvt_intern { typedef typename T::from_type type; };
  45. template<typename T>
  46. struct codecvt_extern { typedef typename T::to_type type; };
  47. #endif // #if !BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) //-------------//
  48. template<typename T>
  49. struct codecvt_state { typedef typename T::state_type type; };
  50. } } } // End namespaces detail, iostreams, boost.
  51. //------------------Definition of codecvt_impl--------------------------------//
  52. #if defined(BOOST_IOSTREAMS_NO_PRIMARY_CODECVT_DEFINITION) || \
  53. defined(BOOST_IOSTREAMS_EMPTY_PRIMARY_CODECVT_DEFINITION) || \
  54. defined(BOOST_IOSTREAMS_NO_LOCALE) \
  55. /**/
  56. namespace boost { namespace iostreams { namespace detail {
  57. template<typename Intern, typename Extern, typename State>
  58. struct codecvt_impl : std::locale::facet, std::codecvt_base {
  59. public:
  60. typedef Intern intern_type;
  61. typedef Extern extern_type;
  62. typedef State state_type;
  63. codecvt_impl(std::size_t refs = 0) : std::locale::facet(refs) { }
  64. std::codecvt_base::result
  65. in( State& state, const Extern* first1, const Extern* last1,
  66. const Extern*& next1, Intern* first2, Intern* last2,
  67. Intern*& next2 ) const
  68. {
  69. return do_in(state, first1, last1, next1, first2, last2, next2);
  70. }
  71. std::codecvt_base::result
  72. out( State& state, const Intern* first1, const Intern* last1,
  73. const Intern*& next1, Extern* first2, Extern* last2,
  74. Extern*& next2 ) const
  75. {
  76. return do_out(state, first1, last1, next1, first2, last2, next2);
  77. }
  78. std::codecvt_base::result
  79. unshift(State& state, Extern* first2, Extern* last2, Extern*& next2) const
  80. {
  81. return do_unshift(state, first2, last2, next2);
  82. }
  83. bool always_noconv() const throw() { return do_always_noconv(); }
  84. int max_length() const throw() { return do_max_length(); }
  85. int encoding() const throw() { return do_encoding(); }
  86. int length( BOOST_IOSTREAMS_CODECVT_CV_QUALIFIER State& state,
  87. const Extern* first1, const Extern* last1,
  88. std::size_t len2 ) const throw()
  89. {
  90. return do_length(state, first1, last1, len2);
  91. }
  92. protected:
  93. std::codecvt_base::result
  94. virtual do_in( State&, const Extern*, const Extern*, const Extern*&,
  95. Intern*, Intern*, Intern*& ) const
  96. {
  97. return std::codecvt_base::noconv;
  98. }
  99. std::codecvt_base::result
  100. virtual do_out( State&, const Intern*, const Intern*, const Intern*&,
  101. Extern*, Extern*, Extern*& ) const
  102. {
  103. return std::codecvt_base::noconv;
  104. }
  105. std::codecvt_base::result
  106. virtual do_unshift(State&, Extern*, Extern*, Extern*&) const
  107. {
  108. return std::codecvt_base::ok;
  109. }
  110. virtual bool do_always_noconv() const throw() { return true; }
  111. virtual int do_max_length() const throw() { return 1; }
  112. virtual int do_encoding() const throw() { return 1; }
  113. virtual int do_length( BOOST_IOSTREAMS_CODECVT_CV_QUALIFIER State&,
  114. const Extern* first1, const Extern* last1,
  115. std::size_t len2 ) const throw()
  116. {
  117. return (std::min)(static_cast<std::size_t>(last1 - first1), len2);
  118. }
  119. };
  120. } } } // End namespaces detail, iostreams, boost.
  121. #endif // no primary codecvt definition, empty definition.
  122. //------------------Definition of BOOST_IOSTREAMS_CODECVT_SPEC----------------//
  123. #if defined(BOOST_IOSTREAMS_NO_PRIMARY_CODECVT_DEFINITION) || \
  124. defined(BOOST_IOSTREAMS_EMPTY_PRIMARY_CODECVT_DEFINITION) \
  125. /**/
  126. # define BOOST_IOSTREAMS_CODECVT_SPEC(state) \
  127. namespace std { \
  128. template<typename Intern, typename Extern> \
  129. class codecvt<Intern, Extern, state> \
  130. : public ::boost::iostreams::detail::codecvt_impl< \
  131. Intern, Extern, state \
  132. > \
  133. { \
  134. public: \
  135. codecvt(std::size_t refs = 0) \
  136. : ::boost::iostreams::detail::codecvt_impl< \
  137. Intern, Extern, state \
  138. >(refs) \
  139. { } \
  140. static std::locale::id id; \
  141. }; \
  142. template<typename Intern, typename Extern> \
  143. std::locale::id codecvt<Intern, Extern, state>::id; \
  144. } \
  145. /**/
  146. #else
  147. # define BOOST_IOSTREAMS_CODECVT_SPEC(state)
  148. #endif // no primary codecvt definition, or empty definition.
  149. namespace boost { namespace iostreams { namespace detail {
  150. //------------------Definition of codecvt_helper------------------------------//
  151. template<typename Intern, typename Extern, typename State>
  152. struct codecvt_helper : std::codecvt<Intern, Extern, State> {
  153. typedef Intern intern_type;
  154. typedef Extern extern_type;
  155. typedef State state_type;
  156. codecvt_helper(std::size_t refs = 0)
  157. #if !defined(BOOST_IOSTREAMS_NO_CODECVT_CTOR_FROM_SIZE_T)
  158. : std::codecvt<Intern, Extern, State>(refs)
  159. #else
  160. : std::codecvt<Intern, Extern, State>()
  161. #endif
  162. { }
  163. #ifdef BOOST_IOSTREAMS_NO_CODECVT_MAX_LENGTH
  164. int max_length() const throw() { return do_max_length(); }
  165. protected:
  166. virtual int do_max_length() const throw() { return 1; }
  167. #endif
  168. };
  169. } } } // End namespaces detail, iostreams, boost.
  170. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CODECVT_HELPER_HPP_INCLUDED