aboutsummaryrefslogtreecommitdiffstats
path: root/CNativeCode/jnitools.h
blob: 1af87223bb16dde720e01178e7781830e61aa9d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

#ifndef JNITOOLS
	#define JNITOOLS

	#include <stdio.h>
	#include <stdlib.h>
	#include <stdarg.h>

	#ifdef _WIN32_
		#include "winstuff.h"
	#endif

	#include <jni.h>

	#ifndef LIBAPIENTRY
		#define LIBAPIENTRY
	#endif
	#ifndef LIBAPI
		#define LIBAPI
	#endif

	#ifndef USE_64BIT_POINTER
		typedef int  PointerHolder;
	#else
		typedef long PointerHolder;
	#endif

	typedef enum {
		T_NOPE = 0,
		T_VOID,
		T_BOOLEAN,
		T_BYTE,
		T_CHAR,
		T_SHORT,
		T_INT,
		T_LONG,
		T_FLOAT,
		T_DOUBLE,
		T_OBJECT,
		T_ARRAY_TYPE_BORDER,
		T_BOOLEAN_ARRAY,
		T_BYTE_ARRAY,
		T_CHAR_ARRAY,
		T_SHORT_ARRAY,
		T_INT_ARRAY,
		T_LONG_ARRAY,
		T_FLOAT_ARRAY,
		T_DOUBLE_ARRAY,
		T_OBJECT_ARRAY
	} JavaBasicType;

	/* Throw an exception by name */
	LIBAPI void LIBAPIENTRY jnitoolsThrowByName(JNIEnv *env, const char *name, const char *msg, ...);

	/* Translates a Java string to a C string using the String.getBytes
	 * method, which uses default local encoding.
	 */
	LIBAPI char * LIBAPIENTRY jnitoolsGetJavaString(JNIEnv *env, jstring jstr);

	/**
	 * signature: e.g. "(II[I)V" for "void <name>(int, int, int[])"
	 *
	 * argIndex:  0-n argument index of the method signature
	 *            -1  return type
	 */
	LIBAPI JavaBasicType LIBAPIENTRY jnitoolsGetJavaBasicType(JNIEnv *env, const char * signature, int argIndex, const char * errText);

	LIBAPI int LIBAPIENTRY jnitoolsGetArgNumber(JNIEnv *env, 
					     const char * signature,
					     const char * errText);
	/**
	 * e.g.: int a[4];
	 *       jarray arr = jnitoolsNativeArray2JavaArray(env, T_INT_ARRAY,
	 *	 				            a, 4);
	 */
	LIBAPI jarray LIBAPIENTRY jnitoolsNativeArray2JavaArray (JNIEnv *env, 
					     JavaBasicType jbt, 
					     void *data, int dataArrayLen, 
					     const char * errText, int arg);

	/**
	 * e.g.: int * a[2];
	 *       a[0] = calloc (4, sizeof(int));
	 *       a[1] = calloc (4, sizeof(int));
	 *       jarray arr = jnitoolsNativePtrArray2JavaArray(env, T_INT_ARRAY,
	 *	 				            a, 2, 8);
	 */
	LIBAPI jarray LIBAPIENTRY jnitoolsNativePtrArray2JavaArray(JNIEnv *env, 
						 JavaBasicType jbt, 
                                                 void *data[/*pointerNumber*/], 
						 int pointerNumber, 
						 int dataArrayLen, 
						 const char * errText, int arg);


	/**
	 * e.g.: int a[4];
	 *       jarray arr = ...
	 *       jnitoolsReleaseJavaArray2NativeArray(env, arr, T_INT_ARRAY,
	 *	 				      a, 4);
	 */
	LIBAPI void LIBAPIENTRY jnitoolsReleaseJavaArray2NativeArray(
						 JNIEnv *env, 
						 jarray arr, JavaBasicType jbt, 
						 void *data, int dataArrayLen, 
						 const char * errText, int arg);

	/**
	 * e.g.: int * a[2];
	 *       a[0] = calloc (4, sizeof(int));
	 *       a[1] = calloc (4, sizeof(int));
	 *       jarray arr = ...
	 *       jnitoolsReleaseJavaArray2NativeArrayPtr (env, arr, T_INT_ARRAY,
	 *	 				          a, 2, 8);
	 */
	LIBAPI void LIBAPIENTRY jnitoolsReleaseJavaArray2NativeArrayPtr (
						 JNIEnv *env, 
						 jarray arr, 
						 JavaBasicType jbt, 
						 void *data[/*pointerNumber*/], 
						 int pointerNumber, 
						 int dataArrayLen, 
						 const char * errText, int arg);

	LIBAPI void * LIBAPIENTRY jnitoolsCreateNativeArray (JavaBasicType jbt, 
							    int dataArrayLen, 
							    const char * errText);

	/**
	 * It is very important that OnLoad returns
	 * the right JNI Version Number ... !!
	 */
	JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM * vm, void *reserved);
	JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved);

#endif