Provide SWIGTYPE MOVE typemaps in swigmove.i

For implementing full move semantics when passing parameters by value.
Based on SWIGTYPE && and std::unique_ptr typemaps which implement move
semantics.

Added for all languages, but untested for: Go, Ocaml, R, Scilab (and
unlikely to be fully functional for same reasons as for std::unique_ptr
support).

Issue #999
This commit is contained in:
William S Fulton 2022-09-16 08:36:25 +01:00
commit dad7c93ca0
41 changed files with 909 additions and 19 deletions

View file

@ -597,8 +597,9 @@ CPP11_TEST_CASES += \
cpp11_initializer_list \
cpp11_initializer_list_extend \
cpp11_lambda_functions \
cpp11_move_only \
cpp11_move_only_valuewrapper \
cpp11_move_only \
cpp11_move_typemaps \
cpp11_move_only_valuewrapper \
cpp11_noexcept \
cpp11_null_pointer_constant \
cpp11_raw_string_literals \

View file

@ -12,19 +12,23 @@ using namespace std;
bool trace = false;
struct MoveOnly {
MoveOnly(int i = 0) { if (trace) cout << "MoveOnly(" << i << ")" << " " << this << endl; Counter::normal_constructor++; }
int val;
MoveOnly(int i = 0) : val(i) { if (trace) cout << "MoveOnly(" << i << ")" << " " << this << endl; Counter::normal_constructor++; }
MoveOnly(const MoveOnly &other) = delete;
MoveOnly & operator=(const MoveOnly &other) = delete;
MoveOnly(MoveOnly &&other) noexcept { if (trace) cout << "MoveOnly(MoveOnly &&)" << " " << this << endl; Counter::move_constructor++; }
MoveOnly & operator=(MoveOnly &&other) noexcept { if (trace) cout << "operator=(MoveOnly &&)" << " " << this << endl; Counter::move_assignment++; return *this; }
MoveOnly(MoveOnly &&other) noexcept : val(std::move(other.val)) { if (trace) cout << "MoveOnly(MoveOnly &&)" << " " << this << endl; Counter::move_constructor++; }
MoveOnly & operator=(MoveOnly &&other) noexcept { if (trace) cout << "operator=(MoveOnly &&)" << " " << this << endl; Counter::move_assignment++; if (this != &other) { val = std::move(other.val); } return *this; }
~MoveOnly() { if (trace) cout << "~MoveOnly()" << " " << this << endl; Counter::destructor++; }
static MoveOnly create() { return MoveOnly(111); }
// static const MoveOnly createConst() { return MoveOnly(111); } // not supported by default
// static void take(MoveOnly mo) { if (trace) cout << "take(MoveOnly)" << " " << &mo << endl; }
// compile error by default, see cpp11_move_typemaps.i
#if defined(WRAP_TAKE_METHOD)
static void take(MoveOnly mo) { if (trace) cout << "take(MoveOnly)" << " " << &mo << endl; }
#endif
};
%}
@ -35,13 +39,14 @@ struct MoveOnly {
%inline %{
// Movable and Copyable
struct MovableCopyable {
MovableCopyable(int i = 0) { if (trace) cout << "MovableCopyable(" << i << ")" << " " << this << endl; Counter::normal_constructor++; }
int val;
MovableCopyable(int i = 0) : val(i) { if (trace) cout << "MovableCopyable(" << i << ")" << " " << this << endl; Counter::normal_constructor++; }
MovableCopyable(const MovableCopyable &other) { if (trace) cout << "MovableCopyable(const MovableCopyable &)" << " " << this << " " << &other << endl; Counter::copy_constructor++;}
MovableCopyable & operator=(const MovableCopyable &other) { if (trace) cout << "operator=(const MovableCopyable &)" << " " << this << " " << &other << endl; Counter::copy_assignment++; return *this; }
MovableCopyable(const MovableCopyable &other) : val(other.val) { if (trace) cout << "MovableCopyable(const MovableCopyable &)" << " " << this << " " << &other << endl; Counter::copy_constructor++;}
MovableCopyable & operator=(const MovableCopyable &other) { if (trace) cout << "operator=(const MovableCopyable &)" << " " << this << " " << &other << endl; Counter::copy_assignment++; if (this != &other) { val = other.val; } return *this; }
MovableCopyable(MovableCopyable &&other) noexcept { if (trace) cout << "MovableCopyable(MovableCopyable &&)" << " " << this << endl; Counter::move_constructor++; }
MovableCopyable & operator=(MovableCopyable &&other) noexcept { if (trace) cout << "operator=(MovableCopyable &&)" << " " << this << endl; Counter::move_assignment++; return *this; }
MovableCopyable(MovableCopyable &&other) noexcept : val(std::move(other.val)) { if (trace) cout << "MovableCopyable(MovableCopyable &&)" << " " << this << endl; Counter::move_constructor++; }
MovableCopyable & operator=(MovableCopyable &&other) noexcept { if (trace) cout << "operator=(MovableCopyable &&)" << " " << this << endl; Counter::move_assignment++; if (this != &other) { val = std::move(other.val); } return *this; }
~MovableCopyable() { if (trace) cout << "~MovableCopyable()" << " " << this << endl; Counter::destructor++; }
static MovableCopyable create() { return MovableCopyable(111); }

View file

@ -0,0 +1,12 @@
%module cpp11_move_typemaps
%include <swigmove.i>
%apply SWIGTYPE MOVE { MoveOnly mo }
%valuewrapper MovableCopyable;
%apply SWIGTYPE MOVE { MovableCopyable mc }
%inline %{
#define WRAP_TAKE_METHOD
%}
%include "cpp11_move_only.i"

View file

@ -0,0 +1,37 @@
using System;
using cpp11_move_typemapsNamespace;
public class cpp11_move_typemaps_runme {
public static void Main() {
Counter.reset_counts();
using (MoveOnly mo = new MoveOnly(111)) {
Counter.check_counts(1, 0, 0, 0, 0, 0);
MoveOnly.take(mo);
Counter.check_counts(1, 0, 0, 1, 0, 2);
}
Counter.check_counts(1, 0, 0, 1, 0, 2);
Counter.reset_counts();
using (MovableCopyable mo = new MovableCopyable(111)) {
Counter.check_counts(1, 0, 0, 0, 0, 0);
MovableCopyable.take(mo);
Counter.check_counts(1, 0, 0, 1, 0, 2);
}
Counter.check_counts(1, 0, 0, 1, 0, 2);
using (MoveOnly mo = new MoveOnly(222)) {
MoveOnly.take(mo);
bool exception_thrown = false;
try {
MoveOnly.take(mo);
} catch (ApplicationException e) {
if (!e.Message.Contains("Cannot release ownership as memory is not owned"))
throw new ApplicationException("incorrect exception message");
exception_thrown = true;
}
if (!exception_thrown)
throw new ApplicationException("double usage of take should have been an error");
}
}
}

View file

@ -0,0 +1,42 @@
module cpp11_move_typemaps_runme;
import cpp11_move_typemaps.Counter;
import cpp11_move_typemaps.MoveOnly;
import cpp11_move_typemaps.MovableCopyable;
import std.conv;
import std.algorithm;
void main() {
{
Counter.reset_counts();
scope MoveOnly mo = new MoveOnly(111);
Counter.check_counts(1, 0, 0, 0, 0, 0);
MoveOnly.take(mo);
Counter.check_counts(1, 0, 0, 1, 0, 2);
}
Counter.check_counts(1, 0, 0, 1, 0, 2);
{
Counter.reset_counts();
scope MovableCopyable mo = new MovableCopyable(111);
Counter.check_counts(1, 0, 0, 0, 0, 0);
MovableCopyable.take(mo);
Counter.check_counts(1, 0, 0, 1, 0, 2);
}
Counter.check_counts(1, 0, 0, 1, 0, 2);
{
scope MoveOnly mo = new MoveOnly(222);
MoveOnly.take(mo);
bool exception_thrown = false;
try {
MoveOnly.take(mo);
} catch (Exception e) {
if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
throw new Exception("incorrect exception message: " ~ e.msg);
exception_thrown = true;
}
if (!exception_thrown)
throw new Exception("double usage of take should have been an error");
}
}

View file

@ -0,0 +1,42 @@
module cpp11_move_typemaps_runme;
import cpp11_move_typemaps.Counter;
import cpp11_move_typemaps.MoveOnly;
import cpp11_move_typemaps.MovableCopyable;
import std.conv;
import std.algorithm;
void main() {
{
Counter.reset_counts();
scope MoveOnly mo = new MoveOnly(111);
Counter.check_counts(1, 0, 0, 0, 0, 0);
MoveOnly.take(mo);
Counter.check_counts(1, 0, 0, 1, 0, 2);
}
Counter.check_counts(1, 0, 0, 1, 0, 2);
{
Counter.reset_counts();
scope MovableCopyable mo = new MovableCopyable(111);
Counter.check_counts(1, 0, 0, 0, 0, 0);
MovableCopyable.take(mo);
Counter.check_counts(1, 0, 0, 1, 0, 2);
}
Counter.check_counts(1, 0, 0, 1, 0, 2);
{
scope MoveOnly mo = new MoveOnly(222);
MoveOnly.take(mo);
bool exception_thrown = false;
try {
MoveOnly.take(mo);
} catch (Exception e) {
if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
throw new Exception("incorrect exception message: " ~ e.msg);
exception_thrown = true;
}
if (!exception_thrown)
throw new Exception("double usage of take should have been an error");
}
}

View file

@ -0,0 +1,3 @@
(dynamic-call "scm_init_cpp11_move_typemaps_module" (dynamic-link "./libcpp11_move_typemaps"))
(load "testsuite.scm")
(load "../schemerunme/cpp11_move_typemaps.scm")

View file

@ -0,0 +1,51 @@
import cpp11_move_typemaps.*;
public class cpp11_move_typemaps_runme {
static {
try {
System.loadLibrary("cpp11_move_typemaps");
} 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 = new MoveOnly(111);
Counter.check_counts(1, 0, 0, 0, 0, 0);
MoveOnly.take(mo);
Counter.check_counts(1, 0, 0, 1, 0, 2);
mo.delete();
}
Counter.check_counts(1, 0, 0, 1, 0, 2);
{
Counter.reset_counts();
MovableCopyable mo = new MovableCopyable(111);
Counter.check_counts(1, 0, 0, 0, 0, 0);
MovableCopyable.take(mo);
Counter.check_counts(1, 0, 0, 1, 0, 2);
mo.delete();
}
Counter.check_counts(1, 0, 0, 1, 0, 2);
{
MoveOnly mo = new MoveOnly(222);
MoveOnly.take(mo);
boolean exception_thrown = false;
try {
MoveOnly.take(mo);
} catch (RuntimeException e) {
if (!e.getMessage().contains("Cannot release ownership as memory is not owned"))
throw new RuntimeException("incorrect exception message");
exception_thrown = true;
}
if (!exception_thrown)
throw new RuntimeException("double usage of take should have been an error");
}
}
}

View file

@ -0,0 +1,30 @@
var cpp11_move_typemaps = require("cpp11_move_typemaps");
cpp11_move_typemaps.Counter.reset_counts();
mo = new cpp11_move_typemaps.MoveOnly(111);
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 0, 0, 0);
cpp11_move_typemaps.MoveOnly.take(mo);
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 1, 0, 2);
delete mo;
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 1, 0, 2);
cpp11_move_typemaps.Counter.reset_counts();
mo = new cpp11_move_typemaps.MovableCopyable(111);
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 0, 0, 0);
cpp11_move_typemaps.MovableCopyable.take(mo);
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 1, 0, 2);
delete mo;
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 1, 0, 2);
mo = new cpp11_move_typemaps.MoveOnly(222);
cpp11_move_typemaps.MoveOnly.take(mo);
exception_thrown = false;
try {
cpp11_move_typemaps.MoveOnly.take(mo);
} catch (e) {
if (!e.message.includes("cannot release ownership as memory is not owned"))
throw new Error("incorrect exception message:" + e.message);
exception_thrown = true;
}
if (!exception_thrown)
throw new Error("double usage of take should have been an error");

