All Downloads are FREE. Search and download functionalities are using the official Maven repository.

godot.core.memory.GodotBinding.kt Maven / Gradle / Ivy

The newest version!
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





© 2015 - 2024 Weber Informatics LLC | Privacy Policy