utility.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #ifndef BOOST_MP11_UTILITY_HPP_INCLUDED
  2. #define BOOST_MP11_UTILITY_HPP_INCLUDED
  3. // Copyright 2015-2020 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_fold.hpp>
  12. #include <boost/mp11/detail/config.hpp>
  13. namespace boost
  14. {
  15. namespace mp11
  16. {
  17. // mp_identity
  18. template<class T> struct mp_identity
  19. {
  20. using type = T;
  21. };
  22. // mp_identity_t
  23. template<class T> using mp_identity_t = typename mp_identity<T>::type;
  24. // mp_inherit
  25. template<class... T> struct mp_inherit: T... {};
  26. // mp_if, mp_if_c
  27. namespace detail
  28. {
  29. template<bool C, class T, class... E> struct mp_if_c_impl
  30. {
  31. };
  32. template<class T, class... E> struct mp_if_c_impl<true, T, E...>
  33. {
  34. using type = T;
  35. };
  36. template<class T, class E> struct mp_if_c_impl<false, T, E>
  37. {
  38. using type = E;
  39. };
  40. } // namespace detail
  41. template<bool C, class T, class... E> using mp_if_c = typename detail::mp_if_c_impl<C, T, E...>::type;
  42. template<class C, class T, class... E> using mp_if = typename detail::mp_if_c_impl<static_cast<bool>(C::value), T, E...>::type;
  43. // mp_valid
  44. #if BOOST_MP11_WORKAROUND( BOOST_MP11_INTEL, != 0 ) // tested at 1800
  45. // contributed by Roland Schulz in https://github.com/boostorg/mp11/issues/17
  46. namespace detail
  47. {
  48. template<class...> using void_t = void;
  49. template<class, template<class...> class F, class... T>
  50. struct mp_valid_impl: mp_false {};
  51. template<template<class...> class F, class... T>
  52. struct mp_valid_impl<void_t<F<T...>>, F, T...>: mp_true {};
  53. } // namespace detail
  54. template<template<class...> class F, class... T> using mp_valid = typename detail::mp_valid_impl<void, F, T...>;
  55. #else
  56. // implementation by Bruno Dutra (by the name is_evaluable)
  57. namespace detail
  58. {
  59. template<template<class...> class F, class... T> struct mp_valid_impl
  60. {
  61. template<template<class...> class G, class = G<T...>> static mp_true check(int);
  62. template<template<class...> class> static mp_false check(...);
  63. using type = decltype(check<F>(0));
  64. };
  65. } // namespace detail
  66. template<template<class...> class F, class... T> using mp_valid = typename detail::mp_valid_impl<F, T...>::type;
  67. #endif
  68. template<class Q, class... T> using mp_valid_q = mp_valid<Q::template fn, T...>;
  69. // mp_defer
  70. namespace detail
  71. {
  72. template<template<class...> class F, class... T> struct mp_defer_impl
  73. {
  74. using type = F<T...>;
  75. };
  76. struct mp_no_type
  77. {
  78. };
  79. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  80. template<template<class...> class F, class... T> struct mp_defer_cuda_workaround
  81. {
  82. using type = mp_if<mp_valid<F, T...>, detail::mp_defer_impl<F, T...>, detail::mp_no_type>;
  83. };
  84. #endif
  85. } // namespace detail
  86. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  87. template<template<class...> class F, class... T> using mp_defer = typename detail::mp_defer_cuda_workaround< F, T...>::type;
  88. #else
  89. template<template<class...> class F, class... T> using mp_defer = mp_if<mp_valid<F, T...>, detail::mp_defer_impl<F, T...>, detail::mp_no_type>;
  90. #endif
  91. // mp_eval_if, mp_eval_if_c
  92. namespace detail
  93. {
  94. template<bool C, class T, template<class...> class F, class... U> struct mp_eval_if_c_impl;
  95. template<class T, template<class...> class F, class... U> struct mp_eval_if_c_impl<true, T, F, U...>
  96. {
  97. using type = T;
  98. };
  99. template<class T, template<class...> class F, class... U> struct mp_eval_if_c_impl<false, T, F, U...>: mp_defer<F, U...>
  100. {
  101. };
  102. } // namespace detail
  103. template<bool C, class T, template<class...> class F, class... U> using mp_eval_if_c = typename detail::mp_eval_if_c_impl<C, T, F, U...>::type;
  104. template<class C, class T, template<class...> class F, class... U> using mp_eval_if = typename detail::mp_eval_if_c_impl<static_cast<bool>(C::value), T, F, U...>::type;
  105. template<class C, class T, class Q, class... U> using mp_eval_if_q = typename detail::mp_eval_if_c_impl<static_cast<bool>(C::value), T, Q::template fn, U...>::type;
  106. // mp_eval_if_not
  107. template<class C, class T, template<class...> class F, class... U> using mp_eval_if_not = mp_eval_if<mp_not<C>, T, F, U...>;
  108. template<class C, class T, class Q, class... U> using mp_eval_if_not_q = mp_eval_if<mp_not<C>, T, Q::template fn, U...>;
  109. // mp_eval_or
  110. template<class T, template<class...> class F, class... U> using mp_eval_or = mp_eval_if_not<mp_valid<F, U...>, T, F, U...>;
  111. template<class T, class Q, class... U> using mp_eval_or_q = mp_eval_or<T, Q::template fn, U...>;
  112. // mp_cond
  113. // so elegant; so doesn't work
  114. // template<class C, class T, class... E> using mp_cond = mp_eval_if<C, T, mp_cond, E...>;
  115. namespace detail
  116. {
  117. template<class C, class T, class... E> struct mp_cond_impl;
  118. } // namespace detail
  119. template<class C, class T, class... E> using mp_cond = typename detail::mp_cond_impl<C, T, E...>::type;
  120. namespace detail
  121. {
  122. template<class C, class T, class... E> using mp_cond_ = mp_eval_if<C, T, mp_cond, E...>;
  123. template<class C, class T, class... E> struct mp_cond_impl: mp_defer<mp_cond_, C, T, E...>
  124. {
  125. };
  126. } // namespace detail
  127. // mp_quote
  128. template<template<class...> class F> struct mp_quote
  129. {
  130. // the indirection through mp_defer works around the language inability
  131. // to expand T... into a fixed parameter list of an alias template
  132. template<class... T> using fn = typename mp_defer<F, T...>::type;
  133. };
  134. // mp_quote_trait
  135. template<template<class...> class F> struct mp_quote_trait
  136. {
  137. template<class... T> using fn = typename F<T...>::type;
  138. };
  139. // mp_invoke_q
  140. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
  141. namespace detail
  142. {
  143. template<class Q, class... T> struct mp_invoke_q_impl: mp_defer<Q::template fn, T...> {};
  144. } // namespace detail
  145. template<class Q, class... T> using mp_invoke_q = typename detail::mp_invoke_q_impl<Q, T...>::type;
  146. #elif BOOST_MP11_WORKAROUND( BOOST_MP11_GCC, < 50000 )
  147. template<class Q, class... T> using mp_invoke_q = typename mp_defer<Q::template fn, T...>::type;
  148. #else
  149. template<class Q, class... T> using mp_invoke_q = typename Q::template fn<T...>;
  150. #endif
  151. // mp_not_fn<P>
  152. template<template<class...> class P> struct mp_not_fn
  153. {
  154. template<class... T> using fn = mp_not< mp_invoke_q<mp_quote<P>, T...> >;
  155. };
  156. template<class Q> using mp_not_fn_q = mp_not_fn<Q::template fn>;
  157. // mp_compose
  158. namespace detail
  159. {
  160. template<class T, class Q> using mp_reverse_invoke_q = mp_invoke_q<Q, T>;
  161. } // namespace detail
  162. #if !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
  163. template<template<class...> class... F> struct mp_compose
  164. {
  165. template<class T> using fn = mp_fold<mp_list<mp_quote<F>...>, T, detail::mp_reverse_invoke_q>;
  166. };
  167. #endif
  168. template<class... Q> struct mp_compose_q
  169. {
  170. template<class T> using fn = mp_fold<mp_list<Q...>, T, detail::mp_reverse_invoke_q>;
  171. };
  172. } // namespace mp11
  173. } // namespace boost
  174. #endif // #ifndef BOOST_MP11_UTILITY_HPP_INCLUDED