iterator_adaptor.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
  7. <title>Iterator Adaptor</title>
  8. <meta name="author" content="David Abrahams, Jeremy Siek, Thomas Witt" />
  9. <meta name="organization" content="Boost Consulting, Indiana University Open Systems Lab, University of Hanover Institute for Transport Railway Operation and Construction" />
  10. <meta name="date" content="2006-09-11" />
  11. <meta name="copyright" content="Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003." />
  12. <link rel="stylesheet" href="../../../rst.css" type="text/css" />
  13. </head>
  14. <body>
  15. <div class="document" id="iterator-adaptor">
  16. <h1 class="title">Iterator Adaptor</h1>
  17. <table class="docinfo" frame="void" rules="none">
  18. <col class="docinfo-name" />
  19. <col class="docinfo-content" />
  20. <tbody valign="top">
  21. <tr><th class="docinfo-name">Author:</th>
  22. <td>David Abrahams, Jeremy Siek, Thomas Witt</td></tr>
  23. <tr><th class="docinfo-name">Contact:</th>
  24. <td><a class="first reference external" href="mailto:dave&#64;boost-consulting.com">dave&#64;boost-consulting.com</a>, <a class="reference external" href="mailto:jsiek&#64;osl.iu.edu">jsiek&#64;osl.iu.edu</a>, <a class="last reference external" href="mailto:witt&#64;ive.uni-hannover.de">witt&#64;ive.uni-hannover.de</a></td></tr>
  25. <tr><th class="docinfo-name">Organization:</th>
  26. <td><a class="first reference external" href="http://www.boost-consulting.com">Boost Consulting</a>, Indiana University <a class="reference external" href="http://www.osl.iu.edu">Open Systems
  27. Lab</a>, University of Hanover <a class="last reference external" href="http://www.ive.uni-hannover.de">Institute for Transport
  28. Railway Operation and Construction</a></td></tr>
  29. <tr><th class="docinfo-name">Date:</th>
  30. <td>2006-09-11</td></tr>
  31. <tr><th class="docinfo-name">Copyright:</th>
  32. <td>Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003.</td></tr>
  33. </tbody>
  34. </table>
  35. <!-- Distributed under the Boost -->
  36. <!-- Software License, Version 1.0. (See accompanying -->
  37. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  38. <table class="docutils field-list" frame="void" rules="none">
  39. <col class="field-name" />
  40. <col class="field-body" />
  41. <tbody valign="top">
  42. <tr class="field"><th class="field-name">abstract:</th><td class="field-body"></td>
  43. </tr>
  44. </tbody>
  45. </table>
  46. <!-- Distributed under the Boost -->
  47. <!-- Software License, Version 1.0. (See accompanying -->
  48. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  49. <!-- Version 1.1 of this ReStructuredText document corresponds to
  50. n1530_, the paper accepted by the LWG. -->
  51. <!-- Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. -->
  52. <p>Each specialization of the <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> class template is derived from
  53. a specialization of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>. The core interface functions
  54. expected by <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> are implemented in terms of the
  55. <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>'s <tt class="docutils literal"><span class="pre">Base</span></tt> template parameter. A class derived
  56. from <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> typically redefines some of the core
  57. interface functions to adapt the behavior of the <tt class="docutils literal"><span class="pre">Base</span></tt> type.
  58. Whether the derived class models any of the standard iterator concepts
  59. depends on the operations supported by the <tt class="docutils literal"><span class="pre">Base</span></tt> type and which
  60. core interface functions of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> are redefined in the
  61. <tt class="docutils literal"><span class="pre">Derived</span></tt> class.</p>
  62. <div class="contents topic" id="table-of-contents">
  63. <p class="topic-title first">Table of Contents</p>
  64. <ul class="simple">
  65. <li><a class="reference internal" href="#overview" id="id6">Overview</a></li>
  66. <li><a class="reference internal" href="#reference" id="id7">Reference</a><ul>
  67. <li><a class="reference internal" href="#iterator-adaptor-requirements" id="id8"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> requirements</a></li>
  68. <li><a class="reference internal" href="#iterator-adaptor-base-class-parameters" id="id9"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> base class parameters</a></li>
  69. <li><a class="reference internal" href="#iterator-adaptor-public-operations" id="id10"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> public operations</a></li>
  70. <li><a class="reference internal" href="#iterator-adaptor-protected-member-functions" id="id11"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> protected member functions</a></li>
  71. <li><a class="reference internal" href="#iterator-adaptor-private-member-functions" id="id12"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> private member functions</a></li>
  72. </ul>
  73. </li>
  74. <li><a class="reference internal" href="#tutorial-example" id="id13">Tutorial Example</a></li>
  75. </ul>
  76. </div>
  77. <div class="section" id="overview">
  78. <h1><a class="toc-backref" href="#id6">Overview</a></h1>
  79. <!-- Distributed under the Boost -->
  80. <!-- Software License, Version 1.0. (See accompanying -->
  81. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  82. <!-- Version 1.2 of this ReStructuredText document corresponds to
  83. n1530_, the paper accepted by the LWG for TR1. -->
  84. <!-- Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. -->
  85. <p>The <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> class template adapts some <tt class="docutils literal"><span class="pre">Base</span></tt><a class="footnote-reference" href="#base" id="id1"><sup>1</sup></a>
  86. type to create a new iterator. Instantiations of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>
  87. are derived from a corresponding instantiation of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>
  88. and implement the core behaviors in terms of the <tt class="docutils literal"><span class="pre">Base</span></tt> type. In
  89. essence, <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> merely forwards all operations to an
  90. instance of the <tt class="docutils literal"><span class="pre">Base</span></tt> type, which it stores as a member.</p>
  91. <table class="docutils footnote" frame="void" id="base" rules="none">
  92. <colgroup><col class="label" /><col /></colgroup>
  93. <tbody valign="top">
  94. <tr><td class="label">[1]</td><td><em>(<a class="fn-backref" href="#id1">1</a>, <a class="fn-backref" href="#id3">2</a>)</em> The term &quot;Base&quot; here does not refer to a base class and is
  95. not meant to imply the use of derivation. We have followed the lead
  96. of the standard library, which provides a base() function to access
  97. the underlying iterator object of a <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> adaptor.</td></tr>
  98. </tbody>
  99. </table>
  100. <p>The user of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> creates a class derived from an
  101. instantiation of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> and then selectively
  102. redefines some of the core member functions described in the
  103. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> core requirements table. The <tt class="docutils literal"><span class="pre">Base</span></tt> type need
  104. not meet the full requirements for an iterator; it need only
  105. support the operations used by the core interface functions of
  106. <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> that have not been redefined in the user's
  107. derived class.</p>
  108. <p>Several of the template parameters of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> default
  109. to <tt class="docutils literal"><span class="pre">use_default</span></tt>. This allows the
  110. user to make use of a default parameter even when she wants to
  111. specify a parameter later in the parameter list. Also, the
  112. defaults for the corresponding associated types are somewhat
  113. complicated, so metaprogramming is required to compute them, and
  114. <tt class="docutils literal"><span class="pre">use_default</span></tt> can help to simplify the implementation. Finally,
  115. the identity of the <tt class="docutils literal"><span class="pre">use_default</span></tt> type is not left unspecified
  116. because specification helps to highlight that the <tt class="docutils literal"><span class="pre">Reference</span></tt>
  117. template parameter may not always be identical to the iterator's
  118. <tt class="docutils literal"><span class="pre">reference</span></tt> type, and will keep users from making mistakes based on
  119. that assumption.</p>
  120. </div>
  121. <div class="section" id="reference">
  122. <h1><a class="toc-backref" href="#id7">Reference</a></h1>
  123. <!-- Distributed under the Boost -->
  124. <!-- Software License, Version 1.0. (See accompanying -->
  125. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  126. <!-- Version 1.4 of this ReStructuredText document corresponds to
  127. n1530_, the paper accepted by the LWG for TR1. -->
  128. <!-- Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. -->
  129. <pre class="literal-block">
  130. template &lt;
  131. class Derived
  132. , class Base
  133. , class Value = use_default
  134. , class CategoryOrTraversal = use_default
  135. , class Reference = use_default
  136. , class Difference = use_default
  137. &gt;
  138. class iterator_adaptor
  139. : public iterator_facade&lt;Derived, <em>V'</em>, <em>C'</em>, <em>R'</em>, <em>D'</em>&gt; // see <a class="reference internal" href="#base-parameters">details</a>
  140. {
  141. friend class iterator_core_access;
  142. public:
  143. iterator_adaptor();
  144. explicit iterator_adaptor(Base const&amp; iter);
  145. typedef Base base_type;
  146. Base const&amp; base() const;
  147. protected:
  148. typedef iterator_adaptor iterator_adaptor_;
  149. Base const&amp; base_reference() const;
  150. Base&amp; base_reference();
  151. private: // Core iterator interface for iterator_facade.
  152. typename iterator_adaptor::reference dereference() const;
  153. template &lt;
  154. class OtherDerived, class OtherIterator, class V, class C, class R, class D
  155. &gt;
  156. bool equal(iterator_adaptor&lt;OtherDerived, OtherIterator, V, C, R, D&gt; const&amp; x) const;
  157. void advance(typename iterator_adaptor::difference_type n);
  158. void increment();
  159. void decrement();
  160. template &lt;
  161. class OtherDerived, class OtherIterator, class V, class C, class R, class D
  162. &gt;
  163. typename iterator_adaptor::difference_type distance_to(
  164. iterator_adaptor&lt;OtherDerived, OtherIterator, V, C, R, D&gt; const&amp; y) const;
  165. private:
  166. Base m_iterator; // exposition only
  167. };
  168. </pre>
  169. <div class="section" id="iterator-adaptor-requirements">
  170. <span id="requirements"></span><h2><a class="toc-backref" href="#id8"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> requirements</a></h2>
  171. <p><tt class="docutils literal"><span class="pre">static_cast&lt;Derived*&gt;(iterator_adaptor*)</span></tt> shall be well-formed.
  172. The <tt class="docutils literal"><span class="pre">Base</span></tt> argument shall be Assignable and Copy Constructible.</p>
  173. </div>
  174. <div class="section" id="iterator-adaptor-base-class-parameters">
  175. <span id="base-parameters"></span><h2><a class="toc-backref" href="#id9"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> base class parameters</a></h2>
  176. <p>The <em>V'</em>, <em>C'</em>, <em>R'</em>, and <em>D'</em> parameters of the <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>
  177. used as a base class in the summary of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>
  178. above are defined as follows:</p>
  179. <pre class="literal-block">
  180. <em>V'</em> = if (Value is use_default)
  181. return iterator_traits&lt;Base&gt;::value_type
  182. else
  183. return Value
  184. <em>C'</em> = if (CategoryOrTraversal is use_default)
  185. return iterator_traversal&lt;Base&gt;::type
  186. else
  187. return CategoryOrTraversal
  188. <em>R'</em> = if (Reference is use_default)
  189. if (Value is use_default)
  190. return iterator_traits&lt;Base&gt;::reference
  191. else
  192. return Value&amp;
  193. else
  194. return Reference
  195. <em>D'</em> = if (Difference is use_default)
  196. return iterator_traits&lt;Base&gt;::difference_type
  197. else
  198. return Difference
  199. </pre>
  200. <!-- ``iterator_adaptor`` models
  201. - - - - - - - - - - - - - - - - - - - - - - - - - - -
  202. In order for ``Derived`` to model the iterator concepts corresponding
  203. to ``iterator_traits<Derived>::iterator_category``, the expressions
  204. involving ``m_iterator`` in the specifications of those private member
  205. functions of ``iterator_adaptor`` that may be called by
  206. ``iterator_facade<Derived, V, C, R, D>`` in evaluating any valid
  207. expression involving ``Derived`` in those concepts' requirements. -->
  208. <!-- The above is confusing and needs a rewrite. -JGS -->
  209. <!-- That's why it's removed. We're embracing inheritance, remember? -->
  210. </div>
  211. <div class="section" id="iterator-adaptor-public-operations">
  212. <h2><a class="toc-backref" href="#id10"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> public operations</a></h2>
  213. <p><tt class="docutils literal"><span class="pre">iterator_adaptor();</span></tt></p>
  214. <table class="docutils field-list" frame="void" rules="none">
  215. <col class="field-name" />
  216. <col class="field-body" />
  217. <tbody valign="top">
  218. <tr class="field"><th class="field-name">Requires:</th><td class="field-body">The <tt class="docutils literal"><span class="pre">Base</span></tt> type must be Default Constructible.</td>
  219. </tr>
  220. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> with
  221. <tt class="docutils literal"><span class="pre">m_iterator</span></tt> default constructed.</td>
  222. </tr>
  223. </tbody>
  224. </table>
  225. <p><tt class="docutils literal"><span class="pre">explicit</span> <span class="pre">iterator_adaptor(Base</span> <span class="pre">const&amp;</span> <span class="pre">iter);</span></tt></p>
  226. <table class="docutils field-list" frame="void" rules="none">
  227. <col class="field-name" />
  228. <col class="field-body" />
  229. <tbody valign="top">
  230. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> with
  231. <tt class="docutils literal"><span class="pre">m_iterator</span></tt> copy constructed from <tt class="docutils literal"><span class="pre">iter</span></tt>.</td>
  232. </tr>
  233. </tbody>
  234. </table>
  235. <p><tt class="docutils literal"><span class="pre">Base</span> <span class="pre">const&amp;</span> <span class="pre">base()</span> <span class="pre">const;</span></tt></p>
  236. <table class="docutils field-list" frame="void" rules="none">
  237. <col class="field-name" />
  238. <col class="field-body" />
  239. <tbody valign="top">
  240. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_iterator</span></tt></td>
  241. </tr>
  242. </tbody>
  243. </table>
  244. </div>
  245. <div class="section" id="iterator-adaptor-protected-member-functions">
  246. <h2><a class="toc-backref" href="#id11"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> protected member functions</a></h2>
  247. <p><tt class="docutils literal"><span class="pre">Base</span> <span class="pre">const&amp;</span> <span class="pre">base_reference()</span> <span class="pre">const;</span></tt></p>
  248. <table class="docutils field-list" frame="void" rules="none">
  249. <col class="field-name" />
  250. <col class="field-body" />
  251. <tbody valign="top">
  252. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">A const reference to <tt class="docutils literal"><span class="pre">m_iterator</span></tt>.</td>
  253. </tr>
  254. </tbody>
  255. </table>
  256. <p><tt class="docutils literal"><span class="pre">Base&amp;</span> <span class="pre">base_reference();</span></tt></p>
  257. <table class="docutils field-list" frame="void" rules="none">
  258. <col class="field-name" />
  259. <col class="field-body" />
  260. <tbody valign="top">
  261. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">A non-const reference to <tt class="docutils literal"><span class="pre">m_iterator</span></tt>.</td>
  262. </tr>
  263. </tbody>
  264. </table>
  265. </div>
  266. <div class="section" id="iterator-adaptor-private-member-functions">
  267. <h2><a class="toc-backref" href="#id12"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> private member functions</a></h2>
  268. <p><tt class="docutils literal"><span class="pre">typename</span> <span class="pre">iterator_adaptor::reference</span> <span class="pre">dereference()</span> <span class="pre">const;</span></tt></p>
  269. <table class="docutils field-list" frame="void" rules="none">
  270. <col class="field-name" />
  271. <col class="field-body" />
  272. <tbody valign="top">
  273. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*m_iterator</span></tt></td>
  274. </tr>
  275. </tbody>
  276. </table>
  277. <pre class="literal-block">
  278. template &lt;
  279. class OtherDerived, class OtherIterator, class V, class C, class R, class D
  280. &gt;
  281. bool equal(iterator_adaptor&lt;OtherDerived, OtherIterator, V, C, R, D&gt; const&amp; x) const;
  282. </pre>
  283. <table class="docutils field-list" frame="void" rules="none">
  284. <col class="field-name" />
  285. <col class="field-body" />
  286. <tbody valign="top">
  287. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_iterator</span> <span class="pre">==</span> <span class="pre">x.base()</span></tt></td>
  288. </tr>
  289. </tbody>
  290. </table>
  291. <p><tt class="docutils literal"><span class="pre">void</span> <span class="pre">advance(typename</span> <span class="pre">iterator_adaptor::difference_type</span> <span class="pre">n);</span></tt></p>
  292. <table class="docutils field-list" frame="void" rules="none">
  293. <col class="field-name" />
  294. <col class="field-body" />
  295. <tbody valign="top">
  296. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_iterator</span> <span class="pre">+=</span> <span class="pre">n;</span></tt></td>
  297. </tr>
  298. </tbody>
  299. </table>
  300. <p><tt class="docutils literal"><span class="pre">void</span> <span class="pre">increment();</span></tt></p>
  301. <table class="docutils field-list" frame="void" rules="none">
  302. <col class="field-name" />
  303. <col class="field-body" />
  304. <tbody valign="top">
  305. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">++m_iterator;</span></tt></td>
  306. </tr>
  307. </tbody>
  308. </table>
  309. <p><tt class="docutils literal"><span class="pre">void</span> <span class="pre">decrement();</span></tt></p>
  310. <table class="docutils field-list" frame="void" rules="none">
  311. <col class="field-name" />
  312. <col class="field-body" />
  313. <tbody valign="top">
  314. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">--m_iterator;</span></tt></td>
  315. </tr>
  316. </tbody>
  317. </table>
  318. <pre class="literal-block">
  319. template &lt;
  320. class OtherDerived, class OtherIterator, class V, class C, class R, class D
  321. &gt;
  322. typename iterator_adaptor::difference_type distance_to(
  323. iterator_adaptor&lt;OtherDerived, OtherIterator, V, C, R, D&gt; const&amp; y) const;
  324. </pre>
  325. <table class="docutils field-list" frame="void" rules="none">
  326. <col class="field-name" />
  327. <col class="field-body" />
  328. <tbody valign="top">
  329. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">y.base()</span> <span class="pre">-</span> <span class="pre">m_iterator</span></tt></td>
  330. </tr>
  331. </tbody>
  332. </table>
  333. </div>
  334. </div>
  335. <div class="section" id="tutorial-example">
  336. <h1><a class="toc-backref" href="#id13">Tutorial Example</a></h1>
  337. <!-- Copyright David Abrahams 2004. Use, modification and distribution is -->
  338. <!-- subject to the Boost Software License, Version 1.0. (See accompanying -->
  339. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  340. <p>In this section we'll further refine the <tt class="docutils literal"><span class="pre">node_iter</span></tt> class
  341. template we developed in the <a class="reference external" href="iterator_facade.html#tutorial-example"><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> tutorial</a>. If you haven't already
  342. read that material, you should go back now and check it out because
  343. we're going to pick up right where it left off.</p>
  344. <div class="sidebar">
  345. <p class="first sidebar-title"><tt class="docutils literal"><span class="pre">node_base*</span></tt> really <em>is</em> an iterator</p>
  346. <p class="last">It's not really a very interesting iterator, since <tt class="docutils literal"><span class="pre">node_base</span></tt>
  347. is an abstract class: a pointer to a <tt class="docutils literal"><span class="pre">node_base</span></tt> just points
  348. at some base subobject of an instance of some other class, and
  349. incrementing a <tt class="docutils literal"><span class="pre">node_base*</span></tt> moves it past this base subobject
  350. to who-knows-where? The most we can do with that incremented
  351. position is to compare another <tt class="docutils literal"><span class="pre">node_base*</span></tt> to it. In other
  352. words, the original iterator traverses a one-element array.</p>
  353. </div>
  354. <p>You probably didn't think of it this way, but the <tt class="docutils literal"><span class="pre">node_base*</span></tt>
  355. object that underlies <tt class="docutils literal"><span class="pre">node_iterator</span></tt> is itself an iterator,
  356. just like all other pointers. If we examine that pointer closely
  357. from an iterator perspective, we can see that it has much in common
  358. with the <tt class="docutils literal"><span class="pre">node_iterator</span></tt> we're building. First, they share most
  359. of the same associated types (<tt class="docutils literal"><span class="pre">value_type</span></tt>, <tt class="docutils literal"><span class="pre">reference</span></tt>,
  360. <tt class="docutils literal"><span class="pre">pointer</span></tt>, and <tt class="docutils literal"><span class="pre">difference_type</span></tt>). Second, even some of the
  361. core functionality is the same: <tt class="docutils literal"><span class="pre">operator*</span></tt> and <tt class="docutils literal"><span class="pre">operator==</span></tt> on
  362. the <tt class="docutils literal"><span class="pre">node_iterator</span></tt> return the result of invoking the same
  363. operations on the underlying pointer, via the <tt class="docutils literal"><span class="pre">node_iterator</span></tt>'s
  364. <a class="reference external" href="iterator_facade.html#implementing-the-core-operations"><tt class="docutils literal"><span class="pre">dereference</span></tt> and <tt class="docutils literal"><span class="pre">equal</span></tt> member functions</a>). The only real behavioral difference
  365. between <tt class="docutils literal"><span class="pre">node_base*</span></tt> and <tt class="docutils literal"><span class="pre">node_iterator</span></tt> can be observed when
  366. they are incremented: <tt class="docutils literal"><span class="pre">node_iterator</span></tt> follows the
  367. <tt class="docutils literal"><span class="pre">m_next</span></tt> pointer, while <tt class="docutils literal"><span class="pre">node_base*</span></tt> just applies an address offset.</p>
  368. <p>It turns out that the pattern of building an iterator on another
  369. iterator-like type (the <tt class="docutils literal"><span class="pre">Base</span></tt><a class="footnote-reference" href="#base" id="id3"><sup>1</sup></a> type) while modifying
  370. just a few aspects of the underlying type's behavior is an
  371. extremely common one, and it's the pattern addressed by
  372. <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>. Using <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> is very much like
  373. using <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>, but because iterator_adaptor tries to
  374. mimic as much of the <tt class="docutils literal"><span class="pre">Base</span></tt> type's behavior as possible, we
  375. neither have to supply a <tt class="docutils literal"><span class="pre">Value</span></tt> argument, nor implement any core
  376. behaviors other than <tt class="docutils literal"><span class="pre">increment</span></tt>. The implementation of
  377. <tt class="docutils literal"><span class="pre">node_iter</span></tt> is thus reduced to:</p>
  378. <pre class="literal-block">
  379. template &lt;class Value&gt;
  380. class node_iter
  381. : public boost::iterator_adaptor&lt;
  382. node_iter&lt;Value&gt; // Derived
  383. , Value* // Base
  384. , boost::use_default // Value
  385. , boost::forward_traversal_tag // CategoryOrTraversal
  386. &gt;
  387. {
  388. private:
  389. struct enabler {}; // a private type avoids misuse
  390. public:
  391. node_iter()
  392. : node_iter::iterator_adaptor_(0) {}
  393. explicit node_iter(Value* p)
  394. : node_iter::iterator_adaptor_(p) {}
  395. template &lt;class OtherValue&gt;
  396. node_iter(
  397. node_iter&lt;OtherValue&gt; const&amp; other
  398. , typename boost::enable_if&lt;
  399. boost::is_convertible&lt;OtherValue*,Value*&gt;
  400. , enabler
  401. &gt;::type = enabler()
  402. )
  403. : node_iter::iterator_adaptor_(other.base()) {}
  404. private:
  405. friend class boost::iterator_core_access;
  406. void increment() { this-&gt;base_reference() = this-&gt;base()-&gt;next(); }
  407. };
  408. </pre>
  409. <p>Note the use of <tt class="docutils literal"><span class="pre">node_iter::iterator_adaptor_</span></tt> here: because
  410. <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> defines a nested <tt class="docutils literal"><span class="pre">iterator_adaptor_</span></tt> type
  411. that refers to itself, that gives us a convenient way to refer to
  412. the complicated base class type of <tt class="docutils literal"><span class="pre">node_iter&lt;Value&gt;</span></tt>. [Note:
  413. this technique is known not to work with Borland C++ 5.6.4 and
  414. Metrowerks CodeWarrior versions prior to 9.0]</p>
  415. <p>You can see an example program that exercises this version of the
  416. node iterators <a class="reference external" href="../example/node_iterator3.cpp">here</a>.</p>
  417. <p>In the case of <tt class="docutils literal"><span class="pre">node_iter</span></tt>, it's not very compelling to pass
  418. <tt class="docutils literal"><span class="pre">boost::use_default</span></tt> as <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>'s <tt class="docutils literal"><span class="pre">Value</span></tt>
  419. argument; we could have just passed <tt class="docutils literal"><span class="pre">node_iter</span></tt>'s <tt class="docutils literal"><span class="pre">Value</span></tt>
  420. along to <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>, and that'd even be shorter! Most
  421. iterator class templates built with <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> are
  422. parameterized on another iterator type, rather than on its
  423. <tt class="docutils literal"><span class="pre">value_type</span></tt>. For example, <tt class="docutils literal"><span class="pre">boost::reverse_iterator</span></tt> takes an
  424. iterator type argument and reverses its direction of traversal,
  425. since the original iterator and the reversed one have all the same
  426. associated types, <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>'s delegation of default
  427. types to its <tt class="docutils literal"><span class="pre">Base</span></tt> saves the implementor of
  428. <tt class="docutils literal"><span class="pre">boost::reverse_iterator</span></tt> from writing:</p>
  429. <pre class="literal-block">
  430. std::iterator_traits&lt;Iterator&gt;::<em>some-associated-type</em>
  431. </pre>
  432. <p>at least four times.</p>
  433. <p>We urge you to review the documentation and implementations of
  434. <a class="reference external" href="reverse_iterator.html"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt></a> and the other Boost <a class="reference external" href="index.html#specialized-adaptors">specialized iterator
  435. adaptors</a> to get an idea of the sorts of things you can do with
  436. <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>. In particular, have a look at
  437. <a class="reference external" href="transform_iterator.html"><tt class="docutils literal"><span class="pre">transform_iterator</span></tt></a>, which is perhaps the most straightforward
  438. adaptor, and also <a class="reference external" href="counting_iterator.html"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt></a>, which demonstrates that
  439. <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>'s <tt class="docutils literal"><span class="pre">Base</span></tt> type needn't be an iterator.</p>
  440. </div>
  441. </div>
  442. <div class="footer">
  443. <hr class="footer" />
  444. <a class="reference external" href="iterator_adaptor.rst">View document source</a>.
  445. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
  446. </div>
  447. </body>
  448. </html>