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,12 +48,16 @@ namespace swig {
return attr; return attr;
} }
template <typename T>
struct container_owner {
// By default, do not add the back-reference (for value types) // By default, do not add the back-reference (for value types)
// Specialization below will check the reference for pointer types. // Specialization below will check the reference for pointer types.
template <typename Type> static bool reference(PyObject* child, PyObject* owner)
bool reference_container_owner(PyObject* child, PyObject* owner)
{ return false; } { return false; }
};
template <>
struct container_owner<swig::pointer_category> {
/** /**
* Call to add a back-reference to the owning object when returning a * 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> * reference from a container. Will only set the reference if <code>child</code>
@ -61,8 +65,7 @@ namespace swig {
* *
* @return if the reference was set or not * @return if the reference was set or not
*/ */
template <> static bool reference(PyObject* child, PyObject* owner) {
bool reference_container_owner<swig::pointer_category>(PyObject* child, PyObject* owner) {
SwigPyObject* swigThis = SWIG_Python_GetSwigThis(child); SwigPyObject* swigThis = SWIG_Python_GetSwigThis(child);
if (swigThis && (swigThis->own & SWIG_POINTER_OWN) != SWIG_POINTER_OWN) { if (swigThis && (swigThis->own & SWIG_POINTER_OWN) != SWIG_POINTER_OWN) {
PyObject_SetAttr(child, container_owner_attribute(), owner); PyObject_SetAttr(child, container_owner_attribute(), owner);
@ -70,6 +73,7 @@ namespace swig {
} }
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