1 /** Managed objects are java objects of which the reference is maintained by the wrapper and the instance is reused 2 * by using a LongMap lookup with the native pointer. 3 * 4 * Use managed objects for classes which are likely to be created by the user and are expected to live long. 5 * @author Xoppa */ 6 %define CREATE_MANAGED_OBJECT(_TYPE) 7 8 %typemap(javaout) _TYPE *, const _TYPE *, _TYPE * const & { 9 return _TYPE.getInstance($jnicall, $owner); 10 } 11 12 %typemap(javadirectorin) _TYPE *, const _TYPE *, _TYPE * const & "_TYPE.getInstance($1, false)" 13 14 %typemap(javadestruct, methodname="delete", methodmodifiers="public synchronized") _TYPE %{ { 15 beforeDelete(); 16 if (swigCPtr != 0) { 17 if (swigCMemOwn) { 18 swigCMemOwn = false; 19 gdxBulletJNI.delete_##_TYPE(swigCPtr); 20 } 21 swigCPtr = 0; 22 } 23 } %} 24 25 %typemap(javabody) _TYPE %{ 26 private long swigCPtr; 27 protected boolean swigCMemOwn; 28 29 protected _TYPE(long cPtr, boolean cMemoryOwn) { 30 swigCMemOwn = cMemoryOwn; 31 swigCPtr = cPtr; 32 addInstance(this); 33 } 34 35 /** Gets call before this object is deleted, override to add custom implementation. */ 36 protected void beforeDelete() { 37 if (swigCPtr != 0) 38 removeInstance(this); 39 } 40 %} 41 42 %typemap(javacode) _TYPE %{ 43 /** Provides direct access to the instances this wrapper managed. */ 44 public final static com.badlogic.gdx.utils.LongMap<_TYPE> instances = new com.badlogic.gdx.utils.LongMap<_TYPE>(); 45 46 /** @return The existing instance for the specified pointer, or null if the instance doesn't exist */ 47 public static _TYPE getInstance(final long swigCPtr) { 48 return swigCPtr == 0 ? null : instances.get(swigCPtr); 49 } 50 51 /** @return The existing isntance for the specified pointer, or a newly created instance if the instance didn't exist */ 52 public static _TYPE getInstance(final long swigCPtr, boolean owner) { 53 if (swigCPtr == 0) 54 return null; 55 _TYPE result = instances.get(swigCPtr); 56 if (result == null) 57 result = new _TYPE(swigCPtr, owner); 58 return result; 59 } 60 61 /** Add the instance to the managed instances. 62 * You should avoid using this method. This method is intended for internal purposes only. */ 63 public static void addInstance(final _TYPE obj) { 64 instances.put(getCPtr(obj), obj); 65 } 66 67 /** Remove the instance to the managed instances. 68 * Be careful using this method. This method is intended for internal purposes only. */ 69 public static void removeInstance(final _TYPE obj) { 70 instances.remove(getCPtr(obj)); 71 } 72 73 /** Take ownership of the native instance, causing the native object to be deleted when this object gets out of scope. */ 74 public void takeOwnership() { 75 swigCMemOwn = true; 76 } 77 78 /** Release ownership of the native instance, causing the native object NOT to be deleted when this object gets out of scope. */ 79 public void releaseOwnership() { 80 swigCMemOwn = false; 81 } 82 83 /** @return True if the native is destroyed when this object gets out of scope, false otherwise. */ 84 public boolean hasOwnership() { 85 return swigCMemOwn; 86 } 87 88 /** @return The value of the pointer to the native instance this object represents. */ 89 public static long getCPtr($javaclassname obj) { 90 return (obj == null) ? 0 : obj.swigCPtr; 91 } 92 %} 93 94 %enddef // CREATE_MANAGED_OBJECT