[orx-olive] Add Once delegate
This commit is contained in:
@@ -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<OliveProgram>(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<T:Any>(val build:() -> T) {
|
||||
companion object {
|
||||
private val values = mutableMapOf<String, Any>()
|
||||
}
|
||||
@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
|
||||
|
||||
Reference in New Issue
Block a user