From 76000379241e781c801040082dbe14e1784cddc6 Mon Sep 17 00:00:00 2001 From: Tamito Kajiyama Date: Wed, 29 Jul 2009 00:18:13 +0000 Subject: 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): ... --- .../intern/python/Iterator/BPy_AdjacencyIterator.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'source/blender/freestyle/intern/python/Iterator/BPy_AdjacencyIterator.cpp') diff --git a/source/blender/freestyle/intern/python/Iterator/BPy_AdjacencyIterator.cpp b/source/blender/freestyle/intern/python/Iterator/BPy_AdjacencyIterator.cpp index 1fba493234e..71105c6896c 100644 --- a/source/blender/freestyle/intern/python/Iterator/BPy_AdjacencyIterator.cpp +++ b/source/blender/freestyle/intern/python/Iterator/BPy_AdjacencyIterator.cpp @@ -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()); } -- cgit v1.2.3