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

@ -597,6 +597,7 @@ CPP11_TEST_CASES += \
cpp11_initializer_list \
cpp11_initializer_list_extend \
cpp11_lambda_functions \
cpp11_move_only \
cpp11_move_only_valuewrapper \
cpp11_noexcept \
cpp11_null_pointer_constant \

View file

@ -0,0 +1,52 @@
%module cpp11_move_only
#if defined(SWIGD)
%rename(trace) debug;
#endif
%include "cpp11_move_only_helper.i"
%ignore MoveOnly::operator=;
//%valuewrapper MoveOnly; // SWIG sets %valuewrapper by default for move-only types
%inline %{
#include <iostream>
using namespace std;
bool debug = false;
struct MoveOnly {
MoveOnly(int i = 0) { if (debug) cout << "MoveOnly(" << i << ")" << " " << this << endl; Counter::normal_constructor++; }
MoveOnly(const MoveOnly &other) = delete;
MoveOnly & operator=(const MoveOnly &other) = delete;
MoveOnly(MoveOnly &&other) noexcept { if (debug) cout << "MoveOnly(MoveOnly &&)" << " " << this << endl; Counter::move_constructor++; }
MoveOnly & operator=(MoveOnly &&other) noexcept { if (debug) cout << "operator=(MoveOnly &&)" << " " << this << endl; Counter::move_assignment++; return *this; }
~MoveOnly() { if (debug) cout << "~MoveOnly()" << " " << this << endl; Counter::destructor++; }
static MoveOnly create() { return MoveOnly(111); }
// static const MoveOnly createConst() { return MoveOnly(111); } // not supported by default
};
%}
%ignore MovableCopyable::operator=;
%ignore MovableCopyable::MovableCopyable(MovableCopyable &&);
// %valuewrapper MovableCopyable; // SWIG does not use valuewrapper by default for copyable types with a default constructor
%inline %{
// Movable and Copyable
struct MovableCopyable {
MovableCopyable(int i = 0) { if (debug) cout << "MovableCopyable(" << i << ")" << " " << this << endl; Counter::normal_constructor++; }
MovableCopyable(const MovableCopyable &other) { if (debug) cout << "MovableCopyable(const MovableCopyable &)" << " " << this << " " << &other << endl; Counter::copy_constructor++;}
MovableCopyable & operator=(const MovableCopyable &other) { if (debug) cout << "operator=(const MovableCopyable &)" << " " << this << " " << &other << endl; Counter::copy_assignment++; return *this; }
MovableCopyable(MovableCopyable &&other) noexcept { if (debug) cout << "MovableCopyable(MovableCopyable &&)" << " " << this << endl; Counter::move_constructor++; }
MovableCopyable & operator=(MovableCopyable &&other) noexcept { if (debug) cout << "operator=(MovableCopyable &&)" << " " << this << endl; Counter::move_assignment++; return *this; }
~MovableCopyable() { if (debug) cout << "~MovableCopyable()" << " " << this << endl; Counter::destructor++; }
static MovableCopyable create() { return MovableCopyable(111); }
static const MovableCopyable createConst() { return MovableCopyable(111); }
};
%}

View file

@ -0,0 +1,71 @@
// Helper interface for cpp11_move_only.i and others
%include <std_string.i>
%catches(std::string) Counter::check_counts;
%inline %{
#include <sstream>
using namespace std;
struct Counter {
static int normal_constructor;
static int copy_constructor;
static int copy_assignment;
static int move_constructor;
static int move_assignment;
static int destructor;
static void reset_counts() {
normal_constructor = 0;
copy_constructor = 0;
copy_assignment = 0;
move_constructor = 0;
move_assignment = 0;
destructor = 0;
}
// Check against expected counts of constructor, assignment operators etc.
// Not observed during development, but compiler optimisation could change the expected values.
// Throws exception if not correct (use %catches to catch them)
static void check_counts(
int normal_constructor,
int copy_constructor,
int copy_assignment,
int move_constructor,
int move_assignment,
int destructor) {
bool match = (
normal_constructor == Counter::normal_constructor &&
copy_constructor == Counter::copy_constructor &&
copy_assignment == Counter::copy_assignment &&
move_constructor == Counter::move_constructor &&
move_assignment == Counter::move_assignment &&
destructor == Counter::destructor);
if (!match) {
std::stringstream ss;
ss << "check_counts failed" << std::endl <<
Counter::normal_constructor << " " <<
Counter::copy_constructor << " " <<
Counter::copy_assignment << " " <<
Counter::move_constructor << " " <<
Counter::move_assignment << " " <<
Counter::destructor << " " <<
" (actual)" << std::endl <<
normal_constructor << " " <<
copy_constructor << " " <<
copy_assignment << " " <<
move_constructor << " " <<
move_assignment << " " <<
destructor << " " <<
" (expected)" << std::endl;
throw ss.str();
}
}
};
int Counter::normal_constructor = 0;
int Counter::copy_constructor = 0;
int Counter::copy_assignment = 0;
int Counter::move_constructor = 0;
int Counter::move_assignment = 0;
int Counter::destructor = 0;
%}

