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. private:
  43. callable_context_wrapper &operator =(callable_context_wrapper const &);
  44. };
  45. template<typename T>
  46. yes_type check_is_expr_handled(T const &);
  47. no_type check_is_expr_handled(private_type_ const &);
  48. template<typename Expr, typename Context, long Arity = Expr::proto_arity_c>
  49. struct is_expr_handled;
  50. template<typename Expr, typename Context>
  51. struct is_expr_handled<Expr, Context, 0>
  52. {
  53. static callable_context_wrapper<Context> &sctx_;
  54. static Expr &sexpr_;
  55. static typename Expr::proto_tag &stag_;
  56. static const bool value =
  57. sizeof(yes_type) ==
  58. sizeof(
  59. detail::check_is_expr_handled(
  60. (sctx_(stag_, proto::value(sexpr_)), 0)
  61. )
  62. );
  63. typedef mpl::bool_<value> type;
  64. };
  65. }
  66. namespace context
  67. {
  68. /// \brief A BinaryFunction that accepts a Proto expression and a
  69. /// callable context and calls the context with the expression tag
  70. /// and children as arguments, effectively fanning the expression
  71. /// out.
  72. ///
  73. /// <tt>callable_eval\<\></tt> requires that \c Context is a
  74. /// PolymorphicFunctionObject that can be invoked with \c Expr's
  75. /// tag and children as expressions, as follows:
  76. ///
  77. /// \code
  78. /// context(Expr::proto_tag(), child_c<0>(expr), child_c<1>(expr), ...)
  79. /// \endcode
  80. template<
  81. typename Expr
  82. , typename Context
  83. , long Arity // = Expr::proto_arity_c
  84. >
  85. struct callable_eval
  86. {};
  87. /// \brief A BinaryFunction that accepts a Proto expression and a
  88. /// callable context and calls the context with the expression tag
  89. /// and children as arguments, effectively fanning the expression
  90. /// out.
  91. ///
  92. /// <tt>callable_eval\<\></tt> requires that \c Context is a
  93. /// PolymorphicFunctionObject that can be invoked with \c Expr's
  94. /// tag and children as expressions, as follows:
  95. ///
  96. /// \code
  97. /// context(Expr::proto_tag(), value(expr))
  98. /// \endcode
  99. template<typename Expr, typename Context>
  100. struct callable_eval<Expr, Context, 0>
  101. {
  102. typedef typename proto::result_of::value<Expr const &>::type value_type;
  103. typedef
  104. typename BOOST_PROTO_RESULT_OF<
  105. Context(typename Expr::proto_tag, value_type)
  106. >::type
  107. result_type;
  108. /// \param expr The current expression
  109. /// \param context The callable evaluation context
  110. /// \return <tt>context(Expr::proto_tag(), value(expr))</tt>
  111. result_type operator ()(Expr &expr, Context &context) const
  112. {
  113. return context(typename Expr::proto_tag(), proto::value(expr));
  114. }
  115. };
  116. /// \brief An evaluation context adaptor that makes authoring a
  117. /// context a simple matter of writing function overloads, rather
  118. /// then writing template specializations.
  119. ///
  120. /// <tt>callable_context\<\></tt> is a base class that implements
  121. /// the context protocol by passing fanned-out expression nodes to
  122. /// the derived context, making it easy to customize the handling
  123. /// of expression types by writing function overloads. Only those
  124. /// expression types needing special handling require explicit
  125. /// handling. All others are dispatched to a user-specified
  126. /// default context, \c DefaultCtx.
  127. ///
  128. /// <tt>callable_context\<\></tt> is defined simply as:
  129. ///
  130. /// \code
  131. /// template<typename Context, typename DefaultCtx = default_context>
  132. /// struct callable_context
  133. /// {
  134. /// template<typename Expr, typename ThisContext = Context>
  135. /// struct eval
  136. /// : mpl::if_<
  137. /// is_expr_handled_<Expr, Context> // For exposition
  138. /// , callable_eval<Expr, ThisContext>
  139. /// , typename DefaultCtx::template eval<Expr, Context>
  140. /// >::type
  141. /// {};
  142. /// };
  143. /// \endcode
  144. ///
  145. /// The Boolean metafunction <tt>is_expr_handled_\<\></tt> uses
  146. /// metaprogramming tricks to determine whether \c Context has
  147. /// an overloaded function call operator that accepts the
  148. /// fanned-out constituents of an expression of type \c Expr.
  149. /// If so, the handling of the expression is dispatched to
  150. /// <tt>callable_eval\<\></tt>. If not, it is dispatched to
  151. /// the user-specified \c DefaultCtx.
  152. ///
  153. /// Below is an example of how to use <tt>callable_context\<\></tt>:
  154. ///
  155. /// \code
  156. /// // An evaluation context that increments all
  157. /// // integer terminals in-place.
  158. /// struct increment_ints
  159. /// : callable_context<
  160. /// increment_ints const // derived context
  161. /// , null_context const // fall-back context
  162. /// >
  163. /// {
  164. /// typedef void result_type;
  165. ///
  166. /// // Handle int terminals here:
  167. /// void operator()(proto::tag::terminal, int &i) const
  168. /// {
  169. /// ++i;
  170. /// }
  171. /// };
  172. /// \endcode
  173. ///
  174. /// With \c increment_ints, we can do the following:
  175. ///
  176. /// \code
  177. /// literal<int> i = 0, j = 10;
  178. /// proto::eval( i - j * 3.14, increment_ints() );
  179. ///
  180. /// assert( i.get() == 1 && j.get() == 11 );
  181. /// \endcode
  182. template<
  183. typename Context
  184. , typename DefaultCtx // = default_context
  185. >
  186. struct callable_context
  187. {
  188. /// A BinaryFunction that accepts an \c Expr and a
  189. /// \c Context, and either fans out the expression and passes
  190. /// it to the context, or else hands off the expression to
  191. /// \c DefaultCtx.
  192. ///
  193. /// If \c Context is a PolymorphicFunctionObject such that
  194. /// it can be invoked with the tag and children of \c Expr,
  195. /// as <tt>ctx(Expr::proto_tag(), child_c\<0\>(expr), child_c\<1\>(expr)...)</tt>,
  196. /// then <tt>eval\<Expr, ThisContext\></tt> inherits from
  197. /// <tt>callable_eval\<Expr, ThisContext\></tt>. Otherwise,
  198. /// <tt>eval\<Expr, ThisContext\></tt> inherits from
  199. /// <tt>DefaultCtx::eval\<Expr, Context\></tt>.
  200. template<typename Expr, typename ThisContext = Context>
  201. struct eval
  202. : mpl::if_c<
  203. detail::is_expr_handled<Expr, Context>::value
  204. , callable_eval<Expr, ThisContext>
  205. , typename DefaultCtx::template eval<Expr, Context>
  206. >::type
  207. {};
  208. };
  209. }
  210. #include <boost/proto/context/detail/callable_eval.hpp>
  211. }}
  212. #endif