ZFFramework
 
Loading...
Searching...
No Matches
ZFImpl_sys_Android_def.h
1#ifndef _ZFI_ZFImpl_sys_Android_def_h_
2#define _ZFI_ZFImpl_sys_Android_def_h_
3
4#include "../ZFImpl_ZF_impl.h"
5#include "ZFImpl_sys_Android.h"
6#include "ZFCore.h"
7
8#if ZF_ENV_sys_Android
9
10// #define _ZFP_ZFImpl_sys_Android_JNI_DEBUG 1
11
12#if _ZFP_ZFImpl_sys_Android_JNI_DEBUG
13 #include <jni.h>
14 #include <android/log.h>
15 #define JNIUtilWrap_Log(fmt, ...) \
16 ((void)__android_log_print(ANDROID_LOG_ERROR, "test", fmt, ##__VA_ARGS__))
17
18 #define JNIUtilWrap_Enable 1
19
20 inline const char *_JNIUtilWrap_file(const char *filePath) {
21 const char *ret = filePath;
22 while(*filePath) {
23 if(*filePath == '/' || *filePath == '\\') {
24 ret = filePath + 1;
25 }
26 ++filePath;
27 }
28 return ret;
29 }
30 template<typename T_Obj>
31 T_Obj _JNIUtilWrap_LogAction(
32 T_Obj obj
33 , const char *callerFile, const char *callerFunction, int callerLine
34 , const char *actionName, const char *type
35 ) {
36 JNIUtilWrap_Log("[%s %s (%s)] %s %s %s",
37 _JNIUtilWrap_file(callerFile), callerFunction, callerLine,
38 type, actionName, (const void *)obj);
39 return obj;
40 }
41
42 #if 1
43 #define JNIUtilWrap_Created(obj, callerFile, callerFunction, callerLine, actionName) \
44 _JNIUtilWrap_LogAction(obj, callerFile, callerFunction, callerLine, actionName, "Create")
45 #endif
46 #ifndef JNIUtilWrap_Released
47 #define JNIUtilWrap_Released(obj, callerFile, callerFunction, callerLine, actionName) \
48 _JNIUtilWrap_LogAction(obj, callerFile, callerFunction, callerLine, actionName, "Release")
49 #endif
50 #ifndef JNIUtilWrap_FuncCalled
51 #if 1
52 #include <ctime>
53 class _JNIUtilWrap_FuncCalledTimeLog {
54 public:
55 _JNIUtilWrap_FuncCalledTimeLog(const char *callerFunction)
56 : _callerFunction(callerFunction)
57 , _startTime(clock())
58 {
59 }
60 ~_JNIUtilWrap_FuncCalledTimeLog(void) {
61 JNIUtilWrap_Log("[%s] %s",
62 _callerFunction,
63 (double)((double)(clock() - _startTime) / CLOCKS_PER_SEC));
64 }
65 private:
66 const char *_callerFunction;
67 clock_t _startTime;
68 };
69 #define JNIUtilWrap_FuncCalled(callerFile, callerFunction, callerLine, actionName) \
70 _JNIUtilWrap_FuncCalledTimeLog(callerFunction), \
71 _JNIUtilWrap_LogAction(NULL, callerFile, callerFunction, callerLine, actionName, "FuncCall")
72 #else
73 #define JNIUtilWrap_FuncCalled(callerFile, callerFunction, callerLine, actionName) \
74 _JNIUtilWrap_LogAction(NULL, callerFile, callerFunction, callerLine, actionName, "FuncCall")
75 #endif
76 #endif
77 #ifndef JNIUtilWrap_CallbackCalled
78 #define JNIUtilWrap_CallbackCalled(className, actionName) \
79 JNIUtilWrap_Log("[%s %s (%s)] Callback %s %s", _JNIUtilWrap_file(__FILE__), __FUNCTION__, __LINE__, className, actionName);
80 #endif
81#endif // #if _ZFP_ZFImpl_sys_Android_JNI_DEBUG
82
84using namespace JNIUtil;
85using namespace AndroidJNIUtil;
86
88
89#define ZFImpl_sys_Android_ROOT_JNI_ID com_ZFFramework
90#define ZFImpl_sys_Android_ROOT_JNI_NAME "com.ZFFramework"
91
92// usually used when declare JNI method
93// note: when class name contains `_`, it must be replaced with `_1`
94#define ZFImpl_sys_Android_JNI_ID(clsId) ZFM_CAT(ZFM_CAT(ZFImpl_sys_Android_ROOT_JNI_ID, _), clsId)
95// usually used when find class
96#define ZFImpl_sys_Android_JNI_NAME(clsName) ZFImpl_sys_Android_ROOT_JNI_NAME "." ZFM_TOSTRING(clsName)
97
98// ============================================================
99// typeid
100ZFTYPEID_ACCESS_ONLY_DECLARE(ZFLIB_ZF_impl, ZFAndroid_JNIGlobalRef, JNIGlobalRef)
101ZFTYPEID_ACCESS_ONLY_REG(ZFLIB_ZF_impl, ZFAndroid_JNIGlobalRef, JNIGlobalRef)
103
104ZFTYPEID_ALIAS_DECLARE(ZFLIB_ZF_impl, ZFAndroid_JNIGlobalRef, JNIGlobalRef, ZFAndroid_jobject, jobject)
105ZFTYPEID_ALIAS_REG(ZFLIB_ZF_impl, ZFAndroid_JNIGlobalRef, JNIGlobalRef, ZFAndroid_jobject, jobject)
106
107// ============================================================
108// Object
109#define ZFImpl_sys_Android_JNI_ID_Object java_lang_Object
110#define ZFImpl_sys_Android_JNI_NAME_Object "java.lang.Object"
111extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassObject(void);
112
113// ============================================================
114// String
115#define ZFImpl_sys_Android_JNI_ID_String java_lang_String
116#define ZFImpl_sys_Android_JNI_NAME_String "java.lang.String"
117extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassString(void);
118
119// ============================================================
120// Number
121#define ZFImpl_sys_Android_JNI_ID_Number java_lang_Number
122#define ZFImpl_sys_Android_JNI_NAME_Number "java.lang.Number"
123extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassNumber(void);
124
125// ============================================================
126// Boolean
127#define ZFImpl_sys_Android_JNI_ID_Boolean java_lang_Boolean
128#define ZFImpl_sys_Android_JNI_NAME_Boolean "java.lang.Boolean"
129extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassBoolean(void);
130
131extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_jobjectBooleanTRUE(void);
132extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_jobjectBooleanFALSE(void);
133inline jobject ZFImpl_sys_Android_newBoolean(ZF_IN jboolean v) {
134 if(v) {
135 return ZFImpl_sys_Android_jobjectBooleanTRUE();
136 }
137 else {
138 return ZFImpl_sys_Android_jobjectBooleanFALSE();
139 }
140}
141
142// ============================================================
143// Byte
144#define ZFImpl_sys_Android_JNI_ID_Byte java_lang_Byte
145#define ZFImpl_sys_Android_JNI_NAME_Byte "java.lang.Byte"
146extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassByte(void);
147extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_newByte(ZF_IN jbyte v);
148
149// ============================================================
150// Character
151#define ZFImpl_sys_Android_JNI_ID_Character java_lang_Character
152#define ZFImpl_sys_Android_JNI_NAME_Character "java.lang.Character"
153extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassCharacter(void);
154extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_newCharacter(ZF_IN jchar v);
155
156// ============================================================
157// Short
158#define ZFImpl_sys_Android_JNI_ID_Short java_lang_Short
159#define ZFImpl_sys_Android_JNI_NAME_Short "java.lang.Short"
160extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassShort(void);
161extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_newShort(ZF_IN jshort v);
162
163// ============================================================
164// Integer
165#define ZFImpl_sys_Android_JNI_ID_Integer java_lang_Integer
166#define ZFImpl_sys_Android_JNI_NAME_Integer "java.lang.Integer"
167extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassInteger(void);
168extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_newInteger(ZF_IN jint v);
169
170// ============================================================
171// Long
172#define ZFImpl_sys_Android_JNI_ID_Long java_lang_Long
173#define ZFImpl_sys_Android_JNI_NAME_Long "java.lang.Long"
174extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassLong(void);
175extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_newLong(ZF_IN jlong v);
176
177// ============================================================
178// Float
179#define ZFImpl_sys_Android_JNI_ID_Float java_lang_Float
180#define ZFImpl_sys_Android_JNI_NAME_Float "java.lang.Float"
181extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassFloat(void);
182extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_newFloat(ZF_IN jfloat v);
183
184// ============================================================
185// Double
186#define ZFImpl_sys_Android_JNI_ID_Double java_lang_Double
187#define ZFImpl_sys_Android_JNI_NAME_Double "java.lang.Double"
188extern ZFLIB_ZF_impl jclass ZFImpl_sys_Android_jclassDouble(void);
189extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_newDouble(ZF_IN jdouble v);
190
191// ============================================================
192// ZFAndroidLog
193#define ZFImpl_sys_Android_JNI_ID_ZFAndroidLog ZFImpl_sys_Android_JNI_ID(NativeUtil_ZFAndroidLog)
194#define ZFImpl_sys_Android_JNI_NAME_ZFAndroidLog ZFImpl_sys_Android_JNI_NAME(NativeUtil.ZFAndroidLog)
195
196// ============================================================
197// jclass util
198zfclassLikePOD ZFLIB_ZF_impl _ZFP_ZFImpl_sys_Android_jclass {
199public:
200 _ZFP_ZFImpl_sys_Android_jclass(
201 ZF_IN const zfchar *funcName
202 , ZF_IN const zfchar *className
203 ) {
204 JNIEnv *jniEnv = JNIGetJNIEnv();
205 cls = JNIUtilFindClass(jniEnv, JNIConvertClassNameForFindClass(className).c_str());
206 ZFCoreAssertWithMessage(cls != NULL, "no class named: %s, for jclass: %s", className, funcName);
207 JNIBlockedDeleteLocalRef(cls);
208 cls = (jclass)JNIUtilNewGlobalRef(jniEnv, cls);
209 }
210 ~_ZFP_ZFImpl_sys_Android_jclass(void) {
211 if(cls != NULL) {
212 JNIUtilDeleteGlobalRef(JNIGetJNIEnv(), cls);
213 }
214 }
215public:
216 jclass cls;
217};
218#define ZFImpl_sys_Android_jclass_DEFINE(funcName, className) \
219 jclass funcName(void) { \
220 static _ZFP_ZFImpl_sys_Android_jclass d(#funcName, className); \
221 return d.cls; \
222 }
223
224// ============================================================
225// utils
229#ifndef JNIConvertZFObjectToJNIType
230#define JNIConvertZFObjectToJNIType(jniEnv, p) JNIConvertPointerToJNIType((jniEnv), zfcast(ZFObject *, (p)))
231#endif
235#ifndef JNIConvertZFObjectFromJNIType
236#define JNIConvertZFObjectFromJNIType(jniEnv, d) zfany((ZFObject *)JNIConvertPointerFromJNIType((jniEnv), (d)))
237#endif
238
239// ============================================================
240extern ZFLIB_ZF_impl void ZFImpl_sys_Android_zfstringFromStringT(
242 , ZF_IN jobject jstr
243 );
244inline zfstring ZFImpl_sys_Android_zfstringFromString(ZF_IN jobject jstr) {
245 zfstring ret;
246 ZFImpl_sys_Android_zfstringFromStringT(ret, jstr);
247 return ret;
248}
249// note returned object must be deleted by DeleteLocalRef
250extern ZFLIB_ZF_impl jobject ZFImpl_sys_Android_zfstringToString(ZF_IN const zfchar *s);
251
252// ============================================================
253extern ZFLIB_ZF_impl zfstring ZFImpl_sys_Android_stackTrace(void);
254extern ZFLIB_ZF_impl void ZFImpl_sys_Android_stackTracePrint(void);
255extern ZFLIB_ZF_impl void ZFImpl_sys_Android_objectInfoT(
256 ZF_OUT zfstring &ret
257 , ZF_IN jobject nativeObject
258 );
259inline zfstring ZFImpl_sys_Android_objectInfo(ZF_IN jobject nativeObject) {
260 zfstring ret;
261 ZFImpl_sys_Android_objectInfoT(ret, nativeObject);
262 return ret;
263}
264
266#endif // #if ZF_ENV_sys_Android
267#endif // #ifndef _ZFI_ZFImpl_sys_Android_def_h_
268
Android's JNI utilities.
header file for ZFCore module
#define ZFCoreAssertWithMessage(exp, fmt,...)
ZFCoreAssert with custom message
Definition ZFCoreLog_CommonLog.h:71
_ZFT_t_zfchar zfchar
char wrapper
Definition ZFCoreTypeDef_CharType.h:17
#define zfclassLikePOD
shows the class is not a POD type, but you may use it like a POD except memset it to 0
Definition ZFCoreTypeDef_ClassType.h:41
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define ZF_OUT
dummy macro that shows the param used as required output
Definition ZFCoreTypeDef_ClassType.h:188
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:196
#define ZFOUTPUT_TYPE_DECLARE(ZFLIB_, T_Type)
see ZFOUTPUT_TYPE
Definition ZFCoreTypeDef_OtherType.h:244
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
global header for ZF_impl module
#define ZFLIB_ZF_impl
used to export symbols
Definition ZFImpl_ZF_impl.h:15
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
#define ZFTYPEID_ALIAS_DECLARE(ZFLIB_, AliasToTypeName, AliasToType, TypeName, Type)
see ZFTYPEID_DECLARE
Definition ZFTypeIdDeclare.h:219
#define ZFTYPEID_ACCESS_ONLY_REG(ZFLIB_, TypeName, Type,...)
see ZFTYPEID_DECLARE
Definition ZFTypeIdDeclare.h:205
#define ZFTYPEID_ACCESS_ONLY_DECLARE(ZFLIB_, TypeName, Type)
see ZFTYPEID_DECLARE
Definition ZFTypeIdDeclare.h:195
#define ZFTYPEID_ALIAS_REG(ZFLIB_, AliasToTypeName, AliasToType, TypeName, Type,...)
see ZFTYPEID_DECLARE
Definition ZFTypeIdDeclare.h:228