• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Actors in Detail</title>
5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Chapter 1. Phoenix 3.2.0">
8<link rel="up" href="../inside.html" title="Inside Phoenix">
9<link rel="prev" href="../inside.html" title="Inside Phoenix">
10<link rel="next" href="expression.html" title="Phoenix Expressions">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="../inside.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../inside.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="expression.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="phoenix.inside.actor"></a><a class="link" href="actor.html" title="Actors in Detail">Actors in Detail</a>
28</h3></div></div></div>
29<h5>
30<a name="phoenix.inside.actor.h0"></a>
31        <span class="phrase"><a name="phoenix.inside.actor.actor"></a></span><a class="link" href="actor.html#phoenix.inside.actor.actor">Actor</a>
32      </h5>
33<p>
34        The main concept is the <code class="computeroutput"><span class="identifier">Actor</span></code>.
35        An <code class="computeroutput"><span class="identifier">Actor</span></code> is a model of the
36        <a href="http://www.boost.org/doc/libs/release/libs/fusion/doc/html/fusion/functional/concepts/poly.html" target="_top">Polymorphic
37        Function Object</a> concept (that can accept 0 to N arguments (where
38        N is a predefined maximum).
39      </p>
40<p>
41        An <code class="computeroutput"><span class="identifier">Actor</span></code> contains a valid
42        Phoenix Expression, a call to one of the function call operator overloads,
43        starts the evaluation process.
44      </p>
45<div class="note"><table border="0" summary="Note">
46<tr>
47<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
48<th align="left">Note</th>
49</tr>
50<tr><td align="left" valign="top"><p>
51          You can set <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LIMIT</span></code>,
52          the predefined maximum arity an actor can take. By default, <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LIMIT</span></code> is set to 10.
53        </p></td></tr>
54</table></div>
55<p>
56        The <code class="computeroutput"><span class="identifier">actor</span></code> template class
57        models the <code class="computeroutput"><span class="identifier">Actor</span></code> concept:
58      </p>
59<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Expr</span><span class="special">&gt;</span>
60<span class="keyword">struct</span> <span class="identifier">actor</span>
61<span class="special">{</span>
62    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Sig</span><span class="special">&gt;</span>
63    <span class="keyword">struct</span> <span class="identifier">result</span><span class="special">;</span>
64
65    <span class="keyword">typename</span> <span class="identifier">result_of</span><span class="special">::</span><span class="identifier">actor</span><span class="special">&lt;</span><span class="identifier">Expr</span><span class="special">&gt;::</span><span class="identifier">type</span>
66    <span class="keyword">operator</span><span class="special">()()</span> <span class="keyword">const</span><span class="special">;</span>
67
68    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T0</span><span class="special">&gt;</span>
69    <span class="keyword">typename</span> <span class="identifier">result_of</span><span class="special">::</span><span class="identifier">actor</span><span class="special">&lt;</span><span class="identifier">Expr</span><span class="special">,</span> <span class="identifier">T0</span> <span class="special">&amp;&gt;::</span><span class="identifier">type</span>
70    <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">T0</span><span class="special">&amp;</span> <span class="identifier">_0</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
71
72    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T0</span><span class="special">&gt;</span>
73    <span class="keyword">typename</span> <span class="identifier">result_of</span><span class="special">::</span><span class="identifier">actor</span><span class="special">&lt;</span><span class="identifier">Expr</span><span class="special">,</span> <span class="identifier">T0</span> <span class="keyword">const</span> <span class="special">&amp;&gt;::</span><span class="identifier">type</span>
74    <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">T0</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">_0</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
75
76    <span class="comment">//...</span>
77<span class="special">};</span>
78</pre>
79<div class="table">
80<a name="phoenix.inside.actor.t0"></a><p class="title"><b>Table 1.9. Actor Concept Requirements</b></p>
81<div class="table-contents"><table class="table" summary="Actor Concept Requirements">
82<colgroup>
83<col>
84<col>
85</colgroup>
86<thead><tr>
87<th>
88                <p>
89                  Expression
90                </p>
91              </th>
92<th>
93                <p>
94                  Semantics
95                </p>
96              </th>
97</tr></thead>
98<tbody>
99<tr>
100<td>
101                <p>
102                  <code class="computeroutput"><span class="identifier">actor</span><span class="special">(</span><span class="identifier">arg0</span><span class="special">,</span>
103                  <span class="identifier">arg1</span><span class="special">,</span>
104                  <span class="special">...,</span> <span class="identifier">argN</span><span class="special">)</span></code>
105                </p>
106              </td>
107<td>
108                <p>
109                  Function call operators to start the evaluation
110                </p>
111              </td>
112</tr>
113<tr>
114<td>
115                <p>
116                  <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span><span class="identifier">Actor</span><span class="special">&lt;</span><span class="identifier">Expr</span><span class="special">&gt;(</span><span class="identifier">Arg0</span><span class="special">,</span>
117                  <span class="identifier">Arg1</span><span class="special">,</span>
118                  <span class="special">...,</span> <span class="identifier">ArgN</span><span class="special">)&gt;::</span><span class="identifier">type</span></code>
119                </p>
120              </td>
121<td>
122                <p>
123                  Result of the evaluation
124                </p>
125              </td>
126</tr>
127<tr>
128<td>
129                <p>
130                  <code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">actor</span><span class="special">&lt;</span><span class="identifier">Expr</span><span class="special">,</span>
131                  <span class="identifier">Arg0</span><span class="special">,</span>
132                  <span class="identifier">Arg1</span><span class="special">,</span>
133                  <span class="special">...,</span> <span class="identifier">ArgN</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
134                </p>
135              </td>
136<td>
137                <p>
138                  Result of the evaluation
139                </p>
140              </td>
141</tr>
142</tbody>
143</table></div>
144</div>
145<br class="table-break"><h5>
146<a name="phoenix.inside.actor.h1"></a>
147        <span class="phrase"><a name="phoenix.inside.actor.function_call_operators"></a></span><a class="link" href="actor.html#phoenix.inside.actor.function_call_operators">Function
148        Call Operators</a>
149      </h5>
150<p>
151        There are 2*N function call operators for 0 to N arguments (N == <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LIMIT</span></code>). The actor class accepts
152        the arguments and forwards the arguments to the default evaluation action.
153      </p>
154<p>
155        Additionally, there exist function call operators accepting permutations
156        of const and non-const references. These operators are created for all N
157        &lt;= <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_PERFECT_FORWARD_LIMIT</span></code>
158        (which defaults to 3).
159      </p>
160<div class="note"><table border="0" summary="Note">
161<tr>
162<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
163<th align="left">Note</th>
164</tr>
165<tr><td align="left" valign="top">
166<p>
167          <span class="bold"><strong>Forwarding Function Problem</strong></span>
168        </p>
169<p>
170          There is a known issue with current C++ called the "<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm" target="_top">Forwarding
171          Function Problem</a>". The problem is that given an arbitrary
172          function <code class="computeroutput"><span class="identifier">F</span></code>, using current
173          C++ language rules, one cannot create a forwarding function <code class="computeroutput"><span class="identifier">FF</span></code> that transparently assumes the arguments
174          of <code class="computeroutput"><span class="identifier">F</span></code>.
175        </p>
176</td></tr>
177</table></div>
178<h5>
179<a name="phoenix.inside.actor.h2"></a>
180        <span class="phrase"><a name="phoenix.inside.actor.context"></a></span><a class="link" href="actor.html#phoenix.inside.actor.context">Context</a>
181      </h5>
182<p>
183        On an actor function call, before calling the evaluation function, the actor
184        created a <span class="bold"><strong>context</strong></span>. This context consists
185        of an <code class="computeroutput"><span class="identifier">Environment</span></code> and an
186        <code class="computeroutput"><span class="identifier">Action</span></code> part. These contain
187        all information necessary to evaluate the given expression.
188      </p>
189<div class="table">
190<a name="phoenix.inside.actor.t1"></a><p class="title"><b>Table 1.10. Context Concept Requirements</b></p>
191<div class="table-contents"><table class="table" summary="Context Concept Requirements">
192<colgroup>
193<col>
194<col>
195</colgroup>
196<thead><tr>
197<th>
198                <p>
199                  Expression
200                </p>
201              </th>
202<th>
203                <p>
204                  Semantics
205                </p>
206              </th>
207</tr></thead>
208<tbody>
209<tr>
210<td>
211                <p>
212                  <code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">context</span><span class="special">&lt;</span><span class="identifier">Env</span><span class="special">,</span>
213                  <span class="identifier">Actions</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
214                </p>
215              </td>
216<td>
217                <p>
218                  Type of a Context
219                </p>
220              </td>
221</tr>
222<tr>
223<td>
224                <p>
225                  <code class="computeroutput"><span class="identifier">context</span><span class="special">(</span><span class="identifier">e</span><span class="special">,</span>
226                  <span class="identifier">a</span><span class="special">)</span></code>
227                </p>
228              </td>
229<td>
230                <p>
231                  A Context containing environment <code class="computeroutput"><span class="identifier">e</span></code>
232                  and actions <code class="computeroutput"><span class="identifier">a</span></code>
233                </p>
234              </td>
235</tr>
236<tr>
237<td>
238                <p>
239                  <code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">env</span><span class="special">&lt;</span><span class="identifier">Context</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
240                </p>
241              </td>
242<td>
243                <p>
244                  Type of the contained Environment
245                </p>
246              </td>
247</tr>
248<tr>
249<td>
250                <p>
251                  <code class="computeroutput"><span class="identifier">env</span><span class="special">(</span><span class="identifier">ctx</span><span class="special">)</span></code>
252                </p>
253              </td>
254<td>
255                <p>
256                  The environment
257                </p>
258              </td>
259</tr>
260<tr>
261<td>
262                <p>
263                  <code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">actions</span><span class="special">&lt;</span><span class="identifier">Context</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
264                </p>
265              </td>
266<td>
267                <p>
268                  Type of the contained Actions
269                </p>
270              </td>
271</tr>
272<tr>
273<td>
274                <p>
275                  <code class="computeroutput"><span class="identifier">actions</span><span class="special">(</span><span class="identifier">ctx</span><span class="special">)</span></code>
276                </p>
277              </td>
278<td>
279                <p>
280                  The actions
281                </p>
282              </td>
283</tr>
284</tbody>
285</table></div>
286</div>
287<br class="table-break"><h5>
288<a name="phoenix.inside.actor.h3"></a>
289        <span class="phrase"><a name="phoenix.inside.actor.environment"></a></span><a class="link" href="actor.html#phoenix.inside.actor.environment">Environment</a>
290      </h5>
291<p>
292        The Environment is a model of <a href="http://www.boost.org/doc/libs/release/libs/fusion/doc/html/fusion/sequence/concepts/random_access_sequence.html" target="_top">Random
293        Access Sequence</a>.
294      </p>
295<p>
296        The arguments passed to the actor's function call operator are collected
297        inside the Environment:
298      </p>
299<p>
300        <span class="inlinemediaobject"><img src="../../images/funnel_in.png"></span>
301      </p>
302<p>
303        Other parts of the library (e.g. the scope module) extends the <code class="computeroutput"><span class="identifier">Environment</span></code> concept to hold other information
304        such as local variables, etc.
305      </p>
306<h5>
307<a name="phoenix.inside.actor.h4"></a>
308        <span class="phrase"><a name="phoenix.inside.actor.actions"></a></span><a class="link" href="actor.html#phoenix.inside.actor.actions">Actions</a>
309      </h5>
310<p>
311        Actions is the part of Phoenix which are responsible for giving the actual
312        expressions a specific behaviour. During the traversal of the Phoenix Expression
313        Tree these actions are called whenever a specified rule in the grammar matches.
314      </p>
315<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">actions</span>
316<span class="special">{</span>
317    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Rule</span><span class="special">&gt;</span>
318    <span class="keyword">struct</span> <span class="identifier">when</span><span class="special">;</span>
319<span class="special">};</span>
320</pre>
321<p>
322        The nested <code class="computeroutput"><span class="identifier">when</span></code> template
323        is required to be <a href="http://www.boost.org/doc/libs/release/doc/html/PrimitiveTransform.html" target="_top">Proto
324        Primitive Transform</a>. No worries, you don't have to learn <a href="http://www.boost.org/doc/libs/release/libs/proto/index.html" target="_top">Boost.Proto</a>
325        just yet! Phoenix provides some wrappers to let you define simple actions
326        without the need to dive deep into proto.
327      </p>
328<p>
329        Phoenix ships with a predefined <code class="computeroutput"><span class="identifier">default_actions</span></code>
330        class that evaluates the expressions with C++ semantics:
331      </p>
332<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">default_actions</span>
333<span class="special">{</span>
334    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Rule</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Dummy</span> <span class="special">=</span> <span class="keyword">void</span><span class="special">&gt;</span>
335    <span class="keyword">struct</span> <span class="identifier">when</span>
336        <span class="special">:</span> <span class="identifier">proto</span><span class="special">::</span><span class="identifier">_default</span><span class="special">&lt;</span><span class="identifier">meta_grammar</span><span class="special">&gt;</span>
337    <span class="special">{};</span>
338<span class="special">};</span>
339</pre>
340<p>
341        For more information on how to use the default_actions class and how to attach
342        custom actions to the evaluation process, see <a class="link" href="actions.html" title="More on Actions">more
343        on actions</a>.
344      </p>
345<h5>
346<a name="phoenix.inside.actor.h5"></a>
347        <span class="phrase"><a name="phoenix.inside.actor.evaluation"></a></span><a class="link" href="actor.html#phoenix.inside.actor.evaluation">Evaluation</a>
348      </h5>
349<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">evaluator</span>
350<span class="special">{</span>
351    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Expr</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Context</span><span class="special">&gt;</span>
352    <span class="emphasis"><em>unspecified</em></span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Expr</span> <span class="special">&amp;,</span> <span class="identifier">Context</span> <span class="special">&amp;);</span>
353<span class="special">};</span>
354
355<span class="identifier">evaluator</span> <span class="keyword">const</span> <span class="identifier">eval</span> <span class="special">=</span> <span class="special">{};</span>
356</pre>
357<p>
358        The evaluation of a Phoenix expression is started by a call to the function
359        call operator of <code class="computeroutput"><span class="identifier">evaluator</span></code>.
360      </p>
361<p>
362        The evaluator is called by the <code class="computeroutput"><span class="identifier">actor</span></code>
363        function operator overloads after the context is built up. For reference,
364        here is a typical <code class="computeroutput"><span class="identifier">actor</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()</span></code>
365        that accepts two arguments:
366      </p>
367<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T0</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T1</span><span class="special">&gt;</span>
368<span class="keyword">typename</span> <span class="identifier">result_of</span><span class="special">::</span><span class="identifier">actor</span><span class="special">&lt;</span><span class="identifier">Expr</span><span class="special">,</span> <span class="identifier">T0</span> <span class="special">&amp;,</span> <span class="identifier">T1</span> <span class="special">&amp;&gt;::</span><span class="identifier">type</span>
369<span class="keyword">operator</span><span class="special">()(</span><span class="identifier">T0</span> <span class="special">&amp;</span><span class="identifier">t0</span><span class="special">,</span> <span class="identifier">T1</span> <span class="special">&amp;</span><span class="identifier">t1</span><span class="special">)</span> <span class="keyword">const</span>
370<span class="special">{</span>
371    <span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector2</span><span class="special">&lt;</span><span class="identifier">T0</span> <span class="special">&amp;,</span> <span class="identifier">T1</span> <span class="special">&amp;&gt;</span> <span class="identifier">env</span><span class="special">(</span><span class="identifier">t0</span><span class="special">,</span> <span class="identifier">t1</span><span class="special">);</span>
372
373    <span class="keyword">return</span> <span class="identifier">eval</span><span class="special">(*</span><span class="keyword">this</span><span class="special">,</span> <span class="identifier">context</span><span class="special">(</span><span class="identifier">env</span><span class="special">,</span> <span class="identifier">default_actions</span><span class="special">()));</span>
374<span class="special">}</span>
375</pre>
376<h5>
377<a name="phoenix.inside.actor.h6"></a>
378        <span class="phrase"><a name="phoenix.inside.actor.result_of__actor"></a></span><a class="link" href="actor.html#phoenix.inside.actor.result_of__actor">result_of::actor</a>
379      </h5>
380<p>
381        For reasons of symmetry to the family of <code class="computeroutput"><span class="identifier">actor</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()</span></code> there is a special metafunction usable
382        for actor result type calculation named <code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">actor</span></code>.
383        This metafunction allows us to directly specify the types of the parameters
384        to be passed to the <code class="computeroutput"><span class="identifier">actor</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()</span></code> function. Here's a typical <code class="computeroutput"><span class="identifier">actor_result</span></code> that accepts two arguments:
385      </p>
386<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">result_of</span>
387<span class="special">{</span>
388    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Expr</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T0</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T1</span><span class="special">&gt;</span>
389    <span class="keyword">struct</span> <span class="identifier">actor</span>
390    <span class="special">{</span>
391        <span class="keyword">typedef</span> <span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector2</span><span class="special">&lt;</span><span class="identifier">T0</span><span class="special">,</span> <span class="identifier">T1</span><span class="special">&gt;</span>                                           <span class="identifier">env_tpe</span><span class="special">;</span>
392        <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">result_of</span><span class="special">::</span><span class="identifier">context</span><span class="special">&lt;</span><span class="identifier">env_type</span><span class="special">,</span> <span class="identifier">default_actions</span><span class="special">&gt;::</span><span class="identifier">type</span>      <span class="identifier">ctx_type</span>
393        <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span><span class="identifier">evaluator</span><span class="special">(</span><span class="identifier">Expr</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">ctx_type</span><span class="special">)&gt;::</span><span class="identifier">type</span> <span class="identifier">type</span><span class="special">;</span>
394    <span class="special">};</span>
395<span class="special">}</span>
396</pre>
397</div>
398<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
399<td align="left"></td>
400<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2010, 2014, 2015 Joel de Guzman, Dan Marsden, Thomas
401      Heller, John Fletcher<p>
402        Distributed under the Boost Software License, Version 1.0. (See accompanying
403        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
404      </p>
405</div></td>
406</tr></table>
407<hr>
408<div class="spirit-nav">
409<a accesskey="p" href="../inside.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../inside.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="expression.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
410</div>
411</body>
412</html>
413