Commit cf24f025 authored by Davis King's avatar Davis King

Added an object that lets you hold a copyable reference to a java array. Also

renamed the objects and generally improved documentation.
parent fa5c666b
...@@ -119,8 +119,26 @@ FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/swig.i ...@@ -119,8 +119,26 @@ FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/swig.i
%module global %module global
%{ %{
#include \"swig_api.h\"
#include <exception> #include <exception>
#include <stdexcept>
static JavaVM *cached_jvm = 0;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
cached_jvm = jvm;
return JNI_VERSION_1_6;
}
static JNIEnv * JNI_GetEnv() {
JNIEnv *env;
jint rc = cached_jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
if (rc == JNI_EDETACHED)
throw std::runtime_error(\"current thread not attached\");
if (rc == JNI_EVERSION)
throw std::runtime_error(\"jni version not supported\");
return env;
}
#include \"swig_api.h\"
%} %}
// Convert all C++ exceptions into java.lang.Exception // Convert all C++ exceptions into java.lang.Exception
......
This diff is collapsed.
This diff is collapsed.
#ifndef EXAMPLE_SWIG_ApI_H_ #ifndef EXAMPLE_SWIG_ApI_H_
#define EXAMPLE_SWIG_ApI_H_ #define EXAMPLE_SWIG_ApI_H_
// This file is essentially a small unit test for the swig cmake scripts and the jvector // This file is essentially a small unit test for the swig cmake scripts and the java array
// classes. All it does it define a few simple functions for writing to and summing // classes. All it does it define a few simple functions for writing to and summing
// arrays. The swig_test.java file then calls these C++ functions and checks if they work // arrays. The swig_test.java file then calls these C++ functions and checks if they work
// correctly. // correctly.
// Let's use the jvector, a tool for efficiently binding java native arrays to C++ function // Let's use java_array.h, a tool for efficiently binding java native arrays to C++
// arguments. You do this by putting this pair of include statements in your swig_api.h // function arguments. You do this by putting this pair of include statements in your
// file. Then after that you can use the jvector and jvector_crit classes. // swig_api.h file. Then after that you can use the java::array, java::array_view, and
#include "jvector.h" // java::array_view_crit classes.
#include "java_array.h"
#ifdef SWIG #ifdef SWIG
%include "jvector.h" %include "java_array.h"
#endif #endif
// ---------------------------------------------------------------------------------------- using namespace java;
// SWIG can't expose templated functions to java. We declare these here as helper // SWIG can't expose templated functions to java. We declare these here as helper
// functions to make the non-templated routines swig will expose easier to write. You can // functions to make the non-templated routines swig will expose easier to write. You can
// see these java exposed methods below (i.e. sum(), sum_crit(), assign(), and // see these java exposed methods below (i.e. sum(), sum_crit(), assign(), and
// assign_crit()). // assign_crit()).
template <typename T> template <typename T>
T tsum(const jvector_crit<T>& arr) T tsum(const array_view_crit<T>& arr)
{ {
T s = 0; T s = 0;
for (auto& v : arr) for (auto& v : arr)
...@@ -32,7 +34,7 @@ T tsum(const jvector_crit<T>& arr) ...@@ -32,7 +34,7 @@ T tsum(const jvector_crit<T>& arr)
return s; return s;
} }
template <typename T> template <typename T>
T tsum(const jvector<T>& arr) T tsum(const array_view<T>& arr)
{ {
T s = 0; T s = 0;
for (auto& v : arr) for (auto& v : arr)
...@@ -56,41 +58,64 @@ void tassign(T& arr) ...@@ -56,41 +58,64 @@ void tassign(T& arr)
// "global", which is where these sum and assign routines will appear. You can see // "global", which is where these sum and assign routines will appear. You can see
// examples of java code that calls them in swig_test.java. // examples of java code that calls them in swig_test.java.
inline int sum_crit(const jvector_crit<int16_t>& arr) { return tsum(arr); } inline int sum_crit(const array_view_crit<int16_t>& arr) { return tsum(arr); }
inline int sum(const jvector<int16_t>& arr) { return tsum(arr); } inline int sum(const array_view<int16_t>& arr) { return tsum(arr); }
inline void assign_crit(jvector_crit<int16_t>& arr) { tassign(arr); } inline void assign_crit(array_view_crit<int16_t>& arr) { tassign(arr); }
inline void assign(jvector<int16_t>& arr) { tassign(arr); } inline void assign(array_view<int16_t>& arr) { tassign(arr); }
inline int sum_crit(const jvector_crit<int32_t>& arr) { return tsum(arr); } inline int sum_crit(const array_view_crit<int32_t>& arr) { return tsum(arr); }
inline int sum(const jvector<int32_t>& arr) { return tsum(arr); } inline int sum(const array_view<int32_t>& arr) { return tsum(arr); }
inline void assign_crit(jvector_crit<int32_t>& arr) { tassign(arr); } inline void assign_crit(array_view_crit<int32_t>& arr) { tassign(arr); }
inline void assign(jvector<int32_t>& arr) { tassign(arr); } inline void assign(array_view<int32_t>& arr) { tassign(arr); }
inline int sum_crit(const jvector_crit<int64_t>& arr) { return tsum(arr); } inline int sum_crit(const array_view_crit<int64_t>& arr) { return tsum(arr); }
inline int sum(const jvector<int64_t>& arr) { return tsum(arr); } inline int sum(const array_view<int64_t>& arr) { return tsum(arr); }
inline void assign_crit(jvector_crit<int64_t>& arr) { tassign(arr); } inline void assign_crit(array_view_crit<int64_t>& arr) { tassign(arr); }
inline void assign(jvector<int64_t>& arr) { tassign(arr); } inline void assign(array_view<int64_t>& arr) { tassign(arr); }
inline int sum_crit(const jvector_crit<char>& arr) { return tsum(arr); } inline int sum_crit(const array_view_crit<char>& arr) { return tsum(arr); }
inline int sum(const jvector<char>& arr) { return tsum(arr); } inline int sum(const array_view<char>& arr) { return tsum(arr); }
inline void assign_crit(jvector_crit<char>& arr) { tassign(arr); } inline void assign_crit(array_view_crit<char>& arr) { tassign(arr); }
inline void assign(jvector<char>& arr) { tassign(arr); } inline void assign(array_view<char>& arr) { tassign(arr); }
inline double sum_crit(const jvector_crit<double>& arr) { return tsum(arr); } inline double sum_crit(const array_view_crit<double>& arr) { return tsum(arr); }
inline double sum(const jvector<double>& arr) { return tsum(arr); } inline double sum(const array_view<double>& arr) { return tsum(arr); }
inline void assign_crit(jvector_crit<double>& arr) { tassign(arr); } inline void assign_crit(array_view_crit<double>& arr) { tassign(arr); }
inline void assign(jvector<double>& arr) { tassign(arr); } inline void assign(array_view<double>& arr) { tassign(arr); }
inline float sum_crit(const jvector_crit<float>& arr) { return tsum(arr); } inline float sum_crit(array<float> arr)
inline float sum(const jvector<float>& arr) { return tsum(arr); } {
inline void assign_crit(jvector_crit<float>& arr) { tassign(arr); } array_view_crit<float> a(arr);
inline void assign(jvector<float>& arr) { tassign(arr); } return tsum(a);
}
inline float sum(const array<float>& arr)
{
array_view<float> a(arr);
return tsum(a);
}
inline void assign_crit(array_view_crit<float>& arr) { tassign(arr); }
inline void assign(array<float>& arr)
{
array_view<float> a(arr);
tassign(a);
}
array<int32_t> make_an_array(size_t s)
{
array<int32_t> arr(s);
array_view_crit<int32_t> a(arr);
for (size_t i = 0; i < a.size(); ++i)
a[i] = i;
return arr;
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -69,6 +69,14 @@ public class swig_test ...@@ -69,6 +69,14 @@ public class swig_test
} }
} }
public static void assertIsEqual(int val1, int val2)
{
if (val1 != val2)
{
throw new RuntimeException("Test failed " + val1 + " should be equal to " + val2);
}
}
public static double sum(double[] arr) public static double sum(double[] arr)
{ {
double s = 0; double s = 0;
...@@ -233,6 +241,13 @@ public class swig_test ...@@ -233,6 +241,13 @@ public class swig_test
assertIs28(global.sum_crit(arr)); assertIs28(global.sum_crit(arr));
} }
} }
{
int[] a = global.make_an_array(4);
for (int i = 0; i < a.length; ++i)
{
assertIsEqual(a[i], i);
}
}
System.out.println("\n\n ALL TESTS COMPLETED SUCCESSFULLY\n"); System.out.println("\n\n ALL TESTS COMPLETED SUCCESSFULLY\n");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment