callable.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file callable.hpp
  3. /// Definintion of callable_context\<\>, an evaluation context for
  4. /// proto::eval() that explodes each node and calls the derived context
  5. /// type with the expressions constituents. If the derived context doesn't
  6. /// have an overload that handles this node, fall back to some other
  7. /// context.
  8. //
  9. // Copyright 2008 Eric Niebler. Distributed under the Boost
  10. // Software License, Version 1.0. (See accompanying file
  11. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_PROTO_CONTEXT_CALLABLE_HPP_EAN_06_23_2007
  13. #define BOOST_PROTO_CONTEXT_CALLABLE_HPP_EAN_06_23_2007
  14. #include <boost/config.hpp>
  15. #include <boost/detail/workaround.hpp>
  16. #include <boost/preprocessor/cat.hpp>
  17. #include <boost/preprocessor/iteration/iterate.hpp>
  18. #include <boost/preprocessor/facilities/intercept.hpp>
  19. #include <boost/preprocessor/repetition/repeat.hpp>
  20. #include <boost/preprocessor/repetition/enum_params.hpp>
  21. #include <boost/preprocessor/repetition/enum_trailing.hpp>
  22. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  23. #include <boost/preprocessor/arithmetic/inc.hpp>
  24. #include <boost/preprocessor/selection/max.hpp>
  25. #include <boost/mpl/if.hpp>
  26. #include <boost/mpl/bool.hpp>
  27. #include <boost/utility/result_of.hpp>
  28. #include <boost/type_traits/remove_cv.hpp>
  29. #include <boost/proto/proto_fwd.hpp>
  30. #include <boost/proto/traits.hpp> // for child_c
  31. namespace boost { namespace proto
  32. {
  33. namespace detail
  34. {
  35. template<typename Context>
  36. struct callable_context_wrapper
  37. : remove_cv<Context>::type
  38. {
  39. callable_context_wrapper();
  40. typedef private_type_ fun_type(...);
  41. operator fun_type *() const;
  42. BOOST_DELETED_FUNCTION(callable_context_wrapper &operator =(callable_context_wrapper const &))
  43. };
  44. template<typename T>
  45. yes_type check_is_expr_handled(T const &);
  46. no_type check_is_expr_handled(private_type_ const &);
  47. template<typename Expr, typename Context, long Arity = Expr::proto_arity_c>
  48. struct is_expr_handled;
  49. template<typename Expr, typename Context>
  50. struct is_expr_handled<Expr, Context, 0>
  51. {
  52. static callable_context_wrapper<Context> &sctx_;
  53. static Expr &sexpr_;
  54. static typename Expr::proto_tag &stag_;
  55. static const bool value =
  56. sizeof(yes_type) ==
  57. sizeof(
  58. detail::check_is_expr_handled(
  59. (sctx_(stag_, proto::value(sexpr_)), 0)
  60. )
  61. );
  62. typedef mpl::bool_<value> type;
  63. };
  64. }
  65. namespace context
  66. {
  67. /// \brief A BinaryFunction that accepts a Proto expression and a
  68. /// callable context and calls the context with the expression tag
  69. /// and children as arguments, effectively fanning the expression
  70. /// out.
  71. ///
  72. /// <tt>callable_eval\<\></tt> requires that \c Context is a
  73. /// PolymorphicFunctionObject that can be invoked with \c Expr's
  74. /// tag and children as expressions, as follows:
  75. ///
  76. /// \code
  77. /// context(Expr::proto_tag(), child_c<0>(expr), child_c<1>(expr), ...)
  78. /// \endcode
  79. template<
  80. typename Expr
  81. , typename Context
  82. , long Arity // = Expr::proto_arity_c
  83. >
  84. struct callable_eval
  85. {};
  86. /// \brief A BinaryFunction that accepts a Proto expression and a
  87. /// callable context and calls the context with the expression tag
  88. /// and children as arguments, effectively fanning the expression
  89. /// out.
  90. ///
  91. /// <tt>callable_eval\<\></tt> requires that \c Context is a
  92. /// PolymorphicFunctionObject that can be invoked with \c Expr's
  93. /// tag and children as expressions, as follows:
  94. ///
  95. /// \code
  96. /// context(Expr::proto_tag(), value(expr))
  97. /// \endcode
  98. template<typename Expr, typename Context>
  99. struct callable_eval<Expr, Context, 0>
  100. {
  101. typedef typename proto::result_of::value<Expr const &>::type value_type;
  102. typedef
  103. typename BOOST_PROTO_RESULT_OF<
  104. Context(typename Expr::proto_tag, value_type)
  105. >::type
  106. result_type;
  107. /// \param expr The current expression
  108. /// \param context The callable evaluation context
  109. /// \return <tt>context(Expr::proto_tag(), value(expr))</tt>
  110. result_type operator ()(Expr &expr, Context &context) const
  111. {
  112. return context(typename Expr::proto_tag(), proto::value(expr));
  113. }
  114. };
  115. /// \brief An evaluation context adaptor that makes authoring a
  116. /// context a simple matter of writing function overloads, rather
  117. /// then writing template specializations.
  118. ///
  119. /// <tt>callable_context\<\></tt> is a base class that implements
  120. /// the context protocol by passing fanned-out expression nodes to
  121. /// the derived context, making it easy to customize the handling
  122. /// of expression types by writing function overloads. Only those
  123. /// expression types needing special handling require explicit
  124. /// handling. All others are dispatched to a user-specified
  125. /// default context, \c DefaultCtx.
  126. ///
  127. /// <tt>callable_context\<\></tt> is defined simply as:
  128. ///
  129. /// \code
  130. /// template<typename Context, typename DefaultCtx = default_context>
  131. /// struct callable_context
  132. /// {
  133. /// template<typename Expr, typename ThisContext = Context>
  134. /// struct eval
  135. /// : mpl::if_<
  136. /// is_expr_handled_<Expr, Context> // For exposition
  137. /// , callable_eval<Expr, ThisContext>
  138. /// , typename DefaultCtx::template eval<Expr, Context>
  139. /// >::type
  140. /// {};
  141. /// };
  142. /// \endcode
  143. ///
  144. /// The Boolean metafunction <tt>is_expr_handled_\<\></tt> uses
  145. /// metaprogramming tricks to determine whether \c Context has
  146. /// an overloaded function call operator that accepts the
  147. /// fanned-out constituents of an expression of type \c Expr.
  148. /// If so, the handling of the expression is dispatched to
  149. /// <tt>callable_eval\<\></tt>. If not, it is dispatched to
  150. /// the user-specified \c DefaultCtx.
  151. ///
  152. /// Below is an example of how to use <tt>callable_context\<\></tt>:
  153. ///
  154. /// \code
  155. /// // An evaluation context that increments all
  156. /// // integer terminals in-place.
  157. /// struct increment_ints
  158. /// : callable_context<
  159. /// increment_ints const // derived context
  160. /// , null_context const // fall-back context
  161. /// >
  162. /// {
  163. /// typedef void result_type;
  164. ///
  165. /// // Handle int terminals here:
  166. /// void operator()(proto::tag::terminal, int &i) const
  167. /// {
  168. /// ++i;
  169. /// }
  170. /// };
  171. /// \endcode
  172. ///
  173. /// With \c increment_ints, we can do the following:
  174. ///
  175. /// \code
  176. /// literal<int> i = 0, j = 10;
  177. /// proto::eval( i - j * 3.14, increment_ints() );
  178. ///
  179. /// assert( i.get() == 1 && j.get() == 11 );
  180. /// \endcode
  181. template<
  182. typename Context
  183. , typename DefaultCtx // = default_context
  184. >
  185. struct callable_context
  186. {
  187. /// A BinaryFunction that accepts an \c Expr and a
  188. /// \c Context, and either fans out the expression and passes
  189. /// it to the context, or else hands off the expression to
  190. /// \c DefaultCtx.
  191. ///
  192. /// If \c Context is a PolymorphicFunctionObject such that
  193. /// it can be invoked with the tag and children of \c Expr,
  194. /// as <tt>ctx(Expr::proto_tag(), child_c\<0\>(expr), child_c\<1\>(expr)...)</tt>,
  195. /// then <tt>eval\<Expr, ThisContext\></tt> inherits from
  196. /// <tt>callable_eval\<Expr, ThisContext\></tt>. Otherwise,
  197. /// <tt>eval\<Expr, ThisContext\></tt> inherits from
  198. /// <tt>DefaultCtx::eval\<Expr, Context\></tt>.
  199. template<typename Expr, typename ThisContext = Context>
  200. struct eval
  201. : mpl::if_c<
  202. detail::is_expr_handled<Expr, Context>::value
  203. , callable_eval<Expr, ThisContext>
  204. , typename DefaultCtx::template eval<Expr, Context>
  205. >::type
  206. {};
  207. };
  208. }
  209. #include <boost/proto/context/detail/callable_eval.hpp>
  210. }}
  211. #endif