From e6c5ccd5190917adcb3518add8a2dc7599bd86f1 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 12 Nov 2012 22:06:14 -0800 Subject: simplified the tags iterator stuff --- include/gras/CMakeLists.txt | 1 + include/gras/element.i | 22 +++++++---------- include/gras/tags.i | 44 ++++++++++++++++++++++++++++++++++ python/gras/GRAS_Block.i | 58 +-------------------------------------------- 4 files changed, 55 insertions(+), 70 deletions(-) create mode 100644 include/gras/tags.i diff --git a/include/gras/CMakeLists.txt b/include/gras/CMakeLists.txt index 1e416e1..8a7ad70 100644 --- a/include/gras/CMakeLists.txt +++ b/include/gras/CMakeLists.txt @@ -13,6 +13,7 @@ install(FILES sbuffer.hpp sbuffer.ipp tags.hpp + tags.i thread_pool.hpp top_block.hpp work_buffer.hpp diff --git a/include/gras/element.i b/include/gras/element.i index e2da4ae..72d9f25 100644 --- a/include/gras/element.i +++ b/include/gras/element.i @@ -19,24 +19,20 @@ %include //////////////////////////////////////////////////////////////////////// -// Create a __str__ for this object +// Operator overloads for Element //////////////////////////////////////////////////////////////////////// - -namespace gras +%extend gras::Element { - %extend Element + std::string __str__(void) const { - std::string __str__(void) - { - return ($self)->to_string(); - } - - bool __eq__(const Element &rhs) - { - return ($self)->get() == rhs.get(); - } + return ($self)->to_string(); + } + bool __eq__(const Element &rhs) const + { + return ($self)->get() == rhs.get(); } + } #endif /*INCLUDED_GRAS_ELEMENT_I*/ diff --git a/include/gras/tags.i b/include/gras/tags.i new file mode 100644 index 0000000..1df9cd2 --- /dev/null +++ b/include/gras/tags.i @@ -0,0 +1,44 @@ +// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +#ifndef INCLUDED_GRAS_TAGS_I +#define INCLUDED_GRAS_TAGS_I + +%{ +#include +%} + +%include +%include + +//////////////////////////////////////////////////////////////////////// +// Turn TagIter into a python iterable object +//////////////////////////////////////////////////////////////////////// +namespace gras +{ + struct TagIter + { + //declared empty + }; +} + +%extend gras::TagIter +{ + %insert("python") + %{ + def __iter__(self): + for i in range(len(self)): + yield self[i] + %} + + size_t __len__(void) const + { + return ($self)->size(); + } + + const Tag & __getitem__(const size_t i) const + { + return (*($self))[i]; + } +} + +#endif /*INCLUDED_GRAS_TAGS_I*/ diff --git a/python/gras/GRAS_Block.i b/python/gras/GRAS_Block.i index ddb5811..718829f 100644 --- a/python/gras/GRAS_Block.i +++ b/python/gras/GRAS_Block.i @@ -83,58 +83,8 @@ struct PyGILPhondler %include %include %include -%include +%include %include -%include - -//////////////////////////////////////////////////////////////////////// -// 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 @@ -220,12 +170,6 @@ struct BlockPython : Block const std::vector &, const std::vector & ) = 0; - - TagIterPython get_input_tags(const size_t which_input) - { - const TagIter it = Block::get_input_tags(which_input); - return TagIterPython(it); - } }; } -- cgit