callable.xml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. Copyright 2012 Eric Niebler
  4. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <header name="boost/proto/context/callable.hpp">
  9. <para>Definintion of <computeroutput><classname alt="boost::proto::context::callable_context">proto::context::callable_context&lt;&gt;</classname></computeroutput>,
  10. an evaluation context for <computeroutput><functionname alt="boost::proto::eval">proto::eval()</functionname></computeroutput>
  11. that fans out each node and calls the derived context type with the expressions constituents. If the derived context
  12. doesn't have an overload that handles this node, fall back to some other context. </para>
  13. <namespace name="boost">
  14. <namespace name="proto">
  15. <namespace name="context">
  16. <struct name="callable_eval">
  17. <template>
  18. <template-type-parameter name="Expr"/>
  19. <template-type-parameter name="Context"/>
  20. </template>
  21. <purpose>A BinaryFunction that accepts a Proto expression and a callable context and calls
  22. the context with the expression tag and children as arguments, effectively fanning the
  23. expression out. </purpose>
  24. <description>
  25. <para>
  26. <computeroutput>proto::context::callable_eval&lt;&gt;</computeroutput> requires that
  27. <computeroutput>Context</computeroutput> is a <conceptname>PolymorphicFunctionObject</conceptname>
  28. that can be invoked with <computeroutput>Expr</computeroutput>'s tag and children as
  29. expressions, as follows:
  30. <programlisting>context(typename Expr::proto_tag(), <functionname>proto::child_c</functionname>&lt;0&gt;(expr), ... <functionname>proto::child_c</functionname>&lt;N&gt;(expr))</programlisting>
  31. </para>
  32. </description>
  33. <typedef name="result_type">
  34. <type>typename boost::result_of&lt;
  35. Context(
  36. typename Expr::proto_tag,
  37. typename proto::result_of::child_c&lt;0&gt;::type,
  38. ...
  39. typename proto::result_of::child_c&lt;N&gt;::type,
  40. )&gt;::type
  41. </type>
  42. </typedef>
  43. <method-group name="public member functions">
  44. <method name="operator()" cv="const">
  45. <type>result_type</type>
  46. <parameter name="expr">
  47. <paramtype>Expr &amp;</paramtype>
  48. <description>
  49. <para>The current expression </para>
  50. </description>
  51. </parameter>
  52. <parameter name="context">
  53. <paramtype>Context &amp;</paramtype>
  54. <description>
  55. <para>The callable evaluation context </para>
  56. </description>
  57. </parameter>
  58. <returns>
  59. <para>
  60. <computeroutput>
  61. context(typename Expr::proto_tag(),
  62. <functionname>proto::child_c</functionname>&lt;0&gt;(expr),...
  63. <functionname>proto::child_c</functionname>&lt;N&gt;(expr))
  64. </computeroutput>
  65. </para>
  66. </returns>
  67. </method>
  68. </method-group>
  69. </struct>
  70. <struct name="callable_context">
  71. <template>
  72. <template-type-parameter name="Context"/>
  73. <template-type-parameter name="DefaultCtx">
  74. <default><classname>proto::context::default_context</classname></default>
  75. </template-type-parameter>
  76. </template>
  77. <purpose>An evaluation context adaptor that makes authoring a context a simple matter of
  78. writing function overloads, rather then writing template specializations.</purpose>
  79. <description>
  80. <para>
  81. <computeroutput>proto::callable_context&lt;&gt;</computeroutput> is a base class that
  82. implements the context protocol by passing fanned-out expression nodes to the derived
  83. context, making it easy to customize the handling of expression types by writing function
  84. overloads. Only those expression types needing special handling require explicit handling.
  85. All others are dispatched to a user-specified default context,
  86. <computeroutput>DefaultCtx</computeroutput>.
  87. </para>
  88. <para>
  89. <computeroutput>proto::callable_context&lt;&gt;</computeroutput> is defined simply as:
  90. </para>
  91. <para>
  92. <programlisting>template&lt;typename Context, typename DefaultCtx = default_context&gt;
  93. struct callable_context {
  94. template&lt;typename Expr, typename ThisContext = Context&gt;
  95. struct eval :
  96. mpl::if_&lt;
  97. is_expr_handled_&lt;Expr, Context&gt;, // For exposition
  98. <classname>proto::context::callable_eval</classname>&lt;Expr, ThisContext&gt;,
  99. typename DefaultCtx::template eval&lt;Expr, Context&gt;
  100. &gt;::type
  101. {};
  102. };</programlisting>
  103. </para>
  104. <para>
  105. The Boolean metafunction <computeroutput>is_expr_handled_&lt;&gt;</computeroutput> uses
  106. metaprogramming tricks to determine whether <computeroutput>Context</computeroutput> has
  107. an overloaded function call operator that accepts the fanned-out constituents of an
  108. expression of type <computeroutput>Expr</computeroutput>. If so, the handling of the
  109. expression is dispatched to
  110. <computeroutput><classname>proto::context::callable_eval&lt;&gt;</classname></computeroutput>.
  111. If not, it is dispatched to the user-specified <computeroutput>DefaultCtx</computeroutput>.
  112. </para>
  113. <para>
  114. <emphasis role="bold">Example:</emphasis>
  115. </para>
  116. <para>
  117. <programlisting>// An evaluation context that increments all
  118. // integer terminals in-place.
  119. struct increment_ints :
  120. <classname>proto::context::callable_context</classname>&lt;
  121. increment_ints const // derived context
  122. <classname>proto::context::null_context</classname> const // fall-back context
  123. &gt;
  124. {
  125. typedef void result_type;
  126. // Handle int terminals here:
  127. void operator()(proto::tag::terminal, int &amp;i) const
  128. {
  129. ++i;
  130. }
  131. };</programlisting>
  132. </para>
  133. <para>
  134. With <computeroutput>increment_ints</computeroutput>, we can do the following:
  135. </para>
  136. <para>
  137. <programlisting><classname>proto::literal</classname>&lt;int&gt; i = 0, j = 10;
  138. proto::eval( i - j * 3.14, increment_ints() );
  139. assert( i.get() == 1 &amp;&amp; j.get() == 11 );</programlisting>
  140. </para>
  141. </description>
  142. <struct name="eval">
  143. <template>
  144. <template-type-parameter name="Expr"/>
  145. <template-type-parameter name="ThisContext">
  146. <default>Context</default>
  147. </template-type-parameter>
  148. </template>
  149. <description>
  150. <para>
  151. A BinaryFunction that accepts an <computeroutput>Expr</computeroutput> and a
  152. <computeroutput>Context</computeroutput>, and either fans out the expression and passes
  153. it to the context, or else hands off the expression to <computeroutput>DefaultCtx</computeroutput>.
  154. </para>
  155. <para>
  156. If <computeroutput>Context</computeroutput> is a <conceptname>PolymorphicFunctionObject</conceptname>
  157. such that it can be invoked with the tag and children of <computeroutput>Expr</computeroutput>, as
  158. <computeroutput>ctx(typename Expr::proto_tag(), child_c&lt;0&gt;(expr),... child_c&lt;N&gt;(expr))</computeroutput>,
  159. then <computeroutput>eval&lt;Expr, ThisContext&gt;</computeroutput> inherits from
  160. <computeroutput><classname>proto::context::callable_eval</classname>&lt;Expr, ThisContext&gt;</computeroutput>.
  161. Otherwise, <computeroutput>eval&lt;Expr, ThisContext&gt;</computeroutput> inherits from
  162. <computeroutput>DefaultCtx::eval&lt;Expr, Context&gt;</computeroutput>.
  163. </para>
  164. </description>
  165. <inherit><type><replaceable>see-below</replaceable></type></inherit>
  166. </struct>
  167. </struct>
  168. </namespace>
  169. </namespace>
  170. </namespace>
  171. </header>