traits_utils.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // traits_utils.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_TRAITS_UTILS_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_UTILITY_TRAITS_UTILS_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. # pragma warning(push)
  13. # pragma warning(disable : 4100) // unreferenced formal parameter
  14. #endif
  15. #include <string>
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. #include <boost/utility/enable_if.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. #include <boost/iterator/transform_iterator.hpp>
  21. #include <boost/xpressive/detail/utility/algorithm.hpp>
  22. namespace boost { namespace xpressive { namespace detail
  23. {
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // char_cast
  26. //
  27. template<typename ToChar, typename FromChar, typename Traits>
  28. inline ToChar
  29. char_cast(FromChar from, Traits const &, typename enable_if<is_same<ToChar, FromChar> >::type * = 0)
  30. {
  31. return from;
  32. }
  33. template<typename ToChar, typename FromChar, typename Traits>
  34. inline ToChar
  35. char_cast(FromChar from, Traits const &tr, typename disable_if<is_same<ToChar, FromChar> >::type * = 0)
  36. {
  37. BOOST_MPL_ASSERT((is_same<FromChar, char>));
  38. return tr.widen(from);
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////
  41. // widen_fun
  42. //
  43. template<typename Traits>
  44. struct widen_fun
  45. {
  46. typedef typename Traits::char_type result_type;
  47. explicit widen_fun(Traits const &tr)
  48. : traits_(tr)
  49. {}
  50. result_type operator()(char ch) const
  51. {
  52. return this->traits_.widen(ch);
  53. }
  54. Traits const &traits_;
  55. };
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // string_cast_
  58. //
  59. template<
  60. typename To
  61. , typename From
  62. , typename ToChar = typename detail::range_data<To>::type
  63. , typename FromChar = typename detail::range_data<From>::type
  64. >
  65. struct string_cast_
  66. {
  67. BOOST_MPL_ASSERT((is_same<FromChar, char>));
  68. typedef To const result_type;
  69. template<typename Traits>
  70. result_type operator()(From const &from, Traits const &tr) const
  71. {
  72. widen_fun<Traits> widen(tr);
  73. To to(
  74. boost::make_transform_iterator(detail::data_begin(from), widen)
  75. , boost::make_transform_iterator(detail::data_end(from), widen)
  76. );
  77. return to;
  78. }
  79. };
  80. template<typename To, typename From, typename Char>
  81. struct string_cast_<To, From, Char, Char>
  82. {
  83. typedef To const result_type;
  84. template<typename Traits>
  85. result_type operator()(From const &from, Traits const &) const
  86. {
  87. To to(detail::data_begin(from), detail::data_end(from));
  88. return to;
  89. }
  90. };
  91. template<typename From, typename Char>
  92. struct string_cast_<From, From, Char, Char>
  93. {
  94. typedef From const &result_type;
  95. template<typename Traits>
  96. result_type operator()(From const &from, Traits const &) const
  97. {
  98. return from;
  99. }
  100. };
  101. ///////////////////////////////////////////////////////////////////////////////
  102. // string_cast
  103. //
  104. template<typename To, typename From, typename Traits>
  105. typename string_cast_<To, From>::result_type
  106. string_cast(From const &from, Traits const &tr)
  107. {
  108. return string_cast_<To, From>()(from, tr);
  109. }
  110. ///////////////////////////////////////////////////////////////////////////////
  111. // translate
  112. //
  113. template<typename Char, typename Traits>
  114. inline Char translate(Char ch, Traits const &tr, mpl::false_) // case-sensitive
  115. {
  116. return tr.translate(ch);
  117. }
  118. template<typename Char, typename Traits>
  119. inline Char translate(Char ch, Traits const &tr, mpl::true_) // case-insensitive
  120. {
  121. return tr.translate_nocase(ch);
  122. }
  123. }}} // namespace boost::xpressive::detail
  124. #if defined(_MSC_VER)
  125. # pragma warning(pop)
  126. #endif
  127. #endif