fix doc example typo
[boost.git] / boost / proto / context / callable.hpp
blobf1c11f1dcaae8d9169c010ca16c904de23aef324
1 #ifndef BOOST_PP_IS_ITERATING
2 ///////////////////////////////////////////////////////////////////////////////
3 /// \file callable.hpp
4 /// Definintion of callable_context\<\>, an evaluation context for
5 /// proto::eval() that explodes each node and calls the derived context
6 /// type with the expressions constituents. If the derived context doesn't
7 /// have an overload that handles this node, fall back to some other
8 /// context.
9 //
10 // Copyright 2008 Eric Niebler. Distributed under the Boost
11 // Software License, Version 1.0. (See accompanying file
12 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_PROTO_CONTEXT_CALLABLE_HPP_EAN_06_23_2007
15 #define BOOST_PROTO_CONTEXT_CALLABLE_HPP_EAN_06_23_2007
17 #include <boost/proto/detail/prefix.hpp> // must be first include
18 #include <boost/config.hpp>
19 #include <boost/detail/workaround.hpp>
20 #include <boost/preprocessor/cat.hpp>
21 #include <boost/preprocessor/iteration/iterate.hpp>
22 #include <boost/preprocessor/facilities/intercept.hpp>
23 #include <boost/preprocessor/repetition/repeat.hpp>
24 #include <boost/preprocessor/repetition/enum_params.hpp>
25 #include <boost/preprocessor/repetition/enum_trailing.hpp>
26 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
27 #include <boost/preprocessor/arithmetic/inc.hpp>
28 #include <boost/preprocessor/selection/max.hpp>
29 #include <boost/mpl/if.hpp>
30 #include <boost/mpl/bool.hpp>
31 #include <boost/utility/result_of.hpp>
32 #include <boost/type_traits/remove_cv.hpp>
33 #include <boost/proto/proto_fwd.hpp>
34 #include <boost/proto/traits.hpp> // for child_c
35 #include <boost/proto/detail/suffix.hpp> // must be last include
37 namespace boost { namespace proto
39 namespace detail
41 template<typename T>
42 yes_type check_is_expr_handled(T const &);
44 no_type check_is_expr_handled(private_type_ const &);
46 template<typename Context, long Arity>
47 struct callable_context_wrapper;
49 template<typename Expr, typename Context, long Arity = Expr::proto_arity_c>
50 struct is_expr_handled;
52 template<typename Expr, typename Context>
53 struct is_expr_handled<Expr, Context, 0>
55 static callable_context_wrapper<Context, 1> &sctx_;
56 static Expr &sexpr_;
57 static typename Expr::proto_tag &stag_;
59 BOOST_STATIC_CONSTANT(bool, value =
61 sizeof(yes_type) ==
62 sizeof(
63 detail::check_is_expr_handled(
64 (sctx_(stag_, proto::value(sexpr_)), 0)
66 )));
68 typedef mpl::bool_<value> type;
72 namespace context
74 /// \brief A BinaryFunction that accepts a Proto expression and a
75 /// callable context and calls the context with the expression tag
76 /// and children as arguments, effectively fanning the expression
77 /// out.
78 ///
79 /// <tt>callable_eval\<\></tt> requires that \c Context is a
80 /// PolymorphicFunctionObject that can be invoked with \c Expr's
81 /// tag and children as expressions, as follows:
82 ///
83 /// \code
84 /// context(Expr::proto_tag(), child_c<0>(expr), child_c<1>(expr), ...)
85 /// \endcode
86 template<
87 typename Expr
88 , typename Context
89 , long Arity BOOST_PROTO_WHEN_BUILDING_DOCS(= Expr::proto_arity_c)
91 struct callable_eval
92 {};
94 /// \brief A BinaryFunction that accepts a Proto expression and a
95 /// callable context and calls the context with the expression tag
96 /// and children as arguments, effectively fanning the expression
97 /// out.
98 ///
99 /// <tt>callable_eval\<\></tt> requires that \c Context is a
100 /// PolymorphicFunctionObject that can be invoked with \c Expr's
101 /// tag and children as expressions, as follows:
103 /// \code
104 /// context(Expr::proto_tag(), value(expr))
105 /// \endcode
106 template<typename Expr, typename Context>
107 struct callable_eval<Expr, Context, 0>
109 typedef typename proto::result_of::value<Expr const &>::type value_type;
111 typedef
112 typename boost::result_of<
113 Context(typename Expr::proto_tag, value_type)
114 >::type
115 result_type;
117 /// \param expr The current expression
118 /// \param context The callable evaluation context
119 /// \return <tt>context(Expr::proto_tag(), value(expr))</tt>
120 result_type operator ()(Expr &expr, Context &context) const
122 return context(typename Expr::proto_tag(), proto::value(expr));
126 /// \brief An evaluation context adaptor that makes authoring a
127 /// context a simple matter of writing function overloads, rather
128 /// then writing template specializations.
130 /// <tt>callable_context\<\></tt> is a base class that implements
131 /// the context protocol by passing fanned-out expression nodes to
132 /// the derived context, making it easy to customize the handling
133 /// of expression types by writing function overloads. Only those
134 /// expression types needing special handling require explicit
135 /// handling. All others are dispatched to a user-specified
136 /// default context, \c DefaultCtx.
138 /// <tt>callable_context\<\></tt> is defined simply as:
140 /// \code
141 /// template<typename Context, typename DefaultCtx = default_context>
142 /// struct callable_context
143 /// {
144 /// template<typename Expr, typename ThisContext = Context>
145 /// struct eval
146 /// : mpl::if_<
147 /// is_expr_handled_<Expr, Context> // For exposition
148 /// , callable_eval<Expr, ThisContext>
149 /// , typename DefaultCtx::template eval<Expr, Context>
150 /// >::type
151 /// {};
152 /// };
153 /// \endcode
155 /// The Boolean metafunction <tt>is_expr_handled_\<\></tt> uses
156 /// metaprogramming tricks to determine whether \c Context has
157 /// an overloaded function call operator that accepts the
158 /// fanned-out constituents of an expression of type \c Expr.
159 /// If so, the handling of the expression is dispatched to
160 /// <tt>callable_eval\<\></tt>. If not, it is dispatched to
161 /// the user-specified \c DefaultCtx.
163 /// Below is an example of how to use <tt>callable_context\<\></tt>:
165 /// \code
166 /// // An evaluation context that increments all
167 /// // integer terminals in-place.
168 /// struct increment_ints
169 /// : callable_context<
170 /// increment_ints const // derived context
171 /// , null_context const // fall-back context
172 /// >
173 /// {
174 /// typedef void result_type;
176 /// // Handle int terminals here:
177 /// void operator()(proto::tag::terminal, int &i) const
178 /// {
179 /// ++i;
180 /// }
181 /// };
182 /// \endcode
184 /// With \c increment_ints, we can do the following:
186 /// \code
187 /// literal<int> i = 0, j = 10;
188 /// proto::eval( i - j * 3.14, increment_ints() );
190 /// assert( i.get() == 1 && j.get() == 11 );
191 /// \endcode
192 template<
193 typename Context
194 , typename DefaultCtx BOOST_PROTO_WHEN_BUILDING_DOCS(= default_context)
196 struct callable_context
198 /// A BinaryFunction that accepts an \c Expr and a
199 /// \c Context, and either fans out the expression and passes
200 /// it to the context, or else hands off the expression to
201 /// \c DefaultCtx.
203 /// If \c Context is a PolymorphicFunctionObject such that
204 /// it can be invoked with the tag and children of \c Expr,
205 /// as <tt>ctx(Expr::proto_tag(), child_c\<0\>(expr), child_c\<1\>(expr)...)</tt>,
206 /// then <tt>eval\<Expr, ThisContext\></tt> inherits from
207 /// <tt>callable_eval\<Expr, ThisContext\></tt>. Otherwise,
208 /// <tt>eval\<Expr, ThisContext\></tt> inherits from
209 /// <tt>DefaultCtx::eval\<Expr, Context\></tt>.
210 template<typename Expr, typename ThisContext = Context>
211 struct eval
212 : mpl::if_<
213 detail::is_expr_handled<Expr, Context>
214 , callable_eval<Expr, ThisContext>
215 , typename DefaultCtx::template eval<Expr, Context>
216 >::type
221 #define BOOST_PROTO_CHILD_N_TYPE(Z, N, Expr) \
222 typedef typename proto::result_of::child_c<Expr const &, N>::type BOOST_PP_CAT(child, N); \
223 /**/
225 #define BOOST_PROTO_CHILD_N(Z, N, expr) \
226 proto::child_c<N>(expr) \
227 /**/
229 #define BOOST_PP_ITERATION_PARAMS_1 \
230 (3, (1, BOOST_PROTO_MAX_ARITY, <boost/proto/context/callable.hpp>)) \
231 /**/
233 #include BOOST_PP_ITERATE()
235 #undef BOOST_PROTO_CHILD_N_TYPE
236 #undef BOOST_PROTO_CHILD_N
240 #endif
242 #else
244 #define N BOOST_PP_ITERATION()
246 namespace detail
248 template<typename Context>
249 struct callable_context_wrapper<Context, N>
250 : remove_cv<Context>::type
252 callable_context_wrapper();
253 typedef
254 private_type_ const &fun_type(
255 BOOST_PP_ENUM_PARAMS(
256 BOOST_PP_INC(N)
257 , detail::dont_care BOOST_PP_INTERCEPT
260 operator fun_type *() const;
263 template<typename Expr, typename Context>
264 struct is_expr_handled<Expr, Context, N>
266 static callable_context_wrapper<Context, N> &sctx_;
267 static Expr &sexpr_;
268 static typename Expr::proto_tag &stag_;
270 BOOST_STATIC_CONSTANT(bool, value =
272 sizeof(yes_type) ==
273 sizeof(
274 detail::check_is_expr_handled(
275 (sctx_(
276 stag_
277 BOOST_PP_ENUM_TRAILING(N, BOOST_PROTO_CHILD_N, sexpr_)
278 ), 0)
280 )));
282 typedef mpl::bool_<value> type;
286 namespace context
288 /// \brief A BinaryFunction that accepts a Proto expression and a
289 /// callable context and calls the context with the expression tag
290 /// and children as arguments, effectively fanning the expression
291 /// out.
293 /// <tt>callable_eval\<\></tt> requires that \c Context is a
294 /// PolymorphicFunctionObject that can be invoked with \c Expr's
295 /// tag and children as expressions, as follows:
297 /// \code
298 /// context(Expr::proto_tag(), child_c\<0\>(expr), child_c\<1\>(expr), ...)
299 /// \endcode
300 template<typename Expr, typename Context>
301 struct callable_eval<Expr, Context, N>
303 BOOST_PP_REPEAT(N, BOOST_PROTO_CHILD_N_TYPE, Expr)
305 typedef
306 typename boost::result_of<
307 Context(
308 typename Expr::proto_tag
309 BOOST_PP_ENUM_TRAILING_PARAMS(N, child)
311 >::type
312 result_type;
314 /// \param expr The current expression
315 /// \param context The callable evaluation context
316 /// \return <tt>context(Expr::proto_tag(), child_c\<0\>(expr), child_c\<1\>(expr), ...)</tt>
317 result_type operator ()(Expr &expr, Context &context) const
319 return context(
320 typename Expr::proto_tag()
321 BOOST_PP_ENUM_TRAILING(N, BOOST_PROTO_CHILD_N, expr)
327 #undef N
329 #endif