std::unique_ptr return by reference typemaps added

This commit is contained in:
William S Fulton 2024-03-02 22:35:55 +00:00
parent 3f1e40d2f4
commit 5712ce6464
29 changed files with 119 additions and 34 deletions

View File

@ -2222,13 +2222,14 @@ void grab(std::unique_ptr<Klass> &&);
</div>
<p>
Move semantics are not provided when wrapping a C++ function that returns a <tt>std::unique_ptr</tt> by rvalue reference.
Move semantics are not provided when wrapping a C++ function that returns a <tt>std::unique_ptr</tt> by reference.
The target language proxy class wrapper that is returned does not own the underlying C++ object.
Example:
This applies to all reference types, such as:
</p>
<div class="code">
<pre>
std::unique_ptr<Klass> &amp; LvalueRefReturn();
std::unique_ptr<Klass> &amp;&amp; RvalueRefReturn();
</pre>
</div>

View File

@ -109,13 +109,15 @@ std::unique_ptr<Klass>&& makeRVRKlassUniquePtr(const char* label) {
return std::move(up);
}
/*
std::unique_ptr<Klass>& makeRefKlassUniquePtr(const char* label) {
static std::unique_ptr<Klass> up;
up.reset(new Klass(label));
#if !defined(SWIGTCL)
up.reset(label ? new Klass(label) : nullptr);
#else // No way to pass a null pointer for a string as "NULL" is only way for Tcl to specify a null pointer
up.reset(strcmp(label, "NULL") != 0 ? new Klass(label) : nullptr);
#endif
return up;
}
*/
int overloadTest() {

View File

@ -212,5 +212,13 @@ public class cpp11_std_unique_ptr_runme {
k1 = cpp11_std_unique_ptr.makeRVRKlassUniquePtr(null);
if (k1 != null)
throw new Exception("not null");
// unique_ptr as output (lvalue ref)
k1 = cpp11_std_unique_ptr.makeRefKlassUniquePtr("lvalueref");
if (k1.getLabel() != "lvalueref")
throw new Exception("wrong object label");
k1 = cpp11_std_unique_ptr.makeRefKlassUniquePtr(null);
if (k1 != null)
throw new Exception("not null");
}
}

View File

@ -208,4 +208,12 @@ void main() {
k1 = makeRVRKlassUniquePtr(null);
if (k1 !is null)
throw new Exception("not null");
// unique_ptr as output (lvalue ref)
k1 = makeRefKlassUniquePtr("lvalueref");
if (k1.getLabel() != "lvalueref")
throw new Exception("wrong object label");
k1 = makeRefKlassUniquePtr(null);
if (k1 !is null)
throw new Exception("not null");
}

View File

@ -236,5 +236,13 @@ public class cpp11_std_unique_ptr_runme {
k1 = cpp11_std_unique_ptr.makeRVRKlassUniquePtr(null);
if (k1 != null)
throw new RuntimeException("not null");
// unique_ptr as output (lvalue ref)
k1 = cpp11_std_unique_ptr.makeRefKlassUniquePtr("lvalueref");
if (!k1.getLabel().equals("lvalueref"))
throw new RuntimeException("wrong object label");
k1 = cpp11_std_unique_ptr.makeRefKlassUniquePtr(null);
if (k1 != null)
throw new RuntimeException("not null");
}
}

View File

@ -219,3 +219,9 @@ k1 = cpp11_std_unique_ptr.makeRVRKlassUniquePtr("first");
if (k1.getLabel() !== "first")
throw new Error("wrong object label");
k1 = cpp11_std_unique_ptr.makeRVRKlassUniquePtr(null);
// unique_ptr as output (lvalue ref)
k1 = cpp11_std_unique_ptr.makeRefKlassUniquePtr("lvalueref");
if (k1.getLabel() !== "lvalueref")
throw new Error("wrong object label");
k1 = cpp11_std_unique_ptr.makeRefKlassUniquePtr(null);

View File

@ -187,3 +187,10 @@ if not (k1:getLabel() == "first") then
error("wrong object label")
end
assert(cpp11_std_unique_ptr.makeRVRKlassUniquePtr(nil) == nil)
-- unique_ptr as output (lvalue ref)
k1 = cpp11_std_unique_ptr.makeRefKlassUniquePtr("lvalueref")
if not (k1:getLabel() == "lvalueref") then
error("wrong object label")
end
assert(cpp11_std_unique_ptr.makeRefKlassUniquePtr(nil) == nil)

View File

