1 [section boost/python/stl_iterator.hpp]
3 <boost/python/stl_iterator.hpp> provides types for creating C++ Iterators from [@http://www.python.org/doc/current/lib/typeiter.html Python iterables].
5 [section Class template `stl_input_iterator`]
6 Instances of `stl_input_iterator<T>` hold a Python iterator and adapt it for use with STL algorithms. `stl_input_iterator<T>` satisfies the requirements for an Input Iterator.
8 [[Template Parameter][Requirements][Semantics][Default]]
9 [[ValueType][ValueType must be CopyConstructible.][Dereferencing an instance of `stl_input_iterator<ValueType>` will return an rvalue of type ValueType.][None]]
12 namespace boost { namespace python
14 template <class ValueType>
15 struct stl_input_iterator
17 typedef std::ptrdiff_t difference_type;
18 typedef ValueType value_type;
19 typedef ValueType* pointer;
20 typedef ValueType reference;
21 typedef std::input_iterator_tag iterator_category;
24 stl_input_iterator(object const& ob);
26 stl_input_iterator& operator++();
27 stl_input_iterator operator++(int);
29 ValueType operator*() const;
31 friend bool operator==(stl_input_iterator const& lhs, stl_input_iterator const& rhs);
32 friend bool operator!=(stl_input_iterator const& lhs, stl_input_iterator const& rhs);
34 object it; // For exposition only
35 object ob; // For exposition only
40 [section Class template `stl_input_iterator` constructors]
45 [[Effects][Creates a past-the-end input iterator, useful for signifying the end of a sequence. ]]
46 [[Postconditions][`this` is past-the-end]]
49 ``stl_input_iterator(object const& ob)``
51 [[Effects][Calls ob.attr("__iter__")() and stores the resulting Python iterator object in this->it. Then, calls this->it.attr("next")() and stores the result in this->ob. If the sequence is exhausted, sets this->ob to object(). ]]
52 [[Postconditions][this is a dereferenceable or past-the-end.]]
55 [section Class template `stl_input_iterator` modifiers]
57 stl_input_iterator &operator++()
60 [[Effects][Calls this->it.attr("next")() and stores the result in this->ob. If the sequence is exhausted, sets this->ob to object(). ]]
61 [[Postconditions][this is a dereferenceable or past-the-end.]]
64 ``stl_input_iterator &operator++(int)``
66 [[Effects][`stl_input_iterator tmp = *this; ++*this; return tmp;`]]
67 [[Postconditions][this is a dereferenceable or past-the-end.]]
70 [section Class template `stl_input_iterator` observers]
72 ValueType operator*() const
75 [[Effects][Returns the current element in the sequence. ]]
76 [[Returns][`extract<ValueType>(this->ob);`]]
79 friend bool operator==(stl_input_iterator const& lhs, stl_input_iterator const& rhs)
82 [[Effects][Returns true if both iterators are dereferenceable or if both iterators are past-the-end, false otherwise. ]]
83 [[Returns][`(lhs.ob == object()) == (rhs.ob == object())`]]
86 friend bool operator!=(stl_input_iterator const& lhs, stl_input_iterator const& rhs)
89 [[Effects][Returns false if both iterators are dereferenceable or if both iterators are past-the-end, true otherwise. ]]
90 [[Returns][`!(lhs == rhs)`]]
95 #include <boost/python/object.hpp>
96 #include <boost/python/stl_iterator.hpp>
100 using namespace boost::python;
101 std::list<int> sequence_to_int_list(object const& ob)
103 stl_input_iterator<int> begin(ob), end;
104 return std::list<int>(begin, end);