1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Attribute Transformation Pseudo Generator (attr_cast)</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="../auxiliary.html" title="Auxiliary Generators"> 9<link rel="prev" href="../auxiliary.html" title="Auxiliary Generators"> 10<link rel="next" href="eol.html" title="End of Line Generator (eol)"> 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="../auxiliary.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../auxiliary.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="eol.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.auxiliary.attr_cast"></a><a class="link" href="attr_cast.html" title="Attribute Transformation Pseudo Generator (attr_cast)">Attribute 28 Transformation Pseudo Generator (<code class="computeroutput"><span class="identifier">attr_cast</span></code>)</a> 29</h5></div></div></div> 30<h6> 31<a name="spirit.karma.reference.auxiliary.attr_cast.h0"></a> 32 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.description"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.description">Description</a> 33 </h6> 34<p> 35 The <code class="computeroutput"><span class="identifier">attr_cast</span><span class="special"><</span><span class="identifier">Exposed</span><span class="special">,</span> 36 <span class="identifier">Transformed</span><span class="special">>()</span></code> 37 component invokes the embedded generator while supplying an attribute 38 of type <code class="computeroutput"><span class="identifier">Transformed</span></code>. 39 The supplied attribute gets created from the original attribute (of type 40 <code class="computeroutput"><span class="identifier">Exposed</span></code>) passed to this 41 component using the customization point <a class="link" href="../../../advanced/customize/transform.html" title="Transform an Attribute to a Different Type (Qi and Karma)"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">transform_attribute</span></code></a>. 42 </p> 43<h6> 44<a name="spirit.karma.reference.auxiliary.attr_cast.h1"></a> 45 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.header"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.header">Header</a> 46 </h6> 47<pre class="programlisting"><span class="comment">// forwards to <boost/spirit/home/karma/auxiliary/attr_cast.hpp></span> 48<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_attr_cast</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 49</pre> 50<p> 51 Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>. 52 </p> 53<h6> 54<a name="spirit.karma.reference.auxiliary.attr_cast.h2"></a> 55 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.namespace"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.namespace">Namespace</a> 56 </h6> 57<div class="informaltable"><table class="table"> 58<colgroup><col></colgroup> 59<thead><tr><th> 60 <p> 61 Name 62 </p> 63 </th></tr></thead> 64<tbody><tr><td> 65 <p> 66 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">attr_cast</span> <span class="comment">// 67 alias: boost::spirit::karma::attr_cast</span></code> 68 </p> 69 </td></tr></tbody> 70</table></div> 71<h6> 72<a name="spirit.karma.reference.auxiliary.attr_cast.h3"></a> 73 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.synopsis"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.synopsis">Synopsis</a> 74 </h6> 75<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="identifier">Exposed</span><span class="special">,</span> <span class="identifier">Transformed</span><span class="special">></span> 76<span class="special"><</span><span class="identifier">unspecified</span><span class="special">></span> <span class="identifier">attr_cast</span><span class="special">(<</span><span class="identifier">unspecified</span><span class="special">>);</span> 77</pre> 78<h6> 79<a name="spirit.karma.reference.auxiliary.attr_cast.h4"></a> 80 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.template_parameters"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.template_parameters">Template 81 parameters</a> 82 </h6> 83<div class="informaltable"><table class="table"> 84<colgroup> 85<col> 86<col> 87<col> 88</colgroup> 89<thead><tr> 90<th> 91 <p> 92 Parameter 93 </p> 94 </th> 95<th> 96 <p> 97 Description 98 </p> 99 </th> 100<th> 101 <p> 102 Default 103 </p> 104 </th> 105</tr></thead> 106<tbody> 107<tr> 108<td> 109 <p> 110 <code class="computeroutput"><span class="identifier">Exposed</span></code> 111 </p> 112 </td> 113<td> 114 <p> 115 The type of the attribute supplied to the <code class="computeroutput"><span class="identifier">attr_cast</span></code>. 116 </p> 117 </td> 118<td> 119 <p> 120 <code class="computeroutput"><span class="identifier">unused_type</span></code> 121 </p> 122 </td> 123</tr> 124<tr> 125<td> 126 <p> 127 <code class="computeroutput"><span class="identifier">Transformed</span></code> 128 </p> 129 </td> 130<td> 131 <p> 132 The type of the attribute expected by the embedded generator 133 <code class="computeroutput"><span class="identifier">g</span></code>. 134 </p> 135 </td> 136<td> 137 <p> 138 <code class="computeroutput"><span class="identifier">unused_type</span></code> 139 </p> 140 </td> 141</tr> 142</tbody> 143</table></div> 144<p> 145 The <code class="computeroutput"><span class="identifier">attr_cast</span></code> is a function 146 template. It is possible to invoke it using the following schemes: 147 </p> 148<pre class="programlisting"><span class="identifier">attr_cast</span><span class="special">(</span><span class="identifier">g</span><span class="special">)</span> 149<span class="identifier">attr_cast</span><span class="special"><</span><span class="identifier">Exposed</span><span class="special">>(</span><span class="identifier">g</span><span class="special">)</span> 150<span class="identifier">attr_cast</span><span class="special"><</span><span class="identifier">Exposed</span><span class="special">,</span> <span class="identifier">Transformed</span><span class="special">>(</span><span class="identifier">g</span><span class="special">)</span> 151</pre> 152<p> 153 depending on which of the attribute types can be deduced properly if 154 not explicitly specified. 155 </p> 156<h6> 157<a name="spirit.karma.reference.auxiliary.attr_cast.h5"></a> 158 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.model_of"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.model_of">Model of</a> 159 </h6> 160<div class="blockquote"><blockquote class="blockquote"><p> 161 <a class="link" href="../generator_concepts/unarygenerator.html" title="UnaryGenerator"><code class="computeroutput"><span class="identifier">UnaryGenerator</span></code></a> 162 </p></blockquote></div> 163<div class="variablelist"> 164<p class="title"><b>Notation</b></p> 165<dl class="variablelist"> 166<dt><span class="term"><code class="computeroutput"><span class="identifier">g</span></code></span></dt> 167<dd><p> 168 A generator object. 169 </p></dd> 170</dl> 171</div> 172<h6> 173<a name="spirit.karma.reference.auxiliary.attr_cast.h6"></a> 174 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.expression_semantics"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.expression_semantics">Expression 175 Semantics</a> 176 </h6> 177<p> 178 Semantics of an expression is defined only where it differs from, or 179 is not defined in <a class="link" href="../generator_concepts/unarygenerator.html" title="UnaryGenerator"><code class="computeroutput"><span class="identifier">UnaryGenerator</span></code></a>. 180 </p> 181<div class="informaltable"><table class="table"> 182<colgroup> 183<col> 184<col> 185</colgroup> 186<thead><tr> 187<th> 188 <p> 189 Expression 190 </p> 191 </th> 192<th> 193 <p> 194 Semantics 195 </p> 196 </th> 197</tr></thead> 198<tbody> 199<tr> 200<td> 201 <p> 202 <code class="computeroutput"><span class="identifier">attr_cast</span><span class="special">(</span><span class="identifier">g</span><span class="special">)</span></code> 203 </p> 204 </td> 205<td> 206 <p> 207 Create a component invoking the generator <code class="computeroutput"><span class="identifier">g</span></code> 208 while passing an attribute of the type as normally expected 209 by <code class="computeroutput"><span class="identifier">g</span></code>. The type 210 of the supplied attribute will be transformed to the type 211 <code class="computeroutput"><span class="identifier">g</span></code> exposes as 212 its attribute type (by using the attribute customization point 213 <a class="link" href="../../../advanced/customize/transform.html" title="Transform an Attribute to a Different Type (Qi and Karma)"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">transform_attribute</span></code></a>). 214 This generator does not fail unless <code class="computeroutput"><span class="identifier">g</span></code> 215 fails. 216 </p> 217 </td> 218</tr> 219<tr> 220<td> 221 <p> 222 <code class="computeroutput"><span class="identifier">attr_cast</span><span class="special"><</span><span class="identifier">Exposed</span><span class="special">>(</span><span class="identifier">g</span><span class="special">)</span></code> 223 </p> 224 </td> 225<td> 226 <p> 227 Create a component invoking the generator <code class="computeroutput"><span class="identifier">g</span></code> 228 while passing an attribute of the type as normally expected 229 by <code class="computeroutput"><span class="identifier">g</span></code>. The supplied 230 attribute is expected to be of the type <code class="computeroutput"><span class="identifier">Exposed</span></code>, 231 it will be transformed to the type <code class="computeroutput"><span class="identifier">g</span></code> 232 exposes as its attribute type (using the attribute customization 233 point <a class="link" href="../../../advanced/customize/transform.html" title="Transform an Attribute to a Different Type (Qi and Karma)"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">transform_attribute</span></code></a>). 234 This generator does not fail unless <code class="computeroutput"><span class="identifier">g</span></code> 235 fails. 236 </p> 237 </td> 238</tr> 239<tr> 240<td> 241 <p> 242 <code class="computeroutput"><span class="identifier">attr_cast</span><span class="special"><</span><span class="identifier">Exposed</span><span class="special">,</span> 243 <span class="identifier">Transformed</span><span class="special">>(</span><span class="identifier">g</span><span class="special">)</span></code> 244 </p> 245 </td> 246<td> 247 <p> 248 Create a component invoking the generator <code class="computeroutput"><span class="identifier">g</span></code> 249 while passing an attribute of type <code class="computeroutput"><span class="identifier">Transformed</span></code>. 250 The supplied attribute is expected to be of the type <code class="computeroutput"><span class="identifier">Exposed</span></code>, it will be transformed 251 to the type <code class="computeroutput"><span class="identifier">Transformed</span></code> 252 (using the attribute customization point <a class="link" href="../../../advanced/customize/transform.html" title="Transform an Attribute to a Different Type (Qi and Karma)"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">transform_attribute</span></code></a>). 253 This generator does not fail unless <code class="computeroutput"><span class="identifier">g</span></code> 254 fails. 255 </p> 256 </td> 257</tr> 258</tbody> 259</table></div> 260<h6> 261<a name="spirit.karma.reference.auxiliary.attr_cast.h7"></a> 262 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.attributes"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.attributes">Attributes</a> 263 </h6> 264<div class="informaltable"><table class="table"> 265<colgroup> 266<col> 267<col> 268</colgroup> 269<thead><tr> 270<th> 271 <p> 272 Expression 273 </p> 274 </th> 275<th> 276 <p> 277 Attribute 278 </p> 279 </th> 280</tr></thead> 281<tbody> 282<tr> 283<td> 284 <p> 285 <code class="computeroutput"><span class="identifier">attr_cast</span><span class="special">(</span><span class="identifier">g</span><span class="special">)</span></code> 286 </p> 287 </td> 288<td> 289 <p> 290 <code class="computeroutput"><span class="identifier">g</span><span class="special">:</span> 291 <span class="identifier">A</span> <span class="special">--></span> 292 <span class="identifier">attr_cast</span><span class="special">(</span><span class="identifier">g</span><span class="special">):</span> 293 <span class="identifier">A</span></code> 294 </p> 295 </td> 296</tr> 297<tr> 298<td> 299 <p> 300 <code class="computeroutput"><span class="identifier">attr_cast</span><span class="special"><</span><span class="identifier">Exposed</span><span class="special">>(</span><span class="identifier">g</span><span class="special">)</span></code> 301 </p> 302 </td> 303<td> 304 <p> 305 <code class="computeroutput"><span class="identifier">g</span><span class="special">:</span> 306 <span class="identifier">A</span> <span class="special">--></span> 307 <span class="identifier">attr_cast</span><span class="special"><</span><span class="identifier">Exposed</span><span class="special">>(</span><span class="identifier">g</span><span class="special">):</span> 308 <span class="identifier">Exposed</span></code> 309 </p> 310 </td> 311</tr> 312<tr> 313<td> 314 <p> 315 <code class="computeroutput"><span class="identifier">attr_cast</span><span class="special"><</span><span class="identifier">Exposed</span><span class="special">,</span> 316 <span class="identifier">Transformed</span><span class="special">>(</span><span class="identifier">g</span><span class="special">)</span></code> 317 </p> 318 </td> 319<td> 320 <p> 321 <code class="computeroutput"><span class="identifier">g</span><span class="special">:</span> 322 <span class="identifier">A</span> <span class="special">--></span> 323 <span class="identifier">attr_cast</span><span class="special"><</span><span class="identifier">Exposed</span><span class="special">,</span> 324 <span class="identifier">Transformed</span><span class="special">>(</span><span class="identifier">g</span><span class="special">):</span> 325 <span class="identifier">Exposed</span></code> 326 </p> 327 </td> 328</tr> 329</tbody> 330</table></div> 331<h6> 332<a name="spirit.karma.reference.auxiliary.attr_cast.h8"></a> 333 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.complexity"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.complexity">Complexity</a> 334 </h6> 335<div class="blockquote"><blockquote class="blockquote"><p> 336 The complexity of this component is fully defined by the complexity 337 of the embedded generator <code class="computeroutput"><span class="identifier">g</span></code>. 338 </p></blockquote></div> 339<h6> 340<a name="spirit.karma.reference.auxiliary.attr_cast.h9"></a> 341 <span class="phrase"><a name="spirit.karma.reference.auxiliary.attr_cast.example"></a></span><a class="link" href="attr_cast.html#spirit.karma.reference.auxiliary.attr_cast.example">Example</a> 342 </h6> 343<div class="note"><table border="0" summary="Note"> 344<tr> 345<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td> 346<th align="left">Note</th> 347</tr> 348<tr><td align="left" valign="top"><p> 349 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> 350 section. 351 </p></td></tr> 352</table></div> 353<p> 354 Some includes: 355 </p> 356<p> 357</p> 358<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> 359<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> 360<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> 361<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> 362<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> 363<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 364<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 365</pre> 366<p> 367 </p> 368<p> 369 Some using declarations: 370 </p> 371<p> 372</p> 373<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">int_</span><span class="special">;</span> 374</pre> 375<p> 376 </p> 377<p> 378 The example references data structure <code class="computeroutput"><span class="identifier">int_data</span></code> 379 which needs a specialization of the customization point <a class="link" href="../../../advanced/customize/transform.html" title="Transform an Attribute to a Different Type (Qi and Karma)"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">transform_attribute</span></code></a>: 380 </p> 381<p> 382</p> 383<pre class="programlisting"><span class="comment">// this is just a test structure we want to use in place of an int</span> 384<span class="keyword">struct</span> <span class="identifier">int_data</span> 385<span class="special">{</span> 386 <span class="keyword">int</span> <span class="identifier">i</span><span class="special">;</span> 387<span class="special">};</span> 388 389<span class="comment">// we provide a custom attribute transformation to allow its use as an int</span> 390<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">spirit</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">traits</span> 391<span class="special">{</span> 392 <span class="keyword">template</span> <span class="special"><></span> 393 <span class="keyword">struct</span> <span class="identifier">transform_attribute</span><span class="special"><</span><span class="identifier">int_data</span> <span class="keyword">const</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">karma</span><span class="special">::</span><span class="identifier">domain</span><span class="special">></span> 394 <span class="special">{</span> 395 <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">type</span><span class="special">;</span> 396 <span class="keyword">static</span> <span class="keyword">int</span> <span class="identifier">pre</span><span class="special">(</span><span class="identifier">int_data</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">d</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">i</span><span class="special">;</span> <span class="special">}</span> 397 <span class="special">};</span> 398<span class="special">}}}</span> 399</pre> 400<p> 401 </p> 402<p> 403 Now we use the <code class="computeroutput"><span class="identifier">attr_cast</span></code> 404 pseudo generator to invoke the attribute transformation: 405 </p> 406<p> 407</p> 408<pre class="programlisting"><span class="identifier">int_data</span> <span class="identifier">d</span> <span class="special">=</span> <span class="special">{</span> <span class="number">1</span> <span class="special">};</span> 409<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">"1"</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">karma</span><span class="special">::</span><span class="identifier">attr_cast</span><span class="special">(</span><span class="identifier">int_</span><span class="special">),</span> <span class="identifier">d</span><span class="special">);</span> 410</pre> 411<p> 412 </p> 413</div> 414<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 415<td align="left"></td> 416<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p> 417 Distributed under the Boost Software License, Version 1.0. (See accompanying 418 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>) 419 </p> 420</div></td> 421</tr></table> 422<hr> 423<div class="spirit-nav"> 424<a accesskey="p" href="../auxiliary.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../auxiliary.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="eol.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 425</div> 426</body> 427</html> 428