summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJosh Blum2012-11-12 20:21:47 -0800
committerJosh Blum2012-11-12 20:21:47 -0800
commitc36e02443021ebda80cf9351d6065fcb892c4f82 (patch)
tree204f61d3f98c64a6c83aa6dada79259bbf93f65d /python
parent502b13e296cb7d7a20ed8b7dc202bbe9b4e607b1 (diff)
downloadsandhi-c36e02443021ebda80cf9351d6065fcb892c4f82.tar.gz
sandhi-c36e02443021ebda80cf9351d6065fcb892c4f82.tar.bz2
sandhi-c36e02443021ebda80cf9351d6065fcb892c4f82.zip
create a proper python iterator from TagIter
Diffstat (limited to 'python')
-rw-r--r--python/gras/GRAS_Block.i55
1 files changed, 51 insertions, 4 deletions
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 <PMC/PMC.i>
////////////////////////////////////////////////////////////////////////
-// 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<size_t> &
) = 0;
- std::vector<Tag> 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<Tag> tags(it.begin(), it.end());
- return tags;
+ return TagIterPython(it);
}
};