• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Provides com.badlogic.gdx.utils.Pool, to use:"
2  * 1. Create a Pool, ie:
3  * %pragma(java) jniclasscode=%{
4  *	public static final Pool<SomeJavaClass> SomeJavaClassPool = new Pool<SomeJavaClass>() {
5  * 		@Override
6  *		protected SomeJavaClass newObject() {
7  *			return new SomeJavaClass();
8  *		}
9  *	};
10  * }
11  * 2. Obtain a pooled object, ie:
12  * %typemap(...) ... {
13  * 		somevar = gdx_takePoolObject(jenv, "SomeJavaClassPool");
14  * }
15  * 3. Release a pooled object, ie:
16  * gdx_releasePoolObject(jenv, "SomeJavaClassPool", somevar);
17  * 4. Or let it be released when out of scope, ie:
18  * gdxPoolAutoRelease autoRelease_somevar(jenv, "SomeJavaClassPool", somevar);
19  */
20 %define CREATE_POOLED_METHODS(JTYPE, CLAZZ)
21 %fragment("gdxPoolMethods##JTYPE", "header") {
22 
23 	/* Gets a global ref to the temp class.  Do not release this. */
JTYPE(JNIEnv * jenv)24 	SWIGINTERN inline jclass gdx_getTempClass##JTYPE(JNIEnv * jenv) {
25 	  static jclass cls = NULL;
26 	  if (cls == NULL) {
27 		cls = (jclass) jenv->NewGlobalRef(jenv->FindClass(CLAZZ));
28 	  }
29 	  return cls;
30 	}
31 
JTYPE(JNIEnv * jenv,const char * poolName)32 	SWIGINTERN inline jobject gdx_takePoolObject##JTYPE(JNIEnv * jenv, const char * poolName) {
33 	  jclass tempClass = gdx_getTempClass##JTYPE(jenv);
34 
35 	  static jfieldID poolField = NULL;
36 	  if (poolField == NULL) {
37 		poolField = jenv->GetStaticFieldID(tempClass, poolName, "Lcom/badlogic/gdx/utils/Pool;");
38 	  }
39 
40 	  jobject poolObject = jenv->GetStaticObjectField(tempClass, poolField);
41 	  jclass poolClass = jenv->GetObjectClass(poolObject);
42 
43 	  static jmethodID obtainMethod = NULL;
44 	  if (obtainMethod == NULL) {
45 		obtainMethod = (jmethodID) jenv->GetMethodID(poolClass, "obtain", "()Ljava/lang/Object;");
46 	  }
47 
48 	  jobject ret = jenv->CallObjectMethod(poolObject, obtainMethod);
49 
50 	  jenv->DeleteLocalRef(poolObject);
51 	  jenv->DeleteLocalRef(poolClass);
52 
53 	  return ret;
54 	}
55 
JTYPE(JNIEnv * jenv,const char * poolName,jobject obj)56 	SWIGINTERN inline void gdx_releasePoolObject##JTYPE(JNIEnv * jenv, const char * poolName, jobject obj) {
57 	  jclass tempClass = gdx_getTempClass##JTYPE(jenv);
58 
59 	  static jfieldID poolField = NULL;
60 	  if (poolField == NULL) {
61 		poolField = jenv->GetStaticFieldID(tempClass, poolName, "Lcom/badlogic/gdx/utils/Pool;");
62 	  }
63 
64 	  jobject poolObject = jenv->GetStaticObjectField(tempClass, poolField);
65 	  jclass poolClass = jenv->GetObjectClass(poolObject);
66 
67 	  static jmethodID freeMethod = NULL;
68 	  if (freeMethod == NULL) {
69 		freeMethod = (jmethodID) jenv->GetMethodID(poolClass, "free", "(Ljava/lang/Object;)V");
70 	  }
71 
72 	  jenv->CallVoidMethod(poolObject, freeMethod, obj);
73 
74 	  jenv->DeleteLocalRef(poolObject);
75 	  jenv->DeleteLocalRef(poolClass);
76 	  jenv->DeleteLocalRef(obj);
77 	}
78 
79 	/*
80 	 * A simple RAII wrapper to release jobjects we obtain from pools in
81 	 * directorin typemaps.  SWIG doesn't have hooks to release them after
82 	 * they're used.
83 	 */
84 	class gdxPoolAutoRelease##JTYPE {
85 	private:
86 	  JNIEnv * jenv;
87 	  const char * poolName;
88 	  jobject obj;
89 	public:
JTYPE(JNIEnv * jenv,const char * poolName,jobject obj)90 	  gdxPoolAutoRelease##JTYPE(JNIEnv * jenv, const char * poolName, jobject obj) :
91 		jenv(jenv), poolName(poolName), obj(obj) { };
JTYPE()92 	  virtual ~gdxPoolAutoRelease##JTYPE() {
93 		gdx_releasePoolObject##JTYPE(this->jenv, this->poolName, this->obj);
94 	  };
95 	};
96 }
97 %enddef