Minor ref cleanup

This commit is contained in:
Alexander Nozik 2021-07-12 13:23:25 +03:00
parent 073d374a9e
commit 5af8fe3e99
2 changed files with 28 additions and 18 deletions

View File

@ -7,10 +7,19 @@ import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.NameToken import space.kscience.dataforge.names.NameToken
import space.kscience.visionforge.* import space.kscience.visionforge.*
/**
* A container with prototype support
*/
public interface PrototypeHolder { public interface PrototypeHolder {
/**
* Build or update prototype tree
*/
@VisionBuilder @VisionBuilder
public fun prototypes(builder: VisionContainerBuilder<Solid>.() -> Unit) public fun prototypes(builder: VisionContainerBuilder<Solid>.() -> Unit)
/**
* Resolve a prototype from this container. Should never return a ref.
*/
public fun getPrototype(name: Name): Solid? public fun getPrototype(name: Name): Solid?
} }
@ -24,7 +33,7 @@ public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder {
override val children: Map<NameToken, Vision> get() = super.childrenInternal.filter { it.key != PROTOTYPES_TOKEN } override val children: Map<NameToken, Vision> get() = super.childrenInternal.filter { it.key != PROTOTYPES_TOKEN }
public var prototypes: MutableVisionGroup? private var prototypes: MutableVisionGroup?
get() = childrenInternal[PROTOTYPES_TOKEN] as? MutableVisionGroup get() = childrenInternal[PROTOTYPES_TOKEN] as? MutableVisionGroup
set(value) { set(value) {
set(PROTOTYPES_TOKEN, value) set(PROTOTYPES_TOKEN, value)
@ -35,7 +44,7 @@ public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder {
/** /**
* Get a prototype redirecting the request to the parent if prototype is not found. * Get a prototype redirecting the request to the parent if prototype is not found.
* If prototype is a ref, then it is unfolded until * If prototype is a ref, then it is unfolded automatically.
*/ */
override fun getPrototype(name: Name): Solid? = override fun getPrototype(name: Name): Solid? =
prototypes?.get(name)?.unref ?: (parent as? PrototypeHolder)?.getPrototype(name) prototypes?.get(name)?.unref ?: (parent as? PrototypeHolder)?.getPrototype(name)

View File

@ -37,16 +37,20 @@ private fun SolidReference.getRefProperty(
inherit: Boolean, inherit: Boolean,
includeStyles: Boolean, includeStyles: Boolean,
includeDefaults: Boolean, includeDefaults: Boolean,
): MetaItem? = buildList { ): MetaItem? = if (!inherit && !includeStyles && !includeDefaults) {
add(getOwnProperty(name)) getOwnProperty(name)
if (includeStyles) { } else {
addAll(getStyleItems(name)) buildList {
} add(getOwnProperty(name))
add(prototype.getProperty(name, inherit, includeStyles, includeDefaults)) if (includeStyles) {
if (inherit) { addAll(getStyleItems(name))
add(parent?.getProperty(name, inherit)) }
} add(prototype.getProperty(name, inherit, includeStyles, includeDefaults))
}.merge() if (inherit) {
add(parent?.getProperty(name, inherit))
}
}.merge()
}
private fun childToken(childName: Name): NameToken = private fun childToken(childName: Name): NameToken =
NameToken(SolidReferenceGroup.REFERENCE_CHILD_PROPERTY_PREFIX, childName.toString()) NameToken(SolidReferenceGroup.REFERENCE_CHILD_PROPERTY_PREFIX, childName.toString())
@ -102,7 +106,8 @@ public class SolidReferenceGroup(
if (childName.isEmpty()) owner.prototype else { if (childName.isEmpty()) owner.prototype else {
val proto = (owner.prototype as? VisionGroup)?.get(childName) val proto = (owner.prototype as? VisionGroup)?.get(childName)
?: error("Prototype with name $childName not found in SolidReferenceGroup ${owner.refName}") ?: error("Prototype with name $childName not found in SolidReferenceGroup ${owner.refName}")
proto.unref as? Solid ?: error("Prototype with name $childName is ${proto::class} but expected Solid") proto.unref as? Solid
?: error("Prototype with name $childName is ${proto::class} but expected Solid")
} }
} }
@ -125,11 +130,7 @@ public class SolidReferenceGroup(
inherit: Boolean, inherit: Boolean,
includeStyles: Boolean, includeStyles: Boolean,
includeDefaults: Boolean, includeDefaults: Boolean,
): MetaItem? = if (!inherit && !includeStyles && !includeDefaults) { ): MetaItem? = getRefProperty(name, inherit, includeStyles, includeDefaults)
getOwnProperty(name)
} else {
getRefProperty(name, inherit, includeStyles, includeDefaults)
}
override var parent: VisionGroup? override var parent: VisionGroup?
get() { get() {