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.
This commit is contained in:
Jake Cobb 2019-02-04 13:48:52 -05:00
commit 30f16b9138

View file

@ -48,28 +48,32 @@ namespace swig {
return attr; return attr;
} }
// By default, do not add the back-reference (for value types) template <typename T>
// Specialization below will check the reference for pointer types. struct container_owner {
template <typename Type> // By default, do not add the back-reference (for value types)
bool reference_container_owner(PyObject* child, PyObject* owner) // Specialization below will check the reference for pointer types.
{ return false; } 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 <code>child</code>
* is a SWIG wrapper object that does not own the pointer.
*
* @return if the reference was set or not
*/
template <> template <>
bool reference_container_owner<swig::pointer_category>(PyObject* child, PyObject* owner) { struct container_owner<swig::pointer_category> {
SwigPyObject* swigThis = SWIG_Python_GetSwigThis(child); /**
if (swigThis && (swigThis->own & SWIG_POINTER_OWN) != SWIG_POINTER_OWN) { * Call to add a back-reference to the owning object when returning a
PyObject_SetAttr(child, container_owner_attribute(), owner); * reference from a container. Will only set the reference if <code>child</code>
return true; * 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(); return self->size();
} }
%typemap(ret, fragment="reference_container_owner") value_type& { %typemap(ret, fragment="reference_container_owner", noblock=1) value_type& {
swig::reference_container_owner<swig::traits<$*1_ltype>::category>($result, $self); (void)swig::container_owner<swig::traits<$*1_ltype>::category>::reference($result, $self);
} }
} }
%enddef %enddef