Movable and move-only types supported in "out" typemaps.

Enhance SWIGTYPE "out" typemaps to use std::move when copying
objects, thereby making use of move semantics when wrapping a function returning
by value if the returned type supports move semantics.

Wrapping functions that return move only types 'by value' now work out the box
without having to provide custom typemaps.

The implementation removed all casts in the "out" typemaps to allow the compiler to
appropriately choose calling a move constructor, where possible, otherwise a copy
constructor. The implementation alsoand required modifying SwigValueWrapper to
change a cast operator from:

  SwigValueWrapper::operator T&() const;

to

  #if __cplusplus >=201103L
    SwigValueWrapper::operator T&&() const;
  #else
    SwigValueWrapper::operator T&() const;
  #endif

This is not backwards compatible for C++11 and later when using the valuewrapper feature
if a cast is explicitly being made in user supplied "out" typemaps. Suggested change
in custom "out" typemaps for C++11 and later code:

1. Try remove the cast altogether to let the compiler use an appropriate implicit cast.
2. Change the cast, for example, from static_cast<X &> to static_cast<X &&>, using the
   __cplusplus macro if all versions of C++ need to be supported.

Issue #999
Closes #1044

More about the commit:
Added some missing "varout" typemaps for Ocaml which was falling back to
use "out" typemaps as they were missing.

Ruby std::set fix for SwigValueWrapper C++11 changes.
This commit is contained in:
William S Fulton 2022-06-22 12:52:06 +01:00
commit bf36bf7d8a
34 changed files with 508 additions and 138 deletions

View file

