GameObjectPool Class
Object pool for efficient GameObject lifecycle management. More...
Declaration
Public Constructors Index
| GameObjectPool (size_t poolSize) | |
|
Constructs a GameObjectPool with the specified capacity. More... | |
Public Member Functions Index
| helios::util::Guid | guid () const noexcept |
|
Returns the unique identifier of this pool. More... | |
| size_t | size () const noexcept |
|
Returns the maximum capacity of this pool. More... | |
| bool | acquire (helios::engine::ecs::EntityHandle &entityHandle) |
|
Acquires an inactive GameObject from the pool. More... | |
| bool | isLocked () const noexcept |
|
Checks if the pool is locked. More... | |
| void | lock () noexcept |
|
Locks the pool for acquire/release operations. More... | |
| bool | addInactive (const helios::engine::ecs::EntityHandle entityHandle) |
|
Adds a EntityHandle to the inactive list without acquiring it. More... | |
| bool | release (const helios::engine::ecs::EntityHandle entityHandle) |
|
Releases a GameObject back to the pool by its EntityHandle. More... | |
| bool | releaseAndRemove (const helios::engine::ecs::EntityHandle entityHandle) |
|
Releases and permanently removes a GameObject from the pool. More... | |
| size_t | activeCount () const noexcept |
|
Returns the number of active game objects. More... | |
| size_t | inactiveCount () const noexcept |
|
Returns the number of inactive game objects. More... | |
| std::span< helios::engine::ecs::EntityHandle > | inactiveGameObjects () |
|
Returns a span of all inactive EntityHandles. More... | |
| std::span< helios::engine::ecs::EntityHandle > | activeGameObjects () |
|
Returns a span of all active EntityHandles. More... | |
Protected Member Attributes Index
| std::vector< size_t > | activeIndex_ |
|
Maps active GameObject EntityIds to their index in activeGameObjects_. More... | |
| std::vector< size_t > | versionIndex_ |
|
Tracks version numbers for active EntityHandles. More... | |
| std::vector< helios::engine::ecs::EntityHandle > | activeGameObjects_ |
|
List of EntityHandles for currently active (in-use) GameObjects. More... | |
| std::vector< helios::engine::ecs::EntityHandle > | inactiveGameObjects_ |
|
List of EntityHandles for currently inactive (available) GameObjects. More... | |
| size_t | poolSize_ = 0 |
|
The maximum number of objects this pool manages. More... | |
| helios::engine::ecs::types::EntityId | minEntityId_ = std::numeric_limits<helios::engine::ecs::types::EntityId>::max() |
|
Minimum EntityId in the pool (used for sparse array offset). More... | |
| helios::engine::ecs::types::EntityId | maxEntityId_ = std::numeric_limits<helios::engine::ecs::types::EntityId>::lowest() |
|
Maximum EntityId in the pool (used for sparse array sizing). More... | |
| size_t | delta_ = 0 |
|
Offset for sparse array indexing (equals minEntityId_ after lock). More... | |
| const helios::util::Guid | guid_ |
|
Unique identifier for this pool instance. More... | |
| bool | locked_ = false |
|
True if the pool is locked and ready for acquire/release operations. More... | |
Description
Object pool for efficient GameObject lifecycle management.
GameObjectPool manages a fixed-size collection of GameObject 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
GameObjects themselves are owned by GameWorld; this pool only tracks their EntityHandles.
- Todo
-
Prevent duplicate EntityHandles from being added to the pool.
Definition at line 45 of file GameObjectPool.ixx.
Public Constructors
GameObjectPool()
| inline explicit |
Constructs a GameObjectPool 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 GameObjects this pool can manage.
Definition at line 115 of file GameObjectPool.ixx.
References activeGameObjects_, guid_, inactiveGameObjects_ and poolSize_.
Public Member Functions
acquire()
| inline nodiscard |
Acquires an inactive GameObject 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 GameObject 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 154 of file GameObjectPool.ixx.
References activeGameObjects_, activeIndex_, delta_, helios::engine::ecs::EntityHandle::entityId, helios::engine::ecs::types::EntityTombstone, inactiveGameObjects_, helios::engine::ecs::EntityHandle::versionId and versionIndex_.
activeCount()
| inline nodiscard noexcept |
Returns the number of active game objects.
- Returns
The number of active game objects.
Definition at line 336 of file GameObjectPool.ixx.
Reference activeGameObjects_.
Referenced by addInactive.
activeGameObjects()
| inline |
Returns a span of all active EntityHandles.
- Returns
Span of active EntityHandles.
Definition at line 363 of file GameObjectPool.ixx.
Reference activeGameObjects_.
addInactive()
| inline |
Adds a EntityHandle to the inactive list without acquiring it.
Used during pool initialization to register pre-created GameObjects. Fails if the pool is already at capacity.
- Parameters
-
entityHandle The EntityHandle of the GameObject to add.
- Returns
True if added successfully, false if pool is full.
Definition at line 211 of file GameObjectPool.ixx.
References activeCount, helios::engine::ecs::EntityHandle::entityId, inactiveCount, inactiveGameObjects_, helios::engine::ecs::EntityHandle::isValid, locked_, maxEntityId_, minEntityId_ and size.
guid()
| inline nodiscard noexcept |
Returns the unique identifier of this pool.
- Returns
The Guid assigned to this pool instance.
Definition at line 130 of file GameObjectPool.ixx.
Reference guid_.
inactiveCount()
| inline nodiscard noexcept |
Returns the number of inactive game objects.
- Returns
The number of inactive game objects.
Definition at line 345 of file GameObjectPool.ixx.
Reference inactiveGameObjects_.
Referenced by addInactive.
inactiveGameObjects()
| inline |
Returns a span of all inactive EntityHandles.
- Returns
Span of inactive EntityHandles.
Definition at line 354 of file GameObjectPool.ixx.
Reference inactiveGameObjects_.
isLocked()
| inline nodiscard noexcept |
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 194 of file GameObjectPool.ixx.
References activeIndex_, delta_, helios::engine::ecs::types::EntityTombstone, locked_, maxEntityId_, minEntityId_ and versionIndex_.
release()
| inline |
Releases a GameObject 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 GameObject 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 244 of file GameObjectPool.ixx.
References activeGameObjects_, activeIndex_, delta_, helios::engine::ecs::EntityHandle::entityId, helios::engine::ecs::types::EntityTombstone, inactiveGameObjects_, helios::engine::ecs::EntityHandle::isValid, helios::engine::ecs::EntityHandle::versionId and versionIndex_.
releaseAndRemove()
| inline |
Releases and permanently removes a GameObject 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 GameObject to remove.
- Returns
True if removed successfully, false if EntityHandle was not active.
Definition at line 298 of file GameObjectPool.ixx.
References activeGameObjects_, activeIndex_, delta_, helios::engine::ecs::EntityHandle::entityId, helios::engine::ecs::types::EntityTombstone, helios::engine::ecs::EntityHandle::isValid, helios::engine::ecs::EntityHandle::versionId and versionIndex_.
size()
| inline nodiscard noexcept |
Returns the maximum capacity of this pool.
- Returns
The pool size specified at construction.
Definition at line 139 of file GameObjectPool.ixx.
Reference poolSize_.
Referenced by addInactive.
Protected Member Attributes
activeGameObjects_
| protected |
List of EntityHandles for currently active (in-use) GameObjects.
Definition at line 67 of file GameObjectPool.ixx.
Referenced by acquire, activeCount, activeGameObjects, GameObjectPool, release and releaseAndRemove.
activeIndex_
| protected |
Maps active GameObject EntityIds to their index in activeGameObjects_.
Enables O(1) lookup for release operations.
Definition at line 55 of file GameObjectPool.ixx.
Referenced by acquire, lock, release and releaseAndRemove.
delta_
| protected |
Offset for sparse array indexing (equals minEntityId_ after lock).
Definition at line 92 of file GameObjectPool.ixx.
Referenced by acquire, lock, release and releaseAndRemove.
guid_
| protected |
Unique identifier for this pool instance.
Definition at line 97 of file GameObjectPool.ixx.
Referenced by GameObjectPool and guid.
inactiveGameObjects_
| protected |
List of EntityHandles for currently inactive (available) GameObjects.
Definition at line 72 of file GameObjectPool.ixx.
Referenced by acquire, addInactive, GameObjectPool, inactiveCount, inactiveGameObjects and release.
locked_
| protected |
True if the pool is locked and ready for acquire/release operations.
Definition at line 102 of file GameObjectPool.ixx.
Referenced by addInactive, isLocked and lock.
maxEntityId_
| protected |
Maximum EntityId in the pool (used for sparse array sizing).
Definition at line 87 of file GameObjectPool.ixx.
Referenced by addInactive and lock.
minEntityId_
| protected |
Minimum EntityId in the pool (used for sparse array offset).
Definition at line 82 of file GameObjectPool.ixx.
Referenced by addInactive and lock.
poolSize_
| protected |
The maximum number of objects this pool manages.
Definition at line 77 of file GameObjectPool.ixx.
Referenced by GameObjectPool and size.
versionIndex_
| protected |
Tracks version numbers for active EntityHandles.
Used to validate that a release operation targets the correct entity version.
Definition at line 62 of file GameObjectPool.ixx.
Referenced by acquire, lock, release and releaseAndRemove.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.