EntityPool Class Template
Object pool for efficient Entity lifecycle management. More...
Declaration
Public Constructors Index
template <typename THandle> | |
| EntityPool (size_t poolSize) | |
|
Constructs a EntityPool with the specified capacity. More... | |
Public Member Functions Index
template <typename THandle> | |
| helios::engine::util::Guid | guid () const noexcept |
|
Returns the unique identifier of this pool. More... | |
template <typename THandle> | |
| size_t | size () const noexcept |
|
Returns the maximum capacity of this pool. More... | |
template <typename THandle> | |
| bool | acquire (THandle &entityHandle) |
|
Acquires an inactive Entity from the pool. More... | |
template <typename THandle> | |
| bool | isLocked () const noexcept |
|
Checks if the pool is locked. More... | |
template <typename THandle> | |
| void | lock () noexcept |
|
Locks the pool for acquire/release operations. More... | |
template <typename THandle> | |
| bool | addInactive (const THandle entityHandle) |
|
Adds a EntityHandle to the inactive list without acquiring it. More... | |
template <typename THandle> | |
| bool | release (const THandle entityHandle) |
|
Releases a Entity back to the pool by its EntityHandle. More... | |
template <typename THandle> | |
| bool | releaseAndRemove (const THandle entityHandle) |
|
Releases and permanently removes a Entity from the pool. More... | |
template <typename THandle> | |
| size_t | activeCount () const noexcept |
|
Returns the number of active game objects. More... | |
template <typename THandle> | |
| size_t | inactiveCount () const noexcept |
|
Returns the number of inactive game objects. More... | |
template <typename THandle> | |
| auto | inactiveEntities () -> std::span< THandle > |
|
Returns a span of all inactive EntityHandles. More... | |
template <typename THandle> | |
| auto | activeEntities () -> std::span< THandle > |
|
Returns a span of all active EntityHandles. More... | |
Protected Member Attributes Index
template <typename THandle> | |
| std::vector< size_t > | activeIndex_ |
|
Maps active Entity EntityIds to their index in activeEntities_. More... | |
template <typename THandle> | |
| std::vector< size_t > | versionIndex_ |
|
Tracks version numbers for active EntityHandles. More... | |
template <typename THandle> | |
| std::vector< THandle > | activeEntities_ |
|
List of EntityHandles for currently active (in-use) Entities. More... | |
template <typename THandle> | |
| std::vector< THandle > | inactiveEntities_ |
|
List of EntityHandles for currently inactive (available) Entities. More... | |
template <typename THandle> | |
| size_t | poolSize_ = 0 |
|
The maximum number of objects this pool manages. More... | |
template <typename THandle> | |
| helios::ecs::types::EntityId | minEntityId_ = std::numeric_limits<helios::ecs::types::EntityId>::max() |
|
Minimum EntityId in the pool (used for sparse array offset). More... | |
template <typename THandle> | |
| helios::ecs::types::EntityId | maxEntityId_ = std::numeric_limits<helios::ecs::types::EntityId>::lowest() |
|
Maximum EntityId in the pool (used for sparse array sizing). More... | |
template <typename THandle> | |
| size_t | delta_ = 0 |
|
Offset for sparse array indexing (equals minEntityId_ after lock). More... | |
template <typename THandle> | |
| const helios::engine::util::Guid | guid_ |
|
Unique identifier for this pool instance. More... | |
template <typename THandle> | |
| bool | locked_ = false |
|
True if the pool is locked and ready for acquire/release operations. More... | |
Description
Object pool for efficient Entity lifecycle management.
EntityPool manages a fixed-size collection of Entity identifiers, tracking which objects are currently active (in-use) and which are inactive (available). This pattern eliminates runtime allocation overhead for frequently spawned entities like projectiles, particles, or enemies.
The pool uses O(1) operations for both acquire and release:
- acquire: Pops from the inactive list and adds to active tracking
- release: Swap-and-pop removal from active list, push to inactive
Entities themselves are owned by GameWorld; this pool only tracks their EntityHandles.
Definition at line 46 of file EntityPool.ixx.
Public Constructors
EntityPool()
| inline explicit |
Constructs a EntityPool with the specified capacity.
Pre-allocates internal storage for the given pool size. The pool starts empty; use addInactive() or a factory to populate it.
- Parameters
-
poolSize The maximum number of Entities this pool can manage.
Definition at line 116 of file EntityPool.ixx.
References helios::engine::runtime::pooling::EntityPool< THandle >::activeEntities_, helios::engine::runtime::pooling::EntityPool< THandle >::inactiveEntities_ and helios::engine::runtime::pooling::registerComponents.
Public Member Functions
acquire()
| inline |
Acquires an inactive Entity from the pool.
Removes a EntityHandle from the inactive list and adds it to the active tracking structures. The caller is responsible for activating the actual Entity in the GameWorld.
- Parameters
-
[out] entityHandle Receives the EntityHandle of the acquired object on success.
- Returns
True if an object was acquired, false if the pool is exhausted.
Definition at line 155 of file EntityPool.ixx.
References helios::engine::runtime::pooling::EntityPool< THandle >::activeEntities_, helios::engine::runtime::pooling::EntityPool< THandle >::activeIndex_, helios::engine::runtime::pooling::EntityPool< THandle >::delta_, helios::engine::runtime::pooling::EntityPool< THandle >::inactiveEntities_, helios::engine::runtime::pooling::registerComponents and helios::engine::runtime::pooling::EntityPool< THandle >::versionIndex_.
activeCount()
| inline noexcept |
Returns the number of active game objects.
- Returns
The number of active game objects.
Definition at line 337 of file EntityPool.ixx.
Reference helios::engine::runtime::pooling::EntityPool< THandle >::activeEntities_.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::addInactive.
activeEntities()
| inline |
Returns a span of all active EntityHandles.
- Returns
Span of active EntityHandles.
Definition at line 364 of file EntityPool.ixx.
Reference helios::engine::runtime::pooling::EntityPool< THandle >::activeEntities_.
addInactive()
| inline |
Adds a EntityHandle to the inactive list without acquiring it.
Used during pool initialization to register pre-created Entities. Fails if the pool is already at capacity.
- Parameters
-
entityHandle The EntityHandle of the Entity to add.
- Returns
True if added successfully, false if pool is full.
Definition at line 212 of file EntityPool.ixx.
References helios::engine::runtime::pooling::EntityPool< THandle >::activeCount, helios::engine::runtime::pooling::EntityPool< THandle >::inactiveCount, helios::engine::runtime::pooling::EntityPool< THandle >::inactiveEntities_, helios::engine::runtime::pooling::EntityPool< THandle >::locked_, helios::engine::runtime::pooling::EntityPool< THandle >::maxEntityId_, helios::engine::runtime::pooling::EntityPool< THandle >::minEntityId_, helios::engine::runtime::pooling::registerComponents and helios::engine::runtime::pooling::EntityPool< THandle >::size.
guid()
| inline noexcept |
Returns the unique identifier of this pool.
- Returns
The Guid assigned to this pool instance.
Definition at line 131 of file EntityPool.ixx.
Reference helios::engine::runtime::pooling::EntityPool< THandle >::guid_.
inactiveCount()
| inline noexcept |
Returns the number of inactive game objects.
- Returns
The number of inactive game objects.
Definition at line 346 of file EntityPool.ixx.
Reference helios::engine::runtime::pooling::EntityPool< THandle >::inactiveEntities_.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::addInactive.
inactiveEntities()
| inline |
Returns a span of all inactive EntityHandles.
- Returns
Span of inactive EntityHandles.
Definition at line 355 of file EntityPool.ixx.
Reference helios::engine::runtime::pooling::EntityPool< THandle >::inactiveEntities_.
isLocked()
| inline noexcept |
Checks if the pool is locked.
- Returns
True if the pool is locked and ready for acquire/release operations.
Definition at line 185 of file EntityPool.ixx.
Reference helios::engine::runtime::pooling::EntityPool< THandle >::locked_.
lock()
| inline noexcept |
Locks the pool for acquire/release operations.
After locking, no more EntityHandles can be added via addInactive(). The sparse arrays are sized based on the min/max EntityIds added.
Definition at line 195 of file EntityPool.ixx.
References helios::engine::runtime::pooling::EntityPool< THandle >::activeIndex_, helios::engine::runtime::pooling::EntityPool< THandle >::delta_, helios::engine::runtime::pooling::EntityPool< THandle >::locked_, helios::engine::runtime::pooling::EntityPool< THandle >::maxEntityId_, helios::engine::runtime::pooling::EntityPool< THandle >::minEntityId_ and helios::engine::runtime::pooling::EntityPool< THandle >::versionIndex_.
release()
| inline |
Releases a Entity back to the pool by its EntityHandle.
Validates the EntityHandle against both the GameWorld and the active tracking list. Uses swap-and-pop for O(1) removal from the active list. The object is marked inactive and added to the inactive list for future acquisition.
- Parameters
-
entityHandle The unique identifier of the Entity to release.
- Returns
True if the object was successfully released, false if the EntityHandle was not found in the GameWorld or not tracked as active.
Definition at line 245 of file EntityPool.ixx.
References helios::engine::runtime::pooling::EntityPool< THandle >::activeEntities_, helios::engine::runtime::pooling::EntityPool< THandle >::activeIndex_, helios::engine::runtime::pooling::EntityPool< THandle >::delta_, helios::engine::runtime::pooling::EntityPool< THandle >::inactiveEntities_, helios::engine::runtime::pooling::registerComponents and helios::engine::runtime::pooling::EntityPool< THandle >::versionIndex_.
releaseAndRemove()
| inline |
Releases and permanently removes a Entity from the pool.
Unlike release(), this method does not add the EntityHandle back to the inactive list. Use this when a pooled object is being destroyed rather than recycled.
- Parameters
-
entityHandle The unique identifier of the Entity to remove.
- Returns
True if removed successfully, false if EntityHandle was not active.
Definition at line 299 of file EntityPool.ixx.
References helios::engine::runtime::pooling::EntityPool< THandle >::activeEntities_, helios::engine::runtime::pooling::EntityPool< THandle >::activeIndex_, helios::engine::runtime::pooling::EntityPool< THandle >::delta_, helios::engine::runtime::pooling::registerComponents and helios::engine::runtime::pooling::EntityPool< THandle >::versionIndex_.
size()
| inline noexcept |
Returns the maximum capacity of this pool.
- Returns
The pool size specified at construction.
Definition at line 140 of file EntityPool.ixx.
Reference helios::engine::runtime::pooling::EntityPool< THandle >::poolSize_.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::addInactive.
Protected Member Attributes
activeEntities_
| protected |
List of EntityHandles for currently active (in-use) Entities.
Definition at line 68 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::acquire, helios::engine::runtime::pooling::EntityPool< THandle >::activeCount, helios::engine::runtime::pooling::EntityPool< THandle >::activeEntities, helios::engine::runtime::pooling::EntityPool< THandle >::EntityPool, helios::engine::runtime::pooling::EntityPool< THandle >::release and helios::engine::runtime::pooling::EntityPool< THandle >::releaseAndRemove.
activeIndex_
| protected |
Maps active Entity EntityIds to their index in activeEntities_.
Enables O(1) lookup for release operations.
Definition at line 56 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::acquire, helios::engine::runtime::pooling::EntityPool< THandle >::lock, helios::engine::runtime::pooling::EntityPool< THandle >::release and helios::engine::runtime::pooling::EntityPool< THandle >::releaseAndRemove.
delta_
| protected |
Offset for sparse array indexing (equals minEntityId_ after lock).
Definition at line 93 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::acquire, helios::engine::runtime::pooling::EntityPool< THandle >::lock, helios::engine::runtime::pooling::EntityPool< THandle >::release and helios::engine::runtime::pooling::EntityPool< THandle >::releaseAndRemove.
guid_
| protected |
Unique identifier for this pool instance.
Definition at line 98 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::guid.
inactiveEntities_
| protected |
List of EntityHandles for currently inactive (available) Entities.
Definition at line 73 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::acquire, helios::engine::runtime::pooling::EntityPool< THandle >::addInactive, helios::engine::runtime::pooling::EntityPool< THandle >::EntityPool, helios::engine::runtime::pooling::EntityPool< THandle >::inactiveCount, helios::engine::runtime::pooling::EntityPool< THandle >::inactiveEntities and helios::engine::runtime::pooling::EntityPool< THandle >::release.
locked_
| protected |
True if the pool is locked and ready for acquire/release operations.
Definition at line 103 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::addInactive, helios::engine::runtime::pooling::EntityPool< THandle >::isLocked and helios::engine::runtime::pooling::EntityPool< THandle >::lock.
maxEntityId_
| protected |
Maximum EntityId in the pool (used for sparse array sizing).
Definition at line 88 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::addInactive and helios::engine::runtime::pooling::EntityPool< THandle >::lock.
minEntityId_
| protected |
Minimum EntityId in the pool (used for sparse array offset).
Definition at line 83 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::addInactive and helios::engine::runtime::pooling::EntityPool< THandle >::lock.
poolSize_
| protected |
The maximum number of objects this pool manages.
Definition at line 78 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::size.
versionIndex_
| protected |
Tracks version numbers for active EntityHandles.
Used to validate that a release operation targets the correct entity version.
Definition at line 63 of file EntityPool.ixx.
Referenced by helios::engine::runtime::pooling::EntityPool< THandle >::acquire, helios::engine::runtime::pooling::EntityPool< THandle >::lock, helios::engine::runtime::pooling::EntityPool< THandle >::release and helios::engine::runtime::pooling::EntityPool< THandle >::releaseAndRemove.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.