diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2022-07-19 20:26:18 +0100 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2022-07-19 20:26:18 +0100 |
commit | 8b654afdefd1f21b0665d6cca7b5725aefc6b797 (patch) | |
tree | f8341a69db6760e71e0412a38e642934e8728e24 /Lib | |
parent | 6e67efa33dbaf0bc4b218af5729f9ea980a70034 (diff) | |
parent | 1b63af0f2c3d28bbaaa8856d7046b0b22c898609 (diff) | |
download | swig-8b654afdefd1f21b0665d6cca7b5725aefc6b797.tar.gz |
Merge branch 'unique_ptr-inputs'
* unique_ptr-inputs:
std::unique_ptr std::auto_ptr tidyup
Add support for std::auto_ptr inputs
Cosmetic formatting and doc updates in std_unique_ptr.i files
Add Perl support for std::unique_ptr inputs
Add Ruby support for std::unique_ptr inputs
Add Python support for std::unique_ptr inputs
Add C# support std::unique_ptr inputs
Java unique_ptr test ownership enhancement to test
Java unique_ptr enhance test for double release
SWIGTYPE && input typemaps now assume object has been moved
Add Java support for std::unique<T> for input parameters.
Closes #692
Conflicts:
CHANGES.current
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/csharp/csharp.swg | 30 | ||||
-rw-r--r-- | Lib/csharp/std_auto_ptr.i | 33 | ||||
-rw-r--r-- | Lib/csharp/std_unique_ptr.i | 33 | ||||
-rw-r--r-- | Lib/java/java.swg | 32 | ||||
-rw-r--r-- | Lib/java/std_auto_ptr.i | 34 | ||||
-rw-r--r-- | Lib/java/std_unique_ptr.i | 34 | ||||
-rw-r--r-- | Lib/perl5/perlrun.swg | 17 | ||||
-rw-r--r-- | Lib/perl5/std_auto_ptr.i | 26 | ||||
-rw-r--r-- | Lib/perl5/std_unique_ptr.i | 26 | ||||
-rw-r--r-- | Lib/python/pyrun.swg | 17 | ||||
-rw-r--r-- | Lib/python/std_auto_ptr.i | 28 | ||||
-rw-r--r-- | Lib/python/std_unique_ptr.i | 28 | ||||
-rw-r--r-- | Lib/ruby/rubyrun.swg | 11 | ||||
-rw-r--r-- | Lib/ruby/std_auto_ptr.i | 26 | ||||
-rw-r--r-- | Lib/ruby/std_unique_ptr.i | 26 | ||||
-rw-r--r-- | Lib/swigerrors.swg | 3 | ||||
-rw-r--r-- | Lib/swigrun.swg | 10 | ||||
-rw-r--r-- | Lib/typemaps/exception.swg | 1 | ||||
-rw-r--r-- | Lib/typemaps/swigtypemaps.swg | 1 |
19 files changed, 318 insertions, 98 deletions
diff --git a/Lib/csharp/csharp.swg b/Lib/csharp/csharp.swg index 94e0458a6..60ab388f3 100644 --- a/Lib/csharp/csharp.swg +++ b/Lib/csharp/csharp.swg @@ -913,6 +913,19 @@ SWIGINTERN const char * SWIG_UnpackData(const char *c, void *ptr, size_t sz) { CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef getCPtr($csclassname obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; } + + CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef swigRelease($csclassname obj) { + if (obj != null) { + if (!obj.swigCMemOwn) + throw new global::System.ApplicationException("Cannot release ownership as memory is not owned"); + global::System.Runtime.InteropServices.HandleRef ptr = obj.swigCPtr; + obj.swigCMemOwn = false; + obj.Dispose(); + return ptr; + } else { + return new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero); + } + } %} // Derived proxy classes @@ -926,6 +939,19 @@ SWIGINTERN const char * SWIG_UnpackData(const char *c, void *ptr, size_t sz) { CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef getCPtr($csclassname obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; } + + CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef swigRelease($csclassname obj) { + if (obj != null) { + if (!obj.swigCMemOwn) + throw new global::System.ApplicationException("Cannot release ownership as memory is not owned"); + global::System.Runtime.InteropServices.HandleRef ptr = obj.swigCPtr; + obj.swigCMemOwn = false; + obj.Dispose(); + return ptr; + } else { + return new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero); + } + } %} %enddef @@ -945,6 +971,10 @@ SWIGINTERN const char * SWIG_UnpackData(const char *c, void *ptr, size_t sz) { CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef getCPtr($csclassname obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; } + + CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef swigRelease($csclassname obj) { + return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; + } %} %typemap(csbody) TYPE (CLASS::*) %{ diff --git a/Lib/csharp/std_auto_ptr.i b/Lib/csharp/std_auto_ptr.i index 068d3a9d1..78d7eaf4c 100644 --- a/Lib/csharp/std_auto_ptr.i +++ b/Lib/csharp/std_auto_ptr.i @@ -1,27 +1,36 @@ /* ----------------------------------------------------------------------------- * std_auto_ptr.i * - * The typemaps here allow handling functions returning std::auto_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::auto_ptr. + * Memory ownership is passed from the std::auto_ptr C++ layer to the proxy + * class when returning a std::auto_ptr from a function. + * Memory ownership is passed from the proxy class to the std::auto_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %auto_ptr(TYPE) %typemap (ctype) std::auto_ptr< TYPE > "void *" -%typemap (imtype, out="System.IntPtr") std::auto_ptr< TYPE > "HandleRef" +%typemap (imtype, out="System.IntPtr") std::auto_ptr< TYPE > "global::System.Runtime.InteropServices.HandleRef" %typemap (cstype) std::auto_ptr< TYPE > "$typemap(cstype, TYPE)" + +%typemap(in) std::auto_ptr< TYPE > +%{ $1.reset((TYPE *)$input); %} + +%typemap(csin) std::auto_ptr< TYPE > "$typemap(cstype, TYPE).swigRelease($csinput)" + %typemap (out) std::auto_ptr< TYPE > %{ - $result = (void *)$1.release(); + $result = (void *)$1.release(); %} + %typemap(csout, excode=SWIGEXCODE) std::auto_ptr< TYPE > { - System.IntPtr cPtr = $imcall; - $typemap(cstype, TYPE) ret = (cPtr == System.IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode - return ret; - } + System.IntPtr cPtr = $imcall; + $typemap(cstype, TYPE) ret = (cPtr == System.IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode + return ret; + } + %template() std::auto_ptr< TYPE >; %enddef namespace std { - template <class T> class auto_ptr {}; -} + template <class T> class auto_ptr {}; +} diff --git a/Lib/csharp/std_unique_ptr.i b/Lib/csharp/std_unique_ptr.i index b2716756a..2233cdc06 100644 --- a/Lib/csharp/std_unique_ptr.i +++ b/Lib/csharp/std_unique_ptr.i @@ -1,27 +1,36 @@ /* ----------------------------------------------------------------------------- * std_unique_ptr.i * - * The typemaps here allow handling functions returning std::unique_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::unique_ptr. + * Memory ownership is passed from the std::unique_ptr C++ layer to the proxy + * class when returning a std::unique_ptr from a function. + * Memory ownership is passed from the proxy class to the std::unique_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %unique_ptr(TYPE) %typemap (ctype) std::unique_ptr< TYPE > "void *" -%typemap (imtype, out="System.IntPtr") std::unique_ptr< TYPE > "HandleRef" +%typemap (imtype, out="System.IntPtr") std::unique_ptr< TYPE > "global::System.Runtime.InteropServices.HandleRef" %typemap (cstype) std::unique_ptr< TYPE > "$typemap(cstype, TYPE)" + +%typemap(in) std::unique_ptr< TYPE > +%{ $1.reset((TYPE *)$input); %} + +%typemap(csin) std::unique_ptr< TYPE > "$typemap(cstype, TYPE).swigRelease($csinput)" + %typemap (out) std::unique_ptr< TYPE > %{ - $result = (void *)$1.release(); + $result = (void *)$1.release(); %} + %typemap(csout, excode=SWIGEXCODE) std::unique_ptr< TYPE > { - System.IntPtr cPtr = $imcall; - $typemap(cstype, TYPE) ret = (cPtr == System.IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode - return ret; - } + System.IntPtr cPtr = $imcall; + $typemap(cstype, TYPE) ret = (cPtr == System.IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode + return ret; + } + %template() std::unique_ptr< TYPE >; %enddef namespace std { - template <class T> class unique_ptr {}; -} + template <class T> class unique_ptr {}; +} diff --git a/Lib/java/java.swg b/Lib/java/java.swg index 23744aeb3..19198b7b4 100644 --- a/Lib/java/java.swg +++ b/Lib/java/java.swg @@ -700,6 +700,7 @@ Swig::LocalRefGuard $1_refguard(jenv, $input); } SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "$1_type reference is null"); return $null; } %} +%typemap(freearg) SWIGTYPE && %{ delete $1; %}; %typemap(out) SWIGTYPE * %{ *($&1_ltype)&$result = $1; %} %typemap(out, fragment="SWIG_PackData", noblock=1) SWIGTYPE (CLASS::*) { @@ -1101,7 +1102,8 @@ Swig::LocalRefGuard $1_refguard(jenv, $input); } jobjectArray "$javainput" %typemap(javain) SWIGTYPE "$&javaclassname.getCPtr($javainput)" -%typemap(javain) SWIGTYPE *, SWIGTYPE &, SWIGTYPE &&, SWIGTYPE [] "$javaclassname.getCPtr($javainput)" +%typemap(javain) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] "$javaclassname.getCPtr($javainput)" +%typemap(javain) SWIGTYPE && "$javaclassname.swigRelease($javainput)" %typemap(javain) SWIGTYPE (CLASS::*) "$javaclassname.getCMemberPtr($javainput)" /* The javaout typemap is used for converting function return types from the return type @@ -1216,6 +1218,18 @@ Swig::LocalRefGuard $1_refguard(jenv, $input); } CPTR_VISIBILITY static long getCPtr($javaclassname obj) { return (obj == null) ? 0 : obj.swigCPtr; } + + CPTR_VISIBILITY static long swigRelease($javaclassname obj) { + long ptr = 0; + if (obj != null) { + if (!obj.swigCMemOwn) + throw new RuntimeException("Cannot release ownership as memory is not owned"); + ptr = obj.swigCPtr; + obj.swigCMemOwn = false; + obj.delete(); + } + return ptr; + } %} // Derived proxy classes @@ -1230,6 +1244,18 @@ Swig::LocalRefGuard $1_refguard(jenv, $input); } CPTR_VISIBILITY static long getCPtr($javaclassname obj) { return (obj == null) ? 0 : obj.swigCPtr; } + + CPTR_VISIBILITY static long swigRelease($javaclassname obj) { + long ptr = 0; + if (obj != null) { + if (!obj.swigCMemOwn) + throw new RuntimeException("Cannot release ownership as memory is not owned"); + ptr = obj.swigCPtr; + obj.swigCMemOwn = false; + obj.delete(); + } + return ptr; + } %} %enddef @@ -1249,6 +1275,10 @@ Swig::LocalRefGuard $1_refguard(jenv, $input); } CPTR_VISIBILITY static long getCPtr($javaclassname obj) { return (obj == null) ? 0 : obj.swigCPtr; } + + CPTR_VISIBILITY static long swigRelease($javaclassname obj) { + return (obj == null) ? 0 : obj.swigCPtr; + } %} %typemap(javabody) TYPE (CLASS::*) %{ diff --git a/Lib/java/std_auto_ptr.i b/Lib/java/std_auto_ptr.i index 0eb5fe155..6d65f0451 100644 --- a/Lib/java/std_auto_ptr.i +++ b/Lib/java/std_auto_ptr.i @@ -1,29 +1,39 @@ /* ----------------------------------------------------------------------------- * std_auto_ptr.i * - * The typemaps here allow handling functions returning std::auto_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::auto_ptr. + * Memory ownership is passed from the std::auto_ptr C++ layer to the proxy + * class when returning a std::auto_ptr from a function. + * Memory ownership is passed from the proxy class to the std::auto_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %auto_ptr(TYPE) + %typemap (jni) std::auto_ptr< TYPE > "jlong" %typemap (jtype) std::auto_ptr< TYPE > "long" %typemap (jstype) std::auto_ptr< TYPE > "$typemap(jstype, TYPE)" +%typemap(in) std::auto_ptr< TYPE > (TYPE *auto_temp) +%{ auto_temp = *(TYPE **)&$input; + $1.reset(auto_temp); %} + +%typemap(javain) std::auto_ptr< TYPE > "$typemap(jstype, TYPE).swigRelease($javainput)" + %typemap (out) std::auto_ptr< TYPE > %{ - jlong lpp = 0; - *(TYPE **) &lpp = $1.release(); - $result = lpp; + jlong lpp = 0; + *(TYPE **) &lpp = $1.release(); + $result = lpp; %} + %typemap(javaout) std::auto_ptr< TYPE > { - long cPtr = $jnicall; - return (cPtr == 0) ? null : new $typemap(jstype, TYPE)(cPtr, true); - } + long cPtr = $jnicall; + return (cPtr == 0) ? null : new $typemap(jstype, TYPE)(cPtr, true); + } + %template() std::auto_ptr< TYPE >; %enddef namespace std { - template <class T> class auto_ptr {}; -} + template <class T> class auto_ptr {}; +} diff --git a/Lib/java/std_unique_ptr.i b/Lib/java/std_unique_ptr.i index 665d913ae..e1e5a2622 100644 --- a/Lib/java/std_unique_ptr.i +++ b/Lib/java/std_unique_ptr.i @@ -1,29 +1,39 @@ /* ----------------------------------------------------------------------------- * std_unique_ptr.i * - * The typemaps here allow handling functions returning std::unique_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::unique_ptr. + * Memory ownership is passed from the std::unique_ptr C++ layer to the proxy + * class when returning a std::unique_ptr from a function. + * Memory ownership is passed from the proxy class to the std::unique_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %unique_ptr(TYPE) + %typemap (jni) std::unique_ptr< TYPE > "jlong" %typemap (jtype) std::unique_ptr< TYPE > "long" %typemap (jstype) std::unique_ptr< TYPE > "$typemap(jstype, TYPE)" +%typemap(in) std::unique_ptr< TYPE > (TYPE *unique_temp) +%{ unique_temp = *(TYPE **)&$input; + $1.reset(unique_temp); %} + +%typemap(javain) std::unique_ptr< TYPE > "$typemap(jstype, TYPE).swigRelease($javainput)" + %typemap (out) std::unique_ptr< TYPE > %{ - jlong lpp = 0; - *(TYPE **) &lpp = $1.release(); - $result = lpp; + jlong lpp = 0; + *(TYPE **) &lpp = $1.release(); + $result = lpp; %} + %typemap(javaout) std::unique_ptr< TYPE > { - long cPtr = $jnicall; - return (cPtr == 0) ? null : new $typemap(jstype, TYPE)(cPtr, true); - } + long cPtr = $jnicall; + return (cPtr == 0) ? null : new $typemap(jstype, TYPE)(cPtr, true); + } + %template() std::unique_ptr< TYPE >; %enddef namespace std { - template <class T> class unique_ptr {}; -} + template <class T> class unique_ptr {}; +} diff --git a/Lib/perl5/perlrun.swg b/Lib/perl5/perlrun.swg index 28703d1a6..71f19cbf8 100644 --- a/Lib/perl5/perlrun.swg +++ b/Lib/perl5/perlrun.swg @@ -210,6 +210,7 @@ SWIG_Perl_ConvertPtrAndOwn(SWIG_MAYBE_PERL_OBJECT SV *sv, void **ptr, swig_type_ swig_cast_info *tc; void *voidptr = (void *)0; SV *tsv = 0; + int check_owned_pointer_release = (flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE; if (own) *own = 0; @@ -286,13 +287,14 @@ SWIG_Perl_ConvertPtrAndOwn(SWIG_MAYBE_PERL_OBJECT SV *sv, void **ptr, swig_type_ /* * DISOWN implementation: we need a perl guru to check this one. */ - if (tsv && (flags & SWIG_POINTER_DISOWN)) { + if (tsv && ((flags & SWIG_POINTER_DISOWN) || check_owned_pointer_release)) { /* * almost copy paste code from below SWIG_POINTER_OWN setting */ SV *obj = sv; HV *stash = SvSTASH(SvRV(obj)); GV *gv = *(GV**)hv_fetch(stash, "OWNER", 5, TRUE); + int owned = 0; if (isGV(gv)) { HV *hv = GvHVn(gv); /* @@ -300,10 +302,21 @@ SWIG_Perl_ConvertPtrAndOwn(SWIG_MAYBE_PERL_OBJECT SV *sv, void **ptr, swig_type_ * Hence, to remove ownership, we delete the entry. */ if (hv_exists_ent(hv, obj, 0)) { - hv_delete_ent(hv, obj, 0, 0); + owned = 1; + if (flags & SWIG_POINTER_DISOWN) { + hv_delete_ent(hv, obj, 0, 0); + } } } + if (check_owned_pointer_release && !owned) { + return SWIG_ERROR_RELEASE_NOT_OWNED; + } } + + if (tsv && (flags & SWIG_POINTER_CLEAR)) { + SvIV_set(tsv, 0); + } + return SWIG_OK; } diff --git a/Lib/perl5/std_auto_ptr.i b/Lib/perl5/std_auto_ptr.i index ecaea2b0f..d062886e4 100644 --- a/Lib/perl5/std_auto_ptr.i +++ b/Lib/perl5/std_auto_ptr.i @@ -1,19 +1,33 @@ /* ----------------------------------------------------------------------------- * std_auto_ptr.i * - * The typemaps here allow handling functions returning std::auto_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::auto_ptr. + * Memory ownership is passed from the std::auto_ptr C++ layer to the proxy + * class when returning a std::auto_ptr from a function. + * Memory ownership is passed from the proxy class to the std::auto_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %auto_ptr(TYPE) +%typemap(in, noblock=1) std::auto_ptr< TYPE > (void *argp = 0, int res = 0) { + res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags); + if (!SWIG_IsOK(res)) { + if (res == SWIG_ERROR_RELEASE_NOT_OWNED) { + %releasenotowned_fail(res, "TYPE *", $symname, $argnum); + } else { + %argument_fail(res, "TYPE *", $symname, $argnum); + } + } + $1.reset((TYPE *)argp); +} + %typemap (out) std::auto_ptr< TYPE > %{ - %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); + %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); %} + %template() std::auto_ptr< TYPE >; %enddef namespace std { - template <class T> class auto_ptr {}; + template <class T> class auto_ptr {}; } diff --git a/Lib/perl5/std_unique_ptr.i b/Lib/perl5/std_unique_ptr.i index 163c7c2d1..1a7ec06fa 100644 --- a/Lib/perl5/std_unique_ptr.i +++ b/Lib/perl5/std_unique_ptr.i @@ -1,19 +1,33 @@ /* ----------------------------------------------------------------------------- * std_unique_ptr.i * - * The typemaps here allow handling functions returning std::unique_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::unique_ptr. + * Memory ownership is passed from the std::unique_ptr C++ layer to the proxy + * class when returning a std::unique_ptr from a function. + * Memory ownership is passed from the proxy class to the std::unique_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %unique_ptr(TYPE) +%typemap(in, noblock=1) std::unique_ptr< TYPE > (void *argp = 0, int res = 0) { + res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags); + if (!SWIG_IsOK(res)) { + if (res == SWIG_ERROR_RELEASE_NOT_OWNED) { + %releasenotowned_fail(res, "TYPE *", $symname, $argnum); + } else { + %argument_fail(res, "TYPE *", $symname, $argnum); + } + } + $1.reset((TYPE *)argp); +} + %typemap (out) std::unique_ptr< TYPE > %{ - %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); + %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); %} + %template() std::unique_ptr< TYPE >; %enddef namespace std { - template <class T> class unique_ptr {}; + template <class T> class unique_ptr {}; } diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index a74843bf1..36a20c422 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -1357,12 +1357,19 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int } } if (sobj) { - if (own) - *own = *own | sobj->own; - if (flags & SWIG_POINTER_DISOWN) { - sobj->own = 0; + if (((flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE) && !sobj->own) { + res = SWIG_ERROR_RELEASE_NOT_OWNED; + } else { + if (own) + *own = *own | sobj->own; + if (flags & SWIG_POINTER_DISOWN) { + sobj->own = 0; + } + if (flags & SWIG_POINTER_CLEAR) { + sobj->ptr = 0; + } + res = SWIG_OK; } - res = SWIG_OK; } else { if (implicit_conv) { SwigPyClientData *data = ty ? (SwigPyClientData *) ty->clientdata : 0; diff --git a/Lib/python/std_auto_ptr.i b/Lib/python/std_auto_ptr.i index fb044f851..d062886e4 100644 --- a/Lib/python/std_auto_ptr.i +++ b/Lib/python/std_auto_ptr.i @@ -1,19 +1,33 @@ /* ----------------------------------------------------------------------------- * std_auto_ptr.i * - * The typemaps here allow handling functions returning std::auto_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::auto_ptr. + * Memory ownership is passed from the std::auto_ptr C++ layer to the proxy + * class when returning a std::auto_ptr from a function. + * Memory ownership is passed from the proxy class to the std::auto_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %auto_ptr(TYPE) +%typemap(in, noblock=1) std::auto_ptr< TYPE > (void *argp = 0, int res = 0) { + res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags); + if (!SWIG_IsOK(res)) { + if (res == SWIG_ERROR_RELEASE_NOT_OWNED) { + %releasenotowned_fail(res, "TYPE *", $symname, $argnum); + } else { + %argument_fail(res, "TYPE *", $symname, $argnum); + } + } + $1.reset((TYPE *)argp); +} + %typemap (out) std::auto_ptr< TYPE > %{ - %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); + %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); %} + %template() std::auto_ptr< TYPE >; %enddef namespace std { - template <class T> class auto_ptr {}; -} + template <class T> class auto_ptr {}; +} diff --git a/Lib/python/std_unique_ptr.i b/Lib/python/std_unique_ptr.i index 331817f76..1a7ec06fa 100644 --- a/Lib/python/std_unique_ptr.i +++ b/Lib/python/std_unique_ptr.i @@ -1,19 +1,33 @@ /* ----------------------------------------------------------------------------- * std_unique_ptr.i * - * The typemaps here allow handling functions returning std::unique_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::unique_ptr. + * Memory ownership is passed from the std::unique_ptr C++ layer to the proxy + * class when returning a std::unique_ptr from a function. + * Memory ownership is passed from the proxy class to the std::unique_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %unique_ptr(TYPE) +%typemap(in, noblock=1) std::unique_ptr< TYPE > (void *argp = 0, int res = 0) { + res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags); + if (!SWIG_IsOK(res)) { + if (res == SWIG_ERROR_RELEASE_NOT_OWNED) { + %releasenotowned_fail(res, "TYPE *", $symname, $argnum); + } else { + %argument_fail(res, "TYPE *", $symname, $argnum); + } + } + $1.reset((TYPE *)argp); +} + %typemap (out) std::unique_ptr< TYPE > %{ - %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); + %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); %} + %template() std::unique_ptr< TYPE >; %enddef namespace std { - template <class T> class unique_ptr {}; -} + template <class T> class unique_ptr {}; +} diff --git a/Lib/ruby/rubyrun.swg b/Lib/ruby/rubyrun.swg index e4c23bde5..6cac4626a 100644 --- a/Lib/ruby/rubyrun.swg +++ b/Lib/ruby/rubyrun.swg @@ -281,6 +281,11 @@ SWIG_Ruby_ConvertPtrAndOwn(VALUE obj, void **ptr, swig_type_info *ty, int flags, own->own = 0; } + if (((flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE)) { + if (!RDATA(obj)->dfree) + return SWIG_ERROR_RELEASE_NOT_OWNED; + } + /* Check to see if the input object is giving up ownership of the underlying C struct or C++ object. If so then we need to reset the destructor since the Ruby object no @@ -292,7 +297,7 @@ SWIG_Ruby_ConvertPtrAndOwn(VALUE obj, void **ptr, swig_type_info *ty, int flags, swig_class *sklass = (swig_class *) ty->clientdata; track = sklass->trackObjects; } - + if (track) { /* We are tracking objects for this class. Thus we change the destructor * to SWIG_RubyRemoveTracking. This allows us to @@ -306,6 +311,10 @@ SWIG_Ruby_ConvertPtrAndOwn(VALUE obj, void **ptr, swig_type_info *ty, int flags, } } + if (flags & SWIG_POINTER_CLEAR) { + DATA_PTR(obj) = 0; + } + /* Do type-checking if type info was provided */ if (ty) { if (ty->clientdata) { diff --git a/Lib/ruby/std_auto_ptr.i b/Lib/ruby/std_auto_ptr.i index ecaea2b0f..d062886e4 100644 --- a/Lib/ruby/std_auto_ptr.i +++ b/Lib/ruby/std_auto_ptr.i @@ -1,19 +1,33 @@ /* ----------------------------------------------------------------------------- * std_auto_ptr.i * - * The typemaps here allow handling functions returning std::auto_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::auto_ptr. + * Memory ownership is passed from the std::auto_ptr C++ layer to the proxy + * class when returning a std::auto_ptr from a function. + * Memory ownership is passed from the proxy class to the std::auto_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %auto_ptr(TYPE) +%typemap(in, noblock=1) std::auto_ptr< TYPE > (void *argp = 0, int res = 0) { + res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags); + if (!SWIG_IsOK(res)) { + if (res == SWIG_ERROR_RELEASE_NOT_OWNED) { + %releasenotowned_fail(res, "TYPE *", $symname, $argnum); + } else { + %argument_fail(res, "TYPE *", $symname, $argnum); + } + } + $1.reset((TYPE *)argp); +} + %typemap (out) std::auto_ptr< TYPE > %{ - %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); + %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); %} + %template() std::auto_ptr< TYPE >; %enddef namespace std { - template <class T> class auto_ptr {}; + template <class T> class auto_ptr {}; } diff --git a/Lib/ruby/std_unique_ptr.i b/Lib/ruby/std_unique_ptr.i index 163c7c2d1..1a7ec06fa 100644 --- a/Lib/ruby/std_unique_ptr.i +++ b/Lib/ruby/std_unique_ptr.i @@ -1,19 +1,33 @@ /* ----------------------------------------------------------------------------- * std_unique_ptr.i * - * The typemaps here allow handling functions returning std::unique_ptr<>, - * which is the most common use of this type. If you have functions taking it - * as parameter, these typemaps can't be used for them and you need to do - * something else (e.g. use shared_ptr<> which SWIG supports fully). + * SWIG library file for handling std::unique_ptr. + * Memory ownership is passed from the std::unique_ptr C++ layer to the proxy + * class when returning a std::unique_ptr from a function. + * Memory ownership is passed from the proxy class to the std::unique_ptr in the + * C++ layer when passed as a parameter to a wrapped function. * ----------------------------------------------------------------------------- */ %define %unique_ptr(TYPE) +%typemap(in, noblock=1) std::unique_ptr< TYPE > (void *argp = 0, int res = 0) { + res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags); + if (!SWIG_IsOK(res)) { + if (res == SWIG_ERROR_RELEASE_NOT_OWNED) { + %releasenotowned_fail(res, "TYPE *", $symname, $argnum); + } else { + %argument_fail(res, "TYPE *", $symname, $argnum); + } + } + $1.reset((TYPE *)argp); +} + %typemap (out) std::unique_ptr< TYPE > %{ - %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); + %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); %} + %template() std::unique_ptr< TYPE >; %enddef namespace std { - template <class T> class unique_ptr {}; + template <class T> class unique_ptr {}; } diff --git a/Lib/swigerrors.swg b/Lib/swigerrors.swg index 1a6d20366..4d5a8e473 100644 --- a/Lib/swigerrors.swg +++ b/Lib/swigerrors.swg @@ -1,4 +1,4 @@ -/* Errors in SWIG */ +/* SWIG Errors applicable to all language modules, values are reserved from -1 to -99 */ #define SWIG_UnknownError -1 #define SWIG_IOError -2 #define SWIG_RuntimeError -3 @@ -13,4 +13,3 @@ #define SWIG_MemoryError -12 #define SWIG_NullReferenceError -13 - diff --git a/Lib/swigrun.swg b/Lib/swigrun.swg index de0db2dc3..9ec98ccf2 100644 --- a/Lib/swigrun.swg +++ b/Lib/swigrun.swg @@ -44,6 +44,8 @@ #define SWIG_POINTER_DISOWN 0x1 #define SWIG_CAST_NEW_MEMORY 0x2 #define SWIG_POINTER_NO_NULL 0x4 +#define SWIG_POINTER_CLEAR 0x8 +#define SWIG_POINTER_RELEASE (SWIG_POINTER_CLEAR | SWIG_POINTER_DISOWN) /* Flags for new pointer objects */ #define SWIG_POINTER_OWN 0x1 @@ -129,7 +131,13 @@ */ #define SWIG_OK (0) +/* Runtime errors are < 0 */ #define SWIG_ERROR (-1) +/* Errors in range -1 to -99 are in swigerrors.swg (errors for all languages including those not using the runtime) */ +/* Errors in range -100 to -199 are language specific errors defined in *errors.swg */ +/* Errors < -200 are generic runtime specific errors */ +#define SWIG_ERROR_RELEASE_NOT_OWNED (-200) + #define SWIG_IsOK(r) (r >= 0) #define SWIG_ArgError(r) ((r != SWIG_ERROR) ? r : SWIG_TypeError) @@ -144,7 +152,7 @@ #define SWIG_OLDOBJ (SWIG_OK) #define SWIG_NEWOBJ (SWIG_OK | SWIG_NEWOBJMASK) #define SWIG_TMPOBJ (SWIG_OK | SWIG_TMPOBJMASK) -/* Check, add and del mask methods */ +/* Check, add and del object mask methods */ #define SWIG_AddNewMask(r) (SWIG_IsOK(r) ? (r | SWIG_NEWOBJMASK) : r) #define SWIG_DelNewMask(r) (SWIG_IsOK(r) ? (r & ~SWIG_NEWOBJMASK) : r) #define SWIG_IsNewObj(r) (SWIG_IsOK(r) && (r & SWIG_NEWOBJMASK)) diff --git a/Lib/typemaps/exception.swg b/Lib/typemaps/exception.swg index e48294c3f..aece8326f 100644 --- a/Lib/typemaps/exception.swg +++ b/Lib/typemaps/exception.swg @@ -19,6 +19,7 @@ #endif #define %varnullref_fmt(_type,_name) %nullref_fmt() %varfail_fmt(_type, _name) #define %outnullref_fmt(_type) %nullref_fmt() %outfail_fmt(_type) +#define %releasenotownedfail_fmt(_type,_name,_argn) "in method '" `_name` "', cannot release ownership as memory is not owned for argument " `_argn`" of type '" `_type`"'" /* setting an error */ #define %error(code,msg...) SWIG_Error(code, msg) diff --git a/Lib/typemaps/swigtypemaps.swg b/Lib/typemaps/swigtypemaps.swg index 4e5bb2b04..733e5acd0 100644 --- a/Lib/typemaps/swigtypemaps.swg +++ b/Lib/typemaps/swigtypemaps.swg @@ -140,6 +140,7 @@ #define %argument_nullref(type, name, argn) SWIG_exception_fail(SWIG_ValueError, %argnullref_fmt(type, name, argn)) #define %variable_fail(code, type, name) SWIG_exception_fail(%default_code(code), %varfail_fmt(type, name)) #define %variable_nullref(type, name) SWIG_exception_fail(SWIG_ValueError, %varnullref_fmt(type, name)) +#define %releasenotowned_fail(code, type, name, argn) SWIG_exception_fail(%default_code(code), %releasenotownedfail_fmt(type, name, argn)) #if defined(SWIG_DIRECTOR_TYPEMAPS) #define %dirout_fail(code, type) SWIG_DirOutFail(%default_code(code), %outfail_fmt(type)) |