]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2007 Joel de Guzman | |
3 | Copyright (c) 2015 Kohei Takahashi | |
4 | ||
5 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
7 | ==============================================================================*/ | |
8 | ||
9 | #ifndef BOOST_PHOENIX_CORE_DETAIL_FUNCTION_EVAL_HPP | |
10 | #define BOOST_PHOENIX_CORE_DETAIL_FUNCTION_EVAL_HPP | |
11 | ||
12 | #include <boost/phoenix/core/limits.hpp> | |
13 | #include <boost/phoenix/support/iterate.hpp> | |
14 | #include <boost/phoenix/core/call.hpp> | |
15 | #include <boost/phoenix/core/expression.hpp> | |
16 | #include <boost/phoenix/core/meta_grammar.hpp> | |
17 | #include <boost/phoenix/core/detail/phx2_result.hpp> | |
18 | #include <boost/utility/result_of.hpp> | |
19 | ||
20 | #ifndef BOOST_PHOENIX_NO_VARIADIC_FUNCTION_EVAL | |
21 | # include <boost/mpl/if.hpp> | |
22 | # include <boost/type_traits/is_reference.hpp> | |
23 | #endif | |
24 | ||
25 | #ifdef BOOST_PHOENIX_NO_VARIADIC_EXPRESSION | |
26 | # include <boost/phoenix/core/detail/cpp03/function_eval_expr.hpp> | |
27 | #else | |
28 | BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG( | |
29 | (boost)(phoenix)(detail)(function_eval) | |
30 | , (meta_grammar)(meta_grammar) | |
31 | , _ | |
32 | ) | |
33 | #endif | |
34 | ||
35 | namespace boost { namespace phoenix { | |
36 | namespace detail | |
37 | { | |
38 | template <typename T> | |
39 | T& help_rvalue_deduction(T& x) | |
40 | { | |
41 | return x; | |
42 | } | |
43 | ||
44 | template <typename T> | |
45 | T const& help_rvalue_deduction(T const& x) | |
46 | { | |
47 | return x; | |
48 | } | |
49 | ||
50 | struct function_eval | |
51 | { | |
52 | template <typename Sig> | |
53 | struct result; | |
54 | ||
55 | #ifdef BOOST_PHOENIX_NO_VARIADIC_FUNCTION_EVAL | |
56 | template <typename This, typename F, typename Context> | |
57 | struct result<This(F, Context)> | |
58 | { | |
59 | typedef typename | |
60 | remove_reference< | |
61 | typename boost::result_of<evaluator(F, Context)>::type | |
62 | >::type | |
63 | fn; | |
64 | ||
65 | typedef typename boost::result_of<fn()>::type type; | |
66 | }; | |
67 | ||
68 | template <typename F, typename Context> | |
69 | typename result<function_eval(F const&, Context const&)>::type | |
70 | operator()(F const & f, Context const & ctx) const | |
71 | { | |
72 | return boost::phoenix::eval(f, ctx)(); | |
73 | } | |
74 | ||
75 | template <typename F, typename Context> | |
76 | typename result<function_eval(F &, Context const&)>::type | |
77 | operator()(F & f, Context const & ctx) const | |
78 | { | |
79 | return boost::phoenix::eval(f, ctx)(); | |
80 | } | |
81 | ||
82 | #include <boost/phoenix/core/detail/cpp03/function_eval.hpp> | |
83 | #else | |
84 | template <typename, typename, typename...> struct result_impl; | |
85 | ||
86 | template <typename F, typename... A, typename Head, typename... Tail> | |
87 | struct result_impl<F, void(A...), Head, Tail...> | |
88 | : result_impl<F, void(A..., Head), Tail...> | |
89 | { | |
90 | }; | |
91 | ||
92 | template <typename F, typename... A, typename Context> | |
93 | struct result_impl<F, void(A...), Context> | |
94 | { | |
95 | typedef typename | |
96 | remove_reference< | |
97 | typename boost::result_of<evaluator(F, Context)>::type | |
98 | >::type | |
99 | fn; | |
100 | ||
101 | template <typename T> | |
102 | struct result_of_evaluator | |
103 | { | |
104 | typedef typename boost::add_reference< | |
105 | typename boost::add_const< | |
106 | typename boost::result_of< | |
107 | boost::phoenix::evaluator(T, Context) | |
108 | >::type | |
109 | >::type | |
110 | >::type type; | |
111 | }; | |
112 | ||
113 | typedef typename | |
114 | boost::result_of< | |
115 | fn(typename result_of_evaluator<A>::type...) | |
116 | >::type | |
117 | type; | |
118 | ||
119 | static type call(F f, A... a, Context ctx) | |
120 | { | |
121 | return boost::phoenix::eval(f, ctx)(help_rvalue_deduction(boost::phoenix::eval(a, ctx))...); | |
122 | } | |
123 | }; | |
124 | ||
125 | template <typename This, typename F, typename... A> | |
126 | struct result<This(F, A...)> | |
127 | : result_impl<F, void(), A...> | |
128 | { | |
129 | }; | |
130 | ||
131 | template <typename F, typename... A> | |
132 | typename result< | |
133 | function_eval( | |
134 | F const & | |
135 | , typename mpl::if_<is_reference<A>, A, A const &>::type... | |
136 | ) | |
137 | >::type | |
138 | // 'A &... a, Context const &ctx' doesn't work as intended: type deduction always fail. | |
139 | operator()(F && f, A &&... a) const | |
140 | { | |
141 | return | |
142 | result< | |
143 | function_eval( | |
144 | typename mpl::if_<is_reference<F>, F, F const &>::type | |
145 | , typename mpl::if_<is_reference<A>, A, A const &>::type... | |
146 | ) | |
147 | >::call(f, a...); | |
148 | } | |
149 | #endif | |
150 | }; | |
151 | ||
152 | } | |
153 | ||
154 | template <typename Dummy> | |
155 | struct default_actions::when<detail::rule::function_eval, Dummy> | |
156 | : phoenix::call<detail::function_eval> | |
157 | {}; | |
158 | }} | |
159 | ||
160 | #endif |