1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>boost/python/stl_iterator.hpp</title> 5<link rel="stylesheet" href="../../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../index.html" title="Boost.Python Reference Manual"> 8<link rel="up" href="../high_level_components.html" title="Chapter 2. High Level Components"> 9<link rel="prev" href="boost_python_scope_hpp.html" title="boost/python/scope.hpp"> 10<link rel="next" href="boost_python_wrapper_hpp.html" title="boost/python/wrapper.hpp"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table> 14<hr> 15<div class="spirit-nav"> 16<a accesskey="p" href="boost_python_scope_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_wrapper_hpp.html"><img src="../../images/next.png" alt="Next"></a> 17</div> 18<div class="section"> 19<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 20<a name="high_level_components.boost_python_stl_iterator_hpp"></a><a class="link" href="boost_python_stl_iterator_hpp.html" title="boost/python/stl_iterator.hpp">boost/python/stl_iterator.hpp</a> 21</h2></div></div></div> 22<div class="toc"><dl class="toc"> 23<dt><span class="section"><a href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.introduction">Introduction</a></span></dt> 24<dt><span class="section"><a href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterato">Class 25 template <code class="computeroutput"><span class="identifier">stl_input_iterator</span></code></a></span></dt> 26<dt><span class="section"><a href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterat0">Class 27 template <code class="computeroutput"><span class="identifier">stl_input_iterator</span></code> 28 constructors</a></span></dt> 29<dt><span class="section"><a href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterat1">Class 30 template <code class="computeroutput"><span class="identifier">stl_input_iterator</span></code> 31 modifiers</a></span></dt> 32<dt><span class="section"><a href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterat2">Class 33 template <code class="computeroutput"><span class="identifier">stl_input_iterator</span></code> 34 observers</a></span></dt> 35<dt><span class="section"><a href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.example">Example</a></span></dt> 36</dl></div> 37<div class="section"> 38<div class="titlepage"><div><div><h3 class="title"> 39<a name="high_level_components.boost_python_stl_iterator_hpp.introduction"></a><a class="link" href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.introduction" title="Introduction">Introduction</a> 40</h3></div></div></div> 41<p> 42 <boost/python/stl_iterator.hpp> provides types for creating C++ Iterators 43 from <a href="http://www.python.org/doc/current/lib/typeiter.html" target="_top">Python 44 iterables</a>. 45 </p> 46</div> 47<div class="section"> 48<div class="titlepage"><div><div><h3 class="title"> 49<a name="high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterato"></a><a class="link" href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterato" title="Class template stl_input_iterator">Class 50 template <code class="computeroutput"><span class="identifier">stl_input_iterator</span></code></a> 51</h3></div></div></div> 52<p> 53 Instances of <code class="computeroutput"><span class="identifier">stl_input_iterator</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code> hold a Python iterator and adapt it 54 for use with STL algorithms. <code class="computeroutput"><span class="identifier">stl_input_iterator</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code> satisfies the requirements for an Input 55 Iterator. 56 </p> 57<div class="informaltable"><table class="table"> 58<colgroup> 59<col> 60<col> 61<col> 62<col> 63</colgroup> 64<thead><tr> 65<th> 66 <p> 67 Template Parameter 68 </p> 69 </th> 70<th> 71 <p> 72 Requirements 73 </p> 74 </th> 75<th> 76 <p> 77 Semantics 78 </p> 79 </th> 80<th> 81 <p> 82 Default 83 </p> 84 </th> 85</tr></thead> 86<tbody><tr> 87<td> 88 <p> 89 ValueType 90 </p> 91 </td> 92<td> 93 <p> 94 ValueType must be CopyConstructible. 95 </p> 96 </td> 97<td> 98 <p> 99 Dereferencing an instance of <code class="computeroutput"><span class="identifier">stl_input_iterator</span><span class="special"><</span><span class="identifier">ValueType</span><span class="special">></span></code> will return an rvalue of 100 type ValueType. 101 </p> 102 </td> 103<td> 104 <p> 105 None 106 </p> 107 </td> 108</tr></tbody> 109</table></div> 110<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span> 111<span class="special">{</span> 112 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">ValueType</span><span class="special">></span> 113 <span class="keyword">struct</span> <span class="identifier">stl_input_iterator</span> 114 <span class="special">{</span> 115 <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ptrdiff_t</span> <span class="identifier">difference_type</span><span class="special">;</span> 116 <span class="keyword">typedef</span> <span class="identifier">ValueType</span> <span class="identifier">value_type</span><span class="special">;</span> 117 <span class="keyword">typedef</span> <span class="identifier">ValueType</span><span class="special">*</span> <span class="identifier">pointer</span><span class="special">;</span> 118 <span class="keyword">typedef</span> <span class="identifier">ValueType</span> <span class="identifier">reference</span><span class="special">;</span> 119 <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">input_iterator_tag</span> <span class="identifier">iterator_category</span><span class="special">;</span> 120 121 <span class="identifier">stl_input_iterator</span><span class="special">();</span> 122 <span class="identifier">stl_input_iterator</span><span class="special">(</span><span class="identifier">object</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">ob</span><span class="special">);</span> 123 124 <span class="identifier">stl_input_iterator</span><span class="special">&</span> <span class="keyword">operator</span><span class="special">++();</span> 125 <span class="identifier">stl_input_iterator</span> <span class="keyword">operator</span><span class="special">++(</span><span class="keyword">int</span><span class="special">);</span> 126 127 <span class="identifier">ValueType</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span><span class="special">;</span> 128 129 <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">stl_input_iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">stl_input_iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">rhs</span><span class="special">);</span> 130 <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!=(</span><span class="identifier">stl_input_iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">stl_input_iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">rhs</span><span class="special">);</span> 131 <span class="keyword">private</span><span class="special">:</span> 132 <span class="identifier">object</span> <span class="identifier">it</span><span class="special">;</span> <span class="comment">// For exposition only</span> 133 <span class="identifier">object</span> <span class="identifier">ob</span><span class="special">;</span> <span class="comment">// For exposition only</span> 134 <span class="special">};</span> 135<span class="special">}}</span> 136</pre> 137</div> 138<div class="section"> 139<div class="titlepage"><div><div><h3 class="title"> 140<a name="high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterat0"></a><a class="link" href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterat0" title="Class template stl_input_iterator constructors">Class 141 template <code class="computeroutput"><span class="identifier">stl_input_iterator</span></code> 142 constructors</a> 143</h3></div></div></div> 144<pre class="programlisting"><span class="identifier">stl_input_iterator</span><span class="special">()</span> 145</pre> 146<div class="variablelist"> 147<p class="title"><b></b></p> 148<dl class="variablelist"> 149<dt><span class="term">Effects</span></dt> 150<dd><p> 151 Creates a past-the-end input iterator, useful for signifying the 152 end of a sequence. 153 </p></dd> 154<dt><span class="term">Postconditions</span></dt> 155<dd><p> 156 <code class="computeroutput"><span class="keyword">this</span></code> is past-the-end 157 </p></dd> 158<dt><span class="term">Throws</span></dt> 159<dd><p> 160 Nothing. 161 </p></dd> 162</dl> 163</div> 164<pre class="programlisting"><span class="identifier">stl_input_iterator</span><span class="special">(</span><span class="identifier">object</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">ob</span><span class="special">)</span></pre> 165<div class="variablelist"> 166<p class="title"><b></b></p> 167<dl class="variablelist"> 168<dt><span class="term">Effects</span></dt> 169<dd><p> 170 Calls ob.attr("__iter__")() and stores the resulting Python 171 iterator object in this->it. Then, calls this->it.attr("next")() 172 and stores the result in this->ob. If the sequence is exhausted, 173 sets this->ob to object(). 174 </p></dd> 175<dt><span class="term">Postconditions</span></dt> 176<dd><p> 177 this is a dereferenceable or past-the-end. 178 </p></dd> 179</dl> 180</div> 181</div> 182<div class="section"> 183<div class="titlepage"><div><div><h3 class="title"> 184<a name="high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterat1"></a><a class="link" href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterat1" title="Class template stl_input_iterator modifiers">Class 185 template <code class="computeroutput"><span class="identifier">stl_input_iterator</span></code> 186 modifiers</a> 187</h3></div></div></div> 188<pre class="programlisting"><span class="identifier">stl_input_iterator</span> <span class="special">&</span><span class="keyword">operator</span><span class="special">++()</span> 189</pre> 190<div class="variablelist"> 191<p class="title"><b></b></p> 192<dl class="variablelist"> 193<dt><span class="term">Effects</span></dt> 194<dd><p> 195 Calls this->it.attr("next")() and stores the result 196 in this->ob. If the sequence is exhausted, sets this->ob to 197 object(). 198 </p></dd> 199<dt><span class="term">Postconditions</span></dt> 200<dd><p> 201 this is a dereferenceable or past-the-end. 202 </p></dd> 203<dt><span class="term">Returns</span></dt> 204<dd><p> 205 <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> 206 </p></dd> 207</dl> 208</div> 209<pre class="programlisting"><span class="identifier">stl_input_iterator</span> <span class="special">&</span><span class="keyword">operator</span><span class="special">++(</span><span class="keyword">int</span><span class="special">)</span></pre> 210<div class="variablelist"> 211<p class="title"><b></b></p> 212<dl class="variablelist"> 213<dt><span class="term">Effects</span></dt> 214<dd><p> 215 <code class="computeroutput"><span class="identifier">stl_input_iterator</span> <span class="identifier">tmp</span> <span class="special">=</span> 216 <span class="special">*</span><span class="keyword">this</span><span class="special">;</span> <span class="special">++*</span><span class="keyword">this</span><span class="special">;</span> <span class="keyword">return</span> <span class="identifier">tmp</span><span class="special">;</span></code> 217 </p></dd> 218<dt><span class="term">Postconditions</span></dt> 219<dd><p> 220 this is a dereferenceable or past-the-end. 221 </p></dd> 222</dl> 223</div> 224</div> 225<div class="section"> 226<div class="titlepage"><div><div><h3 class="title"> 227<a name="high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterat2"></a><a class="link" href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.class_template_stl_input_iterat2" title="Class template stl_input_iterator observers">Class 228 template <code class="computeroutput"><span class="identifier">stl_input_iterator</span></code> 229 observers</a> 230</h3></div></div></div> 231<pre class="programlisting"><span class="identifier">ValueType</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span> 232</pre> 233<div class="variablelist"> 234<p class="title"><b></b></p> 235<dl class="variablelist"> 236<dt><span class="term">Effects</span></dt> 237<dd><p> 238 Returns the current element in the sequence. 239 </p></dd> 240<dt><span class="term">Returns</span></dt> 241<dd><p> 242 <code class="computeroutput"><span class="identifier">extract</span><span class="special"><</span><span class="identifier">ValueType</span><span class="special">>(</span><span class="keyword">this</span><span class="special">-></span><span class="identifier">ob</span><span class="special">);</span></code> 243 </p></dd> 244</dl> 245</div> 246<pre class="programlisting"><span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">stl_input_iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">stl_input_iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">rhs</span><span class="special">)</span> 247</pre> 248<div class="variablelist"> 249<p class="title"><b></b></p> 250<dl class="variablelist"> 251<dt><span class="term">Effects</span></dt> 252<dd><p> 253 Returns true if both iterators are dereferenceable or if both iterators 254 are past-the-end, false otherwise. 255 </p></dd> 256<dt><span class="term">Returns</span></dt> 257<dd><p> 258 <code class="computeroutput"><span class="special">(</span><span class="identifier">lhs</span><span class="special">.</span><span class="identifier">ob</span> <span class="special">==</span> <span class="identifier">object</span><span class="special">())</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">rhs</span><span class="special">.</span><span class="identifier">ob</span> <span class="special">==</span> <span class="identifier">object</span><span class="special">())</span></code> 259 </p></dd> 260</dl> 261</div> 262<pre class="programlisting"><span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!=(</span><span class="identifier">stl_input_iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">stl_input_iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">rhs</span><span class="special">)</span> 263</pre> 264<div class="variablelist"> 265<p class="title"><b></b></p> 266<dl class="variablelist"> 267<dt><span class="term">Effects</span></dt> 268<dd><p> 269 Returns false if both iterators are dereferenceable or if both iterators 270 are past-the-end, true otherwise. 271 </p></dd> 272<dt><span class="term">Returns</span></dt> 273<dd><p> 274 <code class="computeroutput"><span class="special">!(</span><span class="identifier">lhs</span> 275 <span class="special">==</span> <span class="identifier">rhs</span><span class="special">)</span></code> 276 </p></dd> 277</dl> 278</div> 279</div> 280<div class="section"> 281<div class="titlepage"><div><div><h3 class="title"> 282<a name="high_level_components.boost_python_stl_iterator_hpp.example"></a><a class="link" href="boost_python_stl_iterator_hpp.html#high_level_components.boost_python_stl_iterator_hpp.example" title="Example">Example</a> 283</h3></div></div></div> 284<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">object</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 285<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">stl_iterator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 286 287<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">list</span><span class="special">></span> 288 289<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span> 290<span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">sequence_to_int_list</span><span class="special">(</span><span class="identifier">object</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">ob</span><span class="special">)</span> 291<span class="special">{</span> 292 <span class="identifier">stl_input_iterator</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">begin</span><span class="special">(</span><span class="identifier">ob</span><span class="special">),</span> <span class="identifier">end</span><span class="special">;</span> 293 <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">begin</span><span class="special">,</span> <span class="identifier">end</span><span class="special">);</span> 294<span class="special">}</span> 295</pre> 296</div> 297</div> 298<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 299<td align="left"></td> 300<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p> 301 Distributed under the Boost Software License, Version 1.0. (See accompanying 302 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> 303 </p> 304</div></td> 305</tr></table> 306<hr> 307<div class="spirit-nav"> 308<a accesskey="p" href="boost_python_scope_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_wrapper_hpp.html"><img src="../../images/next.png" alt="Next"></a> 309</div> 310</body> 311</html> 312