@ -183,4 +183,11 @@
(unless (null? (makeRVRKlassUniquePtr '()))
(error "null failure"))
; unique_ptr as output (lvalue ref)
(define k1 (makeRefKlassUniquePtr "lvalueref"))
(unless (string=? (Klass-getLabel k1) "lvalueref")
(error "wrong object label" ))
(unless (null? (makeRefKlassUniquePtr '()))
(error "null failure"))
(exit 0)

View File

@ -236,3 +236,11 @@ if (!strcmp(k1.getLabel(), "first"))
endif
k1 = makeRVRKlassUniquePtr([]);
assert(isequal([], k1))
# unique_ptr as output (lvalue ref)
k1 = makeRefKlassUniquePtr("lvalueref");
if (!strcmp(k1.getLabel(), "lvalueref"))
error("wrong object label");
endif
k1 = makeRefKlassUniquePtr([]);
assert(isequal([], k1))

View File

@ -1,6 +1,6 @@
use strict;
use warnings;
use Test::More tests => 60;
use Test::More tests => 62;
BEGIN { use_ok('cpp11_std_unique_ptr') }
require_ok('cpp11_std_unique_ptr');
@ -166,3 +166,8 @@ is(cpp11_std_unique_ptr::makeNullUniquePtr(), undef);
my $k1 = cpp11_std_unique_ptr::makeRVRKlassUniquePtr("first");
is($k1->getLabel, "first", "proper label");
is(cpp11_std_unique_ptr::makeRVRKlassUniquePtr(undef), undef);
# unique_ptr as output (lvalue ref)
my $k1 = cpp11_std_unique_ptr::makeRefKlassUniquePtr("lvalueref");
is($k1->getLabel, "lvalueref", "proper label");
is(cpp11_std_unique_ptr::makeRefKlassUniquePtr(undef), undef);

View File

@ -184,7 +184,11 @@ check::equal(makeNullUniquePtr(), NULL);
# unique_ptr as output (rvalue ref)
$k1 = makeRVRKlassUniquePtr("first");
check::equal($k1->getLabel(), "first", "proper label");
$k1 = makeRVRKlassUniquePtr(NULL);
check::equal(makeRVRKlassUniquePtr(NULL), NULL);
# unique_ptr as output (lvalue ref)
$k1 = makeRefKlassUniquePtr("lvalueref");
check::equal($k1->getLabel(), "lvalueref", "proper label");
check::equal(makeRefKlassUniquePtr(NULL), NULL);
check::done();

View File

@ -192,3 +192,10 @@ if k1.getLabel() != "first":
raise "wrong object label"
if (makeRVRKlassUniquePtr(None) != None):
raise RuntimeError("null failure")
# unique_ptr as output (lvalue ref)
k1 = makeRefKlassUniquePtr("lvalueref")
if k1.getLabel() != "lvalueref":
raise "wrong object label"
if (makeRVRKlassUniquePtr(None) != None):
raise RuntimeError("null failure")

View File

@ -259,3 +259,8 @@ swig_assert_equal_simple(Cpp11_std_unique_ptr::makeNullUniquePtr(), nil)
k1 = Cpp11_std_unique_ptr::makeRVRKlassUniquePtr("first")
swig_assert_equal_simple(k1.getLabel(), "first")
swig_assert_equal_simple(Cpp11_std_unique_ptr::makeRVRKlassUniquePtr(nil), nil)
# unique_ptr as output (lvalue ref)
k1 = Cpp11_std_unique_ptr::makeRefKlassUniquePtr("lvalueref")
swig_assert_equal_simple(k1.getLabel(), "lvalueref")
swig_assert_equal_simple(Cpp11_std_unique_ptr::makeRefKlassUniquePtr(nil), nil)

View File

@ -263,3 +263,12 @@ if {[$k1 getLabel] != "first"} {
if {[makeRVRKlassUniquePtr "NULL"] != "NULL"} {
error "null failure"
}
# unique_ptr as output (lvalue ref)
set k1 [makeRefKlassUniquePtr "lvalueref"]
if {[$k1 getLabel] != "lvalueref"} {
error "wrong object label"
}
if {[makeRefKlassUniquePtr "NULL"] != "NULL"} {
error "null failure"
}

View File

@ -9,9 +9,9 @@
* ----------------------------------------------------------------------------- */
%define %unique_ptr(TYPE)
%typemap (ctype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > && "void *"
%typemap (imtype, out="System.IntPtr") std::unique_ptr< TYPE >, std::unique_ptr< TYPE > && "global::System.Runtime.InteropServices.HandleRef"
%typemap (cstype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > && "$typemap(cstype, TYPE)"
%typemap (ctype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && "void *"
%typemap (imtype, out="System.IntPtr") std::unique_ptr< TYPE >, std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && "global::System.Runtime.InteropServices.HandleRef"
%typemap (cstype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && "$typemap(cstype, TYPE)"
%typemap(in) std::unique_ptr< TYPE >
%{ $1.reset((TYPE *)$input); %}
@ -24,7 +24,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
$result = (void *)$1.release();
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
$result = (void *)$1->get();
%}
@ -34,7 +34,7 @@
$typemap(cstype, TYPE) ret = (cPtr == System.IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, true);$excode
return ret;
}
%typemap(csout, excode=SWIGEXCODE) std::unique_ptr< TYPE > && {
%typemap(csout, excode=SWIGEXCODE) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && {
System.IntPtr cPtr = $imcall;
$typemap(cstype, TYPE) ret = (cPtr == System.IntPtr.Zero) ? null : new $typemap(cstype, TYPE)(cPtr, false);$excode
return ret;

View File

@ -9,9 +9,9 @@
* ----------------------------------------------------------------------------- */
%define %unique_ptr(TYPE)
%typemap (ctype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > && "void *"
%typemap (imtype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > && "void*"
%typemap (dtype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > && "$typemap(dtype, TYPE)"
%typemap (ctype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && "void *"
%typemap (imtype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && "void*"
%typemap (dtype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && "$typemap(dtype, TYPE)"
%typemap(in) std::unique_ptr< TYPE >
%{ $1.reset((TYPE *)$input); %}
@ -26,7 +26,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
$result = (void *)$1.release();
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
$result = (void *)$1->get();
%}
@ -40,7 +40,7 @@
}
%typemap(dout, excode=SWIGEXCODE,
nativepointer="{\n auto ret = cast($dtype)$imcall;$excode\n return ret;\n}"
) std::unique_ptr< TYPE > && {
) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && {
void* cPtr = $imcall;
$typemap(dtype, TYPE) ret = (cPtr is null) ? null : new $typemap(dtype, TYPE)(cPtr, false);$excode
return ret;

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
%set_output(SWIG_NewPointerObj($1->get(), $descriptor(TYPE *), $owner));
%}

View File

@ -10,9 +10,9 @@
%define %unique_ptr(TYPE)
%typemap (jni) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > && "jlong"
%typemap (jtype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > && "long"
%typemap (jstype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > && "$typemap(jstype, TYPE)"
%typemap (jni) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && "jlong"
%typemap (jtype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && "long"
%typemap (jstype) std::unique_ptr< TYPE >, std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && "$typemap(jstype, TYPE)"
%typemap(in) std::unique_ptr< TYPE > (TYPE *unique_temp)
%{ unique_temp = *(TYPE **)&$input;
@ -28,7 +28,7 @@
*(TYPE **) &lpp = $1.release();
$result = lpp;
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
jlong lpp = 0;
*(TYPE **) &lpp = $1->get();
$result = lpp;
@ -39,7 +39,7 @@
long cPtr = $jnicall;
return (cPtr == 0) ? null : new $typemap(jstype, TYPE)(cPtr, true);
}
%typemap(javaout) std::unique_ptr< TYPE > && {
%typemap(javaout) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && {
long cPtr = $jnicall;
return (cPtr == 0) ? null : new $typemap(jstype, TYPE)(cPtr, false);
}

View File

@ -50,7 +50,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
%set_output(SWIG_NewPointerObj($1->get(), $descriptor(TYPE *), $owner | %newpointer_flags));
%}

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
%set_output(SWIG_NewPointerObj($1->get(), $descriptor(TYPE *), $owner | %newpointer_flags));
%}

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
%set_output(SWIG_NewPointerObj($1->get(), $descriptor(TYPE *), $owner | %newpointer_flags));
%}

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
SWIG_NewPointerObj(L, $1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN); SWIG_arg++;
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
SWIG_NewPointerObj(L, $1->get(), $descriptor(TYPE *), $owner); SWIG_arg++;
%}

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
%set_output(SWIG_NewPointerObj($1->get(), $descriptor(TYPE *), $owner));
%}

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
%set_output(SWIG_NewPointerObj($1->get(), $descriptor(TYPE *), $owner | %newpointer_flags));
%}

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
%set_output(SWIG_NewPointerObj($1->get(), $descriptor(TYPE *), $owner | %newpointer_flags));
%}

View File

@ -41,7 +41,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
SWIG_SetPointerZval($result, (void *)$1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN);
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
SWIG_SetPointerZval($result, (void *)$1->get(), $descriptor(TYPE *), $owner);
%}

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
%set_output(SWIG_NewPointerObj($1->get(), $descriptor(TYPE *), $owner | %newpointer_flags));
%}

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
%set_output(SWIG_NewPointerObj($1->get(), $descriptor(TYPE *), $owner | %newpointer_flags));
%}

View File

@ -37,7 +37,7 @@
%typemap (out) std::unique_ptr< TYPE > %{
Tcl_SetObjResult(interp, SWIG_NewInstanceObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN));
%}
%typemap (out) std::unique_ptr< TYPE > && %{
%typemap (out) std::unique_ptr< TYPE > &, std::unique_ptr< TYPE > && %{
Tcl_SetObjResult(interp, SWIG_NewInstanceObj($1->get(), $descriptor(TYPE *), $owner));
%}