Module property_store

Module property_store 

Source
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 PropertyValueStore instance 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_entity when creating a new entity.

Structs§

PropertyStore
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::Property if it doesn’t already exist. In our use case, this method is called in the ctor function of each Property<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.