/////////////////////////////////////////////////////////////////////////////// /// \file callable.hpp /// Definintion of callable_context\<\>, an evaluation context for /// proto::eval() that explodes each node and calls the derived context /// type with the expressions constituents. If the derived context doesn't /// have an overload that handles this node, fall back to some other /// context. // // Copyright 2008 Eric Niebler. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_PROTO_CONTEXT_CALLABLE_HPP_EAN_06_23_2007 #define BOOST_PROTO_CONTEXT_CALLABLE_HPP_EAN_06_23_2007 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // for child_c namespace boost { namespace proto { namespace detail { template struct callable_context_wrapper : remove_cv::type { callable_context_wrapper(); typedef private_type_ fun_type(...); operator fun_type *() const; private: callable_context_wrapper &operator =(callable_context_wrapper const &); }; template yes_type check_is_expr_handled(T const &); no_type check_is_expr_handled(private_type_ const &); template struct is_expr_handled; template struct is_expr_handled { static callable_context_wrapper &sctx_; static Expr &sexpr_; static typename Expr::proto_tag &stag_; static const bool value = sizeof(yes_type) == sizeof( detail::check_is_expr_handled( (sctx_(stag_, proto::value(sexpr_)), 0) ) ); typedef mpl::bool_ type; }; } namespace context { /// \brief A BinaryFunction that accepts a Proto expression and a /// callable context and calls the context with the expression tag /// and children as arguments, effectively fanning the expression /// out. /// /// callable_eval\<\> requires that \c Context is a /// PolymorphicFunctionObject that can be invoked with \c Expr's /// tag and children as expressions, as follows: /// /// \code /// context(Expr::proto_tag(), child_c<0>(expr), child_c<1>(expr), ...) /// \endcode template< typename Expr , typename Context , long Arity // = Expr::proto_arity_c > struct callable_eval {}; /// \brief A BinaryFunction that accepts a Proto expression and a /// callable context and calls the context with the expression tag /// and children as arguments, effectively fanning the expression /// out. /// /// callable_eval\<\> requires that \c Context is a /// PolymorphicFunctionObject that can be invoked with \c Expr's /// tag and children as expressions, as follows: /// /// \code /// context(Expr::proto_tag(), value(expr)) /// \endcode template struct callable_eval { typedef typename proto::result_of::value::type value_type; typedef typename BOOST_PROTO_RESULT_OF< Context(typename Expr::proto_tag, value_type) >::type result_type; /// \param expr The current expression /// \param context The callable evaluation context /// \return context(Expr::proto_tag(), value(expr)) result_type operator ()(Expr &expr, Context &context) const { return context(typename Expr::proto_tag(), proto::value(expr)); } }; /// \brief An evaluation context adaptor that makes authoring a /// context a simple matter of writing function overloads, rather /// then writing template specializations. /// /// callable_context\<\> is a base class that implements /// the context protocol by passing fanned-out expression nodes to /// the derived context, making it easy to customize the handling /// of expression types by writing function overloads. Only those /// expression types needing special handling require explicit /// handling. All others are dispatched to a user-specified /// default context, \c DefaultCtx. /// /// callable_context\<\> is defined simply as: /// /// \code /// template /// struct callable_context /// { /// template /// struct eval /// : mpl::if_< /// is_expr_handled_ // For exposition /// , callable_eval /// , typename DefaultCtx::template eval /// >::type /// {}; /// }; /// \endcode /// /// The Boolean metafunction is_expr_handled_\<\> uses /// metaprogramming tricks to determine whether \c Context has /// an overloaded function call operator that accepts the /// fanned-out constituents of an expression of type \c Expr. /// If so, the handling of the expression is dispatched to /// callable_eval\<\>. If not, it is dispatched to /// the user-specified \c DefaultCtx. /// /// Below is an example of how to use callable_context\<\>: /// /// \code /// // An evaluation context that increments all /// // integer terminals in-place. /// struct increment_ints /// : callable_context< /// increment_ints const // derived context /// , null_context const // fall-back context /// > /// { /// typedef void result_type; /// /// // Handle int terminals here: /// void operator()(proto::tag::terminal, int &i) const /// { /// ++i; /// } /// }; /// \endcode /// /// With \c increment_ints, we can do the following: /// /// \code /// literal i = 0, j = 10; /// proto::eval( i - j * 3.14, increment_ints() ); /// /// assert( i.get() == 1 && j.get() == 11 ); /// \endcode template< typename Context , typename DefaultCtx // = default_context > struct callable_context { /// A BinaryFunction that accepts an \c Expr and a /// \c Context, and either fans out the expression and passes /// it to the context, or else hands off the expression to /// \c DefaultCtx. /// /// If \c Context is a PolymorphicFunctionObject such that /// it can be invoked with the tag and children of \c Expr, /// as ctx(Expr::proto_tag(), child_c\<0\>(expr), child_c\<1\>(expr)...), /// then eval\ inherits from /// callable_eval\. Otherwise, /// eval\ inherits from /// DefaultCtx::eval\. template struct eval : mpl::if_c< detail::is_expr_handled::value , callable_eval , typename DefaultCtx::template eval >::type {}; }; } #include }} #endif