string.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef BOOST_METAPARSE_V1_CPP11_STRING_HPP
  2. #define BOOST_METAPARSE_V1_CPP11_STRING_HPP
  3. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <boost/metaparse/v1/cpp11/fwd/string.hpp>
  8. #include <boost/metaparse/v1/string_tag.hpp>
  9. #include <boost/metaparse/v1/impl/string_iterator.hpp>
  10. #include <boost/metaparse/v1/cpp11/impl/empty_string.hpp>
  11. #include <boost/metaparse/v1/cpp11/impl/size.hpp>
  12. #include <boost/metaparse/v1/cpp11/impl/pop_front.hpp>
  13. #include <boost/metaparse/v1/cpp11/impl/push_front_c.hpp>
  14. #include <boost/metaparse/v1/cpp11/impl/push_back_c.hpp>
  15. #include <boost/metaparse/v1/cpp11/impl/pop_back.hpp>
  16. #include <boost/metaparse/v1/cpp11/impl/string_at.hpp>
  17. #include <type_traits>
  18. /*
  19. * The string type
  20. */
  21. namespace boost
  22. {
  23. namespace metaparse
  24. {
  25. namespace v1
  26. {
  27. template <char... Cs>
  28. struct string
  29. {
  30. typedef string type;
  31. typedef string_tag tag;
  32. };
  33. }
  34. }
  35. }
  36. /*
  37. * Boost.MPL overloads
  38. */
  39. namespace boost
  40. {
  41. namespace mpl
  42. {
  43. // push_back
  44. template <class S>
  45. struct push_back_impl;
  46. template <>
  47. struct push_back_impl<boost::metaparse::v1::string_tag>
  48. {
  49. typedef push_back_impl type;
  50. template <class S, class C>
  51. struct apply :
  52. boost::metaparse::v1::impl::push_back_c<
  53. typename S::type,
  54. C::type::value
  55. >
  56. {};
  57. };
  58. // pop_back
  59. template <class S>
  60. struct pop_back_impl;
  61. template <>
  62. struct pop_back_impl<boost::metaparse::v1::string_tag>
  63. {
  64. typedef pop_back_impl type;
  65. template <class S>
  66. struct apply : boost::metaparse::v1::impl::pop_back<S> {};
  67. };
  68. // push_front
  69. template <class S>
  70. struct push_front_impl;
  71. template <>
  72. struct push_front_impl<boost::metaparse::v1::string_tag>
  73. {
  74. typedef push_front_impl type;
  75. template <class S, class C>
  76. struct apply :
  77. boost::metaparse::v1::impl::push_front_c<
  78. typename S::type,
  79. C::type::value
  80. >
  81. {};
  82. };
  83. // pop_front
  84. template <class S>
  85. struct pop_front_impl;
  86. template <>
  87. struct pop_front_impl<boost::metaparse::v1::string_tag>
  88. {
  89. typedef pop_front_impl type;
  90. template <class S>
  91. struct apply : boost::metaparse::v1::impl::pop_front<S> {};
  92. };
  93. // clear
  94. template <class S>
  95. struct clear_impl;
  96. template <>
  97. struct clear_impl<boost::metaparse::v1::string_tag>
  98. {
  99. typedef clear_impl type;
  100. template <class S>
  101. struct apply : boost::metaparse::v1::string<> {};
  102. };
  103. // begin
  104. template <class S>
  105. struct begin_impl;
  106. template <>
  107. struct begin_impl<boost::metaparse::v1::string_tag>
  108. {
  109. typedef begin_impl type;
  110. template <class S>
  111. struct apply :
  112. boost::metaparse::v1::impl::string_iterator<typename S::type, 0>
  113. {};
  114. };
  115. // end
  116. template <class S>
  117. struct end_impl;
  118. template <>
  119. struct end_impl<boost::metaparse::v1::string_tag>
  120. {
  121. typedef end_impl type;
  122. template <class S>
  123. struct apply :
  124. boost::metaparse::v1::impl::string_iterator<
  125. typename S::type,
  126. boost::metaparse::v1::impl::size<typename S::type>::type::value
  127. >
  128. {};
  129. };
  130. // equal_to
  131. template <class A, class B>
  132. struct equal_to_impl;
  133. template <>
  134. struct equal_to_impl<
  135. boost::metaparse::v1::string_tag,
  136. boost::metaparse::v1::string_tag
  137. >
  138. {
  139. typedef equal_to_impl type;
  140. template <class A, class B>
  141. struct apply : std::is_same<typename A::type, typename B::type> {};
  142. };
  143. template <class T>
  144. struct equal_to_impl<boost::metaparse::v1::string_tag, T>
  145. {
  146. typedef equal_to_impl type;
  147. template <class, class>
  148. struct apply : false_ {};
  149. };
  150. template <class T>
  151. struct equal_to_impl<T, boost::metaparse::v1::string_tag> :
  152. equal_to_impl<boost::metaparse::v1::string_tag, T>
  153. {};
  154. // c_str
  155. template <class S>
  156. struct c_str;
  157. template <char... Cs>
  158. struct c_str<boost::metaparse::v1::string<Cs...>>
  159. {
  160. typedef c_str type;
  161. static constexpr char value[sizeof...(Cs) + 1] = {Cs..., 0};
  162. };
  163. template <>
  164. struct c_str<boost::metaparse::v1::string<>> :
  165. boost::metaparse::v1::impl::empty_string<>
  166. {};
  167. template <char... Cs>
  168. constexpr char c_str<boost::metaparse::v1::string<Cs...>>::value[];
  169. }
  170. }
  171. #if __clang__
  172. # if __has_extension(cxx_string_literal_templates)
  173. # define BOOST_METAPARSE_V1_STRING(...) ::boost::metaparse::string<__VA_ARGS__>
  174. # else
  175. # include <boost/metaparse/v1/cpp11/impl/string.hpp>
  176. # endif
  177. #else
  178. # include <boost/metaparse/v1/cpp11/impl/string.hpp>
  179. #endif
  180. #endif