Add Tcl support for std::unique_ptr and std::auto_ptr

Equivalent to C#/Java implementations.
This commit is contained in:
William S Fulton 2022-08-05 19:07:48 +01:00
commit 5a379d317b
10 changed files with 521 additions and 3 deletions

View file

@ -76,6 +76,19 @@ public static void* swigGetCPtr(typeof(this) obj) {
return (obj is null) ? null : obj.swigCPtr;
}
public static void* swigRelease(typeof(this) obj) {
if (obj !is null) {
if (!obj.swigCMemOwn)
throw new Exception("Cannot release ownership as memory is not owned");
void* ptr = obj.swigCPtr;
obj.swigCMemOwn = false;
obj.dispose();
return ptr;
} else {
return null;
}
}
mixin $imdmodule.SwigOperatorDefinitions;
%}
@ -92,6 +105,19 @@ public static void* swigGetCPtr(typeof(this) obj) {
return (obj is null) ? null : obj.swigCPtr;
}
public static void* swigRelease(typeof(this) obj) {
if (obj !is null) {
if (!obj.swigCMemOwn)
throw new Exception("Cannot release ownership as memory is not owned");
void* ptr = obj.swigCPtr;
obj.swigCMemOwn = false;
obj.dispose();
return ptr;
} else {
return null;
}
}
mixin $imdmodule.SwigOperatorDefinitions;
%}
@ -100,7 +126,7 @@ mixin $imdmodule.SwigOperatorDefinitions;
* Type wrapper classes.
*/
%typemap(dbody) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] %{
%typemap(dbody) SWIGTYPE *, SWIGTYPE &, SWIGTYPE &&, SWIGTYPE [] %{
private void* swigCPtr;
public this(void* cObject, bool futureUse) {
@ -115,6 +141,10 @@ public static void* swigGetCPtr(typeof(this) obj) {
return (obj is null) ? null : obj.swigCPtr;
}
public static void* swigRelease(typeof(this) obj) {
return (obj is null) ? null : obj.swigCPtr;
}
mixin $imdmodule.SwigOperatorDefinitions;
%}

40
Lib/d/std_auto_ptr.i Normal file
View file

@ -0,0 +1,40 @@
/* -----------------------------------------------------------------------------
* std_auto_ptr.i
*
* 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) std::auto_ptr< TYPE > "void*"
%typemap (dtype) std::auto_ptr< TYPE > "$typemap(dtype, TYPE)"
%typemap(in) std::auto_ptr< TYPE >
%{ $1.reset((TYPE *)$input); %}
%typemap(din,
nativepointer="cast(void*)$dinput"
) std::auto_ptr< TYPE > "$typemap(dtype, TYPE).swigRelease($dinput)"
%typemap (out) std::auto_ptr< TYPE > %{
$result = (void *)$1.release();
%}
%typemap(dout, excode=SWIGEXCODE,
nativepointer="{\n auto ret = cast($dtype)$imcall;$excode\n return ret;\n}"
) std::auto_ptr< TYPE > {
void* cPtr = $imcall;
$typemap(dtype, TYPE) ret = (cPtr is null) ? null : new $typemap(dtype, TYPE)(cPtr, true);$excode
return ret;
}
%template() std::auto_ptr< TYPE >;
%enddef
namespace std {
template <class T> class auto_ptr {};
}

40
Lib/d/std_unique_ptr.i Normal file
View file

@ -0,0 +1,40 @@
/* -----------------------------------------------------------------------------
* std_unique_ptr.i
*
* 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) std::unique_ptr< TYPE > "void*"
%typemap (dtype) std::unique_ptr< TYPE > "$typemap(dtype, TYPE)"
%typemap(in) std::unique_ptr< TYPE >
%{ $1.reset((TYPE *)$input); %}
%typemap(din,
nativepointer="cast(void*)$dinput"
) std::unique_ptr< TYPE > "$typemap(dtype, TYPE).swigRelease($dinput)"
%typemap (out) std::unique_ptr< TYPE > %{
$result = (void *)$1.release();
%}
%typemap(dout, excode=SWIGEXCODE,
nativepointer="{\n auto ret = cast($dtype)$imcall;$excode\n return ret;\n}"
) std::unique_ptr< TYPE > {
void* cPtr = $imcall;
$typemap(dtype, TYPE) ret = (cPtr is null) ? null : new $typemap(dtype, TYPE)(cPtr, true);$excode
return ret;
}
%template() std::unique_ptr< TYPE >;
%enddef
namespace std {
template <class T> class unique_ptr {};
}