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:
parent
6ccef6dae1
commit
bf36bf7d8a
34 changed files with 508 additions and 138 deletions
39
Examples/test-suite/java/cpp11_move_only_runme.java
Normal file
39
Examples/test-suite/java/cpp11_move_only_runme.java
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
|
||||
import cpp11_move_only.*;
|
||||
|
||||
public class cpp11_move_only_runme {
|
||||
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("cpp11_move_only");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
|
||||
{
|
||||
Counter.reset_counts();
|
||||
MoveOnly mo = MoveOnly.create();
|
||||
mo.delete();
|
||||
Counter.check_counts(1, 0, 0, 2, 0, 3);
|
||||
}
|
||||
|
||||
{
|
||||
Counter.reset_counts();
|
||||
MovableCopyable mo = MovableCopyable.create();
|
||||
mo.delete();
|
||||
Counter.check_counts(2, 1, 0, 0, 1, 3);
|
||||
}
|
||||
|
||||
// Move semantics not used
|
||||
{
|
||||
Counter.reset_counts();
|
||||
MovableCopyable mo = MovableCopyable.createConst();
|
||||
mo.delete();
|
||||
Counter.check_counts(2, 1, 1, 0, 0, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
import cpp11_move_only_valuewrapper.*;
|
||||
|
||||
public class cpp11_move_only_valuewrapper_runme {
|
||||
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("cpp11_move_only_valuewrapper");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
Counter.reset_counts();
|
||||
{
|
||||
XXX xxx = cpp11_move_only_valuewrapper.createXXX();
|
||||
xxx.delete();
|
||||
}
|
||||
if (cpp11_move_only_valuewrapper.has_cplusplus11())
|
||||
// Was (1, 2, 0, 0, 0, 3) before SwigValueWrapper::operator=(T &&) was added.
|
||||
// Was (1, 1, 0, 1, 0, 3) before SwigValueWrapper::operator T&&() was added with new "out" typemaps
|
||||
Counter.check_counts(1, 0, 0, 2, 0, 3);
|
||||
Counter.reset_counts();
|
||||
{
|
||||
XXX xxx = cpp11_move_only_valuewrapper.createXXX2();
|
||||
xxx.delete();
|
||||
}
|
||||
if (cpp11_move_only_valuewrapper.has_cplusplus11())
|
||||
Counter.check_counts(1, 0, 0, 2, 0, 3);
|
||||
cpp11_move_only_valuewrapper.test1();
|
||||
cpp11_move_only_valuewrapper.test2();
|
||||
cpp11_move_only_valuewrapper.test3();
|
||||
cpp11_move_only_valuewrapper.test4();
|
||||
cpp11_move_only_valuewrapper.test5();
|
||||
cpp11_move_only_valuewrapper.test6();
|
||||
|
||||
// Tests SwigValueWrapper, std::unique_ptr (SWIG not parsing a type that is move-only)
|
||||
Counter.reset_counts();
|
||||
SWIGTYPE_p_std__unique_ptrT_XXX_t ptr = cpp11_move_only_valuewrapper.makeUniqueXXX();
|
||||
cpp11_move_only_valuewrapper.cleanup(ptr);
|
||||
Counter.check_counts(1, 0, 0, 0, 0, 1);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,10 +12,16 @@ public class typemap_out_optimal_runme {
|
|||
}
|
||||
}
|
||||
|
||||
public static XX x = null;
|
||||
public static void main(String argv[]) {
|
||||
XX.setDebug(false);
|
||||
x = XX.create();
|
||||
{
|
||||
XX x = XX.create();
|
||||
x.delete();
|
||||
}
|
||||
{
|
||||
XX x = XX.createConst();
|
||||
x.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue