From 468d53f7797c63cda2ef9ba765f1066550d19ce4 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sun, 17 Mar 2013 17:38:40 -0700 Subject: gras: work on python hooks for props interface --- python/gras/GRAS_Block.i | 59 +++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 21 deletions(-) (limited to 'python/gras/GRAS_Block.i') diff --git a/python/gras/GRAS_Block.i b/python/gras/GRAS_Block.i index 26cf8ea..2f98b1b 100644 --- a/python/gras/GRAS_Block.i +++ b/python/gras/GRAS_Block.i @@ -12,6 +12,7 @@ %feature("nodirector") gras::BlockPython::notify_inactive; %feature("nodirector") gras::BlockPython::notify_topology; %feature("nodirector") gras::BlockPython::work; +%feature("nodirector") gras::BlockPython::_handle_prop_access; //////////////////////////////////////////////////////////////////////// // http://www.swig.org/Doc2.0/Library.html#Library_stl_exceptions @@ -42,27 +43,6 @@ } } -//////////////////////////////////////////////////////////////////////// -// Simple class to deal with smart locking/unlocking of python GIL -//////////////////////////////////////////////////////////////////////// -%{ - -struct PyGILPhondler -{ - PyGILPhondler(void): - s(PyGILState_Ensure()) - { - //NOP - } - ~PyGILPhondler(void) - { - PyGILState_Release(s); - } - PyGILState_STATE s; -}; - -%} - //////////////////////////////////////////////////////////////////////// // SWIG up the representation for IO work arrays //////////////////////////////////////////////////////////////////////// @@ -83,6 +63,7 @@ struct PyGILPhondler //////////////////////////////////////////////////////////////////////// // Make a special block with safe overloads //////////////////////////////////////////////////////////////////////// +%include "GRAS_Utils.i" %inline %{ namespace gras @@ -172,6 +153,26 @@ struct BlockPython : Block } virtual void _Py_propagate_tags(const size_t which_input, const TagIter &iter) = 0; + + void _set_property(const std::string &key, const PMCC &value) + { + PyTSPhondler phil; + return Block::_set_property(key, value); + } + + PMCC _get_property(const std::string &key) + { + PyTSPhondler phil; + return Block::_get_property(key); + } + + PMCC _handle_prop_access(const std::string &key, const PMCC &value, const bool set) + { + PyGILPhondler phil; + return this->_Py_handle_prop_access(key, value, set); + } + + virtual PMCC _Py_handle_prop_access(const std::string &key, const PMCC &value, const bool set) = 0; }; } @@ -201,6 +202,7 @@ class Block(BlockPython): self.set_input_signature(in_sig) self.set_output_signature(out_sig) blocks_ref_container.append(self) + self.__prop_registry = dict(); def set_input_signature(self, sig): self.__in_sig = sig_to_dtype_sig(sig) @@ -279,4 +281,19 @@ class Block(BlockPython): t.offset -= self.get_consumed(i) self.post_output_tag(o, t) + def _Py_handle_prop_access(self, key, value, set): + (getter, setter) = self.__prop_registry[key] + if set: + setter(value()) + return PMCC() + return PMC_M(getter()) + + def register_property(self, key, getter, setter): + self.__prop_registry[key] = (getter, setter) + + def set(self, key, value): + self._set_property(key, PMC_M(value)) + + def get(self, key): + return self._get_property(key)() %} -- cgit