@ -90,7 +90,8 @@
enum SWIGTYPE, SWIGTYPE *,
SWIGTYPE[ANY], SWIGTYPE &, SWIGTYPE && "$result = $1;";
#ifdef __cplusplus
%typemap(out) SWIGTYPE "$result = new $1_type($1);";
%typemap(out) SWIGTYPE
%{ $result = new $1_ltype($1); %}
#else
%typemap(out) SWIGTYPE {
$result = ($&1_ltype) malloc(sizeof($1_type));

View file

@ -399,7 +399,7 @@ SWIGINTERN const char * SWIG_UnpackData(const char *c, void *ptr, size_t sz) {
%typemap(out) SWIGTYPE
#ifdef __cplusplus
%{ $result = new $1_ltype((const $1_ltype &)$1); %}
%{ $result = new $1_ltype($1); %}
#else
{
$&1_ltype $1ptr = ($&1_ltype) malloc(sizeof($1_ltype));

View file

@ -52,7 +52,7 @@
%typemap(out) SWIGTYPE
#ifdef __cplusplus
%{ $result = new $1_ltype((const $1_ltype &)$1); %}
%{ $result = new $1_ltype($1); %}
#else
{
$&1_ltype $1ptr = ($&1_ltype) malloc(sizeof($1_ltype));

View file

@ -130,7 +130,7 @@
#ifdef __cplusplus
{
$&1_ltype resultptr;
resultptr = new $1_ltype((const $1_ltype &) $1);
resultptr = new $1_ltype($1);
$result = SWIG_NewPointerObj (resultptr, $&1_descriptor, 1);
}
#else
@ -145,8 +145,7 @@
%typemap(varout) SWIGTYPE
#ifdef __cplusplus
{
$&1_ltype resultptr;
resultptr = new $1_ltype((const $1_ltype&) $1);
$&1_ltype resultptr = ($&1_ltype)&$1;
$result = SWIG_NewPointerObj (resultptr, $&1_descriptor, 0);
}
#else

View file

@ -665,7 +665,7 @@ Swig::LocalRefGuard $1_refguard(jenv, $input); }
%typemap(out) SWIGTYPE
#ifdef __cplusplus
%{ *($&1_ltype*)&$result = new $1_ltype((const $1_ltype &)$1); %}
%{ *($&1_ltype*)&$result = new $1_ltype($1); %}
#else
{
$&1_ltype $1ptr = ($&1_ltype) malloc(sizeof($1_ltype));

View file

@ -217,7 +217,7 @@ $1=($1_ltype)&temp;%}
#ifdef __cplusplus
%typemap(out) SWIGTYPE
{
$&1_ltype resultptr = new $1_ltype((const $1_ltype &) $1);
$&1_ltype resultptr = new $1_ltype($1);
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
}
#else

View file

@ -127,7 +127,7 @@
#ifdef __cplusplus
{
$&1_ltype resultptr;
resultptr = new $1_ltype(($1_ltype &) $1);
resultptr = new $1_ltype($1);
$result = SWIG_NewPointerObj (resultptr, $&1_descriptor, 1);
}
#else

View file

@ -93,10 +93,14 @@
$1 = *(($&1_ltype) caml_ptr_val($input,$&1_descriptor)) ;
}
%typemap(varout) SWIGTYPE {
$result = SWIG_Ocaml_ptr_to_val("create_$ntype_from_ptr", (void *)&$1, $&1_descriptor);
}
#ifdef __cplusplus
%typemap(out) SWIGTYPE {
$&1_ltype temp = new $ltype((const $1_ltype &) $1);
$&1_ltype temp = new $1_ltype($1);
$result = SWIG_Ocaml_ptr_to_val("create_$ntype_from_ptr", (void *)temp, $&1_descriptor);
}
@ -110,6 +114,10 @@
#endif
%typemap(varout) SWIGTYPE * {
$result = SWIG_Ocaml_ptr_to_val("create_$ntype_from_ptr", (void *)$1, $1_descriptor);
}
%typemap(directorin) SWIGTYPE {
$&ltype temp = new $ltype((const $ltype &)$1);
swig_result = SWIG_Ocaml_ptr_to_val("create_$ltype_from_ptr", (void *)temp, $&1_descriptor);

View file

@ -83,9 +83,18 @@ class wstring;
$result = caml_val_string_len($1.c_str(),$1.size());
}
%typemap(varout) string {
$result = caml_val_string_len($1.c_str(),$1.size());
}
%typemap(out) string * {
$result = caml_val_string_len((*$1).c_str(),(*$1).size());
}
%typemap(varout) string * {
$result = caml_val_string_len((*$1).c_str(),(*$1).size());
}
%typemap(typecheck) string, const string & = char *;
}

View file

@ -443,7 +443,7 @@
%typemap(out, phptype="SWIGTYPE") SWIGTYPE
{
#ifdef __cplusplus
$&1_ltype resultobj = new $1_ltype((const $1_ltype &) $1);
$&1_ltype resultobj = new $1_ltype($1);
#else
$&1_ltype resultobj = ($&1_ltype) malloc(sizeof($1_type));
memcpy(resultobj, &$1, sizeof($1_type));

View file

@ -180,17 +180,14 @@
// Redefine std::set iterator/reverse_iterator typemap
%typemap(out,noblock=1) iterator, reverse_iterator {
$result = SWIG_NewPointerObj(swig::make_set_nonconst_iterator(%static_cast($1,const $type &),
self),
swig::Iterator::descriptor(),SWIG_POINTER_OWN);
$result = SWIG_NewPointerObj((swig::make_set_nonconst_iterator<$type>($1, self)), swig::Iterator::descriptor(), SWIG_POINTER_OWN);
}
// Redefine std::set std::pair<iterator, bool> typemap
%typemap(out,noblock=1,fragment="RubyPairBoolOutputIterator")
std::pair<iterator, bool> {
$result = rb_ary_new2(2);
rb_ary_push($result, SWIG_NewPointerObj(swig::make_set_nonconst_iterator(%static_cast($1,$type &).first),
swig::Iterator::descriptor(),SWIG_POINTER_OWN));
rb_ary_push($result, SWIG_NewPointerObj((swig::make_set_nonconst_iterator($1.first)), swig::Iterator::descriptor(), SWIG_POINTER_OWN));
rb_ary_push($result, SWIG_From(bool)(%static_cast($1,const $type &).second));
}

View file

@ -677,10 +677,12 @@ template<typename T> class SwigValueWrapper {
public:
SwigValueWrapper() : pointer(0) { }
SwigValueWrapper& operator=(const T& t) { SwigSmartPointer tmp(new T(t)); pointer = tmp; return *this; }
#if __cplusplus >= 201103L
#if __cplusplus >=201103L
SwigValueWrapper& operator=(T&& t) { SwigSmartPointer tmp(new T(std::move(t))); pointer = tmp; return *this; }
#endif
operator T&&() const { return std::move(*pointer.ptr); }
#else
operator T&() const { return *pointer.ptr; }
#endif
T *operator&() { return pointer.ptr; }
};%}

View file

@ -146,9 +146,15 @@
}
/* Return by value */
#ifdef __cplusplus
%typemap(out, noblock=1) SWIGTYPE {
%set_output(SWIG_NewPointerObj(%new_copy($1, $ltype), $&descriptor, SWIG_POINTER_OWN | %newpointer_flags));
%set_output(SWIG_NewPointerObj((new $1_ltype($1)), $&descriptor, SWIG_POINTER_OWN | %newpointer_flags));
}
#else
%typemap(out, noblock=1) SWIGTYPE {
%set_output(SWIG_NewPointerObj(%new_copy($1, $1_ltype), $&descriptor, SWIG_POINTER_OWN | %newpointer_flags));
}
#endif
/* -----------------------------------------------------------------------------
* --- Variable input ---
@ -534,7 +540,7 @@
* ------------------------------------------------------------ */
%typemap(throws,noblock=1) SWIGTYPE {
%raise(SWIG_NewPointerObj(%new_copy($1, $ltype),$&descriptor,SWIG_POINTER_OWN), "$type", $&descriptor);
%raise(SWIG_NewPointerObj(%new_copy($1, $1_ltype),$&descriptor,SWIG_POINTER_OWN), "$type", $&descriptor);
}
%typemap(throws,noblock=1) SWIGTYPE * {
@ -703,9 +709,15 @@
/* INSTANCE typemap */
#ifdef __cplusplus
%typemap(out,noblock=1) SWIGTYPE INSTANCE {
%set_output(SWIG_NewInstanceObj((new $1_ltype($1)), $&1_descriptor, SWIG_POINTER_OWN | %newinstance_flags));
}
#else
%typemap(out,noblock=1) SWIGTYPE INSTANCE {
%set_output(SWIG_NewInstanceObj(%new_copy($1, $1_ltype), $&1_descriptor, SWIG_POINTER_OWN | %newinstance_flags));
}
#endif
%typemap(out,noblock=1) SWIGTYPE *INSTANCE, SWIGTYPE &INSTANCE, SWIGTYPE INSTANCE[] {
%set_output(SWIG_NewInstanceObj(%as_voidptr($1), $1_descriptor, $owner | %newinstance_flags));