* These typemaps give more natural support for arrays. The typemaps are not efficient
* as there is a lot of copying of the array values whenever the array is passed to C/C++
* from Java and visa versa. The Java array is expected to be the same size as the C array.
* An exception is thrown if they are not.
short[] fs = new short[] {10, 11, 12};
example.setFiddleSticks(fs);
fs = example.getFiddleSticks();
/* Primitive array support is a combination of SWIG macros and functions in order to reduce
* code bloat and aid maintainability. The SWIG preprocessor expands the macros into functions
* for inclusion in the generated code. */
/* Array support functions declarations macro */
%define JAVA_ARRAYS_DECL(CTYPE, JNITYPE, JAVATYPE, JFUNCNAME)
int SWIG_JavaArrayIn##JFUNCNAME (JNIEnv *jenv, JNITYPE **jarr, CTYPE **carr, JNITYPE##Array input);
void SWIG_JavaArrayArgout##JFUNCNAME (JNIEnv *jenv, JNITYPE *jarr, CTYPE *carr, JNITYPE##Array input);
JNITYPE##Array SWIG_JavaArrayOut##JFUNCNAME (JNIEnv *jenv, CTYPE *result, jsize sz);
/* Array support functions macro */
%define JAVA_ARRAYS_IMPL(CTYPE, JNITYPE, JAVATYPE, JFUNCNAME)
int SWIG_JavaArrayIn##JFUNCNAME (JNIEnv *jenv, JNITYPE **jarr, CTYPE **carr, JNITYPE##Array input) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
sz = JCALL1(GetArrayLength, jenv, input);
*jarr = JCALL2(Get##JAVATYPE##ArrayElements, jenv, input, 0);
%{ *carr = new CTYPE[sz]; %}
%{ *carr = (CTYPE*) calloc(sz, sizeof(CTYPE)); %}
SWIG_JavaThrowException(jenv, SWIG_JavaOutOfMemoryError, "array memory allocation failed");
JAVA_TYPEMAP_ARRAY_ELEMENT_ASSIGN(CTYPE)
void SWIG_JavaArrayArgout##JFUNCNAME (JNIEnv *jenv, JNITYPE *jarr, CTYPE *carr, JNITYPE##Array input) {
jsize sz = JCALL1(GetArrayLength, jenv, input);
jarr[i] = (JNITYPE)carr[i];
JCALL3(Release##JAVATYPE##ArrayElements, jenv, input, jarr, 0);
JNITYPE##Array SWIG_JavaArrayOut##JFUNCNAME (JNIEnv *jenv, CTYPE *result, jsize sz) {
JNITYPE##Array jresult = JCALL1(New##JAVATYPE##Array, jenv, sz);
arr = JCALL2(Get##JAVATYPE##ArrayElements, jenv, jresult, 0);
arr[i] = (JNITYPE)result[i];
JCALL3(Release##JAVATYPE##ArrayElements, jenv, jresult, arr, 0);
#if defined(SWIG_NOINCLUDE) || defined(SWIG_NOARRAYS)
JAVA_ARRAYS_DECL(bool, jboolean, Boolean, Bool) /* bool[] */
JAVA_ARRAYS_DECL(signed char, jbyte, Byte, Schar) /* signed char[] */
JAVA_ARRAYS_DECL(unsigned char, jshort, Short, Uchar) /* unsigned char[] */
JAVA_ARRAYS_DECL(short, jshort, Short, Short) /* short[] */
JAVA_ARRAYS_DECL(unsigned short, jint, Int, Ushort) /* unsigned short[] */
JAVA_ARRAYS_DECL(int, jint, Int, Int) /* int[] */
JAVA_ARRAYS_DECL(unsigned int, jlong, Long, Uint) /* unsigned int[] */
JAVA_ARRAYS_DECL(long, jint, Int, Long) /* long[] */
JAVA_ARRAYS_DECL(unsigned long, jlong, Long, Ulong) /* unsigned long[] */
JAVA_ARRAYS_DECL(jlong, jlong, Long, Longlong) /* long long[] */
JAVA_ARRAYS_DECL(float, jfloat, Float, Float) /* float[] */
JAVA_ARRAYS_DECL(double, jdouble, Double, Double) /* double[] */
/* Bool array element assignment different to other types to keep Visual C++ quiet */
#define JAVA_TYPEMAP_ARRAY_ELEMENT_ASSIGN(CTYPE) (*carr)[i] = ((*jarr)[i] != 0);
JAVA_ARRAYS_IMPL(bool, jboolean, Boolean, Bool) /* bool[] */
#undef JAVA_TYPEMAP_ARRAY_ELEMENT_ASSIGN
#define JAVA_TYPEMAP_ARRAY_ELEMENT_ASSIGN(CTYPE) (*carr)[i] = (CTYPE)(*jarr)[i];
JAVA_ARRAYS_IMPL(signed char, jbyte, Byte, Schar) /* signed char[] */
JAVA_ARRAYS_IMPL(unsigned char, jshort, Short, Uchar) /* unsigned char[] */
JAVA_ARRAYS_IMPL(short, jshort, Short, Short) /* short[] */
JAVA_ARRAYS_IMPL(unsigned short, jint, Int, Ushort) /* unsigned short[] */
JAVA_ARRAYS_IMPL(int, jint, Int, Int) /* int[] */
JAVA_ARRAYS_IMPL(unsigned int, jlong, Long, Uint) /* unsigned int[] */
JAVA_ARRAYS_IMPL(long, jint, Int, Long) /* long[] */
JAVA_ARRAYS_IMPL(unsigned long, jlong, Long, Ulong) /* unsigned long[] */
JAVA_ARRAYS_IMPL(jlong, jlong, Long, Longlong) /* long long[] */
JAVA_ARRAYS_IMPL(float, jfloat, Float, Float) /* float[] */
JAVA_ARRAYS_IMPL(double, jdouble, Double, Double) /* double[] */
/* The rest of this file has the array typemaps */
/* Arrays of primitive types use the following macro. The array typemaps use support functions. */
%define JAVA_ARRAYS_TYPEMAPS(CTYPE, JTYPE, JNITYPE, JFUNCNAME, JNIDESC)
%typemap(jni) CTYPE[ANY], CTYPE[] %{JNITYPE##Array%}
%typemap(jtype) CTYPE[ANY], CTYPE[] %{JTYPE[]%}
%typemap(jstype) CTYPE[ANY], CTYPE[] %{JTYPE[]%}
%typemap(in) CTYPE[] (JNITYPE *jarr)
%{ if (!SWIG_JavaArrayIn##JFUNCNAME(jenv, &jarr, &$1, $input)) return $null; %}
%typemap(in) CTYPE[ANY] (JNITYPE *jarr)
%{ if ($input && JCALL1(GetArrayLength, jenv, $input) != $1_size) {
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "incorrect array size");
if (!SWIG_JavaArrayIn##JFUNCNAME(jenv, &jarr, &$1, $input)) return $null; %}
%typemap(argout) CTYPE[ANY], CTYPE[]
%{ SWIG_JavaArrayArgout##JFUNCNAME(jenv, jarr$argnum, $1, $input); %}
%{$result = SWIG_JavaArrayOut##JFUNCNAME(jenv, $1, $1_dim0); %}
%{$result = SWIG_JavaArrayOut##JFUNCNAME(jenv, $1, FillMeInAsSizeCannotBeDeterminedAutomatically); %}
%typemap(freearg) CTYPE[ANY], CTYPE[]
%typemap(javain) CTYPE[ANY], CTYPE[] "$javainput"
%typemap(javaout) CTYPE[ANY], CTYPE[] {
JAVA_ARRAYS_TYPEMAPS(bool, boolean, jboolean, Bool, "[Z") /* bool[ANY] */
JAVA_ARRAYS_TYPEMAPS(signed char, byte, jbyte, Schar, "[B") /* signed char[ANY] */
JAVA_ARRAYS_TYPEMAPS(unsigned char, short, jshort, Uchar, "[S") /* unsigned char[ANY] */
JAVA_ARRAYS_TYPEMAPS(short, short, jshort, Short, "[S") /* short[ANY] */
JAVA_ARRAYS_TYPEMAPS(unsigned short, int, jint, Ushort, "[I") /* unsigned short[ANY] */
JAVA_ARRAYS_TYPEMAPS(int, int, jint, Int, "[I") /* int[ANY] */
JAVA_ARRAYS_TYPEMAPS(unsigned int, long, jlong, Uint, "[J") /* unsigned int[ANY] */
JAVA_ARRAYS_TYPEMAPS(long, int, jint, Long, "[I") /* long[ANY] */
JAVA_ARRAYS_TYPEMAPS(unsigned long, long, jlong, Ulong, "[J") /* unsigned long[ANY] */
JAVA_ARRAYS_TYPEMAPS(long long, long, jlong, Longlong, "[J") /* long long[ANY] */
JAVA_ARRAYS_TYPEMAPS(float, float, jfloat, Float, "[F") /* float[ANY] */
JAVA_ARRAYS_TYPEMAPS(double, double, jdouble, Double, "[D") /* double[ANY] */
%typecheck(SWIG_TYPECHECK_BOOL_ARRAY) /* Java boolean[] */
%typecheck(SWIG_TYPECHECK_INT8_ARRAY) /* Java byte[] */
signed char[ANY], signed char[]
%typecheck(SWIG_TYPECHECK_INT16_ARRAY) /* Java short[] */
unsigned char[ANY], unsigned char[],
%typecheck(SWIG_TYPECHECK_INT32_ARRAY) /* Java int[] */
unsigned short[ANY], unsigned short[],
%typecheck(SWIG_TYPECHECK_INT64_ARRAY) /* Java long[] */
unsigned int[ANY], unsigned int[],
unsigned long[ANY], unsigned long[],
long long[ANY], long long[]
%typecheck(SWIG_TYPECHECK_INT128_ARRAY) /* Java BigInteger[] */
unsigned long long[ANY], unsigned long long[]
%typecheck(SWIG_TYPECHECK_FLOAT_ARRAY) /* Java float[] */
%typecheck(SWIG_TYPECHECK_DOUBLE_ARRAY) /* Java double[] */
/* Arrays of proxy classes. The typemaps in this macro make it possible to treat an array of
* class/struct/unions as an array of Java classes.
* Use the following macro to use these typemaps for an array of class/struct/unions called name:
* JAVA_ARRAYSOFCLASSES(name) */
%define JAVA_ARRAYSOFCLASSES(ARRAYSOFCLASSES)
%typemap(jni) ARRAYSOFCLASSES[ANY] "jlongArray"
%typemap(jtype) ARRAYSOFCLASSES[ANY] "long[]"
%typemap(jstype) ARRAYSOFCLASSES[ANY] "$javaclassname[]"
%typemap(javain) ARRAYSOFCLASSES[ANY] "$javaclassname.cArrayUnwrap($javainput)"
%typemap(javaout) ARRAYSOFCLASSES[ANY] {
return $javaclassname.cArrayWrap($jnicall, $owner);
%typemap(in) ARRAYSOFCLASSES[] (jlong *jarr, jsize sz)
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
sz = JCALL1(GetArrayLength, jenv, $input);
jarr = JCALL2(GetLongArrayElements, jenv, $input, 0);
$1 = ($1_ltype) calloc(sz, sizeof($*1_ltype));
SWIG_JavaThrowException(jenv, SWIG_JavaOutOfMemoryError, "array memory allocation failed");
$1[i] = **($&1_ltype)(void *)&jarr[i];
%typemap(in) ARRAYSOFCLASSES[ANY] (jlong *jarr, jsize sz)
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
sz = JCALL1(GetArrayLength, jenv, $input);
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "incorrect array size");
jarr = JCALL2(GetLongArrayElements, jenv, $input, 0);
$1 = ($1_ltype) calloc(sz, sizeof($*1_ltype));
SWIG_JavaThrowException(jenv, SWIG_JavaOutOfMemoryError, "array memory allocation failed");
$1[i] = **($&1_ltype)(void *)&jarr[i];
%typemap(argout) ARRAYSOFCLASSES[ANY]
for (i=0; i<sz$argnum; i++) {
*($&1_ltype)(void *)&jarr$argnum[i] = &$1[i];
JCALL3(ReleaseLongArrayElements, jenv, $input, jarr$argnum, 0);
%typemap(out) ARRAYSOFCLASSES[ANY]
$result = JCALL1(NewLongArray, jenv, $1_dim0);
arr = JCALL2(GetLongArrayElements, jenv, $result, 0);
for (i=0; i<$1_dim0; i++) {
*($&1_ltype)(void *)&arr[i] = &$1[i];
JCALL3(ReleaseLongArrayElements, jenv, $result, arr, 0);
%typemap(freearg) ARRAYSOFCLASSES[ANY]
/* Add some code to the proxy class of the array type for converting between type used in
* JNI class (long[]) and type used in proxy class ( ARRAYSOFCLASSES[] ) */
%typemap(javacode) ARRAYSOFCLASSES %{
protected static long[] cArrayUnwrap($javaclassname[] arrayWrapper) {
long[] cArray = new long[arrayWrapper.length];
for (int i=0; i<arrayWrapper.length; i++)
cArray[i] = $javaclassname.getCPtr(arrayWrapper[i]);
protected static $javaclassname[] cArrayWrap(long[] cArray, boolean cMemoryOwn) {
$javaclassname[] arrayWrapper = new $javaclassname[cArray.length];
for (int i=0; i<cArray.length; i++)
arrayWrapper[i] = new $javaclassname(cArray[i], cMemoryOwn);
%enddef /* JAVA_ARRAYSOFCLASSES */
* Use the following to use these typemaps for an array of enums called name:
* %apply ARRAYSOFENUMS[ANY] { name[ANY] }; */
%typemap(jni) ARRAYSOFENUMS[ANY] "jintArray"
%typemap(jtype) ARRAYSOFENUMS[ANY] "int[]"
%typemap(jstype) ARRAYSOFENUMS[ANY] "int[]"
%typemap(javain) ARRAYSOFENUMS[ANY] "$javainput"
%typemap(javaout) ARRAYSOFENUMS[ANY] {
%typemap(in) ARRAYSOFENUMS[] (jint *jarr)
%{ if (!SWIG_JavaArrayInInt(jenv, &jarr, (int **)(void *)&$1, $input)) return $null; %}
%typemap(in) ARRAYSOFENUMS[ANY] (jint *jarr) {
if ($input && JCALL1(GetArrayLength, jenv, $input) != $1_size) {
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "incorrect array size");
if (!SWIG_JavaArrayInInt(jenv, &jarr, (int **)(void *)&$1, $input)) return $null;
%typemap(argout) ARRAYSOFENUMS[ANY]
%{ SWIG_JavaArrayArgoutInt(jenv, jarr$argnum, (int *)$1, $input); %}
%typemap(out) ARRAYSOFENUMS[ANY]
%{$result = SWIG_JavaArrayOutInt(jenv, (int *)$1, $1_dim0); %}
%typemap(freearg) ARRAYSOFENUMS[ANY]