me.qnox.builder.BuildContext.kt Maven / Gradle / Ivy
package me.qnox.builder
import kotlin.reflect.KClass
class BuildContext {
private val preprocessors = mutableMapOf, (Any) -> Unit>()
fun preprocess(builder: T) {
val preprocessor = findPreprocessor(builder::class) as ((T) -> Unit)?
preprocessor?.let { it(builder) }
}
private fun findPreprocessor(type: KClass): ((T) -> Unit)? = preprocessors[type]
fun preprocess(type: KClass, preprocessor: T.() -> Unit) {
preprocessors[type] = preprocessor as (Any) -> Unit
}
}
fun builderContext(block: BuildContext.() -> Unit): BuildContext = BuildContext().apply(block)