stl_iterator.qbk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. [section boost/python/stl_iterator.hpp]
  2. [section Introduction]
  3. <boost/python/stl_iterator.hpp> provides types for creating C++ Iterators from [@http://www.python.org/doc/current/lib/typeiter.html Python iterables].
  4. [endsect]
  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.
  7. [table
  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]]
  10. ]
  11. ``
  12. namespace boost { namespace python
  13. {
  14. template <class ValueType>
  15. struct stl_input_iterator
  16. {
  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;
  22. stl_input_iterator();
  23. stl_input_iterator(object const& ob);
  24. stl_input_iterator& operator++();
  25. stl_input_iterator operator++(int);
  26. ValueType operator*() const;
  27. friend bool operator==(stl_input_iterator const& lhs, stl_input_iterator const& rhs);
  28. friend bool operator!=(stl_input_iterator const& lhs, stl_input_iterator const& rhs);
  29. private:
  30. object it; // For exposition only
  31. object ob; // For exposition only
  32. };
  33. }}
  34. ``
  35. [endsect]
  36. [section Class template `stl_input_iterator` constructors]
  37. ``
  38. stl_input_iterator()
  39. ``
  40. [variablelist
  41. [[Effects][Creates a past-the-end input iterator, useful for signifying the end of a sequence. ]]
  42. [[Postconditions][`this` is past-the-end]]
  43. [[Throws][Nothing.]]
  44. ]
  45. ``stl_input_iterator(object const& ob)``
  46. [variablelist
  47. [[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(). ]]
  48. [[Postconditions][this is a dereferenceable or past-the-end.]]
  49. ]
  50. [endsect]
  51. [section Class template `stl_input_iterator` modifiers]
  52. ``
  53. stl_input_iterator &operator++()
  54. ``
  55. [variablelist
  56. [[Effects][Calls this->it.attr("next")() and stores the result in this->ob. If the sequence is exhausted, sets this->ob to object(). ]]
  57. [[Postconditions][this is a dereferenceable or past-the-end.]]
  58. [[Returns][`*this`]]
  59. ]
  60. ``stl_input_iterator &operator++(int)``
  61. [variablelist
  62. [[Effects][`stl_input_iterator tmp = *this; ++*this; return tmp;`]]
  63. [[Postconditions][this is a dereferenceable or past-the-end.]]
  64. ]
  65. [endsect]
  66. [section Class template `stl_input_iterator` observers]
  67. ``
  68. ValueType operator*() const
  69. ``
  70. [variablelist
  71. [[Effects][Returns the current element in the sequence. ]]
  72. [[Returns][`extract<ValueType>(this->ob);`]]
  73. ]
  74. ``
  75. friend bool operator==(stl_input_iterator const& lhs, stl_input_iterator const& rhs)
  76. ``
  77. [variablelist
  78. [[Effects][Returns true if both iterators are dereferenceable or if both iterators are past-the-end, false otherwise. ]]
  79. [[Returns][`(lhs.ob == object()) == (rhs.ob == object())`]]
  80. ]
  81. ``
  82. friend bool operator!=(stl_input_iterator const& lhs, stl_input_iterator const& rhs)
  83. ``
  84. [variablelist
  85. [[Effects][Returns false if both iterators are dereferenceable or if both iterators are past-the-end, true otherwise. ]]
  86. [[Returns][`!(lhs == rhs)`]]
  87. ]
  88. [endsect]
  89. [section Example]
  90. ``
  91. #include <boost/python/object.hpp>
  92. #include <boost/python/stl_iterator.hpp>
  93. #include <list>
  94. using namespace boost::python;
  95. std::list<int> sequence_to_int_list(object const& ob)
  96. {
  97. stl_input_iterator<int> begin(ob), end;
  98. return std::list<int>(begin, end);
  99. }
  100. ``
  101. [endsect]
  102. [endsect]