list.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #ifndef BOOST_MP11_LIST_HPP_INCLUDED
  2. #define BOOST_MP11_LIST_HPP_INCLUDED
  3. // Copyright 2015-2017 Peter Dimov.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #include <boost/mp11/integral.hpp>
  10. #include <boost/mp11/detail/mp_list.hpp>
  11. #include <boost/mp11/detail/mp_is_list.hpp>
  12. #include <boost/mp11/detail/mp_append.hpp>
  13. #include <boost/mp11/detail/mp_front.hpp>
  14. #include <boost/mp11/detail/mp_rename.hpp>
  15. #include <boost/mp11/detail/config.hpp>
  16. #include <type_traits>
  17. namespace boost
  18. {
  19. namespace mp11
  20. {
  21. // mp_list_c<T, I...>
  22. template<class T, T... I> using mp_list_c = mp_list<std::integral_constant<T, I>...>;
  23. // mp_is_list<L>
  24. // in detail/mp_is_list.hpp
  25. // mp_size<L>
  26. namespace detail
  27. {
  28. template<class L> struct mp_size_impl
  29. {
  30. // An error "no type named 'type'" here means that the argument to mp_size is not a list
  31. };
  32. template<template<class...> class L, class... T> struct mp_size_impl<L<T...>>
  33. {
  34. using type = mp_size_t<sizeof...(T)>;
  35. };
  36. } // namespace detail
  37. template<class L> using mp_size = typename detail::mp_size_impl<L>::type;
  38. // mp_empty<L>
  39. template<class L> using mp_empty = mp_bool< mp_size<L>::value == 0 >;
  40. // mp_assign<L1, L2>
  41. namespace detail
  42. {
  43. template<class L1, class L2> struct mp_assign_impl;
  44. template<template<class...> class L1, class... T, template<class...> class L2, class... U> struct mp_assign_impl<L1<T...>, L2<U...>>
  45. {
  46. using type = L1<U...>;
  47. };
  48. } // namespace detail
  49. template<class L1, class L2> using mp_assign = typename detail::mp_assign_impl<L1, L2>::type;
  50. // mp_clear<L>
  51. template<class L> using mp_clear = mp_assign<L, mp_list<>>;
  52. // mp_front<L>
  53. // in detail/mp_front.hpp
  54. // mp_pop_front<L>
  55. namespace detail
  56. {
  57. template<class L> struct mp_pop_front_impl
  58. {
  59. // An error "no type named 'type'" here means that the argument to mp_pop_front
  60. // is either not a list, or is an empty list
  61. };
  62. template<template<class...> class L, class T1, class... T> struct mp_pop_front_impl<L<T1, T...>>
  63. {
  64. using type = L<T...>;
  65. };
  66. } // namespace detail
  67. template<class L> using mp_pop_front = typename detail::mp_pop_front_impl<L>::type;
  68. // mp_first<L>
  69. template<class L> using mp_first = mp_front<L>;
  70. // mp_rest<L>
  71. template<class L> using mp_rest = mp_pop_front<L>;
  72. // mp_second<L>
  73. namespace detail
  74. {
  75. template<class L> struct mp_second_impl
  76. {
  77. // An error "no type named 'type'" here means that the argument to mp_second
  78. // is either not a list, or has fewer than two elements
  79. };
  80. template<template<class...> class L, class T1, class T2, class... T> struct mp_second_impl<L<T1, T2, T...>>
  81. {
  82. using type = T2;
  83. };
  84. } // namespace detail
  85. template<class L> using mp_second = typename detail::mp_second_impl<L>::type;
  86. // mp_third<L>
  87. namespace detail
  88. {
  89. template<class L> struct mp_third_impl
  90. {
  91. // An error "no type named 'type'" here means that the argument to mp_third
  92. // is either not a list, or has fewer than three elements
  93. };
  94. template<template<class...> class L, class T1, class T2, class T3, class... T> struct mp_third_impl<L<T1, T2, T3, T...>>
  95. {
  96. using type = T3;
  97. };
  98. } // namespace detail
  99. template<class L> using mp_third = typename detail::mp_third_impl<L>::type;
  100. // mp_push_front<L, T...>
  101. namespace detail
  102. {
  103. template<class L, class... T> struct mp_push_front_impl
  104. {
  105. // An error "no type named 'type'" here means that the first argument to mp_push_front is not a list
  106. };
  107. template<template<class...> class L, class... U, class... T> struct mp_push_front_impl<L<U...>, T...>
  108. {
  109. using type = L<T..., U...>;
  110. };
  111. } // namespace detail
  112. template<class L, class... T> using mp_push_front = typename detail::mp_push_front_impl<L, T...>::type;
  113. // mp_push_back<L, T...>
  114. namespace detail
  115. {
  116. template<class L, class... T> struct mp_push_back_impl
  117. {
  118. // An error "no type named 'type'" here means that the first argument to mp_push_back is not a list
  119. };
  120. template<template<class...> class L, class... U, class... T> struct mp_push_back_impl<L<U...>, T...>
  121. {
  122. using type = L<U..., T...>;
  123. };
  124. } // namespace detail
  125. template<class L, class... T> using mp_push_back = typename detail::mp_push_back_impl<L, T...>::type;
  126. // mp_rename<L, B>
  127. // mp_apply<F, L>
  128. // mp_apply_q<Q, L>
  129. // in detail/mp_rename.hpp
  130. // mp_replace_front<L, T>
  131. namespace detail
  132. {
  133. template<class L, class T> struct mp_replace_front_impl
  134. {
  135. // An error "no type named 'type'" here means that the first argument to mp_replace_front
  136. // is either not a list, or is an empty list
  137. };
  138. template<template<class...> class L, class U1, class... U, class T> struct mp_replace_front_impl<L<U1, U...>, T>
  139. {
  140. using type = L<T, U...>;
  141. };
  142. } // namespace detail
  143. template<class L, class T> using mp_replace_front = typename detail::mp_replace_front_impl<L, T>::type;
  144. // mp_replace_first<L, T>
  145. template<class L, class T> using mp_replace_first = typename detail::mp_replace_front_impl<L, T>::type;
  146. // mp_replace_second<L, T>
  147. namespace detail
  148. {
  149. template<class L, class T> struct mp_replace_second_impl
  150. {
  151. // An error "no type named 'type'" here means that the first argument to mp_replace_second
  152. // is either not a list, or has fewer than two elements
  153. };
  154. template<template<class...> class L, class U1, class U2, class... U, class T> struct mp_replace_second_impl<L<U1, U2, U...>, T>
  155. {
  156. using type = L<U1, T, U...>;
  157. };
  158. } // namespace detail
  159. template<class L, class T> using mp_replace_second = typename detail::mp_replace_second_impl<L, T>::type;
  160. // mp_replace_third<L, T>
  161. namespace detail
  162. {
  163. template<class L, class T> struct mp_replace_third_impl
  164. {
  165. // An error "no type named 'type'" here means that the first argument to mp_replace_third
  166. // is either not a list, or has fewer than three elements
  167. };
  168. template<template<class...> class L, class U1, class U2, class U3, class... U, class T> struct mp_replace_third_impl<L<U1, U2, U3, U...>, T>
  169. {
  170. using type = L<U1, U2, T, U...>;
  171. };
  172. } // namespace detail
  173. template<class L, class T> using mp_replace_third = typename detail::mp_replace_third_impl<L, T>::type;
  174. // mp_transform_front<L, F>
  175. namespace detail
  176. {
  177. template<class L, template<class...> class F> struct mp_transform_front_impl
  178. {
  179. // An error "no type named 'type'" here means that the first argument to mp_transform_front
  180. // is either not a list, or is an empty list
  181. };
  182. template<template<class...> class L, class U1, class... U, template<class...> class F> struct mp_transform_front_impl<L<U1, U...>, F>
  183. {
  184. using type = L<F<U1>, U...>;
  185. };
  186. } // namespace detail
  187. template<class L, template<class...> class F> using mp_transform_front = typename detail::mp_transform_front_impl<L, F>::type;
  188. template<class L, class Q> using mp_transform_front_q = mp_transform_front<L, Q::template fn>;
  189. // mp_transform_first<L, F>
  190. template<class L, template<class...> class F> using mp_transform_first = typename detail::mp_transform_front_impl<L, F>::type;
  191. template<class L, class Q> using mp_transform_first_q = mp_transform_first<L, Q::template fn>;
  192. // mp_transform_second<L, F>
  193. namespace detail
  194. {
  195. template<class L, template<class...> class F> struct mp_transform_second_impl
  196. {
  197. // An error "no type named 'type'" here means that the first argument to mp_transform_second
  198. // is either not a list, or has fewer than two elements
  199. };
  200. template<template<class...> class L, class U1, class U2, class... U, template<class...> class F> struct mp_transform_second_impl<L<U1, U2, U...>, F>
  201. {
  202. using type = L<U1, F<U2>, U...>;
  203. };
  204. } // namespace detail
  205. template<class L, template<class...> class F> using mp_transform_second = typename detail::mp_transform_second_impl<L, F>::type;
  206. template<class L, class Q> using mp_transform_second_q = mp_transform_second<L, Q::template fn>;
  207. // mp_transform_third<L, F>
  208. namespace detail
  209. {
  210. template<class L, template<class...> class F> struct mp_transform_third_impl
  211. {
  212. // An error "no type named 'type'" here means that the first argument to mp_transform_third
  213. // is either not a list, or has fewer than three elements
  214. };
  215. template<template<class...> class L, class U1, class U2, class U3, class... U, template<class...> class F> struct mp_transform_third_impl<L<U1, U2, U3, U...>, F>
  216. {
  217. using type = L<U1, U2, F<U3>, U...>;
  218. };
  219. } // namespace detail
  220. template<class L, template<class...> class F> using mp_transform_third = typename detail::mp_transform_third_impl<L, F>::type;
  221. template<class L, class Q> using mp_transform_third_q = mp_transform_third<L, Q::template fn>;
  222. } // namespace mp11
  223. } // namespace boost
  224. #endif // #ifndef BOOST_MP11_LIST_HPP_INCLUDED