Add missing use of move constructor

instead of copy constructor when passing movable types by value.
Additional enhancement when passing movable types to constructors.

Enhancement to e777b054d5.
This commit is contained in:
William S Fulton 2024-01-23 21:55:07 +00:00
parent dcc1471dd3
commit 9441c9513c
7 changed files with 54 additions and 5 deletions

View File

@ -9,9 +9,9 @@ Version 4.3.0 (in progress)
2024-01-23: wsfulton
Add missing use of move constructor instead of copy constructor when
passing movable types. This was previously implemented only for parameters
passed to a global function or static member function and is now extended
to member methods.
passing movable types by value. This was previously implemented only for
parameters passed to a global function or static member function and is
now extended to parameters passed to member methods as well as constructors.
2024-03-01: olly
[Java] #2819 Suppress Java removal warnings for uses of

View File

@ -7,7 +7,7 @@
#endif
%ignore MoveOnly::operator=;
//%valuewrapper MoveOnly; // SWIG sets %valuewrapper by default for move-only types (actually when there is no copy assignment operator)
//%valuewrapper MoveOnly; // SWIG sets %valuewrapper by default for move-only types (actually when there is no assignment operator)
%inline %{
#include <iostream>
@ -71,4 +71,12 @@ struct InstanceMethodsTester {
void global_take_move_only(MoveOnly mo) { if (trace) cout << "global_take_move_only(MoveOnly)" << " " << &mo << endl; }
#endif
void global_take_movable_copyable(MovableCopyable mc) { if (trace) cout << "global_take_movable_copyable(MovableCopyable)" << " " << &mc << endl; }
struct ConstructorTester {
#if defined(WRAP_TAKE_METHOD)
ConstructorTester(MoveOnly mo) { if (trace) cout << "ConstructorTester(MoveOnly)" << " " << &mo << endl; }
#endif
ConstructorTester(MovableCopyable mc) { if (trace) cout << "ConstructorTester(MovableCopyable)" << " " << &mc << endl; }
};
%}

View File

@ -49,5 +49,16 @@ public class cpp11_move_only_runme {
}
Counter.check_counts(2, 0, 1, 1, 0, 3);
}
// Input constructor
Counter.reset_counts();
using (MovableCopyable mc = new MovableCopyable(555)) {
Counter.check_counts(1, 0, 0, 0, 0, 0);
using (ConstructorTester ct = new ConstructorTester(mc)) {
Counter.check_counts(2, 0, 1, 1, 0, 2);
}
Counter.check_counts(2, 0, 1, 1, 0, 2);
}
Counter.check_counts(2, 0, 1, 1, 0, 3);
}
}

View File

@ -70,5 +70,16 @@ public class cpp11_move_only_runme {
mc.delete();
Counter.check_counts(2, 0, 1, 1, 0, 3);
}
// Input constructor
{
Counter.reset_counts();
MovableCopyable mc = new MovableCopyable(555);
Counter.check_counts(1, 0, 0, 0, 0, 0);
ConstructorTester ct = new ConstructorTester(mc);
Counter.check_counts(2, 0, 1, 1, 0, 2);
mc.delete();
Counter.check_counts(2, 0, 1, 1, 0, 3);
}
}
}

View File

@ -35,3 +35,10 @@ var mc = new cpp11_move_only.MovableCopyable(444);
cpp11_move_only.Counter.check_counts(1, 0, 0, 0, 0, 0);
imt.instance_take_movable_copyable(mc);
cpp11_move_only.Counter.check_counts(2, 0, 1, 1, 0, 2);
// Input constructor
cpp11_move_only.Counter.reset_counts();
var mc = new cpp11_move_only.MovableCopyable(555);
cpp11_move_only.Counter.check_counts(1, 0, 0, 0, 0, 0);
var ct = new cpp11_move_only.ConstructorTester(mc);
cpp11_move_only.Counter.check_counts(2, 0, 1, 1, 0, 2);

View File

@ -44,3 +44,12 @@ imt.instance_take_movable_copyable(mc)
Counter.check_counts(2, 0, 1, 1, 0, 2)
del mc
Counter.check_counts(2, 0, 1, 1, 0, 3)
# Input constructor
Counter.reset_counts()
mc = MovableCopyable(555)
Counter.check_counts(1, 0, 0, 0, 0, 0)
ct = ConstructorTester(mc)
Counter.check_counts(2, 0, 1, 1, 0, 2)
del mc
Counter.check_counts(2, 0, 1, 1, 0, 3)

View File

@ -608,7 +608,10 @@ String *Swig_cppconstructor_base_call(const_String_or_char_ptr name, ParmList *p
pname = Copy(Getattr(p, "name"));
}
rcaststr = SwigType_rcaststr(pt, pname);
Append(func, rcaststr);
if (cparse_cplusplus && SwigType_type(pt) == T_USER)
Printv(func, "SWIG_STD_MOVE(", rcaststr, ")", NIL);
else
Printv(func, rcaststr, NIL);
Delete(rcaststr);
comma = 1;
Delete(pname);