godot.core.memory.GodotBinding.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of godot-library-debug Show documentation
Show all versions of godot-library-debug Show documentation
Contains godot api as kotlin classes and jvm cpp interaction code.
package godot.core.memory
import godot.core.KtObject
import godot.core.ObjectID
import java.lang.ref.ReferenceQueue
import java.lang.ref.WeakReference
internal interface GodotBinding {
val objectID: ObjectID
val instance: KtObject?
companion object {
/** Queue to be notified when the GC runs on References.*/
val queue = ReferenceQueue()
fun create(instance: KtObject): GodotBinding {
val id = instance.objectID
return if (id.isReference) {
RefCountedBinding(instance, queue, id)
} else {
ObjectBinding(instance, id)
}
}
}
}
internal class RefCountedBinding(
instance: KtObject,
queue: ReferenceQueue,
override val objectID: ObjectID
) : WeakReference(instance, queue), GodotBinding {
override val instance: KtObject?
get() = this.get()
}
internal class ObjectBinding(
override val instance: KtObject,
override val objectID: ObjectID
) : GodotBinding