1 /*=============================================================================
2 Copyright (c) 2014 Paul Fultz II
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 ==============================================================================*/
8 #ifndef BOOST_HOF_GUARD_FUNCTION_ON_H
9 #define BOOST_HOF_GUARD_FUNCTION_ON_H
17 /// The `proj` function adaptor applies a projection onto the parameters of
18 /// another function. This is useful, for example, to define a function for
19 /// sorting such that the ordering is based off of the value of one of its
22 /// Also, if just a projection is given, then the projection will be called
23 /// for each of its arguments.
25 /// Note: All projections are always evaluated in order from left-to-right.
30 /// template<class Projection, class F>
31 /// constexpr proj_adaptor<Projection, F> by(Projection p, F f);
33 /// template<class Projection>
34 /// constexpr proj_adaptor<Projection> by(Projection p);
39 /// assert(by(p, f)(xs...) == f(p(xs)...));
40 /// assert(by(p)(xs...) == p(xs)...);
45 /// Projection must be:
47 /// * [UnaryInvocable](UnaryInvocable)
48 /// * MoveConstructible
52 /// * [ConstInvocable](ConstInvocable)
53 /// * MoveConstructible
58 /// #include <boost/hof.hpp>
59 /// #include <cassert>
60 /// using namespace boost::hof;
64 /// foo(int x_) : x(x_)
70 /// assert(boost::hof::proj(&foo::x, _ + _)(foo(1), foo(2)) == 3);
76 /// * [Projections](Projections)
77 /// * [Variadic print](<Variadic print>)
83 #include <boost/hof/always.hpp>
84 #include <boost/hof/detail/callable_base.hpp>
85 #include <boost/hof/detail/result_of.hpp>
86 #include <boost/hof/detail/move.hpp>
87 #include <boost/hof/detail/make.hpp>
88 #include <boost/hof/detail/static_const_var.hpp>
89 #include <boost/hof/detail/compressed_pair.hpp>
90 #include <boost/hof/detail/result_type.hpp>
91 #include <boost/hof/apply_eval.hpp>
93 namespace boost { namespace hof {
97 template<class T, class Projection>
103 template<class X, class P>
104 constexpr project_eval(X&& xp, const P& pp) : x(BOOST_HOF_FORWARD(X)(xp)), p(pp)
107 constexpr auto operator()() const BOOST_HOF_RETURNS
108 (p(BOOST_HOF_FORWARD(T)(x)));
111 template<class T, class Projection>
112 constexpr project_eval<T, Projection> make_project_eval(T&& x, const Projection& p)
114 return project_eval<T, Projection>(BOOST_HOF_FORWARD(T)(x), p);
117 template<class T, class Projection>
118 struct project_void_eval
123 template<class X, class P>
124 constexpr project_void_eval(X&& xp, const P& pp) : x(BOOST_HOF_FORWARD(X)(xp)), p(pp)
129 constexpr void_ operator()() const
131 return p(BOOST_HOF_FORWARD(T)(x)), void_();
135 template<class T, class Projection>
136 constexpr project_void_eval<T, Projection> make_project_void_eval(T&& x, const Projection& p)
138 return project_void_eval<T, Projection>(BOOST_HOF_FORWARD(T)(x), p);
141 template<class Projection, class F, class... Ts,
143 std::declval<const F&>()(std::declval<const Projection&>()(std::declval<Ts>())...)
145 constexpr R by_eval(const Projection& p, const F& f, Ts&&... xs)
147 return boost::hof::apply_eval(f, make_project_eval(BOOST_HOF_FORWARD(Ts)(xs), p)...);
150 #if BOOST_HOF_NO_ORDERED_BRACE_INIT
151 #define BOOST_HOF_BY_VOID_RETURN BOOST_HOF_ALWAYS_VOID_RETURN
153 #if BOOST_HOF_NO_CONSTEXPR_VOID
154 #define BOOST_HOF_BY_VOID_RETURN boost::hof::detail::swallow
156 #define BOOST_HOF_BY_VOID_RETURN void
160 template<class Projection, class... Ts>
161 constexpr BOOST_HOF_ALWAYS_VOID_RETURN by_void_eval(const Projection& p, Ts&&... xs)
163 return boost::hof::apply_eval(boost::hof::always(), boost::hof::detail::make_project_void_eval(BOOST_HOF_FORWARD(Ts)(xs), p)...);
168 template<class... Ts>
169 constexpr swallow(Ts&&...)
175 template<class Projection, class F=void>
178 template<class Projection, class F>
179 struct proj_adaptor : detail::compressed_pair<detail::callable_base<Projection>, detail::callable_base<F>>, detail::function_result_type<F>
181 typedef proj_adaptor fit_rewritable_tag;
182 typedef detail::compressed_pair<detail::callable_base<Projection>, detail::callable_base<F>> base;
183 template<class... Ts>
184 constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const
186 return this->second(xs...);;
189 template<class... Ts>
190 constexpr const detail::callable_base<Projection>& base_projection(Ts&&... xs) const
192 return this->first(xs...);
197 template<class Failure>
200 template<class... Ts>
202 : Failure::template of<decltype(std::declval<detail::callable_base<Projection>>()(std::declval<Ts>()))...>
208 : failure_map<by_failure, detail::callable_base<F>>
211 BOOST_HOF_INHERIT_CONSTRUCTOR(proj_adaptor, base)
213 BOOST_HOF_RETURNS_CLASS(proj_adaptor);
215 template<class... Ts>
216 constexpr BOOST_HOF_SFINAE_RESULT(const detail::callable_base<F>&, result_of<const detail::callable_base<Projection>&, id_<Ts>>...)
217 operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
219 boost::hof::detail::by_eval(
220 BOOST_HOF_MANGLE_CAST(const detail::callable_base<Projection>&)(BOOST_HOF_CONST_THIS->base_projection(xs...)),
221 BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)),
222 BOOST_HOF_FORWARD(Ts)(xs)...
227 template<class Projection>
228 struct proj_adaptor<Projection, void> : detail::callable_base<Projection>
230 typedef proj_adaptor fit_rewritable1_tag;
231 template<class... Ts>
232 constexpr const detail::callable_base<Projection>& base_projection(Ts&&... xs) const
234 return boost::hof::always_ref(*this)(xs...);
237 BOOST_HOF_INHERIT_DEFAULT(proj_adaptor, detail::callable_base<Projection>)
239 template<class P, BOOST_HOF_ENABLE_IF_CONVERTIBLE(P, detail::callable_base<Projection>)>
240 constexpr proj_adaptor(P&& p)
241 : detail::callable_base<Projection>(BOOST_HOF_FORWARD(P)(p))
244 BOOST_HOF_RETURNS_CLASS(proj_adaptor);
246 template<class... Ts, class=detail::holder<decltype(std::declval<Projection>()(std::declval<Ts>()))...>>
247 constexpr BOOST_HOF_BY_VOID_RETURN operator()(Ts&&... xs) const
249 #if BOOST_HOF_NO_ORDERED_BRACE_INIT
250 return boost::hof::detail::by_void_eval(this->base_projection(xs...), BOOST_HOF_FORWARD(Ts)(xs)...);
252 #if BOOST_HOF_NO_CONSTEXPR_VOID
255 boost::hof::detail::swallow{
256 (this->base_projection(xs...)(BOOST_HOF_FORWARD(Ts)(xs)), 0)...
262 BOOST_HOF_DECLARE_STATIC_VAR(proj, detail::make<proj_adaptor>);
264 }} // namespace boost::hof