From c36e02443021ebda80cf9351d6065fcb892c4f82 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 12 Nov 2012 20:21:47 -0800 Subject: create a proper python iterator from TagIter --- python/gras/GRAS_Block.i | 55 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) (limited to 'python/gras') diff --git a/python/gras/GRAS_Block.i b/python/gras/GRAS_Block.i index 3ee8691..ddb5811 100644 --- a/python/gras/GRAS_Block.i +++ b/python/gras/GRAS_Block.i @@ -88,9 +88,57 @@ struct PyGILPhondler %include //////////////////////////////////////////////////////////////////////// -// Make a special block with safe overloads +// Create a python iterator class //////////////////////////////////////////////////////////////////////// +%typemap(throws) Stop_Iteration +%{ + PyErr_SetNone(PyExc_StopIteration); + SWIG_fail; +%} +%extend gras::TagIterPython +{ +%insert("python") +%{ + def __iter__(self): return self +%} +} + +%inline %{ + +struct Stop_Iteration +{ + Stop_Iteration(void){} +}; + +namespace gras +{ + +struct TagIterPython +{ + TagIterPython(const TagIter &iter): + _iter(iter), _it(iter.begin()) + { + //NOP + } + + const Tag &next(void) throw (Stop_Iteration) + { + if (_it == _iter.end()) throw Stop_Iteration(); + return *(_it++); + } + + TagIter _iter; + TagIter::const_iterator _it; +}; + +} + +%} + +//////////////////////////////////////////////////////////////////////// +// Make a special block with safe overloads +//////////////////////////////////////////////////////////////////////// %inline %{ namespace gras @@ -173,11 +221,10 @@ struct BlockPython : Block const std::vector & ) = 0; - std::vector get_input_tags(const size_t which_input) + TagIterPython get_input_tags(const size_t which_input) { const TagIter it = Block::get_input_tags(which_input); - std::vector tags(it.begin(), it.end()); - return tags; + return TagIterPython(it); } }; -- cgit