EntityRegistry Class Template
Generic registry for creating and managing strongly-typed entity handles. More...
Declaration
Public Constructors Index
template < ... > | |
| EntityRegistry (const size_t capacity=TCapacity) | |
|
Constructs a registry with pre-allocated capacity. More... | |
Public Member Functions Index
template < ... > | |
| auto | create (TStrongId strongId=TStrongId{}) -> EntityHandle< TStrongId > |
|
Creates a new entity and returns its handle. More... | |
template < ... > | |
| VersionId | version (const EntityId entityId) const |
template < ... > | |
| TStrongId | strongId (const EntityId entityId) const |
|
Looks up the strong ID for an entity index. More... | |
template < ... > | |
| bool | isValid (const EntityHandle< TStrongId > handle) const noexcept |
|
Checks whether the given handle refers to a currently alive entity. More... | |
template < ... > | |
| bool | destroy (const EntityHandle< TStrongId > handle) |
|
Destroys the entity referenced by the given handle. More... | |
Private Member Attributes Index
template < ... > | |
| std::vector< EntityId > | freeList_ |
|
Free list of recycled entity indices available for reuse. More... | |
template < ... > | |
| std::vector< VersionId > | versions_ |
|
Version numbers for each entity slot. More... | |
template < ... > | |
| std::vector< StrongId_t > | strongIds_ |
|
Strong ID values for each entity slot. More... | |
template < ... > | |
| TLookupStrategy | lookupStrategy_ |
|
Lookup strategy instance for strong ID collision detection. More... | |
template < ... > | |
| size_t | strongIdCounter_ = 0 |
|
Auto-increment counter for generating strong IDs when none is provided. More... | |
Description
Generic registry for creating and managing strongly-typed entity handles.
`EntityRegistry` serves as the authoritative source for entity lifecycle management. It is responsible for:
- **Handle Creation:** Generates unique `EntityHandle` instances with versioned IDs and domain-specific strong IDs.
- **Validation:** Determines whether a given handle refers to a currently alive entity.
- **Destruction:** Marks entities as destroyed by incrementing their version and recycling the index for future use (when `TAllowRemoval` is true).
## Versioning
Each entity slot maintains a version number. When an entity is destroyed, its version is incremented. This allows the registry to detect stale handles — handles that reference an entity that has been destroyed and potentially replaced by a new entity at the same index.
## Index Recycling
Destroyed entity indices are added to a free list and reused when creating new entities. This keeps the dense version array compact and minimizes memory growth.
## Strong ID Collision Detection
The registry delegates collision checks to a configurable `TLookupStrategy`, which tracks which strong IDs are currently in use. The default strategy (`HashedLookupStrategy`) provides O(1) amortized lookups.
- Template Parameters
-
TStrongId A strong ID type carrying domain semantics.
TLookupStrategy The strategy used for strong ID collision detection.
TAllowRemoval If false, `destroy()` triggers an assertion instead of removing.
TCapacity Default initial capacity for pre-allocation.
- See Also
- See Also
- See Also
Definition at line 74 of file EntityRegistry.ixx.
Public Constructors
EntityRegistry()
| inline explicit |
Constructs a registry with pre-allocated capacity.
Reserving capacity upfront can improve performance when the approximate number of entities is known in advance.
- Parameters
-
capacity The initial capacity to reserve.
Definition at line 117 of file EntityRegistry.ixx.
Public Member Functions
create()
| inline |
Creates a new entity and returns its handle.
If recycled indices are available in the free list, one is reused. Otherwise, a new index is allocated at the end of the version vector. If the provided `strongId` is invalid, one is auto-assigned from the entity index.
- Parameters
-
strongId Optional domain-specific strong ID. Defaults to auto-assignment.
- Returns
A valid `EntityHandle` for the newly created entity.
Definition at line 137 of file EntityRegistry.ixx.
Reference helios::ecs::EntityRegistry< TStrongId, TLookupStrategy, TAllowRemoval, TCapacity >::strongId.
destroy()
| inline |
Destroys the entity referenced by the given handle.
If the handle is valid, the entity's version is incremented (invalidating all existing handles to it), the strong ID is unregistered, and the index is added to the free list for recycling.
If `TAllowRemoval` is false, triggers an assertion failure.
- Parameters
-
handle The handle of the entity to destroy.
- Returns
True if the entity was successfully destroyed, false if the handle was already invalid.
Definition at line 240 of file EntityRegistry.ixx.
References helios::ecs::types::EntityHandle< TStrongId >::entityId, helios::ecs::EntityRegistry< TStrongId, TLookupStrategy, TAllowRemoval, TCapacity >::isValid and helios::ecs::types::EntityHandle< TStrongId >::strongId.
isValid()
| inline nodiscard noexcept |
Checks whether the given handle refers to a currently alive entity.
A handle is valid if its index is within bounds, its version matches the current version stored in the registry, and its strong ID matches the registered value.
- Parameters
-
handle The handle to validate.
- Returns
True if the handle is valid and the entity is alive.
Definition at line 213 of file EntityRegistry.ixx.
Referenced by helios::ecs::EntityRegistry< TStrongId, TLookupStrategy, TAllowRemoval, TCapacity >::destroy.
strongId()
| inline nodiscard |
Looks up the strong ID for an entity index.
- Parameters
-
entityId The entity index to retrieve the strong ID for.
- Returns
The strong ID for the entity, or a default-constructed (invalid) `TStrongId` if out of bounds.
Definition at line 194 of file EntityRegistry.ixx.
Referenced by helios::ecs::EntityRegistry< TStrongId, TLookupStrategy, TAllowRemoval, TCapacity >::create.
version()
| inline nodiscard |
Looks up the version for an EntityId.
- Parameters
-
entityId The entity to retrieve the version for.
- Returns
The version for the EntityId, or `InvalidVersion` if not found.
Definition at line 179 of file EntityRegistry.ixx.
Reference helios::ecs::types::InvalidVersion.
Private Member Attributes
freeList_
|
Free list of recycled entity indices available for reuse.
Definition at line 80 of file EntityRegistry.ixx.
lookupStrategy_
|
Lookup strategy instance for strong ID collision detection.
Definition at line 100 of file EntityRegistry.ixx.
strongIdCounter_
|
Auto-increment counter for generating strong IDs when none is provided.
Definition at line 105 of file EntityRegistry.ixx.
strongIds_
|
Strong ID values for each entity slot.
Definition at line 95 of file EntityRegistry.ixx.
versions_
|
Version numbers for each entity slot.
The version is incremented when an entity at that index is destroyed, allowing detection of stale handles.
Definition at line 89 of file EntityRegistry.ixx.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.