View file

@ -0,0 +1,28 @@
require("import") -- the import fn
import("cpp11_move_typemaps") -- import code
-- catch "undefined" global variables
local env = _ENV -- Lua 5.2
if not env then env = getfenv () end -- Lua 5.1
setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
cpp11_move_typemaps.Counter.reset_counts()
mo = cpp11_move_typemaps.MoveOnly(111)
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 0, 0, 0)
cpp11_move_typemaps.MoveOnly.take(mo)
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 1, 0, 2)
mo = nil
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 1, 0, 2)
cpp11_move_typemaps.Counter.reset_counts()
mo = cpp11_move_typemaps.MovableCopyable(111)
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 0, 0, 0)
cpp11_move_typemaps.MovableCopyable.take(mo)
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 1, 0, 2)
mo = nil
cpp11_move_typemaps.Counter.check_counts(1, 0, 0, 1, 0, 2)
mo = cpp11_move_typemaps.MoveOnly(222)
cpp11_move_typemaps.MoveOnly.take(mo)
s, msg = pcall(function() cpp11_move_typemaps.MoveOnly.take(mo) end)
assert(s == false and msg:find("Cannot release ownership as memory is not owned", 1, true))

View file

@ -0,0 +1,35 @@
(load-extension "cpp11_move_typemaps.so")
(require (lib "defmacro.ss"))
; Copied from ../schemerunme/cpp11_move_typemaps.scm and modified for exceptions
; Define an equivalent to Guile's gc procedure
(define-macro (gc)
`(collect-garbage 'major))
(Counter-reset-counts)
(define mo (new-MoveOnly 111))
(Counter-check-counts 1 0 0 0 0 0)
(MoveOnly-take mo)
(Counter-check-counts 1 0 0 1 0 2)
(delete-MoveOnly mo)
(Counter-check-counts 1 0 0 1 0 2)
(Counter-reset-counts)
(define mo (new-MovableCopyable 111))
(Counter-check-counts 1 0 0 0 0 0)
(MovableCopyable-take mo)
(Counter-check-counts 1 0 0 1 0 2)
(delete-MovableCopyable mo)
(Counter-check-counts 1 0 0 1 0 2)
(define mo (new-MoveOnly 222))
(MoveOnly-take mo)
(define exception_thrown "no exception thrown for mo")
(with-handlers ([exn:fail? (lambda (exn)
(set! exception_thrown (exn-message exn)))])
(MoveOnly-take mo))
(unless (string-contains? exception_thrown "cannot release ownership as memory is not owned")
(error "Wrong or no exception thrown: " exception_thrown))
(exit 0)

View file

@ -0,0 +1,37 @@
# do not dump Octave core
if exist("crash_dumps_octave_core", "builtin")
crash_dumps_octave_core(0);
endif
cpp11_move_typemaps
Counter.reset_counts();
mo = MoveOnly(111);
Counter_check_counts(1, 0, 0, 0, 0, 0);
MoveOnly.take(mo);
Counter_check_counts(1, 0, 0, 1, 0, 2);
clear mo;
Counter_check_counts(1, 0, 0, 1, 0, 2);
Counter.reset_counts();
mo = MovableCopyable(111);
Counter_check_counts(1, 0, 0, 0, 0, 0);
MovableCopyable.take(mo);
Counter_check_counts(1, 0, 0, 1, 0, 2);
clear mo;
Counter_check_counts(1, 0, 0, 1, 0, 2);
mo = MoveOnly(222);
MoveOnly.take(mo);
exception_thrown = false;
try
MoveOnly.take(mo);
catch e
if (isempty(strfind(e.message, "cannot release ownership as memory is not owned")))
error("incorrect exception message %s", e.message);
endif
exception_thrown = true;
end_try_catch
if (!exception_thrown)
error("double usage of take should have been an error");
endif

View file

@ -0,0 +1,34 @@
use strict;
use warnings;
use Test::More tests => 3;
BEGIN { use_ok('cpp11_move_typemaps') }
require_ok('cpp11_move_typemaps');
{
cpp11_move_typemaps::Counter::reset_counts();
my $mo = new cpp11_move_typemaps::MoveOnly(111);
cpp11_move_typemaps::Counter::check_counts(1, 0, 0, 0, 0, 0);
cpp11_move_typemaps::MoveOnly::take($mo);
cpp11_move_typemaps::Counter::check_counts(1, 0, 0, 1, 0, 2);
undef $mo;
}
cpp11_move_typemaps::Counter::check_counts(1, 0, 0, 1, 0, 2);
{
cpp11_move_typemaps::Counter::reset_counts();
my $mo = new cpp11_move_typemaps::MovableCopyable(111);
cpp11_move_typemaps::Counter::check_counts(1, 0, 0, 0, 0, 0);
cpp11_move_typemaps::MovableCopyable::take($mo);
cpp11_move_typemaps::Counter::check_counts(1, 0, 0, 1, 0, 2);
undef $mo;
}
cpp11_move_typemaps::Counter::check_counts(1, 0, 0, 1, 0, 2);
{
my $mo = new cpp11_move_typemaps::MoveOnly(222);
cpp11_move_typemaps::MoveOnly::take($mo);
eval {
cpp11_move_typemaps::MoveOnly::take($mo);
};
like($@, qr/\bcannot release ownership as memory is not owned\b/, "double usage of takeKlassUniquePtr should be an error");
}

View file

@ -0,0 +1,32 @@
<?php
require "tests.php";
Counter::reset_counts();
$mo = new MoveOnly(111);
Counter::check_counts(1, 0, 0, 0, 0, 0);
MoveOnly::take($mo);
Counter::check_counts(1, 0, 0, 1, 0, 2);
$mo = NULL;
Counter::check_counts(1, 0, 0, 1, 0, 2);
Counter::reset_counts();
$mo = new MovableCopyable(111);
Counter::check_counts(1, 0, 0, 0, 0, 0);
MovableCopyable::take($mo);
Counter::check_counts(1, 0, 0, 1, 0, 2);
$mo = NULL;
Counter::check_counts(1, 0, 0, 1, 0, 2);
$mo = new MoveOnly(222);
MoveOnly::take($mo);
$exception_thrown = false;
try {
MoveOnly::take($mo);
} catch (TypeError $e) {
check::str_contains($e->getMessage(), "Cannot release ownership as memory is not owned", "incorrect exception message: {$e->getMessage()}");
$exception_thrown = true;
}
check::equal($exception_thrown, true, "double usage of takeKlassUniquePtr should have been an error");
check::done();

View file

@ -0,0 +1,29 @@
from cpp11_move_typemaps import *
Counter.reset_counts()
mo = MoveOnly(111)
Counter.check_counts(1, 0, 0, 0, 0, 0)
MoveOnly.take(mo)
Counter.check_counts(1, 0, 0, 1, 0, 2)
del mo
Counter.check_counts(1, 0, 0, 1, 0, 2)
Counter.reset_counts()
mo = MovableCopyable(111)
Counter.check_counts(1, 0, 0, 0, 0, 0)
MovableCopyable.take(mo)
Counter.check_counts(1, 0, 0, 1, 0, 2)
del mo
Counter.check_counts(1, 0, 0, 1, 0, 2)
mo = MoveOnly(222)
MoveOnly.take(mo)
exception_thrown = False
try:
MoveOnly.take(mo)
except RuntimeError as e:
if "cannot release ownership as memory is not owned" not in str(e):
raise RuntimeError("incorrect exception message:" + str(e))
exception_thrown = True
if not exception_thrown:
raise RuntimeError("Should have thrown 'Cannot release ownership as memory is not owned' error")

View file

@ -0,0 +1,36 @@
#!/usr/bin/env ruby
require 'swig_assert'
require 'cpp11_move_typemaps'
Cpp11_move_typemaps::Counter.reset_counts()
mo = Cpp11_move_typemaps::MoveOnly.new(111)
Cpp11_move_typemaps::Counter.check_counts(1, 0, 0, 0, 0, 0)
Cpp11_move_typemaps::MoveOnly.take(mo)
Cpp11_move_typemaps::Counter.check_counts(1, 0, 0, 1, 0, 2)
mo = nil
Cpp11_move_typemaps::Counter.check_counts(1, 0, 0, 1, 0, 2)
Cpp11_move_typemaps::Counter.reset_counts()
mo = Cpp11_move_typemaps::MovableCopyable.new(111)
Cpp11_move_typemaps::Counter.check_counts(1, 0, 0, 0, 0, 0)
Cpp11_move_typemaps::MovableCopyable.take(mo)
Cpp11_move_typemaps::Counter.check_counts(1, 0, 0, 1, 0, 2)
mo = nil
Cpp11_move_typemaps::Counter.check_counts(1, 0, 0, 1, 0, 2)
mo = Cpp11_move_typemaps::MoveOnly.new(222)
Cpp11_move_typemaps::MoveOnly.take(mo)
exception_thrown = false
begin
Cpp11_move_typemaps::MoveOnly.take(mo)
rescue RuntimeError => e
if (!e.to_s.include? "cannot release ownership as memory is not owned")
raise RuntimeError, "incorrect exception message: #{e.to_s}"
end
exception_thrown = true
end
if (!exception_thrown)
raise RuntimeError, "Should have thrown 'Cannot release ownership as memory is not owned' error"
end

View file

@ -0,0 +1,23 @@
(Counter-reset-counts)
(define mo (new-MoveOnly 111))
(Counter-check-counts 1 0 0 0 0 0)
(MoveOnly-take mo)
(Counter-check-counts 1 0 0 1 0 2)
(delete-MoveOnly mo)
(Counter-check-counts 1 0 0 1 0 2)
(Counter-reset-counts)
(define mo (new-MovableCopyable 111))
(Counter-check-counts 1 0 0 0 0 0)
(MovableCopyable-take mo)
(Counter-check-counts 1 0 0 1 0 2)
(delete-MovableCopyable mo)
(Counter-check-counts 1 0 0 1 0 2)
(define mo (new-MoveOnly 222))
(MoveOnly-take mo)
(expect-throw 'misc-error
(MoveOnly-take mo))
; TODO: check the exception message
(exit 0)

View file

@ -0,0 +1,35 @@
if [ catch { load ./cpp11_move_typemaps[info sharedlibextension] cpp11_move_typemaps} err_msg ] {
puts stderr "Could not load shared object:\n$err_msg"
}
Counter_reset_counts
MoveOnly mo 111
Counter_check_counts 1 0 0 0 0 0
MoveOnly_take mo
Counter_check_counts 1 0 0 1 0 2
mo -delete
Counter_check_counts 1 0 0 1 0 2
Counter_reset_counts
MovableCopyable mo 111
Counter_check_counts 1 0 0 0 0 0
MovableCopyable_take mo
Counter_check_counts 1 0 0 1 0 2
mo -delete
Counter_check_counts 1 0 0 1 0 2
MoveOnly mo 222
MoveOnly_take mo
set exception_thrown 0
if [ catch {
MoveOnly_take mo
} e ] {
if {[string first "cannot release ownership as memory is not owned" $e] == -1} {
error "incorrect exception message: $e"
}
set exception_thrown 1
}
if {!$exception_thrown} {
error "Should have thrown 'Cannot release ownership as memory is not owned' error"
}