View file

@ -37,82 +37,28 @@ namespace std {
#endif
%}
%include <std_string.i>
#if defined(SWIGD)
%rename(trace) debug;
#endif
%include "cpp11_move_only_helper.i"
%valuewrapper XXX;
%ignore XXX::operator=;
%catches(std::string) XXX::check_counts;
%inline %{
bool debug = false;
struct XXX {
XXX(int i = 0) { if (debug) cout << "XXX(" << i << ")" << " " << this << endl; normal_constructor_count++; }
XXX(const XXX &other) { if (debug) cout << "XXX(const XXX &)" << " " << this << " " << &other << endl; copy_constructor_count++;}
XXX & operator=(const XXX &other) { if (debug) cout << "operator=(const XXX &)" << " " << this << " " << &other << endl; copy_assignment_count++; return *this; }
XXX(int i = 0) { if (debug) cout << "XXX(" << i << ")" << " " << this << endl; Counter::normal_constructor++; }
XXX(const XXX &other) { if (debug) cout << "XXX(const XXX &)" << " " << this << " " << &other << endl; Counter::copy_constructor++;}
XXX & operator=(const XXX &other) { if (debug) cout << "operator=(const XXX &)" << " " << this << " " << &other << endl; Counter::copy_assignment++; return *this; }
#if defined(__cplusplus) && __cplusplus >= 201103L
XXX(XXX &&other) noexcept { if (debug) cout << "XXX(XXX &&)" << " " << this << endl; move_constructor_count++; }
XXX & operator=(XXX &&other) noexcept { if (debug) cout << "operator=(XXX &&)" << " " << this << endl; move_assignment_count++; return *this; }
XXX(XXX &&other) noexcept { if (debug) cout << "XXX(XXX &&)" << " " << this << endl; Counter::move_constructor++; }
XXX & operator=(XXX &&other) noexcept { if (debug) cout << "operator=(XXX &&)" << " " << this << endl; Counter::move_assignment++; return *this; }
#endif
~XXX() { if (debug) cout << "~XXX()" << " " << this << endl; destructor_count++; }
static int normal_constructor_count;
static int copy_constructor_count;
static int copy_assignment_count;
static int move_constructor_count;
static int move_assignment_count;
static int destructor_count;
static void reset_counts() {
XXX::normal_constructor_count = 0;
XXX::copy_constructor_count = 0;
XXX::copy_assignment_count = 0;
XXX::move_constructor_count = 0;
XXX::move_assignment_count = 0;
XXX::destructor_count = 0;
}
// Check against expected counts of constructor, assignment operators etc.
// Not observed during development, but compiler optimisation could change the expected values.
// Throws exception if not correct (use %catches to catch them)
static void check_counts(
int normal_constructor_count,
int copy_constructor_count,
int copy_assignment_count,
int move_constructor_count,
int move_assignment_count,
int destructor_count) {
bool match = (
normal_constructor_count == XXX::normal_constructor_count &&
copy_constructor_count == XXX::copy_constructor_count &&
copy_assignment_count == XXX::copy_assignment_count &&
move_constructor_count == XXX::move_constructor_count &&
move_assignment_count == XXX::move_assignment_count &&
destructor_count == XXX::destructor_count);
if (!match) {
std::stringstream ss;
ss << "check_counts failed" << std::endl <<
XXX::normal_constructor_count << " " <<
XXX::copy_constructor_count << " " <<
XXX::copy_assignment_count << " " <<
XXX::move_constructor_count << " " <<
XXX::move_assignment_count << " " <<
XXX::destructor_count << " " <<
" (actual)" << std::endl <<
normal_constructor_count << " " <<
copy_constructor_count << " " <<
copy_assignment_count << " " <<
move_constructor_count << " " <<
move_assignment_count << " " <<
destructor_count << " " <<
" (expected)" << std::endl;
throw ss.str();
}
}
~XXX() { if (debug) cout << "~XXX()" << " " << this << endl; Counter::destructor++; }
};
int XXX::normal_constructor_count = 0;
int XXX::copy_constructor_count = 0;
int XXX::copy_assignment_count = 0;
int XXX::move_constructor_count = 0;
int XXX::move_assignment_count = 0;
int XXX::destructor_count = 0;
bool has_cplusplus11() {
#if __cplusplus >= 201103L
return true;
@ -122,20 +68,8 @@ bool has_cplusplus11() {
}
%}
#if defined(SWIGCSHARP)
%typemap(out) std::unique_ptr<XXX> %{
if (debug) cout << "out start" << endl;
#if __cplusplus >= 201103L
$result = new std::unique_ptr<XXX>(std::move(static_cast<std::unique_ptr<XXX>&>($1)));
#else
$result = new std::unique_ptr<XXX>((const std::unique_ptr<XXX> &)$1);
#endif
if (debug) cout << "out done" << endl;
%}
std::unique_ptr<XXX> makeUniqueXXX();
void cleanup(std::unique_ptr<XXX>* p);
#endif
%{
std::unique_ptr<XXX> makeUniqueXXX() {
@ -145,6 +79,7 @@ std::unique_ptr<XXX> makeUniqueXXX() {
void cleanup(std::unique_ptr<XXX>* p) {
delete p;
}
typedef XXX UUU;
%}
%inline %{
@ -152,6 +87,16 @@ XXX createXXX() {
if (debug) cout << "createXXX()" << endl;
return XXX(111);
}
XXX createXXX2() {
if (debug) cout << "createXXX2()" << endl;
return XXX(222);
}
UUU createUnknownType() {
if (debug) cout << "createXXX2()" << endl;
return XXX(222);
}
struct YYY {};
void inputByValue(UUU uuu, XXX xxx, YYY yyy) {}
%}
@ -165,32 +110,32 @@ XXX createXXX() {
%inline %{
// 'unit tests' for SwigValueWrapper
void test1() {
XXX::reset_counts();
Counter::reset_counts();
{
SwigValueWrapper<XXX> x;
x = XXX();
}
#if __cplusplus >= 201103L
XXX::check_counts(1, 0, 0, 1, 0, 2); // was same as < c++11 counts below before move assignment operator added to SwigValueWrapper
Counter::check_counts(1, 0, 0, 1, 0, 2); // was same as < c++11 counts below before move assignment operator added to SwigValueWrapper
#else
XXX::check_counts(1, 1, 0, 0, 0, 2);
Counter::check_counts(1, 1, 0, 0, 0, 2);
#endif
}
void test2() {
XXX::reset_counts();
Counter::reset_counts();
{
SwigValueWrapper<XXX> x;
x = XXX();
x = XXX();
}
#if __cplusplus >= 201103L
XXX::check_counts(2, 0, 0, 2, 0, 4);
Counter::check_counts(2, 0, 0, 2, 0, 4);
#else
XXX::check_counts(2, 2, 0, 0, 0, 4);
Counter::check_counts(2, 2, 0, 0, 0, 4);
#endif
}
void test3() {
XXX::reset_counts();
Counter::reset_counts();
{
SwigValueWrapper<XXX> x;
XXX a(999);
@ -199,41 +144,41 @@ void test3() {
#endif
}
#if __cplusplus >= 201103L
XXX::check_counts(1, 0, 0, 1, 0, 2);
Counter::check_counts(1, 0, 0, 1, 0, 2);
#endif
}
void test4() {
XXX::reset_counts();
Counter::reset_counts();
{
SwigValueWrapper<std::unique_ptr<XXX> > x;
x = std::unique_ptr<XXX>(new XXX(444));
}
XXX::check_counts(1, 0, 0, 0, 0, 1);
Counter::check_counts(1, 0, 0, 0, 0, 1);
}
void test5() {
#if __cplusplus >= 201103L
XXX::reset_counts();
Counter::reset_counts();
{
SwigValueWrapper<std::unique_ptr<XXX> > x;
x = std::unique_ptr<XXX>(new XXX(550));
std::unique_ptr<XXX> x2(new XXX(555));
x = std::move(x2);
}
XXX::check_counts(2, 0, 0, 0, 0, 2);
Counter::check_counts(2, 0, 0, 0, 0, 2);
#endif
}
void test6() {
#if __cplusplus >= 201103L
XXX::reset_counts();
Counter::reset_counts();
{
// emulates how std::unique_ptr typemaps could be wrapped with SwigValueWrapper
void *ptr = 0;
SwigValueWrapper<std::unique_ptr<XXX> > x; // SWIG generated if std::unique_ptr<> definition not parsed
x = makeUniqueXXX(); // SWIG generated code wrapping function returning std::unique_ptr
ptr = new std::unique_ptr<XXX>(std::move((std::unique_ptr<XXX>&)x)); // 'out' typemap (move std::unique_ptr from stack to the heap), note non-const cast std::unique_tr<XXX>&
ptr = new std::unique_ptr<XXX>(x); // 'out' typemap (move std::unique_ptr from stack to the heap)
delete (std::unique_ptr<XXX> *)ptr; // Final cleanup (user needs to call this)
}
XXX::check_counts(1, 0, 0, 0, 0, 1);
Counter::check_counts(1, 0, 0, 0, 0, 1);
#endif
}
%}

View file

@ -0,0 +1,23 @@
using System;
using cpp11_move_onlyNamespace;
public class cpp11_move_only_runme {
public static void Main() {
Counter.reset_counts();
using (MoveOnly mo = MoveOnly.create()) {
}
Counter.check_counts(1, 0, 0, 2, 0, 3);
Counter.reset_counts();
using (MovableCopyable mo = MovableCopyable.create()) {
}
Counter.check_counts(2, 1, 0, 0, 1, 3);
// Move semantics not used
Counter.reset_counts();
using (MovableCopyable mo = MovableCopyable.createConst()) {
}
Counter.check_counts(2, 1, 1, 0, 0, 3);
}
}

View file

@ -4,11 +4,18 @@ using cpp11_move_only_valuewrapperNamespace;
public class cpp11_move_only_valuewrapper_runme {
public static void Main() {
XXX.reset_counts();
Counter.reset_counts();
using (XXX xxx = cpp11_move_only_valuewrapper.createXXX()) {
}
if (cpp11_move_only_valuewrapper.has_cplusplus11())
XXX.check_counts(1, 1, 0, 1, 0, 3); // Was (1, 2, 0, 0, 0, 3) before SwigValueWrapper::operator=(T &&) was added.
// 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();
using (XXX xxx = cpp11_move_only_valuewrapper.createXXX2()) {
}
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();
@ -16,10 +23,11 @@ public class cpp11_move_only_valuewrapper_runme {
cpp11_move_only_valuewrapper.test5();
cpp11_move_only_valuewrapper.test6();
// C# only test (SwigValueWrapper and custom typemaps for std::unique_ptr)
// 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);
}
}

View file

@ -3,10 +3,14 @@ using typemap_out_optimalNamespace;
public class typemap_out_optimal_runme {
public static XX x = null;
public static void Main() {
XX.debug = false;
x = XX.create();
if (XX.debug)
Console.WriteLine("calling create()");
using (XX x = XX.create()) { }
if (XX.debug)
Console.WriteLine("calling createConst()");
using (XX x = XX.createConst()) { }
}
}

View file

@ -6,4 +6,5 @@ void main() {
XX x;
XX.trace = false;
x = XX.create();
x = XX.createConst();
}

View file

@ -6,4 +6,5 @@ void main() {
XX x;
XX.trace = false;
x = XX.create();
x = XX.createConst();
}

View file

@ -5,4 +5,5 @@ import . "swigtests/typemap_out_optimal"
func main() {
SetXXDebug(false)
_ = XXCreate()
_ = XXCreateConst()
}

View 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);
}
}
}

View file

@ -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);
}
}

