Add C++11 std::array container support for Java

This commit is contained in:
William S Fulton 2016-03-14 20:46:56 +00:00
commit 870b814f5e
4 changed files with 128 additions and 1 deletions

View file

@ -5,6 +5,9 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 3.0.9 (in progress)
===========================
2016-03-14: wsfulton
[Java] Add std_array.i for C++11 std::array support.
2016-03-12: wsfulton
[Java, C#, D] Fix static const char member variables wrappers with %javaconst(1)
%csconst(1) or %dmanifestconst.

View file

@ -1,6 +1,6 @@
%module cpp11_li_std_array
#if defined(SWIGPYTHON) || defined(SWIGRUBY)
#if defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGJAVA)
%{
#include <array>

View file

@ -0,0 +1,82 @@
import cpp11_li_std_array.*;
public class cpp11_li_std_array_runme {
static {
try {
System.loadLibrary("cpp11_li_std_array");
} 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);
}
}
private static ArrayInt6 ToArray6(int [] a) {
ArrayInt6 ai = new ArrayInt6();
if (a.length != 6)
throw new RuntimeException("a is incorrect size");
for (int i=0; i<6; ++i)
ai.set(i, a[i]);
return ai;
}
private static void compareContainers(ArrayInt6 actual, int[] expected) {
if (actual.size() != expected.length)
throw new RuntimeException("Sizes are different: " + actual.size() + " " + expected.length);
for (int i=0; i<actual.size(); ++i) {
int actualValue = actual.get(i);
int expectedValue = expected[i];
if (actualValue != expectedValue)
throw new RuntimeException("Value is wrong for element " + i + ". Expected " + expectedValue + " got: " + actualValue);
}
if (actual.isEmpty())
throw new RuntimeException("ai should not be empty");
}
public static void main(String argv[]) {
ArrayInt6 ai = new ArrayInt6();
compareContainers(ai, new int[] {0, 0, 0, 0, 0, 0});
int[] vals = {10, 20, 30, 40, 50, 60};
for (int i=0; i<ai.size(); ++i)
ai.set(i, vals[i]);
compareContainers(ai, vals);
// Check return
compareContainers(cpp11_li_std_array.arrayOutVal(), new int[] {-2, -1, 0, 0, 1, 2});
compareContainers(cpp11_li_std_array.arrayOutConstRef(), new int[] {-2, -1, 0, 0, 1, 2});
compareContainers(cpp11_li_std_array.arrayOutRef(), new int[] {-2, -1, 0, 0, 1, 2});
compareContainers(cpp11_li_std_array.arrayOutPtr(), new int[] {-2, -1, 0, 0, 1, 2});
// Check passing arguments
ai = cpp11_li_std_array.arrayInVal(ToArray6(new int[] {9, 8, 7, 6, 5, 4}));
compareContainers(ai, new int[] {90, 80, 70, 60, 50, 40});
ai = cpp11_li_std_array.arrayInConstRef(ToArray6(new int[] {9, 8, 7, 6, 5, 4}));
compareContainers(ai, new int[] {90, 80, 70, 60, 50, 40});
ai = new ArrayInt6(ToArray6(new int[] {9, 8, 7, 6, 5, 4}));
cpp11_li_std_array.arrayInRef(ai);
compareContainers(ai, new int[] {90, 80, 70, 60, 50, 40});
ai = new ArrayInt6(ToArray6(new int[] {9, 8, 7, 6, 5, 4}));
cpp11_li_std_array.arrayInPtr(ai);
compareContainers(ai, new int[] {90, 80, 70, 60, 50, 40});
// fill
ai.fill(111);
compareContainers(ai, new int[] {111, 111, 111, 111, 111, 111});
// out of range errors
try {
ai.set(ai.size()) = 999;
throw new RuntimeException("Out of range exception not caught");
} catch(OutOfRangeException e) {
}
try {
ai.set(-1) = 999;
throw new RuntimeException("Out of range exception not caught");
} catch(OutOfRangeException e) {
}
}
}

42
Lib/java/std_array.i Normal file
View file

@ -0,0 +1,42 @@
/* -----------------------------------------------------------------------------
* std_array.i
* ----------------------------------------------------------------------------- */
%include <std_common.i>
namespace std {
template<class T, size_t N> class array {
public:
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
array();
array(const array& other);
size_type size() const;
%rename(isEmpty) empty;
bool empty() const;
void fill(const T& u);
%extend {
const_reference get(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("array index out of range");
}
void set(int i, const value_type& val) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
(*self)[i] = val;
else
throw std::out_of_range("array index out of range");
}
}
};
}