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