• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "jniHelpers.h"
2 
GdxPool(const char * const & poolField,const char * const & typeName,const char * const & tempField,const char * const & poolClazz,const char * const & obtainName,const char * const & obtainSig,const char * const & freeName,const char * const & freeSig)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 
~GdxPool()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 
setEnv(JNIEnv * const & e)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 
obtain(JNIEnv * const & e)35 jobject GdxPool::obtain(JNIEnv * const &e) {
36 	if (!env) setEnv(e);
37 	jobject result = env->CallObjectMethod(pool, obtainMethod);
38 	return result;
39 }
40 
free(jobject & obj)41 void GdxPool::free(jobject &obj) {
42 	env->CallVoidMethod(pool, freeMethod, obj);
43 }
44 
temp(JNIEnv * const & e)45 jobject GdxPool::temp(JNIEnv * const &e) {
46 	if (!env) setEnv(e);
47 	return tmp;
48 }
49 
50 
51 
52 
GdxPooledObject(JNIEnv * const & e,GdxPool * const & pool,const bool & autoFree)53 GdxPooledObject::GdxPooledObject(JNIEnv * const &e, GdxPool * const &pool, const bool &autoFree)
54 	: pool(pool), autoFree(autoFree), obj(pool->obtain(e)) {
55 }
GdxPooledObject(JNIEnv * const & e,GdxPool & pool,const bool & autoFree)56 GdxPooledObject::GdxPooledObject(JNIEnv * const &e, GdxPool &pool, const bool &autoFree)
57 	: pool(&pool), autoFree(autoFree), obj(pool.obtain(e)) {
58 }
~GdxPooledObject()59 GdxPooledObject::~GdxPooledObject() {
60 	if (autoFree)
61 		free();
62 }
free()63 void GdxPooledObject::free() {
64 	pool->free(obj);
65 }