• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Generator Directives Forcing Atomic Extraction (as&lt;T&gt;, as_string[], as_wstring[])</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="Spirit 2.5.8">
8<link rel="up" href="../directive.html" title="Generator Directives">
9<link rel="prev" href="columns.html" title="Generator Directive Separating Output Into Columns (columns[])">
10<link rel="next" href="../nonterminal.html" title="Nonterminal Generators">
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="columns.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../directive.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="../nonterminal.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h5 class="title">
27<a name="spirit.karma.reference.directive.as"></a><a class="link" href="as.html" title="Generator Directives Forcing Atomic Extraction (as&lt;T&gt;, as_string[], as_wstring[])">Generator Directives
28          Forcing Atomic Extraction (<code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;,</span> <span class="identifier">as_string</span><span class="special">[],</span> <span class="identifier">as_wstring</span><span class="special">[]</span></code>)</a>
29</h5></div></div></div>
30<h6>
31<a name="spirit.karma.reference.directive.as.h0"></a>
32            <span class="phrase"><a name="spirit.karma.reference.directive.as.description"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.description">Description</a>
33          </h6>
34<p>
35            The <code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
36            class forces the atomic extraction of a container type <code class="computeroutput"><span class="identifier">T</span></code> from it's consumed attribute. Usually,
37            repetitive generators (such as <a class="link" href="../operator/kleene.html" title="Kleene Star Generator (*a)">Kleene
38            (<code class="computeroutput"><span class="special">*</span></code>)</a>, etc) or sequences
39            exposing a <code class="computeroutput"><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span></code>
40            will extract elements from the container supplied as their consumed attribute
41            by looping through the containers iterators. In some cases, this may
42            be undesirable. The <code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> class creates a directive that will
43            pass an unnamed temporary object of type <code class="computeroutput"><span class="identifier">T</span></code>
44            to it's subject, if extracting <code class="computeroutput"><span class="identifier">T</span></code>
45            from it's consumed attribute determined at generation-time to be valid.
46            <a class="link" href="../../../advanced/customize/attribute_as.html" title="Atomically Extract an Attribute Value from a Container (Karma)"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">valid_as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()</span></code></a>
47            is called by <code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
48            to determine validity; if it returns false, the generator fails. Subsequent
49            extraction is performed by calling <a class="link" href="../../../advanced/customize/attribute_as.html" title="Atomically Extract an Attribute Value from a Container (Karma)"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()</span></code></a>.
50          </p>
51<div class="note"><table border="0" summary="Note">
52<tr>
53<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
54<th align="left">Note</th>
55</tr>
56<tr><td align="left" valign="top"><p>
57              <code class="computeroutput"><span class="identifier">T</span></code> is required to be
58              a container type. If <a class="link" href="../../../advanced/customize/is_container.html" title="Determine if a Type Should be Treated as a Container (Qi and Karma)"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">is_container</span></code></a> does not return
59              true for <code class="computeroutput"><span class="identifier">T</span></code>, a compile-time
60              error will occur.
61            </p></td></tr>
62</table></div>
63<h6>
64<a name="spirit.karma.reference.directive.as.h1"></a>
65            <span class="phrase"><a name="spirit.karma.reference.directive.as.header"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.header">Header</a>
66          </h6>
67<pre class="programlisting"><span class="comment">// forwards to &lt;boost/spirit/home/karma/directive/as.hpp&gt;</span>
68<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">spirit</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">karma_as</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
69</pre>
70<p>
71            Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>.
72          </p>
73<h6>
74<a name="spirit.karma.reference.directive.as.h2"></a>
75            <span class="phrase"><a name="spirit.karma.reference.directive.as.namespace"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.namespace">Namespace</a>
76          </h6>
77<div class="informaltable"><table class="table">
78<colgroup><col></colgroup>
79<thead><tr><th>
80                    <p>
81                      Name
82                    </p>
83                  </th></tr></thead>
84<tbody>
85<tr><td>
86                    <p>
87                      <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">as_string</span> <span class="comment">//
88                      alias: boost::spirit::karma::as_string</span></code>
89                    </p>
90                  </td></tr>
91<tr><td>
92                    <p>
93                      <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">as_wstring</span> <span class="comment">//
94                      alias: boost::spirit::karma::as_wstring</span></code>
95                    </p>
96                  </td></tr>
97</tbody>
98</table></div>
99<h6>
100<a name="spirit.karma.reference.directive.as.h3"></a>
101            <span class="phrase"><a name="spirit.karma.reference.directive.as.synopsis"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.synopsis">Synopsis</a>
102          </h6>
103<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
104<span class="keyword">struct</span> <span class="identifier">as</span><span class="special">;</span>
105</pre>
106<h6>
107<a name="spirit.karma.reference.directive.as.h4"></a>
108            <span class="phrase"><a name="spirit.karma.reference.directive.as.template_parameters"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.template_parameters">Template
109            parameters</a>
110          </h6>
111<div class="informaltable"><table class="table">
112<colgroup>
113<col>
114<col>
115<col>
116</colgroup>
117<thead><tr>
118<th>
119                    <p>
120                      Parameter
121                    </p>
122                  </th>
123<th>
124                    <p>
125                      Description
126                    </p>
127                  </th>
128<th>
129                    <p>
130                      Default
131                    </p>
132                  </th>
133</tr></thead>
134<tbody><tr>
135<td>
136                    <p>
137                      <code class="computeroutput"><span class="identifier">T</span></code>
138                    </p>
139                  </td>
140<td>
141                    <p>
142                      A container type.
143                    </p>
144                  </td>
145<td>
146                    <p>
147                      none
148                    </p>
149                  </td>
150</tr></tbody>
151</table></div>
152<h6>
153<a name="spirit.karma.reference.directive.as.h5"></a>
154            <span class="phrase"><a name="spirit.karma.reference.directive.as.model_of"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.model_of">Model
155            of</a>
156          </h6>
157<div class="blockquote"><blockquote class="blockquote"><p>
158              <a class="link" href="../generator_concepts/unarygenerator.html" title="UnaryGenerator"><code class="computeroutput"><span class="identifier">UnaryGenerator</span></code></a>
159            </p></blockquote></div>
160<div class="variablelist">
161<p class="title"><b>Notation</b></p>
162<dl class="variablelist">
163<dt><span class="term"><code class="computeroutput"><span class="identifier">a</span></code></span></dt>
164<dd><p>
165                  A <a class="link" href="../generator_concepts/generator.html" title="Generator"><code class="computeroutput"><span class="identifier">Generator</span></code></a>.
166                </p></dd>
167<dt><span class="term"><code class="computeroutput"><span class="identifier">attr</span></code></span></dt>
168<dd><p>
169                  The attribute supplied to the directive.
170                </p></dd>
171</dl>
172</div>
173<h6>
174<a name="spirit.karma.reference.directive.as.h6"></a>
175            <span class="phrase"><a name="spirit.karma.reference.directive.as.expression_semantics"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.expression_semantics">Expression
176            Semantics</a>
177          </h6>
178<p>
179            Semantics of an expression is defined only where it differs from, or
180            is not defined in <a class="link" href="../generator_concepts/unarygenerator.html" title="UnaryGenerator"><code class="computeroutput"><span class="identifier">UnaryGenerator</span></code></a>.
181          </p>
182<div class="informaltable"><table class="table">
183<colgroup>
184<col>
185<col>
186</colgroup>
187<thead><tr>
188<th>
189                    <p>
190                      Expression
191                    </p>
192                  </th>
193<th>
194                    <p>
195                      Semantics
196                    </p>
197                  </th>
198</tr></thead>
199<tbody>
200<tr>
201<td>
202                    <p>
203                      <code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()[</span><span class="identifier">a</span><span class="special">]</span></code>
204                    </p>
205                  </td>
206<td>
207                    <p>
208                      Extract an instance of <code class="computeroutput"><span class="identifier">T</span></code>
209                      from <code class="computeroutput"><span class="identifier">attr</span></code>,
210                      and invoke the subject generator <code class="computeroutput"><span class="identifier">a</span></code>,
211                      supplying the unnamed temporary as it's attribute.
212                    </p>
213                  </td>
214</tr>
215<tr>
216<td>
217                    <p>
218                      <code class="computeroutput"><span class="identifier">as_string</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code>
219                    </p>
220                  </td>
221<td>
222                    <p>
223                      Equivalent to <code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;()[</span><span class="identifier">a</span><span class="special">]</span></code>
224                    </p>
225                  </td>
226</tr>
227<tr>
228<td>
229                    <p>
230                      <code class="computeroutput"><span class="identifier">as_wstring</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code>
231                    </p>
232                  </td>
233<td>
234                    <p>
235                      Equivalent to <code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">wstring</span><span class="special">&gt;()[</span><span class="identifier">a</span><span class="special">]</span></code>
236                    </p>
237                  </td>
238</tr>
239</tbody>
240</table></div>
241<h6>
242<a name="spirit.karma.reference.directive.as.h7"></a>
243            <span class="phrase"><a name="spirit.karma.reference.directive.as.attributes"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.attributes">Attributes</a>
244          </h6>
245<p>
246            See <a class="link" href="../../quick_reference/compound_attribute_rules.html#spirit.karma.quick_reference.compound_attribute_rules.notation">Compound
247            Attribute Notation</a>.
248          </p>
249<div class="informaltable"><table class="table">
250<colgroup>
251<col>
252<col>
253</colgroup>
254<thead><tr>
255<th>
256                    <p>
257                      Expression
258                    </p>
259                  </th>
260<th>
261                    <p>
262                      Attribute
263                    </p>
264                  </th>
265</tr></thead>
266<tbody><tr>
267<td>
268                    <p>
269                      <code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()[</span><span class="identifier">a</span><span class="special">]</span></code>
270                    </p>
271                  </td>
272<td>
273                    <p>
274                      <code class="computeroutput"><span class="identifier">a</span><span class="special">:</span>
275                      <span class="identifier">A</span> <span class="special">--&gt;</span>
276                      <span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()[</span><span class="identifier">a</span><span class="special">]:</span>
277                      <span class="identifier">T</span></code>
278                    </p>
279                  </td>
280</tr></tbody>
281</table></div>
282<h6>
283<a name="spirit.karma.reference.directive.as.h8"></a>
284            <span class="phrase"><a name="spirit.karma.reference.directive.as.complexity"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.complexity">Complexity</a>
285          </h6>
286<div class="blockquote"><blockquote class="blockquote"><p>
287              The complexity is defined by the complexity of the subject generator,
288              <code class="computeroutput"><span class="identifier">a</span></code>, and the complexity
289              of the extraction unnamed contianer of type <code class="computeroutput"><span class="identifier">T</span></code>
290              from the attribute <code class="computeroutput"><span class="identifier">attr</span></code>.
291            </p></blockquote></div>
292<h6>
293<a name="spirit.karma.reference.directive.as.h9"></a>
294            <span class="phrase"><a name="spirit.karma.reference.directive.as.example"></a></span><a class="link" href="as.html#spirit.karma.reference.directive.as.example">Example</a>
295          </h6>
296<div class="note"><table border="0" summary="Note">
297<tr>
298<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
299<th align="left">Note</th>
300</tr>
301<tr><td align="left" valign="top"><p>
302              The test harness for the example(s) below is presented in the <a class="link" href="../basics.html#spirit.karma.reference.basics.examples">Basics Examples</a>
303              section.
304            </p></td></tr>
305</table></div>
306<p>
307            Some using declarations:
308          </p>
309<p>
310</p>
311<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">utree</span><span class="special">;</span>
312<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">utree_type</span><span class="special">;</span>
313<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">utf8_symbol_type</span><span class="special">;</span>
314<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">as</span><span class="special">;</span>
315<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">as_string</span><span class="special">;</span>
316<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">char_</span><span class="special">;</span>
317<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">double_</span><span class="special">;</span>
318</pre>
319<p>
320          </p>
321<p>
322            Simple usage of <code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>,
323            <code class="computeroutput"><span class="identifier">as_string</span></code> and <code class="computeroutput"><span class="identifier">as_wstring</span></code>:
324          </p>
325<p>
326            To properly handle string concatenation with <a class="link" href="../../../support/utree.html" title="The utree data structure"><code class="computeroutput"><span class="identifier">utree</span></code></a>, we make use of <code class="computeroutput"><span class="identifier">as_string</span><span class="special">[]</span></code>.
327            We also use <code class="computeroutput"><span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
328            to explicitly extract a <a class="link" href="../../../support/utree.html" title="The utree data structure"><code class="computeroutput"><span class="identifier">utree</span></code></a> symbol node.
329          </p>
330<p>
331</p>
332<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">utf8_symbol_type</span><span class="special">&gt;</span> <span class="identifier">as_symbol_type</span><span class="special">;</span>
333<span class="identifier">as_symbol_type</span> <span class="keyword">const</span> <span class="identifier">as_symbol</span> <span class="special">=</span> <span class="identifier">as_symbol_type</span><span class="special">();</span>
334
335<span class="identifier">utree</span> <span class="identifier">ut</span><span class="special">;</span>
336<span class="identifier">ut</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="string">"xyz"</span><span class="special">);</span>
337<span class="identifier">ut</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">1.23</span><span class="special">);</span>
338
339<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">"xyz1.23"</span><span class="special">,</span> <span class="identifier">as_string</span><span class="special">[*</span><span class="identifier">char_</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span><span class="special">,</span> <span class="identifier">ut</span><span class="special">);</span>
340<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">"xyz1.23"</span><span class="special">,</span> <span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;()[*</span><span class="identifier">char_</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span><span class="special">,</span> <span class="identifier">ut</span><span class="special">);</span>
341
342<span class="identifier">ut</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span>
343
344<span class="identifier">ut</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">utf8_symbol_type</span><span class="special">(</span><span class="string">"xyz"</span><span class="special">));</span>
345<span class="identifier">ut</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">1.23</span><span class="special">);</span>
346
347<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">"xyz1.23"</span><span class="special">,</span> <span class="identifier">as_symbol</span><span class="special">[*</span><span class="identifier">char_</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span><span class="special">,</span> <span class="identifier">ut</span><span class="special">);</span>
348<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">"xyz1.23"</span><span class="special">,</span> <span class="identifier">as</span><span class="special">&lt;</span><span class="identifier">utf8_symbol_type</span><span class="special">&gt;()[*</span><span class="identifier">char_</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span><span class="special">,</span> <span class="identifier">ut</span><span class="special">);</span>
349</pre>
350<p>
351          </p>
352</div>
353<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
354<td align="left"></td>
355<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
356        Distributed under the Boost Software License, Version 1.0. (See accompanying
357        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>)
358      </p>
359</div></td>
360</tr></table>
361<hr>
362<div class="spirit-nav">
363<a accesskey="p" href="columns.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../directive.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="../nonterminal.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
364</div>
365</body>
366</html>
367