first_of.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*=============================================================================
  2. Copyright (c) 2012 Paul Fultz II
  3. first_of.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_FUNCTION_CONDITIONAL_H
  8. #define BOOST_HOF_GUARD_FUNCTION_CONDITIONAL_H
  9. /// first_of
  10. /// ========
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `first_of` function adaptor combines several functions together. If
  16. /// the first function can not be called, then it will try to call the next
  17. /// function. This can be very useful when overloading functions using
  18. /// template constraints(such as with `enable_if`).
  19. ///
  20. /// Note: This is different than the [`match`](match.md) function adaptor, which
  21. /// can lead to ambiguities. Instead, `first_of` will call the first function
  22. /// that is callable, regardless if there is another function that could be
  23. /// called as well.
  24. ///
  25. /// Synopsis
  26. /// --------
  27. ///
  28. /// template<class... Fs>
  29. /// constexpr first_of_adaptor<Fs...> first_of(Fs... fs);
  30. ///
  31. /// Requirements
  32. /// ------------
  33. ///
  34. /// Fs must be:
  35. ///
  36. /// * [ConstInvocable](ConstInvocable)
  37. /// * MoveConstructible
  38. ///
  39. /// Example
  40. /// -------
  41. ///
  42. /// #include <boost/hof.hpp>
  43. /// #include <iostream>
  44. /// using namespace boost::hof;
  45. ///
  46. /// struct for_ints
  47. /// {
  48. /// void operator()(int) const
  49. /// {
  50. /// printf("Int\n");
  51. /// }
  52. /// };
  53. ///
  54. /// struct for_floats
  55. /// {
  56. /// void operator()(float) const
  57. /// {
  58. /// printf("Float\n");
  59. /// }
  60. /// };
  61. ///
  62. /// int main() {
  63. /// first_of(for_ints(), for_floats())(3.0);
  64. /// }
  65. ///
  66. /// This will print `Int` because the `for_floats` function object won't ever be
  67. /// called. Due to the conversion rules in C++, the `for_ints` function can be
  68. /// called on floats, so it is chosen by `first_of` first, even though
  69. /// `for_floats` is a better match.
  70. ///
  71. /// So, the order of the functions in the `first_of_adaptor` are very important
  72. /// to how the function is chosen.
  73. ///
  74. /// References
  75. /// ----------
  76. ///
  77. /// * [POO51](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0051r2.pdf) - Proposal for C++
  78. /// Proposal for C++ generic overload function
  79. /// * [Conditional overloading](<Conditional overloading>)
  80. ///
  81. #include <boost/hof/reveal.hpp>
  82. #include <boost/hof/detail/compressed_pair.hpp>
  83. #include <boost/hof/detail/callable_base.hpp>
  84. #include <boost/hof/detail/delegate.hpp>
  85. #include <boost/hof/detail/join.hpp>
  86. #include <boost/hof/detail/seq.hpp>
  87. #include <boost/hof/detail/make.hpp>
  88. #include <boost/hof/detail/static_const_var.hpp>
  89. namespace boost { namespace hof {
  90. namespace detail {
  91. template<class F1, class F2>
  92. struct basic_first_of_adaptor : F1, F2
  93. {
  94. BOOST_HOF_INHERIT_DEFAULT(basic_first_of_adaptor, F1, F2)
  95. template<class A, class B,
  96. BOOST_HOF_ENABLE_IF_CONVERTIBLE(A, F1),
  97. BOOST_HOF_ENABLE_IF_CONVERTIBLE(B, F2)>
  98. constexpr basic_first_of_adaptor(A&& f1, B&& f2)
  99. noexcept(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F1, A&&) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F2, B&&))
  100. : F1(BOOST_HOF_FORWARD(A)(f1)), F2(BOOST_HOF_FORWARD(B)(f2))
  101. {}
  102. template<class X,
  103. class=typename std::enable_if<
  104. BOOST_HOF_IS_CONVERTIBLE(X, F1) &&
  105. BOOST_HOF_IS_DEFAULT_CONSTRUCTIBLE(F2)
  106. >::type>
  107. constexpr basic_first_of_adaptor(X&& x)
  108. BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F1, X&&)
  109. : F1(BOOST_HOF_FORWARD(X)(x))
  110. {}
  111. template<class... Ts>
  112. struct select
  113. : std::conditional
  114. <
  115. is_invocable<F1, Ts...>::value,
  116. F1,
  117. F2
  118. >
  119. {};
  120. BOOST_HOF_RETURNS_CLASS(basic_first_of_adaptor);
  121. template<class... Ts, class F=typename select<Ts...>::type>
  122. constexpr BOOST_HOF_SFINAE_RESULT(typename select<Ts...>::type, id_<Ts>...)
  123. operator()(Ts && ... xs) const
  124. BOOST_HOF_SFINAE_RETURNS
  125. (
  126. BOOST_HOF_RETURNS_STATIC_CAST(const F&)(*BOOST_HOF_CONST_THIS)(BOOST_HOF_FORWARD(Ts)(xs)...)
  127. );
  128. };
  129. template <class F1, class F2>
  130. constexpr const F1& which(std::true_type, const F1& f1, const F2&) noexcept
  131. {
  132. return f1;
  133. }
  134. template <class F1, class F2>
  135. constexpr const F2& which(std::false_type, const F1&, const F2& f2) noexcept
  136. {
  137. return f2;
  138. }
  139. template<class F1, class F2>
  140. struct conditional_kernel : compressed_pair<F1, F2>
  141. {
  142. typedef compressed_pair<F1, F2> base;
  143. BOOST_HOF_INHERIT_CONSTRUCTOR(conditional_kernel, base)
  144. template<class... Ts>
  145. struct select
  146. : std::conditional
  147. <
  148. is_invocable<F1, Ts...>::value,
  149. F1,
  150. F2
  151. >
  152. {};
  153. BOOST_HOF_RETURNS_CLASS(conditional_kernel);
  154. template<class... Ts, class PickFirst=is_invocable<F1, Ts...>>
  155. constexpr BOOST_HOF_SFINAE_RESULT(typename select<Ts...>::type, id_<Ts>...)
  156. operator()(Ts && ... xs) const
  157. BOOST_HOF_SFINAE_RETURNS
  158. (
  159. boost::hof::detail::which(
  160. BOOST_HOF_RETURNS_CONSTRUCT(PickFirst)(),
  161. BOOST_HOF_MANGLE_CAST(const F1&)(BOOST_HOF_CONST_THIS->first(xs...)),
  162. BOOST_HOF_MANGLE_CAST(const F2&)(BOOST_HOF_CONST_THIS->second(xs...))
  163. )
  164. (BOOST_HOF_FORWARD(Ts)(xs)...)
  165. );
  166. };
  167. }
  168. template<class F, class... Fs>
  169. struct first_of_adaptor
  170. : detail::conditional_kernel<F, BOOST_HOF_JOIN(first_of_adaptor, Fs...) >
  171. {
  172. typedef first_of_adaptor fit_rewritable_tag;
  173. typedef BOOST_HOF_JOIN(first_of_adaptor, Fs...) kernel_base;
  174. typedef detail::conditional_kernel<F, kernel_base > base;
  175. BOOST_HOF_INHERIT_DEFAULT(first_of_adaptor, base)
  176. template<class X, class... Xs,
  177. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(base, X, kernel_base),
  178. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(kernel_base, Xs...)>
  179. constexpr first_of_adaptor(X&& f1, Xs&& ... fs)
  180. noexcept(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(base, X&&, kernel_base) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(kernel_base, Xs&&...))
  181. : base(BOOST_HOF_FORWARD(X)(f1), kernel_base(BOOST_HOF_FORWARD(Xs)(fs)...))
  182. {}
  183. template<class X, class... Xs,
  184. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(base, X)>
  185. constexpr first_of_adaptor(X&& f1)
  186. BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(base, X&&)
  187. : base(BOOST_HOF_FORWARD(X)(f1))
  188. {}
  189. struct failure
  190. : failure_for<F, Fs...>
  191. {};
  192. };
  193. template<class F>
  194. struct first_of_adaptor<F> : F
  195. {
  196. typedef first_of_adaptor fit_rewritable_tag;
  197. BOOST_HOF_INHERIT_CONSTRUCTOR(first_of_adaptor, F);
  198. struct failure
  199. : failure_for<F>
  200. {};
  201. };
  202. template<class F1, class F2>
  203. struct first_of_adaptor<F1, F2>
  204. : detail::conditional_kernel<F1, F2>
  205. {
  206. typedef detail::conditional_kernel<F1, F2> base;
  207. typedef first_of_adaptor fit_rewritable_tag;
  208. BOOST_HOF_INHERIT_CONSTRUCTOR(first_of_adaptor, base);
  209. struct failure
  210. : failure_for<F1, F2>
  211. {};
  212. };
  213. BOOST_HOF_DECLARE_STATIC_VAR(first_of, detail::make<first_of_adaptor>);
  214. }} // namespace boost::hof
  215. #endif