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

@ -15,6 +15,9 @@
<li><a href="#CPlusPlus11_core_language_changes">Core language changes</a>
<ul>
<li><a href="#CPlusPlus11_rvalue_reference_and_move_semantics">Rvalue reference and move semantics</a>
<ul>
<li><a href="#CPlusPlus11_move_only">Movable and move-only types</a>
</ul>
<li><a href="#CPlusPlus11_generalized_constant_expressions">Generalized constant expressions</a>
<li><a href="#CPlusPlus11_extern_template">Extern template</a>
<li><a href="#CPlusPlus11_initializer_lists">Initializer lists</a>
@ -123,6 +126,82 @@ example.i:18: Warning 503: Can't wrap 'operator =' unless renamed to a valid ide
</pre>
</div>
<H4><a name="CPlusPlus11_move_only">7.2.1.1 Movable and move-only types</a></H4>
<p>
SWIG has traditionally relied on wrapped C++ types to be copy constructible or copy assignable, either via an explicit or implicit copy constructor and copy assignment operator.
Prior to C++11, a function could not return nor take a type by value that was not copyable.
In C++11 this is no longer the case. A type can also be movable if it has has a move constructor and a move assignment operator.
A move-only type is movable but not copyable; it has both the copy constructor and copy assignment operator deleted.
Movable types can appear in function signatures for passing 'by value' and in C++11 the object can then be moved rather than copied.
</p>
<p>
SWIG has been enhanced with support for both copyable and/or movable types but this is currently just for function return values.
</p>
<p>
The support for function return values is generically implemented in the "out" <tt>SWIGTYPE</tt> typemap which supports any type, including copyable, movable and move-only types.
The typemap code is very simple and written so that the compiler will call the move constructor if possible,
otherwise the copy constructor:
</p>
<div class="code"><pre>
%typemap(out) SWIGTYPE %{
$result = new $1_ltype($1);
%}
</pre></div>
<p>
The above typemap is for C# and when used to wrap a move-only type such as:
</p>
<div class="code"><pre>
struct MoveOnly {
int val;
MoveOnly(): val(0) {}
MoveOnly(const MoveOnly &amp;) = delete;
MoveOnly(MoveOnly &amp;&amp;) = default;
MoveOnly &amp; operator=(const MoveOnly &amp;) = delete;
MoveOnly &amp; operator=(MoveOnly &amp;&amp;) = default;
static MoveOnly create() { return MoveOnly(); }
};
</pre></div>
<p>
will generate wrapper code for the <tt>create</tt> factory method:
</p>
<div class="code"><pre>
SWIGEXPORT void * SWIGSTDCALL CSharp_MoveOnly_create() {
void * jresult ;
SwigValueWrapper&lt; MoveOnly &gt; result;
result = MoveOnly::create();
jresult = new MoveOnly(result);
return jresult;
}
</pre></div>
<p>
<tt>SwigValueWrapper</tt> is covered in <a href="SWIGPlus.html#SWIGPlus_nn19">Pass and return by value</a>.
Note that the generated code could be optimised further using the <a href="Typemaps.html#Typemaps_optimal">"optimal" attribute</a> in the "out" typemap.
</p>
<p>
There is currently only partial support for move-only types as
support for move-only types used as a parameter in a function, that are passed 'by value', is not yet available.
</p>
<p>
<b>Compatibility note:</b>
SWIG-4.1.0 introduced support for taking advantage of types with move semantics and wrapping functions that return movable or move-only types 'by value'.
</p>
<H3><a name="CPlusPlus11_generalized_constant_expressions">7.2.2 Generalized constant expressions</a></H3>

View file

@ -297,6 +297,9 @@
<li><a href="CPlusPlus11.html#CPlusPlus11_core_language_changes">Core language changes</a>
<ul>
<li><a href="CPlusPlus11.html#CPlusPlus11_rvalue_reference_and_move_semantics">Rvalue reference and move semantics</a>
<ul>
<li><a href="CPlusPlus11.html#CPlusPlus11_move_only">Movable and move-only types</a>
</ul>
<li><a href="CPlusPlus11.html#CPlusPlus11_generalized_constant_expressions">Generalized constant expressions</a>
<li><a href="CPlusPlus11.html#CPlusPlus11_extern_template">Extern template</a>
<li><a href="CPlusPlus11.html#CPlusPlus11_initializer_lists">Initializer lists</a>

View file

@ -584,7 +584,7 @@ automatically generate a wrapper for one.
<li>
If a C++ class does not declare an explicit copy constructor, SWIG will
automatically generate a wrapper for one if the <tt>%copyctor</tt> is used.
automatically generate a wrapper for one if <tt>%copyctor</tt> is used.
</li>
<li>
@ -1352,16 +1352,19 @@ following:
<div class="code">
<pre>
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x, y);
Vector x;
Vector y;
Vector r;
x = *a;
y = *b;
r = cross_product(x, y);
return new Vector(r);
}</pre>
</div>
<p>
In order for the wrapper code to compile, <tt>Vector</tt> must define a copy constructor and a
default constructor.
In order for the wrapper code to compile, <tt>Vector</tt> must define a default constructor, copy assignment operator (and/or a move assignment operator for C++11 and later).
The <a href="CPlusPlus11.html#CPlusPlus11_move_only">Movable and move-only types</a> section should be read regarding C++11 move semantics and return by value.
</p>
<p>
@ -1374,9 +1377,12 @@ called the "Fulton Transform". This produces a wrapper that looks like this:
<div class="code">
<pre>
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper&lt;Vector&gt; x = *a;
SwigValueWrapper&lt;Vector&gt; y = *b;
SwigValueWrapper&lt;Vector&gt; r = cross_product(x, y);
SwigValueWrapper&lt;Vector&gt; x;
SwigValueWrapper&lt;Vector&gt; y;
SwigValueWrapper&lt;Vector&gt; r;
x = *a;
y = *b;
r = cross_product(x, y);
return new Vector(r);
}
</pre>

View file

@ -3364,7 +3364,7 @@ Consider running the following code through SWIG:
<div class="code">
<pre>
%typemap(out) SWIGTYPE %{
$result = new $1_ltype((const $1_ltype &amp;)$1);
$result = new $1_ltype($1);
%}
%inline %{
@ -3414,7 +3414,7 @@ If the typemap code is kept the same and just the "optimal" attribute specified
<div class="code">
<pre>
%typemap(out, optimal="1") SWIGTYPE %{
$result = new $1_ltype((const $1_ltype &amp;)$1);
$result = new $1_ltype($1);
%}
</pre>
</div>
@ -3441,7 +3441,7 @@ SWIGEXPORT void * SWIGSTDCALL CSharp_XX_create() {
void * jresult ;
XX result;
result = XX::create();
jresult = new XX((const XX &amp;)result);
jresult = new XX(result);
return jresult;
}
@ -3456,7 +3456,7 @@ With the "optimal" attribute, the code is:
<pre>
SWIGEXPORT void * SWIGSTDCALL CSharp_XX_create() {
void * jresult ;
jresult = new XX((const XX &amp;)XX::create());
jresult = new XX(XX::create());
return jresult;
}
</pre>
@ -3513,7 +3513,7 @@ It should be clear that the above code cannot be used as the argument to the cop
</p>
<p>
Secondly, if the typemaps uses <tt>$1</tt> more than once, then multiple calls to the wrapped function
Secondly, if the typemap uses <tt>$1</tt> more than once, then multiple calls to the wrapped function
will be made. Obviously that is not very optimal.
In fact SWIG attempts to detect this and will issue a warning something like:
</p>