1 /*=============================================================================
2 Copyright (c) 2005-2010 Joel de Guzman
3 Copyright (c) 2010 Eric Niebler
4 Copyright (c) 2010 Thomas Heller
5 Copyright (c) 2014 John Fletcher
7 Distributed under the Boost Software License, Version 1.0. (See accompanying
8 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 ==============================================================================*/
10 #ifndef BOOST_PHOENIX_CORE_ACTOR_HPP
11 #define BOOST_PHOENIX_CORE_ACTOR_HPP
13 #include <boost/phoenix/core/limits.hpp>
15 #include <boost/is_placeholder.hpp>
16 #include <boost/mpl/identity.hpp>
17 #include <boost/mpl/eval_if.hpp>
18 #include <boost/phoenix/core/domain.hpp>
19 #include <boost/phoenix/core/environment.hpp>
20 #include <boost/phoenix/core/is_nullary.hpp>
21 #include <boost/phoenix/core/meta_grammar.hpp>
22 #include <boost/phoenix/support/iterate.hpp>
23 #include <boost/phoenix/support/vector.hpp>
24 #include <boost/proto/extends.hpp>
25 #include <boost/proto/make_expr.hpp>
26 #include <boost/utility/result_of.hpp>
27 #include <boost/mpl/void.hpp>
29 #ifndef BOOST_PHOENIX_NO_VARIADIC_ACTOR
30 # include <boost/mpl/if.hpp>
31 # include <boost/type_traits/is_reference.hpp>
32 # include <boost/phoenix/core/detail/index_sequence.hpp>
37 #pragma warning(disable: 4522) // 'this' used in base member initializer list
38 #pragma warning(disable: 4510) // default constructor could not be generated
39 #pragma warning(disable: 4610) // can never be instantiated - user defined cons
42 namespace boost { namespace phoenix
44 template <typename Expr>
49 struct error_expecting_arguments
52 error_expecting_arguments(T const&) {}
55 struct error_invalid_lambda_expr
58 error_invalid_lambda_expr(T const&) {}
62 struct result_type_deduction_helper
64 typedef T const & type;
68 struct result_type_deduction_helper<T &>
74 struct result_type_deduction_helper<T const &>
76 typedef T const & type;
81 BOOST_PROTO_CALLABLE()
83 typedef void result_type;
85 template <typename T1, typename T2>
86 void operator()(T1 & t1, T2 const & t2) const
88 proto::value(t1) = proto::value(t2);
92 #ifdef BOOST_PHOENIX_NO_VARIADIC_ACTOR
93 #include <boost/phoenix/core/detail/cpp03/assign.hpp>
95 struct assign : proto::transform<assign>
97 typedef assign proto_grammer;
99 template <typename Expr, typename State, typename Data
100 , typename Indices = typename detail::make_index_sequence<proto::arity_of<Expr>::value>::type >
103 template <std::size_t>
104 struct proto_expr { typedef proto::_ type; };
106 template <std::size_t Index>
111 proto::_child_c<Index>
112 , proto::call<proto::_child_c<Index>(proto::_state)>
117 template <typename Expr, typename State, typename Data>
118 struct impl<Expr, State, Data, detail::index_sequence<> >
120 proto::terminal<proto::_>
121 , do_assign(proto::_, proto::_state)
122 >::template impl<Expr, State, Data>
126 template <typename Expr, typename State, typename Data
127 , std::size_t... Indices>
128 struct impl<Expr, State, Data, detail::index_sequence<Indices...> >
130 proto::nary_expr<typename proto_expr<Indices>::type...>
131 , proto::and_<typename nth_assign<Indices>::type...>
132 >::template impl<Expr, State, Data>
141 #ifdef BOOST_PHOENIX_NO_VARIADIC_ACTOR
142 // Bring in the result_of::actor<>
143 #include <boost/phoenix/core/detail/cpp03/actor_result_of.hpp>
145 template <typename Expr, typename... A>
149 typename boost::phoenix::evaluator::impl<
152 typename vector_chooser<sizeof...(A) + 1>::
153 template apply<const ::boost::phoenix::actor<Expr> *, A...>::type&
161 template <typename Expr, typename... A>
162 struct actor : actor_impl<Expr, A...> {};
164 template <typename Expr>
165 struct nullary_actor_result : actor_impl<Expr> {};
168 template <typename Expr>
172 // avoid calling result_of::actor when this is false
173 typename mpl::eval_if_c<
174 result_of::is_nullary<Expr>::value
175 , nullary_actor_result<Expr>
176 , mpl::identity<detail::error_expecting_arguments>
182 ////////////////////////////////////////////////////////////////////////////
186 // The actor class. The main thing! In phoenix, everything is an actor
187 // This class is responsible for full function evaluation. Partial
188 // function evaluation involves creating a hierarchy of actor objects.
190 ////////////////////////////////////////////////////////////////////////////
191 template <typename Expr>
197 is_custom_terminal<Expr>
198 , mpl::bool_<is_placeholder<Expr>::value>
200 , proto::terminal<Expr>
201 , mpl::identity<Expr>
205 BOOST_PROTO_BASIC_EXTENDS(expr_type, actor<expr_type>, phoenix_domain)
207 // providing operator= to be assignable
208 actor& operator=(actor const& other)
210 detail::assign()(*this, other);
213 actor& operator=(actor & other)
215 detail::assign()(*this, other);
219 template <typename A0>
220 typename proto::result_of::make_expr<
226 operator=(A0 const & a0) const
228 return proto::make_expr<proto::tag::assign, phoenix_domain>(this->proto_expr_, a0);
231 template <typename A0>
232 typename proto::result_of::make_expr<
238 operator=(A0 & a0) const
240 return proto::make_expr<proto::tag::assign, phoenix_domain>(this->proto_expr_, a0);
243 template <typename A0>
244 typename proto::result_of::make_expr<
245 proto::tag::subscript
250 operator[](A0 const & a0) const
252 return proto::make_expr<proto::tag::subscript, phoenix_domain>(this->proto_expr_, a0);
255 template <typename A0>
256 typename proto::result_of::make_expr<
257 proto::tag::subscript
262 operator[](A0 & a0) const
264 return proto::make_expr<proto::tag::subscript, phoenix_domain>(this->proto_expr_, a0);
267 template <typename Sig>
270 typename result_of::actor<proto_base_expr>::type
273 typedef vector1<const actor<Expr> *> env_type;
274 env_type env = {this};
276 return phoenix::eval(*this, phoenix::context(env, default_actions()));
279 typename result_of::actor<proto_base_expr>::type
282 typedef vector1<const actor<Expr> *> env_type;
283 env_type env = {this};
285 return phoenix::eval(*this, phoenix::context(env, default_actions()));
288 template <typename Env>
289 typename evaluator::impl<
290 proto_base_expr const &
291 , typename result_of::context<
293 , default_actions const &
297 eval(Env const & env) const
299 return phoenix::eval(*this, phoenix::context(env, default_actions()));
302 #ifdef BOOST_PHOENIX_NO_VARIADIC_ACTOR
304 #include <boost/phoenix/core/detail/cpp03/actor_operator.hpp>
306 template <typename This, typename... A>
307 struct result<This(A...)>
310 , typename mpl::if_<is_reference<A>, A, A const &>::type...
314 template <typename... A>
315 typename result<actor(A...)>::type
316 operator()(A &&... a)
319 typename vector_chooser<sizeof...(A) + 1>::template apply<
321 , typename mpl::if_<is_reference<A>, A, A const &>::type...
325 env_type env = {this, a...};
326 return phoenix::eval(*this, phoenix::context(env, default_actions()));
329 template <typename... A>
330 typename result<actor(A...)>::type
331 operator()(A &&... a) const
334 typename vector_chooser<sizeof...(A) + 1>::template apply<
336 , typename mpl::if_<is_reference<A>, A, A const &>::type...
340 env_type env = {this, a...};
341 return phoenix::eval(*this, phoenix::context(env, default_actions()));
349 // specialize boost::result_of to return the proper result type
350 template <typename Expr>
351 struct result_of<phoenix::actor<Expr>()>
352 : phoenix::result_of::actor<typename phoenix::actor<Expr>::proto_base_expr>
355 template <typename Expr>
356 struct result_of<phoenix::actor<Expr> const()>
357 : phoenix::result_of::actor<typename phoenix::actor<Expr>::proto_base_expr>