utility.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/mp_front.hpp>
  13. #include <boost/mp11/detail/mp_rename.hpp>
  14. #include <boost/mp11/detail/mp_defer.hpp>
  15. #include <boost/mp11/detail/config.hpp>
  16. namespace boost
  17. {
  18. namespace mp11
  19. {
  20. // mp_identity
  21. template<class T> struct mp_identity
  22. {
  23. using type = T;
  24. };
  25. // mp_identity_t
  26. template<class T> using mp_identity_t = typename mp_identity<T>::type;
  27. // mp_inherit
  28. template<class... T> struct mp_inherit: T... {};
  29. // mp_if, mp_if_c
  30. // mp_valid
  31. // mp_defer
  32. // moved to detail/mp_defer.hpp
  33. // mp_eval_if, mp_eval_if_c
  34. namespace detail
  35. {
  36. template<bool C, class T, template<class...> class F, class... U> struct mp_eval_if_c_impl;
  37. template<class T, template<class...> class F, class... U> struct mp_eval_if_c_impl<true, T, F, U...>
  38. {
  39. using type = T;
  40. };
  41. template<class T, template<class...> class F, class... U> struct mp_eval_if_c_impl<false, T, F, U...>: mp_defer<F, U...>
  42. {
  43. };
  44. } // namespace detail
  45. 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;
  46. 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;
  47. 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;
  48. // mp_eval_if_not
  49. 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...>;
  50. 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...>;
  51. // mp_eval_or
  52. template<class T, template<class...> class F, class... U> using mp_eval_or = mp_eval_if_not<mp_valid<F, U...>, T, F, U...>;
  53. template<class T, class Q, class... U> using mp_eval_or_q = mp_eval_or<T, Q::template fn, U...>;
  54. // mp_valid_and_true
  55. template<template<class...> class F, class... T> using mp_valid_and_true = mp_eval_or<mp_false, F, T...>;
  56. template<class Q, class... T> using mp_valid_and_true_q = mp_valid_and_true<Q::template fn, T...>;
  57. // mp_cond
  58. // so elegant; so doesn't work
  59. // template<class C, class T, class... E> using mp_cond = mp_eval_if<C, T, mp_cond, E...>;
  60. namespace detail
  61. {
  62. template<class C, class T, class... E> struct mp_cond_impl;
  63. } // namespace detail
  64. template<class C, class T, class... E> using mp_cond = typename detail::mp_cond_impl<C, T, E...>::type;
  65. namespace detail
  66. {
  67. template<class C, class T, class... E> using mp_cond_ = mp_eval_if<C, T, mp_cond, E...>;
  68. template<class C, class T, class... E> struct mp_cond_impl: mp_defer<mp_cond_, C, T, E...>
  69. {
  70. };
  71. } // namespace detail
  72. // mp_quote
  73. template<template<class...> class F> struct mp_quote
  74. {
  75. // the indirection through mp_defer works around the language inability
  76. // to expand T... into a fixed parameter list of an alias template
  77. template<class... T> using fn = typename mp_defer<F, T...>::type;
  78. };
  79. // mp_quote_trait
  80. template<template<class...> class F> struct mp_quote_trait
  81. {
  82. template<class... T> using fn = typename F<T...>::type;
  83. };
  84. // mp_invoke_q
  85. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
  86. namespace detail
  87. {
  88. template<class Q, class... T> struct mp_invoke_q_impl: mp_defer<Q::template fn, T...> {};
  89. } // namespace detail
  90. template<class Q, class... T> using mp_invoke_q = typename detail::mp_invoke_q_impl<Q, T...>::type;
  91. #elif BOOST_MP11_WORKAROUND( BOOST_MP11_GCC, < 50000 )
  92. template<class Q, class... T> using mp_invoke_q = typename mp_defer<Q::template fn, T...>::type;
  93. #else
  94. template<class Q, class... T> using mp_invoke_q = typename Q::template fn<T...>;
  95. #endif
  96. // mp_not_fn<P>
  97. template<template<class...> class P> struct mp_not_fn
  98. {
  99. template<class... T> using fn = mp_not< mp_invoke_q<mp_quote<P>, T...> >;
  100. };
  101. template<class Q> using mp_not_fn_q = mp_not_fn<Q::template fn>;
  102. // mp_compose
  103. namespace detail
  104. {
  105. template<class L, class Q> using mp_compose_helper = mp_list< mp_apply_q<Q, L> >;
  106. } // namespace detail
  107. #if !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
  108. template<template<class...> class... F> struct mp_compose
  109. {
  110. template<class... T> using fn = mp_front< mp_fold<mp_list<mp_quote<F>...>, mp_list<T...>, detail::mp_compose_helper> >;
  111. };
  112. #endif
  113. template<class... Q> struct mp_compose_q
  114. {
  115. template<class... T> using fn = mp_front< mp_fold<mp_list<Q...>, mp_list<T...>, detail::mp_compose_helper> >;
  116. };
  117. } // namespace mp11
  118. } // namespace boost
  119. #endif // #ifndef BOOST_MP11_UTILITY_HPP_INCLUDED