Allow multiple fields
Fix the declareField template to append fields without deduplication, which was not working because I cannot find a way to convert indentifiers to strings.
This commit is contained in:
parent
dd977991ad
commit
ff27285cff
|
@ -68,8 +68,7 @@ type
|
|||
updateProc: Script[EndpointSpec]
|
||||
spec: EndpointSpec
|
||||
|
||||
Field = tuple[name: string; value: Preserve]
|
||||
Fields = seq[Field]
|
||||
Fields = seq[Preserve]
|
||||
# TODO: compile-time tuples
|
||||
|
||||
Turn = object
|
||||
|
@ -553,36 +552,27 @@ proc bootModule*(bootProc: ActivationScript): Future[void] =
|
|||
true
|
||||
return g.future
|
||||
|
||||
proc registerField*(facet: Facet; field: Field): int =
|
||||
# TODO: should be hidden, but used by declareField.
|
||||
for i in 0..facet.fields.high:
|
||||
if facet.fields[i].name == field.name:
|
||||
facet.fields[i] = field
|
||||
return i
|
||||
facet.fields.add(field)
|
||||
return facet.fields.high
|
||||
|
||||
template declareField*(facet: Facet; F: untyped; T: typedesc; init: T): untyped =
|
||||
## Declare getter and setter procs for field `F` of type `T` initalized with `init`.
|
||||
# TODO: do more at compile-time, use a tuple rather than a sequence of Preserves.
|
||||
facet.fields.add(toPreserve(init))
|
||||
let
|
||||
`F FieldOff` = facet.fields.high
|
||||
`F FieldId` = facet.actor.dataspace.generateId.FieldId
|
||||
`F FeildOff` = registerField(facet,
|
||||
(nimIdentNormalize("`F`"), toPreserve(init), ))
|
||||
facet.actor.dataspace.dataflow.defineObservableProperty(`F FieldId`)
|
||||
|
||||
proc `F`(fields: Fields): T =
|
||||
facet.actor.dataspace.dataflow.recordObservation(`F FieldId`)
|
||||
fromPreserve[T](result, facet.fields[`F FeildOff`].value)
|
||||
fromPreserve[T](result, facet.fields[`F FieldOff`])
|
||||
|
||||
proc `F =`(fields: var Fields; x: T) =
|
||||
facet.actor.dataspace.dataflow.recordDamage(`F FieldId`)
|
||||
facet.fields[`F FeildOff`].value = toPreserve[T](x)
|
||||
facet.fields[`F FieldOff`] = toPreserve[T](x)
|
||||
|
||||
proc `F =`(fields: var Fields; x: Preserve) =
|
||||
assert(x.kind == facet.fields[`F FeildOff`].value.kind, "invalid Preserves item for field type")
|
||||
assert(x.kind == facet.fields[`F FieldOff`].kind, "invalid Preserves item for field type")
|
||||
facet.actor.dataspace.dataflow.recordDamage(`F FieldId`)
|
||||
facet.fields[`F FeildOff`].value = x
|
||||
facet.fields[`F FieldOff`] = x
|
||||
|
||||
|
||||
#[
|
||||
|
|
Loading…
Reference in New Issue