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

@ -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
}
%}