1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Dereference the Iterator pointing into a Container Attribute</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="../iterate.html" title="Extract Attribute Values to Generate Output from a Container (Karma)"> 9<link rel="prev" href="next_iterator.html" title="Increment the Iterator pointing into a Container Attribute"> 10<link rel="next" href="compare_iterators.html" title="Compare two Iterator pointing into a Container Attribute for Equality"> 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="next_iterator.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../iterate.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="compare_iterators.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.advanced.customize.iterate.deref_iterator"></a><a class="link" href="deref_iterator.html" title="Dereference the Iterator pointing into a Container Attribute">Dereference 28 the Iterator pointing into a Container Attribute</a> 29</h5></div></div></div> 30<h6> 31<a name="spirit.advanced.customize.iterate.deref_iterator.h0"></a> 32 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.deref_iterator"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.deref_iterator">deref_iterator</a> 33 </h6> 34<p> 35 The template <code class="computeroutput"><span class="identifier">deref_iterator</span></code> 36 is a type used as an attribute customization point. It is invoked by 37 the <span class="emphasis"><em>Karma</em></span> repetitive generators (such as <a class="link" href="../../../karma/reference/operator/list.html" title="List Generator (a % b)">List 38 (<code class="computeroutput"><span class="special">%</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/kleene.html" title="Kleene Star Generator (*a)">Kleene 39 (unary <code class="computeroutput"><span class="special">*</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/plus.html" title="Plus Generator (+a)">Plus (unary <code class="computeroutput"><span class="special">+</span></code>)</a>, and <a class="link" href="../../../karma/reference/directive/repeat.html" title="Repetition Generator Directive (repeat[])">Repeat</a>) 40 in order to dereference an iterator pointing to an element of a container 41 holding the attributes to generate output from. 42 </p> 43<h6> 44<a name="spirit.advanced.customize.iterate.deref_iterator.h1"></a> 45 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.module_headers"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.module_headers">Module 46 Headers</a> 47 </h6> 48<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">home</span><span class="special">/</span><span class="identifier">support</span><span class="special">/</span><span class="identifier">container</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<div class="note"><table border="0" summary="Note"> 54<tr> 55<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td> 56<th align="left">Note</th> 57</tr> 58<tr><td align="left" valign="top"><p> 59 This header file does not need to be included directly by any user 60 program as it is normally included by other Spirit header files relying 61 on its content. 62 </p></td></tr> 63</table></div> 64<h6> 65<a name="spirit.advanced.customize.iterate.deref_iterator.h2"></a> 66 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.namespace"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.namespace">Namespace</a> 67 </h6> 68<div class="informaltable"><table class="table"> 69<colgroup><col></colgroup> 70<thead><tr><th> 71 <p> 72 Name 73 </p> 74 </th></tr></thead> 75<tbody><tr><td> 76 <p> 77 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">traits</span></code> 78 </p> 79 </td></tr></tbody> 80</table></div> 81<h6> 82<a name="spirit.advanced.customize.iterate.deref_iterator.h3"></a> 83 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.synopsis"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.synopsis">Synopsis</a> 84 </h6> 85<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Enable</span><span class="special">></span> 86<span class="keyword">struct</span> <span class="identifier">deref_iterator</span> 87<span class="special">{</span> 88 <span class="keyword">typedef</span> <span class="special"><</span><span class="identifier">unspecified</span><span class="special">></span> <span class="identifier">type</span><span class="special">;</span> 89 <span class="keyword">static</span> <span class="identifier">type</span> <span class="identifier">call</span><span class="special">(</span><span class="identifier">Iterator</span><span class="special">&</span> <span class="identifier">it</span><span class="special">);</span> 90<span class="special">};</span> 91</pre> 92<h6> 93<a name="spirit.advanced.customize.iterate.deref_iterator.h4"></a> 94 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.template_parameters"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.template_parameters">Template 95 parameters</a> 96 </h6> 97<div class="informaltable"><table class="table"> 98<colgroup> 99<col> 100<col> 101<col> 102</colgroup> 103<thead><tr> 104<th> 105 <p> 106 Parameter 107 </p> 108 </th> 109<th> 110 <p> 111 Description 112 </p> 113 </th> 114<th> 115 <p> 116 Default 117 </p> 118 </th> 119</tr></thead> 120<tbody> 121<tr> 122<td> 123 <p> 124 <code class="computeroutput"><span class="identifier">Iterator</span></code> 125 </p> 126 </td> 127<td> 128 <p> 129 The type, <code class="computeroutput"><span class="identifier">Iterator</span></code> 130 of the iterator to dereference. This is the same as the type 131 returned by the customization point <a class="link" href="container_iterator.html" title="Determine the Type of the Iterator of a Container"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">container_iterator</span></code></a>. 132 </p> 133 </td> 134<td> 135 <p> 136 none 137 </p> 138 </td> 139</tr> 140<tr> 141<td> 142 <p> 143 <code class="computeroutput"><span class="identifier">Enable</span></code> 144 </p> 145 </td> 146<td> 147 <p> 148 Helper template parameter usable to selectively enable or disable 149 certain specializations of <code class="computeroutput"><span class="identifier">deref_iterator</span></code> 150 utilizing SFINAE (i.e. <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">enable_if</span></code> 151 or <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">disable_if</span></code>). 152 </p> 153 </td> 154<td> 155 <p> 156 <code class="computeroutput"><span class="keyword">void</span></code> 157 </p> 158 </td> 159</tr> 160</tbody> 161</table></div> 162<div class="variablelist"> 163<p class="title"><b>Notation</b></p> 164<dl class="variablelist"> 165<dt><span class="term"><code class="computeroutput"><span class="identifier">Iterator</span></code></span></dt> 166<dd><p> 167 An iterator type. 168 </p></dd> 169<dt><span class="term"><code class="computeroutput"><span class="identifier">it</span></code></span></dt> 170<dd><p> 171 An instance of an iterator of type <code class="computeroutput"><span class="identifier">Iterator</span></code>. 172 </p></dd> 173<dt><span class="term"><code class="computeroutput"><span class="identifier">C</span></code></span></dt> 174<dd><p> 175 A container type whose iterator type is <code class="computeroutput"><span class="identifier">Iterator</span></code>. 176 </p></dd> 177</dl> 178</div> 179<h6> 180<a name="spirit.advanced.customize.iterate.deref_iterator.h5"></a> 181 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.expression_semantics"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.expression_semantics">Expression 182 Semantics</a> 183 </h6> 184<div class="informaltable"><table class="table"> 185<colgroup> 186<col> 187<col> 188</colgroup> 189<thead><tr> 190<th> 191 <p> 192 Expression 193 </p> 194 </th> 195<th> 196 <p> 197 Semantics 198 </p> 199 </th> 200</tr></thead> 201<tbody> 202<tr> 203<td> 204 <p> 205 <code class="computeroutput"><span class="identifier">deref_iterator</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">>::</span><span class="identifier">type</span></code> 206 </p> 207 </td> 208<td> 209 <p> 210 Metafunction result evaluating to the type returned by dereferencing 211 the iterator. 212 </p> 213 </td> 214</tr> 215<tr> 216<td> 217 <p> 218 <code class="computeroutput"><span class="identifier">deref_iterator</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">>::</span><span class="identifier">call</span><span class="special">(</span><span class="identifier">it</span><span class="special">)</span></code> 219 </p> 220 </td> 221<td> 222 <p> 223 Return the element in the container referred to by the iterator. 224 The type of the returned value is the same as returned by the 225 metafunction result <code class="computeroutput"><span class="identifier">type</span></code>. 226 </p> 227 </td> 228</tr> 229</tbody> 230</table></div> 231<h6> 232<a name="spirit.advanced.customize.iterate.deref_iterator.h6"></a> 233 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.predefined_specializations"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.predefined_specializations">Predefined 234 Specializations</a> 235 </h6> 236<p> 237 <a href="http://boost-spirit.com" target="_top">Spirit</a> predefines specializations 238 of this customization point for several types. The following table lists 239 those types together with the types returned by the embedded typedef 240 <code class="computeroutput"><span class="identifier">type</span></code>: 241 </p> 242<div class="informaltable"><table class="table"> 243<colgroup> 244<col> 245<col> 246</colgroup> 247<thead><tr> 248<th> 249 <p> 250 Template Parameters 251 </p> 252 </th> 253<th> 254 <p> 255 Semantics 256 </p> 257 </th> 258</tr></thead> 259<tbody> 260<tr> 261<td> 262 <p> 263 <code class="computeroutput"><span class="identifier">Iterator</span></code> 264 </p> 265 </td> 266<td> 267 <p> 268 The metafunction result <code class="computeroutput"><span class="identifier">type</span></code> 269 evaluates to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">iterator_traits</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">>::</span><span class="identifier">reference</span></code> 270 and the function <code class="computeroutput"><span class="identifier">call</span><span class="special">()</span></code> returns <code class="computeroutput"><span class="special">*</span><span class="identifier">it</span></code>. 271 </p> 272 </td> 273</tr> 274<tr> 275<td> 276 <p> 277 <code class="computeroutput"><span class="identifier">unused_type</span></code><code class="computeroutput"> 278 <span class="keyword">const</span><span class="special">*</span></code> 279 </p> 280 </td> 281<td> 282 <p> 283 The metafunction result <code class="computeroutput"><span class="identifier">type</span></code> 284 evaluates to <code class="computeroutput"><span class="identifier">unused_type</span></code> 285 and the function <code class="computeroutput"><span class="identifier">call</span><span class="special">()</span></code> returns <code class="computeroutput"><span class="identifier">unused</span></code>. 286 </p> 287 </td> 288</tr> 289</tbody> 290</table></div> 291<h6> 292<a name="spirit.advanced.customize.iterate.deref_iterator.h7"></a> 293 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.when_to_implement"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.when_to_implement">When 294 to implement</a> 295 </h6> 296<p> 297 The customization point <code class="computeroutput"><span class="identifier">deref_iterator</span></code> 298 needs to be implemented for a specific iterator type whenever the container 299 this iterator belongs to is to be used as an attribute in place of a 300 STL container. It is applicable for generators (<span class="emphasis"><em>Spirit.Karma</em></span>) 301 only. As a rule of thumb: it has to be implemented whenever a certain 302 iterator type belongs to a container which is to be passed as an attribute 303 to a generator normally exposing a STL container, <code class="computeroutput"><span class="identifier">C</span></code> 304 and if the container type does not expose the interface of a STL container 305 (i.e. <code class="computeroutput"><span class="identifier">is_container</span><span class="special"><</span><span class="identifier">C</span><span class="special">>::</span><span class="identifier">type</span></code> would normally return <code class="computeroutput"><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">false_</span></code>). 306 </p> 307<h6> 308<a name="spirit.advanced.customize.iterate.deref_iterator.h8"></a> 309 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.related_attribute_customization_points"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.related_attribute_customization_points">Related 310 Attribute Customization Points</a> 311 </h6> 312<p> 313 If this customization point is implemented, the following other customization 314 points might need to be implemented as well. 315 </p> 316<div class="informaltable"><table class="table"> 317<colgroup> 318<col> 319<col> 320</colgroup> 321<thead><tr> 322<th> 323 <p> 324 Name 325 </p> 326 </th> 327<th> 328 <p> 329 When to implement 330 </p> 331 </th> 332</tr></thead> 333<tbody> 334<tr> 335<td> 336 <p> 337 <a class="link" href="../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> 338 </p> 339 </td> 340<td> 341 <p> 342 Needs to be implemented whenever a type is to be used as a 343 container attribute in <span class="emphasis"><em>Karma</em></span>. 344 </p> 345 </td> 346</tr> 347<tr> 348<td> 349 <p> 350 <a class="link" href="container_iterator.html" title="Determine the Type of the Iterator of a Container"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">container_iterator</span></code></a> 351 </p> 352 </td> 353<td> 354 <p> 355 Karma: <a class="link" href="../../../karma/reference/operator/list.html" title="List Generator (a % b)">List 356 (<code class="computeroutput"><span class="special">%</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/kleene.html" title="Kleene Star Generator (*a)">Kleene (unary 357 <code class="computeroutput"><span class="special">*</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/plus.html" title="Plus Generator (+a)">Plus (unary 358 <code class="computeroutput"><span class="special">+</span></code>)</a>, <a class="link" href="../../../karma/reference/directive/repeat.html" title="Repetition Generator Directive (repeat[])">Repeat</a>. 359 </p> 360 </td> 361</tr> 362<tr> 363<td> 364 <p> 365 <a class="link" href="begin_container.html" title="Get the Iterator pointing to the Begin of a Container Attribute"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">begin_container</span></code></a> 366 </p> 367 </td> 368<td> 369 <p> 370 Karma: <a class="link" href="../../../karma/reference/operator/list.html" title="List Generator (a % b)">List 371 (<code class="computeroutput"><span class="special">%</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/kleene.html" title="Kleene Star Generator (*a)">Kleene (unary 372 <code class="computeroutput"><span class="special">*</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/plus.html" title="Plus Generator (+a)">Plus (unary 373 <code class="computeroutput"><span class="special">+</span></code>)</a>, <a class="link" href="../../../karma/reference/directive/repeat.html" title="Repetition Generator Directive (repeat[])">Repeat</a>. 374 </p> 375 </td> 376</tr> 377<tr> 378<td> 379 <p> 380 <a class="link" href="end_container.html" title="Get the Iterator pointing to the End of a Container Attribute"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">end_container</span></code></a> 381 </p> 382 </td> 383<td> 384 <p> 385 Karma: <a class="link" href="../../../karma/reference/operator/list.html" title="List Generator (a % b)">List 386 (<code class="computeroutput"><span class="special">%</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/kleene.html" title="Kleene Star Generator (*a)">Kleene (unary 387 <code class="computeroutput"><span class="special">*</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/plus.html" title="Plus Generator (+a)">Plus (unary 388 <code class="computeroutput"><span class="special">+</span></code>)</a>, <a class="link" href="../../../karma/reference/directive/repeat.html" title="Repetition Generator Directive (repeat[])">Repeat</a>. 389 </p> 390 </td> 391</tr> 392<tr> 393<td> 394 <p> 395 <a class="link" href="deref_iterator.html" title="Dereference the Iterator pointing into a Container Attribute"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">deref_iterator</span></code></a> 396 </p> 397 </td> 398<td> 399 <p> 400 Karma: <a class="link" href="../../../karma/reference/operator/list.html" title="List Generator (a % b)">List 401 (<code class="computeroutput"><span class="special">%</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/kleene.html" title="Kleene Star Generator (*a)">Kleene (unary 402 <code class="computeroutput"><span class="special">*</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/plus.html" title="Plus Generator (+a)">Plus (unary 403 <code class="computeroutput"><span class="special">+</span></code>)</a>, <a class="link" href="../../../karma/reference/directive/repeat.html" title="Repetition Generator Directive (repeat[])">Repeat</a>. 404 </p> 405 </td> 406</tr> 407<tr> 408<td> 409 <p> 410 <a class="link" href="next_iterator.html" title="Increment the Iterator pointing into a Container Attribute"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">next_iterator</span></code></a> 411 </p> 412 </td> 413<td> 414 <p> 415 Karma: <a class="link" href="../../../karma/reference/operator/list.html" title="List Generator (a % b)">List 416 (<code class="computeroutput"><span class="special">%</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/kleene.html" title="Kleene Star Generator (*a)">Kleene (unary 417 <code class="computeroutput"><span class="special">*</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/plus.html" title="Plus Generator (+a)">Plus (unary 418 <code class="computeroutput"><span class="special">+</span></code>)</a>, <a class="link" href="../../../karma/reference/directive/repeat.html" title="Repetition Generator Directive (repeat[])">Repeat</a>. 419 </p> 420 </td> 421</tr> 422<tr> 423<td> 424 <p> 425 <a class="link" href="compare_iterators.html" title="Compare two Iterator pointing into a Container Attribute for Equality"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">compare_iterators</span></code></a> 426 </p> 427 </td> 428<td> 429 <p> 430 Karma: <a class="link" href="../../../karma/reference/operator/list.html" title="List Generator (a % b)">List 431 (<code class="computeroutput"><span class="special">%</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/kleene.html" title="Kleene Star Generator (*a)">Kleene (unary 432 <code class="computeroutput"><span class="special">*</span></code>)</a>, <a class="link" href="../../../karma/reference/operator/plus.html" title="Plus Generator (+a)">Plus (unary 433 <code class="computeroutput"><span class="special">+</span></code>)</a>, <a class="link" href="../../../karma/reference/directive/repeat.html" title="Repetition Generator Directive (repeat[])">Repeat</a>. 434 </p> 435 </td> 436</tr> 437</tbody> 438</table></div> 439<h6> 440<a name="spirit.advanced.customize.iterate.deref_iterator.h9"></a> 441 <span class="phrase"><a name="spirit.advanced.customize.iterate.deref_iterator.example"></a></span><a class="link" href="deref_iterator.html#spirit.advanced.customize.iterate.deref_iterator.example">Example</a> 442 </h6> 443<p> 444 Here are the header files needed to make the example code below compile: 445 </p> 446<p> 447</p> 448<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> 449<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 450<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">vector</span><span class="special">></span> 451</pre> 452<p> 453 </p> 454<p> 455 The example (for the full source code please see here: <a href="../../../../../../example/karma/customize_counter.cpp" target="_top">customize_counter.cpp</a>) 456 uses the data structure 457 </p> 458<p> 459</p> 460<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">client</span> 461<span class="special">{</span> 462 <span class="keyword">struct</span> <span class="identifier">counter</span> 463 <span class="special">{</span> 464 <span class="comment">// expose the current value of the counter as our iterator</span> 465 <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">iterator</span><span class="special">;</span> 466 467 <span class="comment">// expose 'int' as the type of each generated element</span> 468 <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">type</span><span class="special">;</span> 469 470 <span class="identifier">counter</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">max_count</span><span class="special">)</span> 471 <span class="special">:</span> <span class="identifier">counter_</span><span class="special">(</span><span class="number">0</span><span class="special">),</span> <span class="identifier">max_count_</span><span class="special">(</span><span class="identifier">max_count</span><span class="special">)</span> 472 <span class="special">{}</span> 473 474 <span class="keyword">int</span> <span class="identifier">counter_</span><span class="special">;</span> 475 <span class="keyword">int</span> <span class="identifier">max_count_</span><span class="special">;</span> 476 <span class="special">};</span> 477<span class="special">}</span> 478</pre> 479<p> 480 </p> 481<p> 482 as a direct attribute to the <a class="link" href="../../../karma/reference/operator/list.html" title="List Generator (a % b)">List 483 (<code class="computeroutput"><span class="special">%</span></code>)</a> generator. This 484 type does not expose any of the interfaces of an STL container. It does 485 not even expose the usual semantics of a container. The presented customization 486 points build a counter instance which is incremented each time it is 487 accessed. The examples shows how to enable its use as an attribute to 488 <span class="emphasis"><em>Karma's</em></span> repetitive generators. 489 </p> 490<p> 491 In order to make this data structure compatible we need to specialize 492 a couple of attribute customization points: <a class="link" href="../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>, <a class="link" href="container_iterator.html" title="Determine the Type of the Iterator of a Container"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">container_iterator</span></code></a>, <a class="link" href="begin_container.html" title="Get the Iterator pointing to the Begin of a Container Attribute"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">begin_container</span></code></a>, and <a class="link" href="end_container.html" title="Get the Iterator pointing to the End of a Container Attribute"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">end_container</span></code></a>. In addition, 493 we specialize one of the iterator related customization points as well: 494 <a class="link" href="deref_iterator.html" title="Dereference the Iterator pointing into a Container Attribute"><code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">deref_iterator</span></code></a>. 495 </p> 496<p> 497</p> 498<pre class="programlisting"><span class="comment">// All specializations of attribute customization points have to be placed into</span> 499<span class="comment">// the namespace boost::spirit::traits.</span> 500<span class="comment">//</span> 501<span class="comment">// Note that all templates below are specialized using the 'const' type.</span> 502<span class="comment">// This is necessary as all attributes in Karma are 'const'.</span> 503<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> 504<span class="special">{</span> 505 <span class="comment">// The specialization of the template 'is_container<>' will tell the </span> 506 <span class="comment">// library to treat the type 'client::counter' as a container providing </span> 507 <span class="comment">// the items to generate output from.</span> 508 <span class="keyword">template</span> <span class="special"><></span> 509 <span class="keyword">struct</span> <span class="identifier">is_container</span><span class="special"><</span><span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span> <span class="keyword">const</span><span class="special">></span> 510 <span class="special">:</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">true_</span> 511 <span class="special">{};</span> 512 513 <span class="comment">// The specialization of the template 'container_iterator<>' will be</span> 514 <span class="comment">// invoked by the library to evaluate the iterator type to be used</span> 515 <span class="comment">// for iterating the data elements in the container. </span> 516 <span class="keyword">template</span> <span class="special"><></span> 517 <span class="keyword">struct</span> <span class="identifier">container_iterator</span><span class="special"><</span><span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span> <span class="keyword">const</span><span class="special">></span> 518 <span class="special">{</span> 519 <span class="keyword">typedef</span> <span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">type</span><span class="special">;</span> 520 <span class="special">};</span> 521 522 <span class="comment">// The specialization of the templates 'begin_container<>' and </span> 523 <span class="comment">// 'end_container<>' below will be used by the library to get the iterators </span> 524 <span class="comment">// pointing to the begin and the end of the data to generate output from. </span> 525 <span class="comment">// These specializations respectively return the initial and maximum </span> 526 <span class="comment">// counter values.</span> 527 <span class="comment">//</span> 528 <span class="comment">// The passed argument refers to the attribute instance passed to the list </span> 529 <span class="comment">// generator.</span> 530 <span class="keyword">template</span> <span class="special"><></span> 531 <span class="keyword">struct</span> <span class="identifier">begin_container</span><span class="special"><</span><span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span> <span class="keyword">const</span><span class="special">></span> 532 <span class="special">{</span> 533 <span class="keyword">static</span> <span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span><span class="special">::</span><span class="identifier">iterator</span> 534 <span class="identifier">call</span><span class="special">(</span><span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">c</span><span class="special">)</span> 535 <span class="special">{</span> 536 <span class="keyword">return</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">counter_</span><span class="special">;</span> 537 <span class="special">}</span> 538 <span class="special">};</span> 539 540 <span class="keyword">template</span> <span class="special"><></span> 541 <span class="keyword">struct</span> <span class="identifier">end_container</span><span class="special"><</span><span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span> <span class="keyword">const</span><span class="special">></span> 542 <span class="special">{</span> 543 <span class="keyword">static</span> <span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span><span class="special">::</span><span class="identifier">iterator</span> 544 <span class="identifier">call</span><span class="special">(</span><span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">c</span><span class="special">)</span> 545 <span class="special">{</span> 546 <span class="keyword">return</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">max_count_</span><span class="special">;</span> 547 <span class="special">}</span> 548 <span class="special">};</span> 549<span class="special">}}}</span> 550</pre> 551<p> 552 </p> 553<p> 554</p> 555<pre class="programlisting"><span class="comment">// All specializations of attribute customization points have to be placed into</span> 556<span class="comment">// the namespace boost::spirit::traits.</span> 557<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> 558<span class="special">{</span> 559 <span class="comment">// The specialization of the template 'deref_iterator<>' will be used to </span> 560 <span class="comment">// dereference the iterator associated with our counter data structure.</span> 561 <span class="comment">// Since we expose the current value as the iterator we just return the </span> 562 <span class="comment">// current iterator as the return value.</span> 563 <span class="keyword">template</span> <span class="special"><></span> 564 <span class="keyword">struct</span> <span class="identifier">deref_iterator</span><span class="special"><</span><span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span><span class="special">::</span><span class="identifier">iterator</span><span class="special">></span> 565 <span class="special">{</span> 566 <span class="keyword">typedef</span> <span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span><span class="special">::</span><span class="identifier">type</span> <span class="identifier">type</span><span class="special">;</span> 567 568 <span class="keyword">static</span> <span class="identifier">type</span> <span class="identifier">call</span><span class="special">(</span><span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span><span class="special">::</span><span class="identifier">iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">it</span><span class="special">)</span> 569 <span class="special">{</span> 570 <span class="keyword">return</span> <span class="identifier">it</span><span class="special">;</span> 571 <span class="special">}</span> 572 <span class="special">};</span> 573<span class="special">}}}</span> 574</pre> 575<p> 576 </p> 577<p> 578 The last code snippet shows an example using an instance of the data 579 structure <code class="computeroutput"><span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span></code> to generate output from a 580 <a class="link" href="../../../karma/reference/operator/list.html" title="List Generator (a % b)">List (<code class="computeroutput"><span class="special">%</span></code>)</a> generator: 581 </p> 582<p> 583</p> 584<pre class="programlisting"><span class="comment">// use the instance of a 'client::counter' instead of a STL vector</span> 585<span class="identifier">client</span><span class="special">::</span><span class="identifier">counter</span> <span class="identifier">count</span><span class="special">(</span><span class="number">4</span><span class="special">);</span> 586<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">karma</span><span class="special">::</span><span class="identifier">format</span><span class="special">(</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">int_</span> <span class="special">%</span> <span class="string">", "</span><span class="special">,</span> <span class="identifier">count</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">// prints: '0, 1, 2, 3'</span> 587</pre> 588<p> 589 </p> 590<p> 591 As you can see, the specializations for the customization points as defined 592 above enable the seamless integration of the custom data structure without 593 having to modify the output format or the generator itself. 594 </p> 595<p> 596 For other examples of how to use the customization point <code class="computeroutput"><span class="identifier">deref_iterator</span></code> please see here: <a class="link" href="next_iterator.html#spirit.advanced.customize.iterate.next_iterator.example">use_as_container</a>. 597 </p> 598</div> 599<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 600<td align="left"></td> 601<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p> 602 Distributed under the Boost Software License, Version 1.0. (See accompanying 603 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>) 604 </p> 605</div></td> 606</tr></table> 607<hr> 608<div class="spirit-nav"> 609<a accesskey="p" href="next_iterator.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../iterate.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="compare_iterators.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 610</div> 611</body> 612</html> 613