1 #include "jniHelpers.h" 2 3 GdxPool::GdxPool(const char * const &poolField, const char * const &typeName, const char * const &tempField, 4 const char * const &poolClazz, 5 const char * const &obtainName, const char * const &obtainSig, 6 const char * const &freeName, const char * const &freeSig) 7 : env(0), cls(0), pool(0), tmp(0), poolField(poolField), typeName(typeName), tempField(tempField), poolClazz(poolClazz), obtainName(obtainName), obtainSig(obtainSig), freeName(freeName), freeSig(freeSig) 8 { } 9 10 GdxPool::~GdxPool() { 11 if (pool) 12 env->DeleteGlobalRef(pool); 13 if (tmp) 14 env->DeleteGlobalRef(tmp); 15 if (cls) 16 env->DeleteGlobalRef(cls); 17 } 18 19 void GdxPool::setEnv(JNIEnv * const &e) { 20 env = e; 21 cls = (jclass)env->NewGlobalRef(env->FindClass(typeName)); 22 jfieldID poolFieldID = env->GetStaticFieldID(cls, poolField, poolClazz); 23 pool = env->NewGlobalRef(env->GetStaticObjectField(cls, poolFieldID)); 24 jclass poolClass = env->GetObjectClass(pool); 25 obtainMethod = env->GetMethodID(poolClass, obtainName, obtainSig); 26 freeMethod = env->GetMethodID(poolClass, freeName, freeSig); 27 env->DeleteLocalRef(poolClass); 28 29 if (typeName && tempField) { 30 jfieldID tempFieldID = env->GetStaticFieldID(cls, tempField, typeName); 31 tmp = env->NewGlobalRef(env->GetStaticObjectField(cls, tempFieldID)); 32 } 33 } 34 35 jobject GdxPool::obtain(JNIEnv * const &e) { 36 if (!env) setEnv(e); 37 jobject result = env->CallObjectMethod(pool, obtainMethod); 38 return result; 39 } 40 41 void GdxPool::free(jobject &obj) { 42 env->CallVoidMethod(pool, freeMethod, obj); 43 } 44 45 jobject GdxPool::temp(JNIEnv * const &e) { 46 if (!env) setEnv(e); 47 return tmp; 48 } 49 50 51 52 53 GdxPooledObject::GdxPooledObject(JNIEnv * const &e, GdxPool * const &pool, const bool &autoFree) 54 : pool(pool), autoFree(autoFree), obj(pool->obtain(e)) { 55 } 56 GdxPooledObject::GdxPooledObject(JNIEnv * const &e, GdxPool &pool, const bool &autoFree) 57 : pool(&pool), autoFree(autoFree), obj(pool.obtain(e)) { 58 } 59 GdxPooledObject::~GdxPooledObject() { 60 if (autoFree) 61 free(); 62 } 63 void GdxPooledObject::free() { 64 pool->free(obj); 65 }