Expand description
A PropertyStore implements the registry pattern for property value stores: A PropertyStore
wraps a vector of PropertyValueStores, one for each concrete property type. The implementor
of crate::entity::property::Property is the value type. Since there’s a 1-1 correspondence between property types
and their value stores, we implement the index method for each property type to make
property lookup fast. The PropertyStore stores a list of all properties in the form of
boxed PropertyValueStore instances, which provide a type-erased interface to the backing
storage (including index) of the property. Storage is only allocated as-needed, so the
instantiation of a PropertyValueStore for a property that is never used is negligible.
There’s no need, then, for lazy initialization of the PropertyValueStores themselves.
This module also implements the initialization of “static” data associated with a property,
that is, data that is the same across all crate::context::Context instances, which is computed before main()
using ctor magic. (Each property implements a ctor that calls add_to_property_registry().)
For simplicity, a property’s ctor implementation, supplied by a macro, just calls
add_to_property_registry<E: Entity, P: Property<E>>(), which does all the work. The
add_to_property_registry function adds the following metadata to global metadata stores:
Metadata stored on PROPERTY_METADATA, which for each property stores:
- a list of dependent (derived) properties, and
- a constructor function to create a new
PropertyValueStoreinstance for the property.
Metadata stored on ENTITY_METADATA, which for each entity stores:
- a list of properties associated with the entity, and
- a list of required properties for the entity. These are properties for
which values must be supplied to
add_entitywhen creating a new entity.
Structs§
- Property
Store - A wrapper around a vector of property value stores.
Functions§
- add_
to_ property_ registry - Adds a new item to the registry. The job of this method is to create whatever “singleton”
data/metadata is associated with the
crate::entity::property::Propertyif it doesn’t already exist. In our use case, this method is called in thectorfunction of eachProperty<E>type. - get_
registered_ property_ count - A convenience getter for
NEXT_ENTITY_INDEX. - initialize_
property_ id - Encapsulates the synchronization logic for initializing an item’s index.