Added changes to the AdjacencyIterator type to support Python's
iterator protocol. Now the following code in the conventional
SWIG-based syntax:
it = AdjacencyIterator(iter)
while not it.isEnd():
ve = it.getObject()
...
it.increment()
can be written using the iterator protocol as follows:
for ve in AdjacencyIterator(iter):
...
This commit is contained in:
@@ -11,6 +11,7 @@ extern "C" {
|
||||
|
||||
/*--------------- Python API function prototypes for AdjacencyIterator instance -----------*/
|
||||
static int AdjacencyIterator___init__(BPy_AdjacencyIterator *self, PyObject *args);
|
||||
static PyObject * AdjacencyIterator_iternext(BPy_AdjacencyIterator *self);
|
||||
|
||||
static PyObject * AdjacencyIterator_isIncoming(BPy_AdjacencyIterator *self);
|
||||
static PyObject * AdjacencyIterator_getObject(BPy_AdjacencyIterator *self);
|
||||
@@ -76,8 +77,8 @@ PyTypeObject AdjacencyIterator_Type = {
|
||||
|
||||
/*** Added in release 2.2 ***/
|
||||
/* Iterators */
|
||||
NULL, /* getiterfunc tp_iter; */
|
||||
NULL, /* iternextfunc tp_iternext; */
|
||||
PyObject_SelfIter, /* getiterfunc tp_iter; */
|
||||
(iternextfunc)AdjacencyIterator_iternext, /* iternextfunc tp_iternext; */
|
||||
|
||||
/*** Attribute descriptor and subclassing stuff ***/
|
||||
BPy_AdjacencyIterator_methods, /* struct PyMethodDef *tp_methods; */
|
||||
@@ -142,6 +143,16 @@ int AdjacencyIterator___init__(BPy_AdjacencyIterator *self, PyObject *args )
|
||||
|
||||
}
|
||||
|
||||
PyObject * AdjacencyIterator_iternext(BPy_AdjacencyIterator *self) {
|
||||
if (self->a_it->isEnd()) {
|
||||
PyErr_SetNone(PyExc_StopIteration);
|
||||
return NULL;
|
||||
}
|
||||
ViewEdge *ve = self->a_it->operator->();
|
||||
self->a_it->increment();
|
||||
return BPy_ViewEdge_from_ViewEdge_ptr( ve );
|
||||
}
|
||||
|
||||
PyObject * AdjacencyIterator_isIncoming(BPy_AdjacencyIterator *self) {
|
||||
return PyBool_from_bool(self->a_it->isIncoming());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user