1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Parser Directive for Attribute Commit/Rollback (hold[])</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="Parser Directives"> 9<link rel="prev" href="skip.html" title="Parser Directive Re-Establishing Skipping (skip[])"> 10<link rel="next" href="as.html" title="Parser Directives Forcing Atomic Assignment (as<T>, as_string[], as_wstring[])"> 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="skip.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="as.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.qi.reference.directive.hold"></a><a class="link" href="hold.html" title="Parser Directive for Attribute Commit/Rollback (hold[])">Parser Directive 28 for Attribute Commit/Rollback (<code class="computeroutput"><span class="identifier">hold</span><span class="special">[]</span></code>)</a> 29</h5></div></div></div> 30<h6> 31<a name="spirit.qi.reference.directive.hold.h0"></a> 32 <span class="phrase"><a name="spirit.qi.reference.directive.hold.description"></a></span><a class="link" href="hold.html#spirit.qi.reference.directive.hold.description">Description</a> 33 </h6> 34<p> 35 The <code class="computeroutput"><span class="identifier">hold</span><span class="special">[]</span></code> 36 directive helps managing attributes, mainly for alternative parsers. 37 It instantiates a new attribute instance for the embedded parser. The 38 value of that attribute instance is copied to the outer attribute if 39 the embedded parser succeeds and it is discarded otherwise. Alternative 40 parsers normally do not rollback changes made to the outer attribute 41 by an failed alternative. Wrapping those alternatives into a <code class="computeroutput"><span class="identifier">hold</span><span class="special">[]</span></code> 42 directive ensures that only the succeeding alternative gets to modify 43 the attribute. 44 </p> 45<h6> 46<a name="spirit.qi.reference.directive.hold.h1"></a> 47 <span class="phrase"><a name="spirit.qi.reference.directive.hold.header"></a></span><a class="link" href="hold.html#spirit.qi.reference.directive.hold.header">Header</a> 48 </h6> 49<pre class="programlisting"><span class="comment">// forwards to <boost/spirit/home/qi/directive/hold.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">qi_hold</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<h6> 56<a name="spirit.qi.reference.directive.hold.h2"></a> 57 <span class="phrase"><a name="spirit.qi.reference.directive.hold.namespace"></a></span><a class="link" href="hold.html#spirit.qi.reference.directive.hold.namespace">Namespace</a> 58 </h6> 59<div class="informaltable"><table class="table"> 60<colgroup><col></colgroup> 61<thead><tr><th> 62 <p> 63 Name 64 </p> 65 </th></tr></thead> 66<tbody><tr><td> 67 <p> 68 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">hold</span> <span class="comment">// alias: 69 boost::spirit::qi::hold</span></code> 70 </p> 71 </td></tr></tbody> 72</table></div> 73<h6> 74<a name="spirit.qi.reference.directive.hold.h3"></a> 75 <span class="phrase"><a name="spirit.qi.reference.directive.hold.model_of"></a></span><a class="link" href="hold.html#spirit.qi.reference.directive.hold.model_of">Model 76 of</a> 77 </h6> 78<div class="blockquote"><blockquote class="blockquote"><p> 79 <a class="link" href="../parser_concepts/unaryparser.html" title="UnaryParser"><code class="computeroutput"><span class="identifier">UnaryParser</span></code></a> 80 </p></blockquote></div> 81<div class="variablelist"> 82<p class="title"><b>Notation</b></p> 83<dl class="variablelist"> 84<dt><span class="term"><code class="computeroutput"><span class="identifier">a</span></code></span></dt> 85<dd><p> 86 A <a class="link" href="../parser_concepts/parser.html" title="Parser"><code class="computeroutput"><span class="identifier">Parser</span></code></a>. 87 </p></dd> 88</dl> 89</div> 90<h6> 91<a name="spirit.qi.reference.directive.hold.h4"></a> 92 <span class="phrase"><a name="spirit.qi.reference.directive.hold.expression_semantics"></a></span><a class="link" href="hold.html#spirit.qi.reference.directive.hold.expression_semantics">Expression 93 Semantics</a> 94 </h6> 95<p> 96 Semantics of an expression is defined only where it differs from, or 97 is not defined in <a class="link" href="../parser_concepts/unaryparser.html" title="UnaryParser"><code class="computeroutput"><span class="identifier">UnaryParser</span></code></a>. 98 </p> 99<div class="informaltable"><table class="table"> 100<colgroup> 101<col> 102<col> 103</colgroup> 104<thead><tr> 105<th> 106 <p> 107 Expression 108 </p> 109 </th> 110<th> 111 <p> 112 Semantics 113 </p> 114 </th> 115</tr></thead> 116<tbody><tr> 117<td> 118 <p> 119 <code class="computeroutput"><span class="identifier">hold</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code> 120 </p> 121 </td> 122<td> 123 <p> 124 Create a new attribute instance while parsing <code class="computeroutput"><span class="identifier">a</span></code>, copying the result to 125 the outer attribute only after <code class="computeroutput"><span class="identifier">a</span></code> 126 succeeds. 127 </p> 128 </td> 129</tr></tbody> 130</table></div> 131<h6> 132<a name="spirit.qi.reference.directive.hold.h5"></a> 133 <span class="phrase"><a name="spirit.qi.reference.directive.hold.attributes"></a></span><a class="link" href="hold.html#spirit.qi.reference.directive.hold.attributes">Attributes</a> 134 </h6> 135<p> 136 See <a class="link" href="../../quick_reference/compound_attribute_rules.html#spirit.qi.quick_reference.compound_attribute_rules.notation">Compound 137 Attribute Notation</a>. 138 </p> 139<div class="informaltable"><table class="table"> 140<colgroup> 141<col> 142<col> 143</colgroup> 144<thead><tr> 145<th> 146 <p> 147 Expression 148 </p> 149 </th> 150<th> 151 <p> 152 Attribute 153 </p> 154 </th> 155</tr></thead> 156<tbody><tr> 157<td> 158 <p> 159 <code class="computeroutput"><span class="identifier">hold</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code> 160 </p> 161 </td> 162<td> 163 <p> 164</p> 165<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">hold</span><span class="special">[</span><span class="identifier">a</span><span class="special">]:</span> <span class="identifier">A</span> 166<span class="identifier">a</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--></span> <span class="identifier">hold</span><span class="special">[</span><span class="identifier">a</span><span class="special">]:</span> <span class="identifier">Unused</span></pre> 167<p> 168 </p> 169 </td> 170</tr></tbody> 171</table></div> 172<div class="note"><table border="0" summary="Note"> 173<tr> 174<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td> 175<th align="left">Note</th> 176</tr> 177<tr><td align="left" valign="top"><p> 178 The <code class="computeroutput"><span class="identifier">hold</span><span class="special">[]</span></code> 179 directive uses <code class="computeroutput"><span class="identifier">swap</span><span class="special">()</span></code> to implement the rollback/commit 180 semantics for the attribute. For this reason the attribute type needs 181 to to be usable with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> 182 (needs to either define a proper overload for <code class="computeroutput"><span class="identifier">swap</span><span class="special">(</span><span class="identifier">attribute_type</span><span class="special">&,</span> <span class="identifier">attribute_type</span><span class="special">&)</span></code> or expose a member function 183 <code class="computeroutput"><span class="identifier">attribute_type</span><span class="special">::</span><span class="identifier">swap</span><span class="special">(</span><span class="identifier">attribute_type</span><span class="special">&)</span></code>. 184 </p></td></tr> 185</table></div> 186<h6> 187<a name="spirit.qi.reference.directive.hold.h6"></a> 188 <span class="phrase"><a name="spirit.qi.reference.directive.hold.complexity"></a></span><a class="link" href="hold.html#spirit.qi.reference.directive.hold.complexity">Complexity</a> 189 </h6> 190<div class="blockquote"><blockquote class="blockquote"><p> 191 The complexity is defined by the complexity of the subject parser, 192 <code class="computeroutput"><span class="identifier">a</span></code> 193 </p></blockquote></div> 194<h6> 195<a name="spirit.qi.reference.directive.hold.h7"></a> 196 <span class="phrase"><a name="spirit.qi.reference.directive.hold.example"></a></span><a class="link" href="hold.html#spirit.qi.reference.directive.hold.example">Example</a> 197 </h6> 198<div class="note"><table border="0" summary="Note"> 199<tr> 200<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td> 201<th align="left">Note</th> 202</tr> 203<tr><td align="left" valign="top"><p> 204 The test harness for the example(s) below is presented in the <a class="link" href="../basics.html#spirit.qi.reference.basics.examples">Basics Examples</a> 205 section. 206 </p></td></tr> 207</table></div> 208<p> 209 Some using declarations: 210 </p> 211<p> 212</p> 213<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">qi</span><span class="special">::</span><span class="identifier">hold</span><span class="special">;</span> 214<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">qi</span><span class="special">::</span><span class="identifier">int_</span><span class="special">;</span> 215<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">qi</span><span class="special">::</span><span class="identifier">attr</span><span class="special">;</span> 216</pre> 217<p> 218 </p> 219<p> 220 The use of <code class="computeroutput"><span class="identifier">hold</span><span class="special">[]</span></code> 221 here will make sure the changes to the attribute caused by the (failing) 222 first alternative will not be visible after the whole parsing succeeded. 223 </p> 224<p> 225</p> 226<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">v</span><span class="special">;</span> 227<span class="identifier">test_phrase_parser_attr</span><span class="special">(</span><span class="string">"123"</span><span class="special">,</span> 228 <span class="identifier">hold</span><span class="special">[</span><span class="identifier">int_</span> <span class="special">>></span> <span class="char">':'</span> <span class="special">>></span> <span class="identifier">int_</span><span class="special">]</span> <span class="special">|</span> <span class="identifier">int_</span> <span class="special">>></span> <span class="identifier">attr</span><span class="special">(</span><span class="number">0</span><span class="special">),</span> <span class="identifier">v</span><span class="special">);</span> 229<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">v</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special"><<</span> <span class="string">","</span> <span class="special"><<</span> <span class="identifier">v</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="comment">// will output: >123,0<</span> 230</pre> 231<p> 232 </p> 233</div> 234<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 235<td align="left"></td> 236<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p> 237 Distributed under the Boost Software License, Version 1.0. (See accompanying 238 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>) 239 </p> 240</div></td> 241</tr></table> 242<hr> 243<div class="spirit-nav"> 244<a accesskey="p" href="skip.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="as.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 245</div> 246</body> 247</html> 248