1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Generator Directive for Temporary Output Buffering (buffer[])</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="maxwidth.html" title="Generator Directives Controlling the Maximum Field Width (maxwidth[])"> 10<link rel="next" href="omit.html" title="Generator Directives Consuming Attributes (omit[] and skip[])"> 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="maxwidth.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="omit.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.buffer"></a><a class="link" href="buffer.html" title="Generator Directive for Temporary Output Buffering (buffer[])">Generator 28 Directive for Temporary Output Buffering (<code class="computeroutput"><span class="identifier">buffer</span><span class="special">[]</span></code>)</a> 29</h5></div></div></div> 30<h6> 31<a name="spirit.karma.reference.directive.buffer.h0"></a> 32 <span class="phrase"><a name="spirit.karma.reference.directive.buffer.description"></a></span><a class="link" href="buffer.html#spirit.karma.reference.directive.buffer.description">Description</a> 33 </h6> 34<p> 35 All generator components (except the <a class="link" href="../operator/alternative.html" title="Alternative Generator (a | b)">Alternative 36 (<code class="computeroutput"><span class="special">|</span></code>)</a> generator) pass 37 their generated output directly to the underlying output stream. If a 38 generator fails halfway through, the output generated so far is not 'rolled 39 back'. The buffering generator directive allows to avoid this unwanted 40 output to be generated. It temporarily redirects the output produced 41 by the embedded generator into a buffer. This buffer is flushed to the 42 underlying stream only after the embedded generator succeeded, but is 43 discarded otherwise. 44 </p> 45<h6> 46<a name="spirit.karma.reference.directive.buffer.h1"></a> 47 <span class="phrase"><a name="spirit.karma.reference.directive.buffer.header"></a></span><a class="link" href="buffer.html#spirit.karma.reference.directive.buffer.header">Header</a> 48 </h6> 49<pre class="programlisting"><span class="comment">// forwards to <boost/spirit/home/karma/directive/buffer.hpp></span> 50<span class="preprocessor">#include</span> <span class="special"><</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_buffer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 51</pre> 52<p> 53 Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>. 54 </p> 55<div class="informaltable"><table class="table"> 56<colgroup><col></colgroup> 57<thead><tr><th> 58 <p> 59 Name 60 </p> 61 </th></tr></thead> 62<tbody><tr><td> 63 <p> 64 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">buffer</span> <span class="comment">// 65 alias: boost::spirit::karma::buffer</span></code> 66 </p> 67 </td></tr></tbody> 68</table></div> 69<h6> 70<a name="spirit.karma.reference.directive.buffer.h2"></a> 71 <span class="phrase"><a name="spirit.karma.reference.directive.buffer.model_of"></a></span><a class="link" href="buffer.html#spirit.karma.reference.directive.buffer.model_of">Model of</a> 72 </h6> 73<div class="blockquote"><blockquote class="blockquote"><p> 74 <a class="link" href="../generator_concepts/unarygenerator.html" title="UnaryGenerator"><code class="computeroutput"><span class="identifier">UnaryGenerator</span></code></a> 75 </p></blockquote></div> 76<div class="variablelist"> 77<p class="title"><b>Notation</b></p> 78<dl class="variablelist"> 79<dt><span class="term"><code class="computeroutput"><span class="identifier">a</span></code></span></dt> 80<dd><p> 81 A generator object 82 </p></dd> 83<dt><span class="term"><code class="computeroutput"><span class="identifier">A</span></code></span></dt> 84<dd><p> 85 Attribute type of generator <code class="computeroutput"><span class="identifier">a</span></code> 86 </p></dd> 87</dl> 88</div> 89<h6> 90<a name="spirit.karma.reference.directive.buffer.h3"></a> 91 <span class="phrase"><a name="spirit.karma.reference.directive.buffer.expression_semantics"></a></span><a class="link" href="buffer.html#spirit.karma.reference.directive.buffer.expression_semantics">Expression 92 Semantics</a> 93 </h6> 94<p> 95 Semantics of an expression is defined only where it differs from, or 96 is not defined in <a class="link" href="../generator_concepts/unarygenerator.html" title="UnaryGenerator"><code class="computeroutput"><span class="identifier">UnaryGenerator</span></code></a>. 97 </p> 98<div class="informaltable"><table class="table"> 99<colgroup> 100<col> 101<col> 102</colgroup> 103<thead><tr> 104<th> 105 <p> 106 Expression 107 </p> 108 </th> 109<th> 110 <p> 111 Semantics 112 </p> 113 </th> 114</tr></thead> 115<tbody><tr> 116<td> 117 <p> 118 <code class="computeroutput"><span class="identifier">buffer</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code> 119 </p> 120 </td> 121<td> 122 <p> 123 The embedded generator <code class="computeroutput"><span class="identifier">a</span></code> 124 is invoked but its output is temporarily intercepted and stored 125 in an internal buffer. If <code class="computeroutput"><span class="identifier">a</span></code> 126 succeeds the buffer content is flushed to the underlying output 127 stream, otherwise the buffer content is discarded. The buffer 128 directive succeeds as long as the embedded generator succeeded 129 (unless the underlying output stream reports an error). 130 </p> 131 </td> 132</tr></tbody> 133</table></div> 134<div class="tip"><table border="0" summary="Tip"> 135<tr> 136<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../images/tip.png"></td> 137<th align="left">Tip</th> 138</tr> 139<tr><td align="left" valign="top"><p> 140 If you want to make the buffered generator succeed regardless of the 141 outcome of the embedded generator, simply wrap the <code class="computeroutput"><span class="identifier">buffer</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code> into an additional optional: <code class="computeroutput"><span class="special">-</span><span class="identifier">buffer</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code> (see <a class="link" href="../operator/optional.html" title="Optional Generator (-a)">Optional 142 (unary <code class="computeroutput"><span class="special">-</span></code>)</a>). 143 </p></td></tr> 144</table></div> 145<h6> 146<a name="spirit.karma.reference.directive.buffer.h4"></a> 147 <span class="phrase"><a name="spirit.karma.reference.directive.buffer.attributes"></a></span><a class="link" href="buffer.html#spirit.karma.reference.directive.buffer.attributes">Attributes</a> 148 </h6> 149<p> 150 See <a class="link" href="../../quick_reference/compound_attribute_rules.html#spirit.karma.quick_reference.compound_attribute_rules.notation">Compound 151 Attribute Notation</a>. 152 </p> 153<div class="informaltable"><table class="table"> 154<colgroup> 155<col> 156<col> 157</colgroup> 158<thead><tr> 159<th> 160 <p> 161 Expression 162 </p> 163 </th> 164<th> 165 <p> 166 Attribute 167 </p> 168 </th> 169</tr></thead> 170<tbody><tr> 171<td> 172 <p> 173 <code class="computeroutput"><span class="identifier">buffer</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code> 174 </p> 175 </td> 176<td> 177 <p> 178</p> 179<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">--></span> <span class="identifier">buffer</span><span class="special">[</span><span class="identifier">a</span><span class="special">]:</span> <span class="identifier">A</span> 180<span class="identifier">a</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--></span> <span class="identifier">buffer</span><span class="special">[</span><span class="identifier">a</span><span class="special">]:</span> <span class="identifier">Unused</span></pre> 181<p> 182 </p> 183 </td> 184</tr></tbody> 185</table></div> 186<h6> 187<a name="spirit.karma.reference.directive.buffer.h5"></a> 188 <span class="phrase"><a name="spirit.karma.reference.directive.buffer.complexity"></a></span><a class="link" href="buffer.html#spirit.karma.reference.directive.buffer.complexity">Complexity</a> 189 </h6> 190<div class="blockquote"><blockquote class="blockquote"><p> 191 The overall complexity of the buffering generator directive is defined 192 by the complexity of its embedded generator. The complexity of the 193 buffering directive generator itself is O(N), where N is the number 194 of characters buffered. 195 </p></blockquote></div> 196<h6> 197<a name="spirit.karma.reference.directive.buffer.h6"></a> 198 <span class="phrase"><a name="spirit.karma.reference.directive.buffer.example"></a></span><a class="link" href="buffer.html#spirit.karma.reference.directive.buffer.example">Example</a> 199 </h6> 200<div class="note"><table border="0" summary="Note"> 201<tr> 202<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td> 203<th align="left">Note</th> 204</tr> 205<tr><td align="left" valign="top"><p> 206 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> 207 section. 208 </p></td></tr> 209</table></div> 210<p> 211 Some includes: 212 </p> 213<p> 214</p> 215<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">></span> 216<span class="preprocessor">#include</span> <span class="special"><</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">></span> 217<span class="preprocessor">#include</span> <span class="special"><</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">></span> 218<span class="preprocessor">#include</span> <span class="special"><</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">></span> 219<span class="preprocessor">#include</span> <span class="special"><</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">></span> 220<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 221<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 222</pre> 223<p> 224 </p> 225<p> 226 Some using declarations: 227 </p> 228<p> 229</p> 230<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> 231<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">buffer</span><span class="special">;</span> 232</pre> 233<p> 234 </p> 235<p> 236 Basic usage of a buffering generator directive. It shows how the partial 237 output generated in the first example does not show up in the generated 238 output as the plus generator fails (no data is available, see <a class="link" href="../operator/plus.html" title="Plus Generator (+a)">Plus 239 (unary <code class="computeroutput"><span class="special">+</span></code>)</a>). 240 </p> 241<p> 242</p> 243<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">v</span><span class="special">;</span> <span class="comment">// empty container</span> 244<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">""</span><span class="special">,</span> <span class="special">-</span><span class="identifier">buffer</span><span class="special">[</span><span class="char">'['</span> <span class="special"><<</span> <span class="special">+</span><span class="identifier">double_</span> <span class="special"><<</span> <span class="char">']'</span><span class="special">],</span> <span class="identifier">v</span><span class="special">);</span> 245 246<span class="identifier">v</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> <span class="comment">// now, fill the container</span> 247<span class="identifier">v</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> 248<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">"[1.02.0]"</span><span class="special">,</span> <span class="identifier">buffer</span><span class="special">[</span><span class="char">'['</span> <span class="special"><<</span> <span class="special">+</span><span class="identifier">double_</span> <span class="special"><<</span> <span class="char">']'</span><span class="special">],</span> <span class="identifier">v</span><span class="special">);</span> 249</pre> 250<p> 251 </p> 252</div> 253<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 254<td align="left"></td> 255<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p> 256 Distributed under the Boost Software License, Version 1.0. (See accompanying 257 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>) 258 </p> 259</div></td> 260</tr></table> 261<hr> 262<div class="spirit-nav"> 263<a accesskey="p" href="maxwidth.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="omit.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 264</div> 265</body> 266</html> 267