ZFFramework
 
Loading...
Searching...
No Matches
JNIUtil.h
Go to the documentation of this file.
1
5
6#ifndef _JNIUtil_h_
7#define _JNIUtil_h_
8
9// ============================================================
11#ifndef _JNI_EXPORT
12 #ifdef _WIN32
13 #define _JNI_EXPORT __declspec(dllimport)
14 #else
15 #define _JNI_EXPORT __attribute__((visibility("default")))
16 #endif
17#endif
18
19// ============================================================
20#ifndef NEED_JNIUTIL
21 #if (defined(ANDROID) || defined(__ANDROID__) || defined(JNI_VERSION_1_1) || defined(JNIEXPORT) || defined(JNICALL))
25 #define NEED_JNIUTIL 1
26 #endif
27#endif
28
29#if NEED_JNIUTIL
30#include <cstdlib>
31#include <jni.h>
32
33#if defined(__cplusplus) || defined(c_plusplus)
34 #define _JNI_EXTERN_C extern "C"
35#else
36 #define _JNI_EXTERN_C
37#endif
38
39namespace JNIUtil {
40
41// ============================================================
42// wrapper
46class _JNI_EXPORT JNIString {
47public:
48 JNIString(void);
49 JNIString(JNIString const &ref);
50 JNIString(const char *s);
51 ~JNIString(void);
52 JNIString &operator = (JNIString const &ref);
53 JNIString &operator = (const char *s);
54 const char *c_str(void) const;
55 jsize length(void) const;
56 operator const char * (void) const;
57 JNIString &operator += (char c);
58 JNIString &operator += (const char *s);
59 void clear(void);
60public:
61 void *_d;
62};
63
64class JNIType;
68class _JNI_EXPORT JNIParamTypeContainer {
69public:
70 JNIParamTypeContainer(void);
71 ~JNIParamTypeContainer(void);
72private:
73 JNIParamTypeContainer(const JNIParamTypeContainer &ref);
74 JNIParamTypeContainer &operator = (const JNIParamTypeContainer &ref);
75public:
77 JNIParamTypeContainer &add(const JNIType &paramType);
78public:
80 void remove(jint i);
82 void removeAll(void);
83public:
85 jsize count(void) const;
87 const JNIType &get(jint i) const;
88public:
89 void *_d;
90};
91
92// ============================================================
93// common
97extern _JNI_EXPORT bool JNIInit(JavaVM *javaVM, jint version = JNI_VERSION_1_1);
98
102typedef JNIEnv *(*JNIGetJNIEnvCallback)(void);
106extern _JNI_EXPORT bool JNIInit(JNIGetJNIEnvCallback callback);
107
111extern _JNI_EXPORT JavaVM *JNIGetJavaVM(void);
112
116extern _JNI_EXPORT jint JNIGetDesiredVersion(void);
117
121extern _JNI_EXPORT JNIEnv *JNIGetJNIEnv(void);
122
130extern _JNI_EXPORT void JNIConvertClassNameToClassSig(JNIString &ret, const char *className);
132inline JNIString JNIConvertClassNameToClassSig(const char *className) {
133 JNIString ret;
134 JNIConvertClassNameToClassSig(ret, className);
135 return ret;
136}
137
144extern _JNI_EXPORT void JNIConvertClassNameFromClassSig(JNIString &ret, const char *classSig);
146inline JNIString JNIConvertClassNameFromClassSig(const char *className) {
147 JNIString ret;
148 JNIConvertClassNameFromClassSig(ret, className);
149 return ret;
150}
151
159extern _JNI_EXPORT void JNIConvertClassNameForFindClass(JNIString &ret, const char *className);
161inline JNIString JNIConvertClassNameForFindClass(const char *className) {
162 JNIString ret;
163 JNIConvertClassNameForFindClass(ret, className);
164 return ret;
165}
166
170#define JNIConstructorName "<init>"
171
172// ============================================================
173// JNI types
177class _JNI_EXPORT JNIType {
178public:
179 typedef enum {
180 T_boolean,
181 T_byte,
182 T_char,
183 T_short,
184 T_int,
185 T_long,
186 T_float,
187 T_double,
188 T_void,
189 T_object,
190 T_array,
191 } Type;
192
193public:
197 JNIType(void);
206 JNIType(JNIType::Type type,
207 const char *classNameOrArrayElementTypeId = NULL);
208 JNIType(const JNIType &ref);
209 JNIType &operator = (const JNIType &ref);
210
211public:
215 void setType(JNIType::Type type,
216 const char *classNameOrArrayElementTypeId = NULL);
220 JNIType::Type getType(void) const;
221
222public:
230 const char *getId(void) const;
231
232public:
234 static const JNIType &S_boolean(void);
236 static const JNIType &S_byte(void);
238 static const JNIType &S_char(void);
240 static const JNIType &S_short(void);
242 static const JNIType &S_int(void);
244 static const JNIType &S_long(void);
246 static const JNIType &S_float(void);
248 static const JNIType &S_double(void);
250 static const JNIType &S_void(void);
251
252public:
256 static inline JNIType S_object(const char *className) {
257 return JNIType(JNIType::T_object, className);
258 }
262 static inline JNIType S_array(const JNIType &type) {
263 return JNIType(JNIType::T_array, type.getId());
264 }
265
266public:
268 static const JNIType &S_object_Class(void);
270 static const JNIType &S_object_Object(void);
272 static const JNIType &S_object_String(void);
273public:
275 static const JNIType &S_array_Class(void);
277 static const JNIType &S_array_Object(void);
279 static const JNIType &S_array_String(void);
280
281private:
282 JNIType::Type _type;
283 JNIString *_id;
284};
285
286// ============================================================
287// signature utilities
302extern _JNI_EXPORT JNIString JNIGetMethodSig(
303 const JNIType &returnType
304 , const JNIParamTypeContainer &paramTypeList
305 );
307extern _JNI_EXPORT JNIString JNIGetMethodSig(
308 const JNIType &returnType
309 , const JNIType *paramTypeList
310 , jsize paramCount
311 );
312
314#define _JNI_METHOD_DECLARE_BEGIN(OwnerClassId, ReturnType, MethodName, ...) \
315 _JNI_EXTERN_C JNIEXPORT ReturnType JNICALL Java_##OwnerClassId##_##MethodName( \
316 JNIEnv *jniEnv, jclass jniCls, ##__VA_ARGS__)
346#define JNI_METHOD_DECLARE_BEGIN(OwnerClassId, ReturnType, MethodName, ...) \
347 _JNI_METHOD_DECLARE_BEGIN(OwnerClassId, ReturnType, MethodName, ##__VA_ARGS__)
349#define JNI_METHOD_DECLARE_END()
350
354#define JNI_ONLOAD_ENTRY(paramJavaVM, paramReserved) \
355 _JNI_EXTERN_C JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* paramJavaVM, void* paramReserved)
356
357// ============================================================
358// pointer converter
362#ifndef JNIPointerUseLong
363 #define JNIPointerUseLong 1
364#endif
368#ifndef JNIPointer
369 #if JNIPointerUseLong
370 #define JNIPointer jlong
371 #else
372 #define JNIPointer jbyteArray
373 #endif
374#endif
378#ifndef JNIPointerInvalid
379 #if JNIPointerUseLong
380 #define JNIPointerInvalid 0
381 #else
382 #define JNIPointerInvalid NULL
383 #endif
384#endif
388#ifndef JNIPointerJNIType
389 #if JNIPointerUseLong
390 #define JNIPointerJNIType JNIType::S_long()
391 #else
392 #define JNIPointerJNIType JNIType::S_array(JNIType::S_byte())
393 #endif
394#endif
395
396jbyteArray _JNIConvertPointerToJNITypeAction(JNIEnv *jniEnv, void *p);
397void *_JNIConvertPointerFromJNITypeAction(JNIEnv *jniEnv, jbyteArray d);
398
399#if JNIPointerUseLong
400 #define _JNIConvertPointerToJNIType(jniEnv, p) (JNIPointer)(p)
401 #define _JNIConvertPointerFromJNIType(jniEnv, p) (void *)(p)
402#else
403 #define _JNIConvertPointerToJNIType(jniEnv, p) _JNIConvertPointerToJNITypeAction(jniEnv, p)
404 #define _JNIConvertPointerFromJNIType(jniEnv, d) _JNIConvertPointerFromJNITypeAction(jniEnv, d)
405#endif
406
416#ifndef JNIConvertPointerToJNIType
417 #define JNIConvertPointerToJNIType(jniEnv, p) _JNIConvertPointerToJNIType(jniEnv, p)
418#endif
422#ifndef JNIConvertPointerFromJNIType
423 #define JNIConvertPointerFromJNIType(jniEnv, d) _JNIConvertPointerFromJNIType(jniEnv, d)
424#endif
425
426// ============================================================
427// local/global ref cleaner
429namespace JNIUtilPrivate {
430 class _JNI_EXPORT LocalRefDel {
431 public:
432 LocalRefDel(jobject obj)
433 : objSaved(obj)
434 {
435 }
436 ~LocalRefDel(void) {
437 if(this->objSaved) {
438 JNIGetJNIEnv()->DeleteLocalRef(this->objSaved);
439 }
440 }
441 public:
442 jobject objSaved;
443 };
444 class _JNI_EXPORT GlobalRefDel {
445 public:
446 GlobalRefDel(jobject obj)
447 : objSaved(obj)
448 {
449 }
450 ~GlobalRefDel(void) {
451 if(this->objSaved) {
452 JNIGetJNIEnv()->DeleteGlobalRef(this->objSaved);
453 }
454 }
455 public:
456 jobject objSaved;
457 };
458 #define _JNIUtil_uniqueName(name) _JNIUtil_uniqueName2(name, __LINE__)
459 #define _JNIUtil_uniqueName2(name, line) _JNIUtil_uniqueName3(name, line)
460 #define _JNIUtil_uniqueName3(name, line) _JNIUtil_##name##_##line
461} // namespace JNIUtilPrivate
463
465#define JNILineDeleteLocalRef(obj_) \
466 (JNIUtilPrivate::LocalRefDel(obj_).objSaved)
468#define JNILineDeleteGlobalRef(obj_) \
469 (JNIUtilPrivate::GlobalRefDel(obj_).objSaved)
470
472#define JNIBlockedDeleteLocalRef(obj_) \
473 JNIUtilPrivate::LocalRefDel _JNIUtil_uniqueName(jniRef)(obj_)
475#define JNIBlockedDeleteGlobalRef(obj_) \
476 JNIUtilPrivate::GlobalRefDel _JNIUtil_uniqueName(jniRef)(obj_)
477
478} // namespace JNIUtil
479#endif // #if NEED_JNIUTIL
480
481#endif // #ifndef _JNIUtil_h_
482
#define _JNI_EXPORT
used to export symbols
Definition JNIUtil.h:15
ZFUIMargin & operator+=(ZFUIMargin &v0, const ZFUIMargin &v1)
v0 += v1
Definition ZFUITypeDef.h:325