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