1/*=============================================================================
2 Copyright (c) 2015 Paul Fultz II
3 apply_eval.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_APPLY_EVAL_H
9#define BOOST_HOF_GUARD_APPLY_EVAL_H
10
11/// apply_eval
12/// ==========
13///
14/// Description
15/// -----------
16///
17/// The `apply_eval` function work like [`apply`](/include/boost/hof/apply), except it calls
18/// [`eval`](/include/boost/hof/eval) on each of its arguments. Each [`eval`](/include/boost/hof/eval) call is
19/// always ordered from left-to-right.
20///
21/// Synopsis
22/// --------
23///
24/// template<class F, class... Ts>
25/// constexpr auto apply_eval(F&& f, Ts&&... xs);
26///
27/// Semantics
28/// ---------
29///
30/// assert(apply_eval(f)(xs...) == f(eval(xs)...));
31///
32/// Requirements
33/// ------------
34///
35/// F must be:
36///
37/// * [ConstInvocable](ConstInvocable)
38///
39/// Ts must be:
40///
41/// * [EvaluatableFunctionObject](EvaluatableFunctionObject)
42///
43/// Example
44/// -------
45///
46/// #include <boost/hof.hpp>
47/// #include <cassert>
48///
49/// struct sum_f
50/// {
51/// template<class T, class U>
52/// T operator()(T x, U y) const
53/// {
54/// return x+y;
55/// }
56/// };
57///
58/// int main() {
59/// assert(boost::hof::apply_eval(sum_f(), []{ return 1; }, []{ return 2; }) == 3);
60/// }
61///
62
63#include <boost/hof/config.hpp>
64#include <boost/hof/returns.hpp>
65#include <boost/hof/detail/forward.hpp>
66#include <boost/hof/detail/static_const_var.hpp>
67#include <boost/hof/apply.hpp>
68#include <boost/hof/eval.hpp>
69
70#if BOOST_HOF_NO_ORDERED_BRACE_INIT
71#include <boost/hof/pack.hpp>
72#include <boost/hof/capture.hpp>
73#endif
74
75namespace boost { namespace hof {
76
77namespace detail {
78
79#if BOOST_HOF_NO_ORDERED_BRACE_INIT
80template<class R, class F, class Pack>
81constexpr R eval_ordered(const F& f, Pack&& p)
82{
83 return p(f);
84}
85
86template<class R, class F, class Pack, class T, class... Ts>
87constexpr R eval_ordered(const F& f, Pack&& p, T&& x, Ts&&... xs)
88{
89 return boost::hof::detail::eval_ordered<R>(f, boost::hof::pack_join(BOOST_HOF_FORWARD(Pack)(p), boost::hof::pack_forward(boost::hof::eval(x))), BOOST_HOF_FORWARD(Ts)(xs)...);
90}
91#else
92template<class R>
93struct eval_helper
94{
95 R result;
96
97 template<class F, class... Ts>
98 constexpr eval_helper(const F& f, Ts&&... xs) : result(boost::hof::apply(f, BOOST_HOF_FORWARD(Ts)(xs)...))
99 {}
100};
101
102template<>
103struct eval_helper<void>
104{
105 int x;
106 template<class F, class... Ts>
107 constexpr eval_helper(const F& f, Ts&&... xs) : x((boost::hof::apply(f, BOOST_HOF_FORWARD(Ts)(xs)...), 0))
108 {}
109};
110#endif
111
112struct apply_eval_f
113{
114 template<class F, class... Ts, class R=decltype(
115 boost::hof::apply(std::declval<const F&>(), boost::hof::eval(std::declval<Ts>())...)
116 ),
117 class=typename std::enable_if<(!std::is_void<R>::value)>::type
118 >
119 constexpr R operator()(const F& f, Ts&&... xs) const BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(boost::hof::apply(f, boost::hof::eval(BOOST_HOF_FORWARD(Ts)(xs))...))
120 {
121 return
122#if BOOST_HOF_NO_ORDERED_BRACE_INIT
123 boost::hof::detail::eval_ordered<R>
124 (f, boost::hof::pack(), BOOST_HOF_FORWARD(Ts)(xs)...);
125#else
126 boost::hof::detail::eval_helper<R>
127 {f, boost::hof::eval(BOOST_HOF_FORWARD(Ts)(xs))...}.result;
128#endif
129 }
130
131 template<class F, class... Ts, class R=decltype(
132 boost::hof::apply(std::declval<const F&>(), boost::hof::eval(std::declval<Ts>())...)
133 ),
134 class=typename std::enable_if<(std::is_void<R>::value)>::type
135 >
136 constexpr typename detail::holder<Ts...>::type
137 operator()(const F& f, Ts&&... xs) const BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(boost::hof::apply(f, boost::hof::eval(BOOST_HOF_FORWARD(Ts)(xs))...))
138 {
139 return (typename detail::holder<Ts...>::type)
140#if BOOST_HOF_NO_ORDERED_BRACE_INIT
141 boost::hof::detail::eval_ordered<R>
142 (f, boost::hof::pack(), BOOST_HOF_FORWARD(Ts)(xs)...);
143#else
144 boost::hof::detail::eval_helper<R>
145 {f, boost::hof::eval(BOOST_HOF_FORWARD(Ts)(xs))...};
146#endif
147 }
148};
149
150}
151
152BOOST_HOF_DECLARE_STATIC_VAR(apply_eval, detail::apply_eval_f);
153
154}} // namespace boost::hof
155
156#endif
157

source code of boost/libs/hof/include/boost/hof/apply_eval.hpp