From 30f16b913843fa7696544f1e61c3d4a00fb11bc2 Mon Sep 17 00:00:00 2001 From: Jake Cobb Date: Mon, 4 Feb 2019 13:48:52 -0500 Subject: [PATCH] Python - Struct spec. for container owner Use a struct with specialization to dispatch the container owner reference function instead of a function. Avoids possible future problems if overloading were introduced. --- Lib/python/pycontainer.swg | 46 +++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index 34bf3c110..ec31bad0d 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -48,28 +48,32 @@ namespace swig { return attr; } - // By default, do not add the back-reference (for value types) - // Specialization below will check the reference for pointer types. - template - bool reference_container_owner(PyObject* child, PyObject* owner) - { return false; } + template + struct container_owner { + // By default, do not add the back-reference (for value types) + // Specialization below will check the reference for pointer types. + static bool reference(PyObject* child, PyObject* owner) + { return false; } + }; - /** - * Call to add a back-reference to the owning object when returning a - * reference from a container. Will only set the reference if child - * is a SWIG wrapper object that does not own the pointer. - * - * @return if the reference was set or not - */ template <> - bool reference_container_owner(PyObject* child, PyObject* owner) { - SwigPyObject* swigThis = SWIG_Python_GetSwigThis(child); - if (swigThis && (swigThis->own & SWIG_POINTER_OWN) != SWIG_POINTER_OWN) { - PyObject_SetAttr(child, container_owner_attribute(), owner); - return true; + struct container_owner { + /** + * Call to add a back-reference to the owning object when returning a + * reference from a container. Will only set the reference if child + * is a SWIG wrapper object that does not own the pointer. + * + * @return if the reference was set or not + */ + static bool reference(PyObject* child, PyObject* owner) { + SwigPyObject* swigThis = SWIG_Python_GetSwigThis(child); + if (swigThis && (swigThis->own & SWIG_POINTER_OWN) != SWIG_POINTER_OWN) { + PyObject_SetAttr(child, container_owner_attribute(), owner); + return true; + } + return false; } - return false; - } + }; } } @@ -804,8 +808,8 @@ namespace swig return self->size(); } - %typemap(ret, fragment="reference_container_owner") value_type& { - swig::reference_container_owner::category>($result, $self); + %typemap(ret, fragment="reference_container_owner", noblock=1) value_type& { + (void)swig::container_owner::category>::reference($result, $self); } } %enddef