swig/Examples/test-suite/java/cpp11_move_only_valuewrapper_runme.java
William S Fulton bf36bf7d8a 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.
2022-06-30 17:26:48 +01:00

44 lines
1.6 KiB
Java

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