1 #ifndef jniHelpers_H 2 #define jniHelpers_H 3 4 #include <jni.h> 5 6 #ifndef GDXPOOL_POOLCLAZZ 7 #define GDXPOOL_POOLCLAZZ "Lcom/badlogic/gdx/utils/Pool;" 8 #endif 9 10 #ifndef GDXPOOL_OBTAINFUNC 11 #define GDXPOOL_OBTAINFUNC "obtain" 12 #endif 13 14 #ifndef GDXPOOL_OBTAINSIG 15 #define GDXPOOL_OBTAINSIG "()Ljava/lang/Object;" 16 #endif 17 18 #ifndef GDXPOOL_FREEFUNC 19 #define GDXPOOL_FREEFUNC "free" 20 #endif 21 22 #ifndef GDXPOOL_FREESIG 23 #define GDXPOOL_FREESIG "(Ljava/lang/Object;)V" 24 #endif 25 26 27 struct GdxPool { 28 const char * const &poolField; 29 const char * const &typeName; 30 const char * const &tempField; 31 const char * const &poolClazz; 32 const char * const &obtainName; 33 const char * const &obtainSig; 34 const char * const &freeName; 35 const char * const &freeSig; 36 37 JNIEnv *env; 38 jclass cls; 39 jobject pool; 40 jmethodID obtainMethod; 41 jmethodID freeMethod; 42 jobject tmp; 43 44 /** eg: if you have in java: 45 * class CommonJNI { 46 * public static com.xxx.a.Pool<com.xxx.b.Clazz> poolClazz; 47 * public static com.xxx.b.Clazz tempClazz; 48 * } 49 * then construct using: 50 * GdxPool(jenv, jclass("CommonJNI"), "poolClazz", "Lcom/xxx/b/Clazz;", "tempClazz"); */ 51 GdxPool(const char * const &poolField, const char * const &typeName = 0, const char * const &tempField = 0, 52 const char * const &poolClazz = GDXPOOL_POOLCLAZZ, 53 const char * const &obtainName = GDXPOOL_OBTAINFUNC, const char * const &obtainSig = GDXPOOL_OBTAINSIG, 54 const char * const &freeName = GDXPOOL_FREEFUNC, const char * const &freeSig = GDXPOOL_FREESIG); 55 virtual ~GdxPool(); 56 void setEnv(JNIEnv * const &env); 57 /** Obtain a jobject from the pool */ 58 jobject obtain(JNIEnv * const &env); 59 /** Free a jobject back to the pool */ 60 void free(jobject &obj); 61 /** Get the temp instance (if available) */ 62 jobject temp(JNIEnv * const &e); 63 }; 64 65 struct GdxPooledObject { 66 GdxPool * const &pool; 67 jobject obj; 68 const bool autoFree; 69 70 GdxPooledObject(JNIEnv * const &e, GdxPool * const &pool, const bool &autoFree); 71 GdxPooledObject(JNIEnv * const &e, GdxPool &pool, const bool &autoFree); 72 virtual ~GdxPooledObject(); 73 74 void free(); 75 }; 76 77 #endif //jniHelpers_H