diff options
author | Josh Blum | 2013-04-04 22:07:59 -0700 |
---|---|---|
committer | Josh Blum | 2013-04-04 22:07:59 -0700 |
commit | 0f493354d46706ed81b81699c4e5197ee2b82d08 (patch) | |
tree | d177af0a64fa643a5630091dd797d38492e73912 /python | |
parent | 06b800166e995cf53ec0f87200427512e1518d8f (diff) | |
download | sandhi-0f493354d46706ed81b81699c4e5197ee2b82d08.tar.gz sandhi-0f493354d46706ed81b81699c4e5197ee2b82d08.tar.bz2 sandhi-0f493354d46706ed81b81699c4e5197ee2b82d08.zip |
gras: revisit container storage after yesterdays learning
The mechanisms of connect are now resonsible for grabbing the container ref.
When the object is a shared ptr, the element contructor overload sets weakself.
When the object is in python, the python reference is held,
even if its shared ptr underneath that.
* removed the need for shared_from_this
* removed the ref stuff in python Block
Diffstat (limited to 'python')
-rw-r--r-- | python/gras/GRAS_Block.i | 43 | ||||
-rw-r--r-- | python/gras/GRAS_HierBlock.i | 37 | ||||
-rw-r--r-- | python/gras/GRAS_Utils.i | 22 |
3 files changed, 56 insertions, 46 deletions
diff --git a/python/gras/GRAS_Block.i b/python/gras/GRAS_Block.i index 9cd96e2..f429e30 100644 --- a/python/gras/GRAS_Block.i +++ b/python/gras/GRAS_Block.i @@ -56,7 +56,6 @@ %{ #include <gras/block.hpp> #include <iostream> -#include <boost/make_shared.hpp> %} %include <gras/element.i> @@ -65,42 +64,6 @@ %include "GRAS_Utils.i" //////////////////////////////////////////////////////////////////////// -// Create a reference holder for python objects -//////////////////////////////////////////////////////////////////////// -%inline %{ - -struct PyObjectRefHolder -{ - PyObjectRefHolder(PyObject *o): - o(o) - { - Py_INCREF(o); - } - ~PyObjectRefHolder(void) - { - PyGILPhondler phil; - Py_DECREF(o); - } - PyObject *o; -}; - -struct WeakElementPyObject : gras::WeakElement -{ - WeakElementPyObject(PyObject *o): - o(o) - { - //NOP - } - boost::shared_ptr<void> lock(void) - { - return boost::make_shared<PyObjectRefHolder>(o); - } - PyObject *o; -}; - -%} - -//////////////////////////////////////////////////////////////////////// // Make a special block with safe overloads //////////////////////////////////////////////////////////////////////// %inline %{ @@ -122,11 +85,6 @@ struct BlockPython : Block this->reset(); } - void _Py_init_weak_ref__(PyObject *o) - { - this->weak_self.reset(new WeakElementPyObject(o)); - } - void notify_active(void) { PyGILPhondler phil; @@ -241,7 +199,6 @@ def sig_to_dtype_sig(sig): class Block(BlockPython): def __init__(self, name='Block', in_sig=None, out_sig=None): BlockPython.__init__(self, name) - self._Py_init_weak_ref__(self) self.set_input_signature(in_sig) self.set_output_signature(out_sig) self.__prop_registry = dict() diff --git a/python/gras/GRAS_HierBlock.i b/python/gras/GRAS_HierBlock.i index 4afe8fe..2ffb349 100644 --- a/python/gras/GRAS_HierBlock.i +++ b/python/gras/GRAS_HierBlock.i @@ -20,6 +20,7 @@ %{ #include <gras/hier_block.hpp> #include <gras/top_block.hpp> +#include <boost/make_shared.hpp> %} //////////////////////////////////////////////////////////////////////// @@ -29,10 +30,37 @@ %include <gras/hier_block.i> %include <gras/top_block.hpp> +%include "GRAS_Utils.i" + +//////////////////////////////////////////////////////////////////////// +// weak element overload for python +//////////////////////////////////////////////////////////////////////// +%inline %{ + +struct WeakElementPyObject : gras::WeakElement +{ + WeakElementPyObject(PyObject *o): + o(o) + { + //NOP + } + boost::shared_ptr<const void> lock(void) + { + return boost::make_shared<PyObjectRefHolder>(o); + } + PyObject *o; +}; + +inline void set_weak_py_self(gras::Element &elem, PyObject *o) +{ + elem.weak_self.reset(new WeakElementPyObject(o)); +} + +%} + //////////////////////////////////////////////////////////////////////// // Make a special top block with python safe unlocking wait //////////////////////////////////////////////////////////////////////// -%include "GRAS_Utils.i" %inline %{ namespace gras @@ -109,13 +137,16 @@ struct HierBlockPython : HierBlock def to_element(obj): if isinstance(obj, Element): return obj - try: return obj.shared_to_element() + try: + elem = obj.to_element() + set_weak_py_self(elem, obj) + return elem except: raise Exception('cant coerce obj %s to element'%(obj)) def internal_connect__(fcn, obj, *args): if len(args) == 1: - elem = (args[0]) + elem = to_element(args[0]) fcn(obj, elem) return diff --git a/python/gras/GRAS_Utils.i b/python/gras/GRAS_Utils.i index afe3314..1b23aca 100644 --- a/python/gras/GRAS_Utils.i +++ b/python/gras/GRAS_Utils.i @@ -41,3 +41,25 @@ struct PyTSPhondler }; %} + +//////////////////////////////////////////////////////////////////////// +// Create a reference holder for python objects +//////////////////////////////////////////////////////////////////////// +%inline %{ + +struct PyObjectRefHolder +{ + PyObjectRefHolder(PyObject *o): + o(o) + { + Py_INCREF(o); + } + ~PyObjectRefHolder(void) + { + PyGILPhondler phil; + Py_DECREF(o); + } + PyObject *o; +}; + +%} |