Library of Assembled Shared Sources
|
Exposing iterators to Python.
Lass provides three sets of building blocks to add iterator support to your Python exports:
PyIteratorRange
: the central C++ type that implements the Python Iterator protocol. It's used for all exported Python iterators from the bindings. It's Python type is _lass.PyIteratorRange
and will be type-hinted as Iterator[Any]
.
PyIteratorRange
uses the pimpl idiom to hold an object that implements the impl::PyIteratorRangeImplBase
interface to perform the actual C++ iteration.
There's two common implementations of this interface:
impl::PyIteratorRangeImpl
: for iterating over a C++ iterator pairimpl::PyIndexIteratorRangeImpl
: for iterating over a sequence by index.You can directly return a PyIteratorRange
from your bindings, or you can use one of the convenience methods below.
Two groups of macros:
PY_CLASS_ITERFUNC()
, PY_CLASS_ITERFUNC_DOC()
and PY_CLASS_ITERFUNC_EX()
PY_CLASS_FREE_ITERFUNC()
, PY_CLASS_FREE_ITERFUNC_DOC()
and PY_CLASS_ITERFUNC_EX()
These older macros help you to define the __iter__
method for your class by directly returning a PyIteratorRange
object.
Range views:
ContainerRangeView
with makeContainerRangeView()
MemberRangeView
with makeMemberRangeView()
and makeMemberRangeViewFactory()
FreeRangeView
with makeFreeRangeView()
and makeFreeRangeViewFactory()
IndexedRangeView
with makeIndexedRangeView()
and makeIndexedRangeViewFactory()
FreeRangeView
with makeFreeRangeView()
and makeFreeRangeViewFactory()
The benefit of using these is that they'll use the actual value-type in the type-hints instead of Any
.
Macros | |
#define | PY_CLASS_ITERFUNC_EX(t_cppClass, i_cppBegin, i_cppEnd, s_doc, i_dispatcher) |
Exports a set of C++ iterators to Python. | |
#define | PY_CLASS_ITERFUNC_DOC(i_cppClass, i_cppBegin, icppEnd, s_doc) |
convenience macro, wraps PY_CLASS_ITERFUNC_EX with i_dispatcher = lassPyImpl_method_ ## i_cppClass ## LINE. | |
#define | PY_CLASS_ITERFUNC(i_cppClass, i_cppBegin, icppEnd) |
convenience macro, wraps PY_CLASS_ITERFUNC_DOC with s_doc = 0. | |
#define | PY_CLASS_FREE_ITERFUNC_DOC(i_cppClass, i_cppBegin, icppEnd, s_doc) |
convenience macro, wraps PY_CLASS_ITERFUNC_EX with i_dispatcher = lassPyImpl_method_ ## i_cppClass ## LINE. | |
#define | PY_CLASS_FREE_ITERFUNC(i_cppClass, i_cppBegin, icppEnd) |
convenience macro, wraps PY_CLASS_ITERFUNC_DOC with s_doc = 0. | |
Functions | |
template<typename T> | |
auto | makeContainerRangeView (const ShadoweePtr< T > &self) |
Returns a ContainerRangeView iterating over self->begin() to self->end() | |
template<typename T, typename GetIterator> | |
auto | makeMemberRangeView (const ShadoweePtr< T > &self, GetIterator begin, GetIterator end) |
Returns a MemberRangeView iterating over (self->*begin)() and (self->*end)() | |
template<typename T, typename GetIterator> | |
auto | makeMemberRangeViewFactory (GetIterator begin, GetIterator end) |
Returns a callable creating a MemberRangeView iterating over (self->*begin)() to (self->*end)() | |
template<typename T, typename GetIterator> | |
auto | makeFreeRangeView (const ShadoweePtr< T > &self, GetIterator begin, GetIterator end) |
Returns a FreeRangeView iterating over begin(*self) and end(*self) | |
template<typename T, typename GetIterator> | |
auto | makeFreeRangeViewFactory (GetIterator begin, GetIterator end) |
Returns a callable creating a FreeRangeView iterating over begin(*self) and end(*self) | |
template<typename T, typename AtMethod, typename SizeMethod> | |
auto | makeIndexedRangeView (const ShadoweePtr< T > &self, AtMethod atMethod, SizeMethod sizeMethod) |
Returns a IndexedRangeView iterating over (self->*atMethod)(index) for index in 0 to (self->*sizeMethod)() - 1 | |
template<typename T, typename AtMethod, typename SizeMethod> | |
auto | makeIndexedRangeViewFactory (AtMethod atMethod, SizeMethod sizeMethod) |
Returns callable that creates a IndexedRangeView iterating over (self->*atMethod)(index) for index in 0 to (self->*sizeMethod)() - 1 | |
template<typename T, typename AtFunc, typename SizeFunc> | |
auto | makeFreeIndexedRangeView (const ShadoweePtr< T > &self, AtFunc atFunc, SizeFunc sizeFunc) |
Returns a FreeIndexedRangeView iterating over atFunc(*self, index) for index in 0 to sizeFunc(*self) - 1 | |
template<typename T, typename AtFunc, typename SizeFunc> | |
auto | makeFreeIndexedRangeViewFactory (AtFunc atFunc, SizeFunc sizeFunc) |
Returns a callable that creates FreeIndexedRangeView iterating over atFunc(*self, index) for index in 0 to sizeFunc(*self) - 1 | |
#define PY_CLASS_ITERFUNC_EX | ( | t_cppClass, | |
i_cppBegin, | |||
i_cppEnd, | |||
s_doc, | |||
i_dispatcher ) |
Exports a set of C++ iterators to Python.
t_cppClass | the C++ class you're exporting an iterator of |
i_cppBegin | the name of the method in C++ that provides the beginning of the exported iterator range |
i_cppEnd | the name of the method in C++ that provides the beginning of the exported iterator range |
s_doc | documentation of method as shown in Python (zero terminated C string) |
i_dispatcher | A unique name of the static C++ dispatcher function to be generated. This name will be used for the names of automatic generated variables and functions and should be unique per exported C++ class/method pair. |
Invoke this macro to export an iterator range to python. The returned object will support the iterator protocol by default. The class generating the export will also support the iterator generator protocol.
Definition at line 868 of file pyiteratorrange.h.
|
Returns a ContainerRangeView
iterating over self->begin()
to self->end()
It automatically deduces ValueType
from the container's iterators.
This function can be used as a free method:
Definition at line 285 of file pyiteratorrange.h.
References ContainerRangeView().
|
Returns a callable creating a MemberRangeView
iterating over (self->*begin)()
to (self->*end)()
Can be used to create a free method:
Definition at line 378 of file pyiteratorrange.h.
|
Returns a callable creating a FreeRangeView
iterating over begin(*self)
and end(*self)
Can be used to create a free method:
Definition at line 479 of file pyiteratorrange.h.
|
Returns callable that creates a IndexedRangeView
iterating over (self->*atMethod)(index)
for index in 0 to (self->*sizeMethod)() - 1
Can be used to create a free method:
Definition at line 588 of file pyiteratorrange.h.
|
Returns a callable that creates FreeIndexedRangeView
iterating over atFunc(*self, index)
for index in 0 to sizeFunc(*self) - 1
Can be used to create a free method:
Definition at line 696 of file pyiteratorrange.h.