From 0f493354d46706ed81b81699c4e5197ee2b82d08 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Thu, 4 Apr 2013 22:07:59 -0700 Subject: 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 --- python/gras/GRAS_Block.i | 43 ------------------------------------------- python/gras/GRAS_HierBlock.i | 37 ++++++++++++++++++++++++++++++++++--- python/gras/GRAS_Utils.i | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 46 deletions(-) (limited to 'python') 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 #include -#include %} %include @@ -64,42 +63,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 lock(void) - { - return boost::make_shared(o); - } - PyObject *o; -}; - -%} - //////////////////////////////////////////////////////////////////////// // Make a special block with safe overloads //////////////////////////////////////////////////////////////////////// @@ -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 #include +#include %} //////////////////////////////////////////////////////////////////////// @@ -29,10 +30,37 @@ %include %include +%include "GRAS_Utils.i" + +//////////////////////////////////////////////////////////////////////// +// weak element overload for python +//////////////////////////////////////////////////////////////////////// +%inline %{ + +struct WeakElementPyObject : gras::WeakElement +{ + WeakElementPyObject(PyObject *o): + o(o) + { + //NOP + } + boost::shared_ptr lock(void) + { + return boost::make_shared(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; +}; + +%} -- cgit