• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Plus Generator (+a)</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="../operator.html" title="Generator Operators">
9<link rel="prev" href="kleene.html" title="Kleene Star Generator (*a)">
10<link rel="next" href="list.html" title="List Generator (a % b)">
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="kleene.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../operator.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="list.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.operator.plus"></a><a class="link" href="plus.html" title="Plus Generator (+a)">Plus Generator
28          (<code class="computeroutput"><span class="special">+</span><span class="identifier">a</span></code>)</a>
29</h5></div></div></div>
30<h6>
31<a name="spirit.karma.reference.operator.plus.h0"></a>
32            <span class="phrase"><a name="spirit.karma.reference.operator.plus.description"></a></span><a class="link" href="plus.html#spirit.karma.reference.operator.plus.description">Description</a>
33          </h6>
34<p>
35            The Plus generator is used to repeat the execution of an embedded generator
36            one or more times. It succeeds if the embedded generator has been successfully
37            executed at least once.
38          </p>
39<h6>
40<a name="spirit.karma.reference.operator.plus.h1"></a>
41            <span class="phrase"><a name="spirit.karma.reference.operator.plus.header"></a></span><a class="link" href="plus.html#spirit.karma.reference.operator.plus.header">Header</a>
42          </h6>
43<pre class="programlisting"><span class="comment">// forwards to &lt;boost/spirit/home/karma/operator/plus.hpp&gt;</span>
44<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_plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
45</pre>
46<p>
47            Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>.
48          </p>
49<h6>
50<a name="spirit.karma.reference.operator.plus.h2"></a>
51            <span class="phrase"><a name="spirit.karma.reference.operator.plus.model_of"></a></span><a class="link" href="plus.html#spirit.karma.reference.operator.plus.model_of">Model
52            of</a>
53          </h6>
54<div class="blockquote"><blockquote class="blockquote"><p>
55              <a class="link" href="../generator_concepts/unarygenerator.html" title="UnaryGenerator"><code class="computeroutput"><span class="identifier">UnaryGenerator</span></code></a>
56            </p></blockquote></div>
57<h6>
58<a name="spirit.karma.reference.operator.plus.h3"></a>
59            <span class="phrase"><a name="spirit.karma.reference.operator.plus.expression_semantics"></a></span><a class="link" href="plus.html#spirit.karma.reference.operator.plus.expression_semantics">Expression
60            Semantics</a>
61          </h6>
62<p>
63            Semantics of an expression is defined only where it differs from, or
64            is not defined in <a class="link" href="../generator_concepts/unarygenerator.html" title="UnaryGenerator"><code class="computeroutput"><span class="identifier">UnaryGenerator</span></code></a>.
65          </p>
66<div class="informaltable"><table class="table">
67<colgroup>
68<col>
69<col>
70</colgroup>
71<thead><tr>
72<th>
73                    <p>
74                      Expression
75                    </p>
76                  </th>
77<th>
78                    <p>
79                      Semantics
80                    </p>
81                  </th>
82</tr></thead>
83<tbody><tr>
84<td>
85                    <p>
86                      <code class="computeroutput"><span class="special">+</span><span class="identifier">a</span></code>
87                    </p>
88                  </td>
89<td>
90                    <p>
91                      The generator <code class="computeroutput"><span class="identifier">a</span></code>
92                      is executed one or more times depending on the availability
93                      of an attribute. The execution of <code class="computeroutput"><span class="identifier">a</span></code>
94                      stops after the attribute values passed to the plus generator
95                      are exhausted. The plus generator succeeds as long as its embedded
96                      generator has been successfully executed at least once (unless
97                      the underlying output stream reports an error).
98                    </p>
99                  </td>
100</tr></tbody>
101</table></div>
102<div class="note"><table border="0" summary="Note">
103<tr>
104<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
105<th align="left">Note</th>
106</tr>
107<tr><td align="left" valign="top"><p>
108              All failing iterations of the embedded generator will consume one element
109              from the supplied attribute. The overall <code class="computeroutput"><span class="special">+</span><span class="identifier">a</span></code> will succeed as long as at least
110              one invocation of the embedded generator will succeed (unless the underlying
111              output stream reports an error).
112            </p></td></tr>
113</table></div>
114<h6>
115<a name="spirit.karma.reference.operator.plus.h4"></a>
116            <span class="phrase"><a name="spirit.karma.reference.operator.plus.attributes"></a></span><a class="link" href="plus.html#spirit.karma.reference.operator.plus.attributes">Attributes</a>
117          </h6>
118<p>
119            See <a class="link" href="../../quick_reference/compound_attribute_rules.html#spirit.karma.quick_reference.compound_attribute_rules.notation">Compound
120            Attribute Notation</a>.
121          </p>
122<div class="informaltable"><table class="table">
123<colgroup>
124<col>
125<col>
126</colgroup>
127<thead><tr>
128<th>
129                    <p>
130                      Expression
131                    </p>
132                  </th>
133<th>
134                    <p>
135                      Attribute
136                    </p>
137                  </th>
138</tr></thead>
139<tbody><tr>
140<td>
141                    <p>
142                      <code class="computeroutput"><span class="special">+</span><span class="identifier">a</span></code>
143                      (unary <code class="computeroutput"><span class="special">+</span></code>)
144                    </p>
145                  </td>
146<td>
147                    <p>
148</p>
149<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span> <span class="special">--&gt;</span> <span class="special">+</span><span class="identifier">a</span><span class="special">:</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span>
150<span class="identifier">a</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--&gt;</span> <span class="special">+</span><span class="identifier">a</span><span class="special">:</span> <span class="identifier">Unused</span></pre>
151<p>
152                    </p>
153                  </td>
154</tr></tbody>
155</table></div>
156<div class="important"><table border="0" summary="Important">
157<tr>
158<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../images/important.png"></td>
159<th align="left">Important</th>
160</tr>
161<tr><td align="left" valign="top"><p>
162              The table above uses <code class="computeroutput"><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span></code> as a placeholder only. The notation
163              of <code class="computeroutput"><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span></code>
164              stands for <span class="emphasis"><em>any STL container</em></span> holding elements
165              of type <code class="computeroutput"><span class="identifier">A</span></code>.
166            </p></td></tr>
167</table></div>
168<p>
169            The Plus generator will execute its embedded generator once for each
170            element in the provided container attribute as long as the embedded generator
171            succeeds. On each iteration it will pass the next consecutive element
172            from the container attribute to the embedded generator. Therefore the
173            number of iterations will not be larger than the number of elements in
174            the container passed as its attribute. An empty container will make the
175            plus generator fail.
176          </p>
177<p>
178            It is important to note, that the plus generator does not perform any
179            buffering of the output generated by its embedded elements. That means
180            that any failing element generator might have already generated some
181            output, which is <span class="emphasis"><em>not</em></span> rolled back.
182          </p>
183<div class="tip"><table border="0" summary="Tip">
184<tr>
185<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../images/tip.png"></td>
186<th align="left">Tip</th>
187</tr>
188<tr><td align="left" valign="top">
189<p>
190              The simplest way to force a plus generator to behave as if it did buffering
191              is to wrap it into a buffering directive (see <a class="link" href="../directive/buffer.html" title="Generator Directive for Temporary Output Buffering (buffer[])"><code class="computeroutput"><span class="identifier">buffer</span></code></a>):
192            </p>
193<p>
194</p>
195<pre class="programlisting"><span class="identifier">buffer</span><span class="special">[+</span><span class="identifier">a</span><span class="special">]</span></pre>
196<p>
197            </p>
198<p>
199              which will <span class="emphasis"><em>not</em></span> generate any output in case of
200              a failing generator <code class="computeroutput"><span class="special">+</span><span class="identifier">a</span></code>. The expression:
201            </p>
202<p>
203</p>
204<pre class="programlisting"><span class="special">+(</span><span class="identifier">buffer</span><span class="special">[</span><span class="identifier">a</span><span class="special">])</span></pre>
205<p>
206            </p>
207<p>
208              will not generate any partial output from a generator <code class="computeroutput"><span class="identifier">a</span></code> if it fails generating in the middle
209              of its output. The overall expression will still generate the output
210              as produced by all successful invocations of the generator <code class="computeroutput"><span class="identifier">a</span></code>.
211            </p>
212</td></tr>
213</table></div>
214<h6>
215<a name="spirit.karma.reference.operator.plus.h5"></a>
216            <span class="phrase"><a name="spirit.karma.reference.operator.plus.complexity"></a></span><a class="link" href="plus.html#spirit.karma.reference.operator.plus.complexity">Complexity</a>
217          </h6>
218<div class="blockquote"><blockquote class="blockquote"><p>
219              The overall complexity of the plus generator is defined by the complexity
220              of its embedded generator multiplied by the number of executed iterations.
221              The complexity of the plus generator itself is O(N), where N is the
222              number of elements in the container passed as its attribute.
223            </p></blockquote></div>
224<h6>
225<a name="spirit.karma.reference.operator.plus.h6"></a>
226            <span class="phrase"><a name="spirit.karma.reference.operator.plus.example"></a></span><a class="link" href="plus.html#spirit.karma.reference.operator.plus.example">Example</a>
227          </h6>
228<div class="note"><table border="0" summary="Note">
229<tr>
230<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
231<th align="left">Note</th>
232</tr>
233<tr><td align="left" valign="top"><p>
234              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>
235              section.
236            </p></td></tr>
237</table></div>
238<p>
239            Some includes:
240          </p>
241<p>
242</p>
243<pre class="programlisting"><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</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
244<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">support_utree</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
245<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">phoenix_core</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
246<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">phoenix_operator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
247<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">std_pair</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
248<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
249<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
250</pre>
251<p>
252          </p>
253<p>
254            Some using declarations:
255          </p>
256<p>
257</p>
258<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">karma</span><span class="special">::</span><span class="identifier">double_</span><span class="special">;</span>
259<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">space</span><span class="special">;</span>
260</pre>
261<p>
262          </p>
263<p>
264            Basic usage of a plus generator:
265          </p>
266<p>
267</p>
268<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v1</span><span class="special">;</span>
269<span class="identifier">v1</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">1.0</span><span class="special">);</span>
270<span class="identifier">v1</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">2.0</span><span class="special">);</span>
271<span class="identifier">v1</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">3.0</span><span class="special">);</span>
272<span class="identifier">test_generator_attr_delim</span><span class="special">(</span><span class="string">"1.0 2.0 3.0 "</span><span class="special">,</span> <span class="special">+</span><span class="identifier">double_</span><span class="special">,</span> <span class="identifier">space</span><span class="special">,</span> <span class="identifier">v1</span><span class="special">);</span>
273</pre>
274<p>
275          </p>
276<p>
277            A more sophisticated use case showing how to leverage the fact that plus
278            is failing for empty containers passed as its attribute:
279          </p>
280<p>
281</p>
282<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v2</span><span class="special">;</span>               <span class="comment">// empty container</span>
283<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">"empty"</span><span class="special">,</span> <span class="special">+</span><span class="identifier">double_</span> <span class="special">|</span> <span class="string">"empty"</span><span class="special">,</span> <span class="identifier">v2</span><span class="special">);</span>
284</pre>
285<p>
286          </p>
287</div>
288<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
289<td align="left"></td>
290<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
291        Distributed under the Boost Software License, Version 1.0. (See accompanying
292        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>)
293      </p>
294</div></td>
295</tr></table>
296<hr>
297<div class="spirit-nav">
298<a accesskey="p" href="kleene.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../operator.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="list.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
299</div>
300</body>
301</html>
302