1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 http://spirit.sourceforge.net/
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_ACTION_DISPATCH_APRIL_18_2008_0720AM)
10 #define BOOST_SPIRIT_ACTION_DISPATCH_APRIL_18_2008_0720AM
16 #include<boost/config.hpp>
18 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_LAMBDAS) && \
19 !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE)
21 #include <type_traits>
25 #include <boost/spirit/home/support/attributes.hpp>
27 namespace boost { namespace phoenix
29 template <typename Expr>
33 namespace boost { namespace spirit { namespace traits
35 template <typename Component>
36 struct action_dispatch
38 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_LAMBDAS) && \
39 !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE)
40 // omit function parameters without specializing for each possible
41 // type of callable entity
42 // many thanks to Eelis/##iso-c++ for this contribution
45 // this will be used to pass around POD types which are safe
46 // to go through the ellipsis operator (if ever used)
50 // the first parameter is a placeholder to obtain SFINAE when
51 // doing overload resolution, the second one is the actual
52 // forwarder, where we can apply our implementation
53 template <typename, typename T>
54 struct fwd_storage { typedef T type; };
56 // gcc should accept fake<T>() but it prints a sorry, needs
57 // a check once the bug is sorted out, use a FAKE_CALL macro for now
61 #define BOOST_SPIRIT_FAKE_CALL(T) (*(T*)0)
63 // the forwarders, here we could tweak the implementation of
64 // how parameters are passed to the functions, if needed
67 template<typename F, typename... Rest>
68 auto operator()(F && f, Rest&&...) -> decltype(f())
76 template<typename F, typename A, typename... Rest>
77 auto operator()(F && f, A && a, Rest&&...) -> decltype(f(a))
83 struct fwd_attrib_context
85 template<typename F, typename A, typename B, typename... Rest>
86 auto operator()(F && f, A && a, B && b, Rest&&...)
93 struct fwd_attrib_context_pass
95 template<typename F, typename A, typename B, typename C
97 auto operator()(F && f, A && a, B && b, C && c, Rest&&...)
98 -> decltype(f(a, b, c))
104 // SFINAE for our calling syntax, the forwarders are stored based
105 // on what function call gives a proper result
106 // this code can probably be more generic once implementations are
108 template <typename F>
109 static auto do_call(F && f, ...)
110 -> typename fwd_storage<decltype(f()), fwd_none>::type
115 template <typename F, typename A>
116 static auto do_call(F && f, fwd_tag<A>, ...)
117 -> typename fwd_storage<decltype(f(BOOST_SPIRIT_FAKE_CALL(A)))
123 template <typename F, typename A, typename B>
124 static auto do_call(F && f, fwd_tag<A>, fwd_tag<B>, ...)
125 -> typename fwd_storage<
126 decltype(f(BOOST_SPIRIT_FAKE_CALL(A), BOOST_SPIRIT_FAKE_CALL(B)))
127 , fwd_attrib_context>::type
132 template <typename F, typename A, typename B, typename C>
133 static auto do_call(F && f, fwd_tag<A>, fwd_tag<B>, fwd_tag<C>, ...)
134 -> typename fwd_storage<
135 decltype(f(BOOST_SPIRIT_FAKE_CALL(A), BOOST_SPIRIT_FAKE_CALL(B)
136 , BOOST_SPIRIT_FAKE_CALL(C)))
137 , fwd_attrib_context_pass>::type
142 // this function calls the forwarder and is responsible for
143 // stripping the tail of the parameters
144 template <typename F, typename... A>
145 static void caller(F && f, A && ... a)
147 do_call(f, fwd_tag<typename std::remove_reference<A>::type>()...)
148 (std::forward<F>(f), std::forward<A>(a)...);
151 #undef BOOST_SPIRIT_FAKE_CALL
154 template <typename F, typename Attribute, typename Context>
155 bool operator()(F const& f, Attribute& attr, Context& context)
158 caller(f, attr, context, pass);
162 // general handler for everything not explicitly specialized below
163 template <typename F, typename Attribute, typename Context>
164 bool operator()(F const& f, Attribute& attr, Context& context)
167 f(attr, context, pass);
172 // handler for phoenix actors
174 // If the component this action has to be invoked for is a tuple, we
175 // wrap any non-fusion tuple into a fusion tuple (done by pass_attribute)
176 // and pass through any fusion tuple.
177 template <typename Eval, typename Attribute, typename Context>
178 bool operator()(phoenix::actor<Eval> const& f
179 , Attribute& attr, Context& context)
182 typename pass_attribute<Component, Attribute>::type attr_wrap(attr);
183 f(attr_wrap, context, pass);
187 // specializations for plain function pointers taking different number of
189 template <typename RT, typename A0, typename A1, typename A2
190 , typename Attribute, typename Context>
191 bool operator()(RT(*f)(A0, A1, A2), Attribute& attr, Context& context)
194 f(attr, context, pass);
198 template <typename RT, typename A0, typename A1
199 , typename Attribute, typename Context>
200 bool operator()(RT(*f)(A0, A1), Attribute& attr, Context& context)
206 template <typename RT, typename A0, typename Attribute, typename Context>
207 bool operator()(RT(*f)(A0), Attribute& attr, Context&)
213 template <typename RT, typename Attribute, typename Context>
214 bool operator()(RT(*f)(), Attribute&, Context&)