mp11.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #ifndef BOOST_LEAF_DETAIL_MP11_HPP_INCLUDED
  2. #define BOOST_LEAF_DETAIL_MP11_HPP_INCLUDED
  3. // Copyright 2015-2017 Peter Dimov.
  4. // Copyright 2019 Emil Dotchevski.
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. //
  8. // See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt
  10. #include <type_traits>
  11. #include <cstddef>
  12. namespace boost { namespace leaf { namespace leaf_detail_mp11 {
  13. // mp_list<T...>
  14. template<class... T> struct mp_list
  15. {
  16. };
  17. // mp_identity
  18. template<class T> struct mp_identity
  19. {
  20. using type = T;
  21. };
  22. // mp_inherit
  23. template<class... T> struct mp_inherit: T... {};
  24. // mp_if, mp_if_c
  25. namespace detail
  26. {
  27. template<bool C, class T, class... E> struct mp_if_c_impl
  28. {
  29. };
  30. template<class T, class... E> struct mp_if_c_impl<true, T, E...>
  31. {
  32. using type = T;
  33. };
  34. template<class T, class E> struct mp_if_c_impl<false, T, E>
  35. {
  36. using type = E;
  37. };
  38. } // namespace detail
  39. template<bool C, class T, class... E> using mp_if_c = typename detail::mp_if_c_impl<C, T, E...>::type;
  40. template<class C, class T, class... E> using mp_if = typename detail::mp_if_c_impl<static_cast<bool>(C::value), T, E...>::type;
  41. // mp_bool
  42. template<bool B> using mp_bool = std::integral_constant<bool, B>;
  43. using mp_true = mp_bool<true>;
  44. using mp_false = mp_bool<false>;
  45. // mp_to_bool
  46. template<class T> using mp_to_bool = mp_bool<static_cast<bool>( T::value )>;
  47. // mp_not<T>
  48. template<class T> using mp_not = mp_bool< !T::value >;
  49. // mp_int
  50. template<int I> using mp_int = std::integral_constant<int, I>;
  51. // mp_size_t
  52. template<std::size_t N> using mp_size_t = std::integral_constant<std::size_t, N>;
  53. // mp_set_contains<S, V>
  54. namespace detail
  55. {
  56. template<class S, class V> struct mp_set_contains_impl;
  57. template<template<class...> class L, class... T, class V> struct mp_set_contains_impl<L<T...>, V>
  58. {
  59. using type = mp_to_bool<std::is_base_of<mp_identity<V>, mp_inherit<mp_identity<T>...> > >;
  60. };
  61. } // namespace detail
  62. template<class S, class V> using mp_set_contains = typename detail::mp_set_contains_impl<S, V>::type;
  63. // mp_set_push_back<S, T...>
  64. namespace detail
  65. {
  66. template<class S, class... T> struct mp_set_push_back_impl;
  67. template<template<class...> class L, class... U> struct mp_set_push_back_impl<L<U...>>
  68. {
  69. using type = L<U...>;
  70. };
  71. template<template<class...> class L, class... U, class T1, class... T> struct mp_set_push_back_impl<L<U...>, T1, T...>
  72. {
  73. using S = mp_if<mp_set_contains<L<U...>, T1>, L<U...>, L<U..., T1>>;
  74. using type = typename mp_set_push_back_impl<S, T...>::type;
  75. };
  76. } // namespace detail
  77. template<class S, class... T> using mp_set_push_back = typename detail::mp_set_push_back_impl<S, T...>::type;
  78. // mp_unique<L>
  79. namespace detail
  80. {
  81. template<class L> struct mp_unique_impl;
  82. template<template<class...> class L, class... T> struct mp_unique_impl<L<T...>>
  83. {
  84. using type = mp_set_push_back<L<>, T...>;
  85. };
  86. } // namespace detail
  87. template<class L> using mp_unique = typename detail::mp_unique_impl<L>::type;
  88. // mp_append<L...>
  89. namespace detail
  90. {
  91. template<class... L> struct mp_append_impl;
  92. template<> struct mp_append_impl<>
  93. {
  94. using type = mp_list<>;
  95. };
  96. template<template<class...> class L, class... T> struct mp_append_impl<L<T...>>
  97. {
  98. using type = L<T...>;
  99. };
  100. template<template<class...> class L1, class... T1, template<class...> class L2, class... T2, class... Lr> struct mp_append_impl<L1<T1...>, L2<T2...>, Lr...>
  101. {
  102. using type = typename mp_append_impl<L1<T1..., T2...>, Lr...>::type;
  103. };
  104. }
  105. template<class... L> using mp_append = typename detail::mp_append_impl<L...>::type;
  106. // mp_front<L>
  107. namespace detail
  108. {
  109. template<class L> struct mp_front_impl
  110. {
  111. // An error "no type named 'type'" here means that the argument to mp_front
  112. // is either not a list, or is an empty list
  113. };
  114. template<template<class...> class L, class T1, class... T> struct mp_front_impl<L<T1, T...>>
  115. {
  116. using type = T1;
  117. };
  118. } // namespace detail
  119. template<class L> using mp_front = typename detail::mp_front_impl<L>::type;
  120. // mp_pop_front<L>
  121. namespace detail
  122. {
  123. template<class L> struct mp_pop_front_impl
  124. {
  125. // An error "no type named 'type'" here means that the argument to mp_pop_front
  126. // is either not a list, or is an empty list
  127. };
  128. template<template<class...> class L, class T1, class... T> struct mp_pop_front_impl<L<T1, T...>>
  129. {
  130. using type = L<T...>;
  131. };
  132. } // namespace detail
  133. template<class L> using mp_pop_front = typename detail::mp_pop_front_impl<L>::type;
  134. // mp_first<L>
  135. template<class L> using mp_first = mp_front<L>;
  136. // mp_rest<L>
  137. template<class L> using mp_rest = mp_pop_front<L>;
  138. // mp_remove_if<L, P>
  139. namespace detail
  140. {
  141. template<class L, template<class...> class P> struct mp_remove_if_impl;
  142. template<template<class...> class L, class... T, template<class...> class P> struct mp_remove_if_impl<L<T...>, P>
  143. {
  144. template<class U> using _f = mp_if<P<U>, mp_list<>, mp_list<U>>;
  145. using type = mp_append<L<>, _f<T>...>;
  146. };
  147. } // namespace detail
  148. template<class L, template<class...> class P> using mp_remove_if = typename detail::mp_remove_if_impl<L, P>::type;
  149. // integer_sequence
  150. template<class T, T... I> struct integer_sequence
  151. {
  152. };
  153. // detail::make_integer_sequence_impl
  154. namespace detail
  155. {
  156. // iseq_if_c
  157. template<bool C, class T, class E> struct iseq_if_c_impl;
  158. template<class T, class E> struct iseq_if_c_impl<true, T, E>
  159. {
  160. using type = T;
  161. };
  162. template<class T, class E> struct iseq_if_c_impl<false, T, E>
  163. {
  164. using type = E;
  165. };
  166. template<bool C, class T, class E> using iseq_if_c = typename iseq_if_c_impl<C, T, E>::type;
  167. // iseq_identity
  168. template<class T> struct iseq_identity
  169. {
  170. using type = T;
  171. };
  172. template<class S1, class S2> struct append_integer_sequence;
  173. template<class T, T... I, T... J> struct append_integer_sequence<integer_sequence<T, I...>, integer_sequence<T, J...>>
  174. {
  175. using type = integer_sequence< T, I..., ( J + sizeof...(I) )... >;
  176. };
  177. template<class T, T N> struct make_integer_sequence_impl;
  178. template<class T, T N> struct make_integer_sequence_impl_
  179. {
  180. private:
  181. static_assert( N >= 0, "make_integer_sequence<T, N>: N must not be negative" );
  182. static T const M = N / 2;
  183. static T const R = N % 2;
  184. using S1 = typename make_integer_sequence_impl<T, M>::type;
  185. using S2 = typename append_integer_sequence<S1, S1>::type;
  186. using S3 = typename make_integer_sequence_impl<T, R>::type;
  187. using S4 = typename append_integer_sequence<S2, S3>::type;
  188. public:
  189. using type = S4;
  190. };
  191. template<class T, T N> struct make_integer_sequence_impl: iseq_if_c<N == 0, iseq_identity<integer_sequence<T>>, iseq_if_c<N == 1, iseq_identity<integer_sequence<T, 0>>, make_integer_sequence_impl_<T, N> > >
  192. {
  193. };
  194. } // namespace detail
  195. // make_integer_sequence
  196. template<class T, T N> using make_integer_sequence = typename detail::make_integer_sequence_impl<T, N>::type;
  197. // index_sequence
  198. template<std::size_t... I> using index_sequence = integer_sequence<std::size_t, I...>;
  199. // make_index_sequence
  200. template<std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>;
  201. // index_sequence_for
  202. template<class... T> using index_sequence_for = make_integer_sequence<std::size_t, sizeof...(T)>;
  203. // implementation by Bruno Dutra (by the name is_evaluable)
  204. namespace detail
  205. {
  206. template<template<class...> class F, class... T> struct mp_valid_impl
  207. {
  208. template<template<class...> class G, class = G<T...>> static mp_true check(int);
  209. template<template<class...> class> static mp_false check(...);
  210. using type = decltype(check<F>(0));
  211. };
  212. } // namespace detail
  213. template<template<class...> class F, class... T> using mp_valid = typename detail::mp_valid_impl<F, T...>::type;
  214. } } }
  215. #endif