Python std::vector back-reference changes

Give reference in container_owner a more unique name (back_reference).
Change back-reference 'ret' typemap to have a function name so that they
are less unwittingly be used elsewhere where not intended.

Note that they can be overridden by users if needed using:

%extend std::vector {
%typemap(ret)
  value_type const& __getitem__,
  value_type const& front,
  value_type const& back
  { ...  }
}

These override the SWIG supplied versions because the SWIG supplied
typemaps use non-const value_type&, but the methods use const, so the
above have a higher precedence in the typemap search algorithm.
This commit is contained in:
William S Fulton 2019-02-18 18:50:22 +00:00
commit a628bf9b6f

View file

@ -52,7 +52,7 @@ namespace swig {
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) {
static bool back_reference(PyObject* child, PyObject* owner) {
return false;
}
};
@ -66,7 +66,7 @@ namespace swig {
*
* returns whether the reference was set or not
*/
static bool reference(PyObject* child, PyObject* owner) {
static bool back_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);
@ -809,8 +809,10 @@ namespace swig
return self->size();
}
%typemap(ret, fragment="reference_container_owner", noblock=1) value_type& {
(void)swig::container_owner<swig::traits<$*1_ltype>::category>::reference($result, $self);
// Although __getitem__, front, back actually use a const value_type& return type, the typemaps below
// use non-const so that they can be easily overridden by users if necessary.
%typemap(ret, fragment="reference_container_owner", noblock=1) value_type& __getitem__, value_type& front, value_type& back {
(void)swig::container_owner<swig::traits<$*1_ltype>::category>::back_reference($result, $self);
}
}
%enddef