diff --git a/orx-jvm/orx-olive/src/main/kotlin/OliveProgram.kt b/orx-jvm/orx-olive/src/main/kotlin/OliveProgram.kt index 482a66ce..2146d2a4 100644 --- a/orx-jvm/orx-olive/src/main/kotlin/OliveProgram.kt +++ b/orx-jvm/orx-olive/src/main/kotlin/OliveProgram.kt @@ -5,8 +5,11 @@ import org.openrndr.Program import java.io.File import java.nio.file.Files import java.nio.file.Paths +import kotlin.reflect.KProperty import kotlin.streams.toList + + open class OliveProgram(private val sourceLocation: String, private val scriptHost: OliveScriptHost, resources: Resources?) : Program() { val olive = extend(Olive(scriptMode = ScriptMode.OLIVE_PROGRAM, resources = resources)) { script = sourceLocation @@ -14,6 +17,18 @@ open class OliveProgram(private val sourceLocation: String, private val scriptHo } } +/** + * Delegate used to create instances exactly once. Instances survive a script reload. + */ +class Once(val build:() -> T) { + companion object { + private val values = mutableMapOf() + } + @Suppress("UNCHECKED_CAST") + operator fun getValue(thisRef:Any?, property:KProperty<*>) : T = values.getOrPut(property.name) { build() } as T +} + + fun stackRootClassName(thread: Thread = Thread.currentThread(), sanitize: Boolean = true): String { val root = Thread.currentThread().stackTrace.last() val rootClass = root.className