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

godot.extensions.GodotStaticDelegate.kt Maven / Gradle / Ivy

There is a newer version: 0.10.0-4.3.0
Show newest version
@file:Suppress("ClassName")

package godot.extensions

import godot.Object
import godot.RefCounted
import godot.core.memory.MemoryManager
import godot.core.memory.GodotStatic
import godot.core.variantMapper
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty


internal object UNINITIALIZED_VALUE

class GodotStaticDelegate(val factory: () -> T) : GodotStatic, ReadWriteProperty {

    private var _value: Any? = UNINITIALIZED_VALUE

    override fun getValue(thisRef: Any, property: KProperty<*>): T {
        return synchronized(this) {
            if (_value === UNINITIALIZED_VALUE) {
                _value = factory()
            }
            @Suppress("UNCHECKED_CAST")
            _value as T
        }
    }

    override fun collect() {
        val v1 = _value

        if (v1 != UNINITIALIZED_VALUE && v1 != null) {
            require(v1 is Object)
            if (v1 !is RefCounted && MemoryManager.isInstanceValid(v1)) {
                v1.free()
            }
        }

        _value = UNINITIALIZED_VALUE;
    }

    override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
        synchronized(this) {
            collect()
            _value = value
        }
    }
}

inline fun  godotStatic(noinline factory: () -> T): GodotStaticDelegate {

    val variantType = variantMapper[T::class]
    checkNotNull(variantType) {
        "Can't create a GodotStatic with generic ${T::class}."
    }

    return GodotStaticDelegate(factory).also{
        it.registerAsSingleton()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy