Provide SWIGTYPE MOVE typemaps in swigmove.i

For implementing full move semantics when passing parameters by value.
Based on SWIGTYPE && and std::unique_ptr typemaps which implement move
semantics.

Added for all languages, but untested for: Go, Ocaml, R, Scilab (and
unlikely to be fully functional for same reasons as for std::unique_ptr
support).

Issue #999
This commit is contained in:
William S Fulton 2022-09-16 08:36:25 +01:00
commit dad7c93ca0
41 changed files with 909 additions and 19 deletions

16
Lib/csharp/swigmove.i Normal file
View file

@ -0,0 +1,16 @@
/* -----------------------------------------------------------------------------
* swigmove.i
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in, canthrow=1, fragment="<memory>") SWIGTYPE MOVE ($&1_type argp)
%{ argp = ($&1_ltype)$input;
if (!argp) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "Attempt to dereference null $1_type", 0);
return $null;
}
SwigValueWrapper< $1_ltype >::reset($1, argp); %}
%typemap(csin) SWIGTYPE MOVE "$&csclassname.swigRelease($csinput)"

16
Lib/d/swigmove.i Normal file
View file

@ -0,0 +1,16 @@
/* -----------------------------------------------------------------------------
* swigmove.i
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in, canthrow=1) SWIGTYPE MOVE ($&1_type argp)
%{ argp = ($&1_ltype)$input;
if (!argp) {
SWIG_DSetPendingException(SWIG_DIllegalArgumentException, "Attempt to dereference null $1_type");
return $null;
}
SwigValueWrapper< $1_ltype >::reset($1, argp); %}
%typemap(din) SWIGTYPE MOVE "$dclassname.swigRelease($dinput)"

15
Lib/go/swigmove.i Normal file
View file

@ -0,0 +1,15 @@
/* -----------------------------------------------------------------------------
* swigmove.i
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in) SWIGTYPE MOVE ($&1_type argp)
%{
argp = ($&1_ltype)$input;
if (argp == NULL) {
_swig_gopanic("Attempt to dereference null $1_type");
}
SwigValueWrapper< $1_ltype >::reset($1, argp);
%}

19
Lib/guile/swigmove.i Normal file
View file

@ -0,0 +1,19 @@
/* -----------------------------------------------------------------------------
* swigmove.i
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in, noblock=1) SWIGTYPE MOVE (void *argp = 0, int res = 0) {
res = SWIG_ConvertPtr($input, &argp, $&1_descriptor, SWIG_POINTER_RELEASE);
if (!SWIG_IsOK(res)) {
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
%releasenotowned_fail(res, "$1_type", $symname, $argnum);
} else {
%argument_fail(res, "$1_type", $symname, $argnum);
}
}
if (!argp) { %argument_nullref("$1_type", $symname, $argnum); }
SwigValueWrapper< $1_ltype >::reset($1, ($&1_type)argp);
}

16
Lib/java/swigmove.i Normal file
View file

@ -0,0 +1,16 @@
/* -----------------------------------------------------------------------------
* swigmove.i
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in) SWIGTYPE MOVE ($&1_type argp)
%{ argp = *($&1_ltype*)&$input;
if (!argp) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "Attempt to dereference null $1_type");
return $null;
}
SwigValueWrapper< $1_ltype >::reset($1, argp); %}
%typemap(javain) SWIGTYPE MOVE "$&javaclassname.swigRelease($javainput)"

View file

@ -0,0 +1 @@
%include <typemaps/swigmove.swg>

View file

@ -0,0 +1 @@
%include <typemaps/swigmove.swg>

18
Lib/lua/swigmove.i Normal file
View file

@ -0,0 +1,18 @@
/* -----------------------------------------------------------------------------
* swigmove.i
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in, checkfn="lua_isuserdata", noblock=1) SWIGTYPE MOVE (void *argp = 0, int res = 0) {
res = SWIG_ConvertPtr(L, $input, &argp, $&1_descriptor, SWIG_POINTER_RELEASE);
if (!SWIG_IsOK(res)) {
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
lua_pushfstring(L, "Cannot release ownership as memory is not owned for argument $argnum of type '$1_type' in $symname"); SWIG_fail;
} else {
SWIG_fail_ptr("$symname", $argnum, $&1_descriptor);
}
}
SwigValueWrapper< $1_ltype >::reset($1, ($&1_type)argp);
}

19
Lib/mzscheme/swigmove.i Normal file
View file

@ -0,0 +1,19 @@
/* -----------------------------------------------------------------------------
* swigmove.i
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in, noblock=1) SWIGTYPE MOVE (void *argp = 0, int res = 0) {
res = SWIG_ConvertPtr($input, &argp, $&1_descriptor, SWIG_POINTER_RELEASE);
if (!SWIG_IsOK(res)) {
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
scheme_signal_error(FUNC_NAME ": cannot release ownership as memory is not owned for argument $argnum of type '$1_type'");
} else {
%argument_fail(res, "$1_type", $symname, $argnum);
}
}
if (argp == NULL) scheme_signal_error(FUNC_NAME ": swig-type-error (null reference)");
SwigValueWrapper< $1_ltype >::reset($1, ($&1_type)argp);
}

11
Lib/ocaml/swigmove.i Normal file
View file

@ -0,0 +1,11 @@
/* -----------------------------------------------------------------------------
* swigmove.i
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in, noblock=1) SWIGTYPE MOVE (void *argp = 0) {
argp1 = ($&1_ltype) caml_ptr_val($input,$&1_descriptor);
SwigValueWrapper< $1_ltype >::reset($1, ($&1_type)argp);
}

1
Lib/octave/swigmove.i Normal file
View file

@ -0,0 +1 @@
%include <typemaps/swigmove.swg>

1
Lib/perl5/swigmove.i Normal file
View file

@ -0,0 +1 @@
%include <typemaps/swigmove.swg>

24
Lib/php/swigmove.i Normal file
View file

@ -0,0 +1,24 @@
/* -----------------------------------------------------------------------------
* swigmove.i
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in, noblock=1) SWIGTYPE MOVE (void *argp = 0, int res = 0) {
res = SWIG_ConvertPtr(&$input, &argp, $&1_descriptor, SWIG_POINTER_RELEASE);
if (!SWIG_IsOK(res)) {
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
zend_type_error("Cannot release ownership as memory is not owned for argument $argnum of $&1_descriptor of $symname");
return;
} else {
zend_type_error("Expected $&1_descriptor for argument $argnum of $symname");
return;
}
}
if (!argp) {
zend_type_error("Invalid null reference for argument $argnum of $&1_descriptor of $symname");
return;
}
SwigValueWrapper< $1_ltype >::reset($1, ($&1_type)argp);
}

1
Lib/python/swigmove.i Normal file
View file

@ -0,0 +1 @@
%include <typemaps/swigmove.swg>

1
Lib/r/swigmove.i Normal file
View file

@ -0,0 +1 @@
%include <typemaps/swigmove.swg>

1
Lib/ruby/swigmove.i Normal file
View file

@ -0,0 +1 @@
%include <typemaps/swigmove.swg>

1
Lib/scilab/swigmove.i Normal file
View file

@ -0,0 +1 @@
%include <typemaps/swigmove.swg>

View file

@ -656,6 +656,16 @@ namespace std {
* arg1 = *inarg1; // Assignment from a pointer
* arg1 = Vector(1,2,3); // Assignment from a value
*
* SwigValueWrapper is a drop in replacement to modify normal value semantics by
* using the heap instead of the stack to copy/move the underlying object it is
* managing. Smart pointers also manage an underlying object on the heap, so
* SwigValueWrapper has characteristics of a smart pointer. The reset function
* is specific smart pointer functionality, but cannot be a non-static member as
* when SWIG modifies typemap code it assumes non-static member function calls
* are routed to the underlying object, changing for example $1.f() to (&x)->f().
* The reset function was added as an optimisation to avoid some copying/moving
* and to take ownership of an object already created on the heap.
*
* The class offers a strong guarantee of exception safety.
* With regards to the implementation, the private SwigSmartPointer nested class is
* a simple smart pointer providing exception safety, much like std::auto_ptr.
@ -677,6 +687,7 @@ template<typename T> class SwigValueWrapper {
SwigSmartPointer(T *p) : ptr(p) { }
~SwigSmartPointer() { delete ptr; }
SwigSmartPointer& operator=(SwigSmartPointer& rhs) { T* oldptr = ptr; ptr = 0; delete oldptr; ptr = rhs.ptr; rhs.ptr = 0; return *this; }
void reset(T *p) { T* oldptr = ptr; ptr = 0; delete oldptr; ptr = p; }
} pointer;
SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs);
SwigValueWrapper(const SwigValueWrapper<T>& rhs);
@ -690,6 +701,7 @@ public:
operator T&() const { return *pointer.ptr; }
#endif
T *operator&() const { return pointer.ptr; }
static void reset(SwigValueWrapper& t, T *p) { t.pointer.reset(p); }
};
/*

1
Lib/tcl/swigmove.i Normal file
View file

@ -0,0 +1 @@
%include <typemaps/swigmove.swg>

19
Lib/typemaps/swigmove.swg Normal file
View file

@ -0,0 +1,19 @@
/* -----------------------------------------------------------------------------
* swigmove.swg
*
* Input typemaps library for implementing full move semantics when passing
* parameters by value.
* ----------------------------------------------------------------------------- */
%typemap(in, noblock=1) SWIGTYPE MOVE (void *argp = 0, int res = 0) {
res = SWIG_ConvertPtr($input, &argp, $&1_descriptor, SWIG_POINTER_RELEASE);
if (!SWIG_IsOK(res)) {
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
%releasenotowned_fail(res, "$1_type", $symname, $argnum);
} else {
%argument_fail(res, "$1_type", $symname, $argnum);
}
}
if (!argp) { %argument_nullref("$1_type", $symname, $argnum); }
SwigValueWrapper< $1_ltype >::reset($1, ($&1_type)argp);
}