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/include/phoenix_core.hpp>
26 #include <boost/spirit/home/support/attributes.hpp>
28 namespace boost { namespace spirit { namespace traits
30 template <typename Component>
31 struct action_dispatch
33 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_LAMBDAS) && \
34 !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE)
35 // omit function parameters without specializing for each possible
36 // type of callable entity
37 // many thanks to Eelis/##iso-c++ for this contribution
40 // this will be used to pass around POD types which are safe
41 // to go through the ellipsis operator (if ever used)
45 // the first parameter is a placeholder to obtain SFINAE when
46 // doing overload resolution, the second one is the actual
47 // forwarder, where we can apply our implementation
48 template <typename, typename T>
49 struct fwd_storage { typedef T type; };
51 // gcc should accept fake<T>() but it prints a sorry, needs
52 // a check once the bug is sorted out, use a FAKE_CALL macro for now
56 #define BOOST_SPIRIT_FAKE_CALL(T) (*(T*)0)
58 // the forwarders, here we could tweak the implementation of
59 // how parameters are passed to the functions, if needed
62 template<typename F, typename... Rest>
63 auto operator()(F && f, Rest&&...) -> decltype(f())
71 template<typename F, typename A, typename... Rest>
72 auto operator()(F && f, A && a, Rest&&...) -> decltype(f(a))
78 struct fwd_attrib_context
80 template<typename F, typename A, typename B, typename... Rest>
81 auto operator()(F && f, A && a, B && b, Rest&&...)
88 struct fwd_attrib_context_pass
90 template<typename F, typename A, typename B, typename C
92 auto operator()(F && f, A && a, B && b, C && c, Rest&&...)
93 -> decltype(f(a, b, c))
99 // SFINAE for our calling syntax, the forwarders are stored based
100 // on what function call gives a proper result
101 // this code can probably be more generic once implementations are
103 template <typename F>
104 static auto do_call(F && f, ...)
105 -> typename fwd_storage<decltype(f()), fwd_none>::type
110 template <typename F, typename A>
111 static auto do_call(F && f, fwd_tag<A>, ...)
112 -> typename fwd_storage<decltype(f(BOOST_SPIRIT_FAKE_CALL(A)))
118 template <typename F, typename A, typename B>
119 static auto do_call(F && f, fwd_tag<A>, fwd_tag<B>, ...)
120 -> typename fwd_storage<
121 decltype(f(BOOST_SPIRIT_FAKE_CALL(A), BOOST_SPIRIT_FAKE_CALL(B)))
122 , fwd_attrib_context>::type
127 template <typename F, typename A, typename B, typename C>
128 static auto do_call(F && f, fwd_tag<A>, fwd_tag<B>, fwd_tag<C>, ...)
129 -> typename fwd_storage<
130 decltype(f(BOOST_SPIRIT_FAKE_CALL(A), BOOST_SPIRIT_FAKE_CALL(B)
131 , BOOST_SPIRIT_FAKE_CALL(C)))
132 , fwd_attrib_context_pass>::type
137 // this function calls the forwarder and is responsible for
138 // stripping the tail of the parameters
139 template <typename F, typename... A>
140 static void caller(F && f, A && ... a)
142 do_call(f, fwd_tag<typename std::remove_reference<A>::type>()...)
143 (std::forward<F>(f), std::forward<A>(a)...);
146 #undef BOOST_SPIRIT_FAKE_CALL
149 template <typename F, typename Attribute, typename Context>
150 bool operator()(F const& f, Attribute& attr, Context& context)
153 caller(f, attr, context, pass);
157 // general handler for everything not explicitly specialized below
158 template <typename F, typename Attribute, typename Context>
159 bool operator()(F const& f, Attribute& attr, Context& context)
162 f(attr, context, pass);
167 // handler for phoenix actors
169 // If the component this action has to be invoked for is a tuple, we
170 // wrap any non-fusion tuple into a fusion tuple (done by pass_attribute)
171 // and pass through any fusion tuple.
172 template <typename Eval, typename Attribute, typename Context>
173 bool operator()(phoenix::actor<Eval> const& f
174 , Attribute& attr, Context& context)
177 typename pass_attribute<Component, Attribute>::type attr_wrap(attr);
178 f(attr_wrap, context, pass);
182 // specializations for plain function pointers taking different number of
184 template <typename RT, typename A0, typename A1, typename A2
185 , typename Attribute, typename Context>
186 bool operator()(RT(*f)(A0, A1, A2), Attribute& attr, Context& context)
189 f(attr, context, pass);
193 template <typename RT, typename A0, typename A1
194 , typename Attribute, typename Context>
195 bool operator()(RT(*f)(A0, A1), Attribute& attr, Context& context)
201 template <typename RT, typename A0, typename Attribute, typename Context>
202 bool operator()(RT(*f)(A0), Attribute& attr, Context&)
208 template <typename RT, typename Attribute, typename Context>
209 bool operator()(RT(*f)(), Attribute&, Context&)