View file

@ -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();
}
}
}

View file

@ -0,0 +1,17 @@
from cpp11_move_only import *
Counter.reset_counts()
mo = MoveOnly.create()
del mo
Counter.check_counts(1, 0, 0, 2, 0, 3)
Counter.reset_counts()
mo = MovableCopyable.create()
del mo
Counter.check_counts(2, 1, 0, 0, 1, 3)
# Move semantics not used
Counter.reset_counts()
mo = MovableCopyable.createConst()
del mo
Counter.check_counts(2, 1, 1, 0, 0, 3)

View file

@ -2,3 +2,6 @@ from typemap_out_optimal import *
cvar.XX_debug = False
x = XX.create()
del x
x = XX.createConst()
del x

View file

@ -4,15 +4,15 @@
// Just the following languages tested
#if defined (SWIGCSHARP) || defined (SWIGD)
%typemap(out, optimal="1") SWIGTYPE %{
$result = new $1_ltype((const $1_ltype &)$1);
$result = new $1_ltype($1);
%}
#elif defined (SWIGJAVA)
%typemap(out, optimal="1") SWIGTYPE %{
*($&1_ltype*)&$result = new $1_ltype((const $1_ltype &)$1);
*($&1_ltype*)&$result = new $1_ltype($1);
%}
#elif defined (SWIGUTL)
%typemap(out,noblock="1", optimal="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));
}
#endif
@ -32,9 +32,14 @@ struct XX {
XX(const XX &other) { if (debug) cout << "XX(const XX &)" << endl; }
XX& operator =(const XX &other) { if (debug) cout << "operator=(const XX &)" << endl; return *this; }
~XX() { if (debug) cout << "~XX()" << endl; }
static XX create() {
// Note: best observed RVO for C#, Java and Python with g++-6 to g++-10 (just one constructor and one destructor call)
static XX create() {
return XX(123);
}
static const XX createConst() {
return XX(456);
}
static bool debug;
};
bool XX::debug = true;