| 1 | /*============================================================================= |
| 2 | Copyright (c) 2012 Paul Fultz II |
| 3 | match.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 | |
| 8 | #ifndef BOOST_HOF_GUARD_FUNCTION_OVERLOAD_H |
| 9 | #define BOOST_HOF_GUARD_FUNCTION_OVERLOAD_H |
| 10 | |
| 11 | /// match |
| 12 | /// ===== |
| 13 | /// |
| 14 | /// Description |
| 15 | /// ----------- |
| 16 | /// |
| 17 | /// The `match` function adaptor combines several functions together and |
| 18 | /// resolves which one should be called by using C++ overload resolution. This |
| 19 | /// is different than the [`first_of`](/include/boost/hof/conditional) adaptor which resolves |
| 20 | /// them based on order. |
| 21 | /// |
| 22 | /// Synopsis |
| 23 | /// -------- |
| 24 | /// |
| 25 | /// template<class... Fs> |
| 26 | /// constexpr match_adaptor<Fs...> match(Fs...fs); |
| 27 | /// |
| 28 | /// Requirements |
| 29 | /// ------------ |
| 30 | /// |
| 31 | /// Fs must be: |
| 32 | /// |
| 33 | /// * [ConstInvocable](ConstInvocable) |
| 34 | /// * MoveConstructible |
| 35 | /// |
| 36 | /// Example |
| 37 | /// ------- |
| 38 | /// |
| 39 | /// #include <boost/hof.hpp> |
| 40 | /// using namespace boost::hof; |
| 41 | /// |
| 42 | /// struct int_class |
| 43 | /// { |
| 44 | /// int operator()(int) const |
| 45 | /// { |
| 46 | /// return 1; |
| 47 | /// } |
| 48 | /// }; |
| 49 | /// |
| 50 | /// struct foo |
| 51 | /// {}; |
| 52 | /// |
| 53 | /// struct foo_class |
| 54 | /// { |
| 55 | /// foo operator()(foo) const |
| 56 | /// { |
| 57 | /// return foo(); |
| 58 | /// } |
| 59 | /// }; |
| 60 | /// |
| 61 | /// typedef match_adaptor<int_class, foo_class> fun; |
| 62 | /// |
| 63 | /// static_assert(std::is_same<int, decltype(fun()(1))>::value, "Failed match"); |
| 64 | /// static_assert(std::is_same<foo, decltype(fun()(foo()))>::value, "Failed match"); |
| 65 | /// |
| 66 | /// int main() {} |
| 67 | /// |
| 68 | /// References |
| 69 | /// ---------- |
| 70 | /// |
| 71 | /// * [POO51](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0051r2.pdf) - Proposal for C++ |
| 72 | /// Proposal for C++ generic overload function |
| 73 | /// |
| 74 | |
| 75 | #include <boost/hof/reveal.hpp> |
| 76 | #include <boost/hof/detail/callable_base.hpp> |
| 77 | #include <boost/hof/detail/delegate.hpp> |
| 78 | #include <boost/hof/detail/move.hpp> |
| 79 | #include <boost/hof/detail/make.hpp> |
| 80 | #include <boost/hof/detail/static_const_var.hpp> |
| 81 | |
| 82 | namespace boost { namespace hof { |
| 83 | |
| 84 | template<class...Fs> struct match_adaptor; |
| 85 | |
| 86 | template<class F, class...Fs> |
| 87 | struct match_adaptor<F, Fs...> : detail::callable_base<F>, match_adaptor<Fs...> |
| 88 | { |
| 89 | typedef match_adaptor<Fs...> base; |
| 90 | typedef match_adaptor fit_rewritable_tag; |
| 91 | |
| 92 | struct failure |
| 93 | : failure_for<detail::callable_base<F>, Fs...> |
| 94 | {}; |
| 95 | |
| 96 | BOOST_HOF_INHERIT_DEFAULT(match_adaptor, detail::callable_base<F>, base); |
| 97 | |
| 98 | template<class X, class... Xs, BOOST_HOF_ENABLE_IF_CONVERTIBLE(X, detail::callable_base<F>), BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(base, Xs...)> |
| 99 | constexpr match_adaptor(X&& f1, Xs&& ... fs) |
| 100 | : detail::callable_base<F>(BOOST_HOF_FORWARD(X)(f1)), base(BOOST_HOF_FORWARD(Xs)(fs)...) |
| 101 | {} |
| 102 | |
| 103 | using F::operator(); |
| 104 | using base::operator(); |
| 105 | }; |
| 106 | |
| 107 | template<class F> |
| 108 | struct match_adaptor<F> : detail::callable_base<F> |
| 109 | { |
| 110 | typedef detail::callable_base<F> base; |
| 111 | typedef match_adaptor fit_rewritable_tag; |
| 112 | using F::operator(); |
| 113 | |
| 114 | BOOST_HOF_INHERIT_CONSTRUCTOR(match_adaptor, detail::callable_base<F>); |
| 115 | }; |
| 116 | |
| 117 | BOOST_HOF_DECLARE_STATIC_VAR(match, detail::make<match_adaptor>); |
| 118 | |
| 119 | }} // namespace boost::hof |
| 120 | |
| 121 | #endif |
| 122 | |