iterator.qbk 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. [section boost/python/iterator.hpp]
  2. [section Introduction]
  3. <boost/python/iterator.hpp> provides types and functions for creating [@http://www.python.org/doc/current/lib/typeiter.html Python iterators] from C++ Containers and Iterators. Note that if your `class_` supports random-access iterators, implementing [@http://www.python.org/doc/current/ref/sequence-types.html#l2h-128 __getitem__] (also known as the Sequence Protocol) may serve you better than using this facility: Python will automatically create an iterator type for you (see [@http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-35 `iter()`]), and each access can be range-checked, leaving no possiblity of accessing through an invalidated C++ iterator.
  4. [endsect]
  5. [section Class template `iterator`]
  6. Instances of `iterator<C,P>` hold a reference to a callable Python object which, when invoked from Python, expects a single argument c convertible to C and creates a Python iterator that traverses `[c.begin(), c.end())`. The optional [link concepts.callpolicies CallPolicies] `P` can be used to control how elements are returned during iteration.
  7. In the table below, c is an instance of Container.
  8. [table
  9. [[Template Parameter][Requirements][Semantics][Default]]
  10. [[Container][`[c.begin(),c.end()`) is a valid Iterator range.][The result will convert its argument to c and call c.begin() and c.end() to acquire iterators. To invoke Container's const `begin()` and `end()` functions, make it const.][ ]]
  11. [[NextPolicies][A default-constructible model of [link concepts.callpolicies CallPolicies].][Applied to the resulting iterators' `next()` method.][An unspecified model of [link concepts.callpolicies CallPolicies] which always makes a copy of the result of deferencing the underlying C++ iterator]]
  12. ]
  13. ``
  14. namespace boost { namespace python
  15. {
  16. template <class Container, class NextPolicies = unspecified>
  17. struct iterator : object
  18. {
  19. iterator();
  20. };
  21. }}
  22. ``
  23. [endsect]
  24. [section Class template iterator constructors]
  25. ``iterator()``
  26. [variablelist
  27. [[Effects][Initializes its base class with the result of:
  28. ``range<NextPolicies>(&iterators<Container>::begin, &iterators<Container>::end)``]]
  29. [[Postconditions][`this->get()` points to a Python callable object which creates a Python iterator as described above.]]
  30. [[Rationale][Provides an easy way to create iterators for the common case where a C++ class being wrapped provides `begin()` and `end()`.]]
  31. ]
  32. [endsect]
  33. [section Class template `iterators`]
  34. A utility class template which provides a way to reliably call its argument's `begin()` and `end()` member functions. Note that there is no portable way to take the address of a member function of a C++ standard library container, so `iterators<>` can be particularly helpful when wrapping them.
  35. In the table below, x is an instance of C.
  36. [table
  37. [[Required Valid Expression][Type]]
  38. [[x.begin()][Convertible to C::const_iterator if C is a const type; convertible to C::iterator otherwise.]]
  39. [[x.end()][Convertible to C::const_iterator if C is a const type; convertible to C::iterator otherwise.]]
  40. ]
  41. ``
  42. namespace boost { namespace python
  43. {
  44. template <class C>
  45. struct iterators
  46. {
  47. typedef typename C::const_iterator iterator;
  48. static iterator begin(C& x);
  49. static iterator end(C& x);
  50. };
  51. }}
  52. ``
  53. [endsect]
  54. [section Class template iterators nested types]
  55. If C is a const type,``typedef typename C::const_iterator iterator;``
  56. Otherwise: ``typedef typename C::iterator iterator;``
  57. [endsect]
  58. [section Class template iterators static functions]
  59. ``static iterator begin(C&);``
  60. [variablelist [[Returns][`x.begin()`]]]
  61. ``static iterator end(C&);``
  62. [variablelist [[Returns][`x.end()`]]]
  63. [endsect]
  64. [section Functions]
  65. ``
  66. template <class NextPolicies, class Target, class Accessor1, class Accessor2>
  67. object range(Accessor1 start, Accessor2 finish);
  68. template <class NextPolicies, class Accessor1, class Accessor2>
  69. object range(Accessor1 start, Accessor2 finish);
  70. template <class Accessor1, class Accessor2>
  71. object range(Accessor1 start, Accessor2 finish);
  72. ``
  73. [variablelist
  74. [[Requires][ NextPolicies is a default-constructible model of [link concepts.callpolicies CallPolicies].]]
  75. [[Effects][The first form creates a Python callable object which, when invoked, converts its argument to a Target object x, and creates a Python iterator which traverses `[bind(start,_1)(x), bind(finish,_1)(x))`, applying NextPolicies to the iterator's `next()` function.
  76. The second form is identical to the first, except that Target is deduced from Accessor1 as follows:
  77. # If Accessor1 is a function type, Target is the type of its first argument.
  78. # If Accessor1 is a data member pointer of the form `R (T::*)`, Target is identical to `T`.
  79. # If Accessor1 is a member function pointer of the form `R (T::*)(arguments...) cv-opt`, where cv-opt is an optional cv-qualifier, Target is identical to `T`.
  80. The third form is identical to the second, except that NextPolicies is an unspecified model of [link concepts.callpolicies CallPolicies] which always makes a copy of the result of deferencing the underlying C++ iterator
  81. ]]
  82. [[Rationale][The use of `boost::bind()` allows C++ iterators to be accessed through functions, member functions or data member pointers. Customization of NextPolicies (e.g. using [link function_invocation_and_creation.models_of_callpolicies.boost_python_return_internal_ref.class_template_return_internal_r return_internal_reference]) is useful when it is expensive to copy sequence elements of a wrapped class type. Customization of Target is useful when Accessor1 is a function object, or when a base class of the intended target type would otherwise be deduced.]]
  83. ]
  84. [endsect]
  85. [section Example]
  86. ``
  87. #include <boost/python/module.hpp>
  88. #include <boost/python/class.hpp>
  89. #include <vector>
  90. using namespace boost::python;
  91. BOOST_PYTHON_MODULE(demo)
  92. {
  93. class_<std::vector<double> >("dvec")
  94. .def("__iter__", iterator<std::vector<double> >())
  95. ;
  96. }
  97. ``
  98. [endsect]
  99. [endsect]