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... | |
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::core::data::EntityId | minEntityId_ = std::numeric_limits<helios::engine::core::data::EntityId>::max() |
|
Minimum EntityId in the pool (used for sparse array offset). More... | |
| helios::engine::core::data::EntityId | maxEntityId_ = std::numeric_limits<helios::engine::core::data::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 43 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 113 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 152 of file GameObjectPool.ixx.
References activeGameObjects_, activeIndex_, delta_, helios::engine::ecs::EntityHandle::entityId, helios::engine::core::data::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 334 of file GameObjectPool.ixx.
Reference activeGameObjects_.
Referenced by addInactive.
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 209 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 128 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 343 of file GameObjectPool.ixx.
Reference inactiveGameObjects_.
Referenced by addInactive.
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 192 of file GameObjectPool.ixx.
References activeIndex_, delta_, helios::engine::core::data::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 242 of file GameObjectPool.ixx.
References activeGameObjects_, activeIndex_, delta_, helios::engine::ecs::EntityHandle::entityId, helios::engine::core::data::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 296 of file GameObjectPool.ixx.
References activeGameObjects_, activeIndex_, delta_, helios::engine::ecs::EntityHandle::entityId, helios::engine::core::data::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 137 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 65 of file GameObjectPool.ixx.
Referenced by acquire, activeCount, 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 53 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 90 of file GameObjectPool.ixx.
Referenced by acquire, lock, release and releaseAndRemove.
guid_
| protected |
Unique identifier for this pool instance.
Definition at line 95 of file GameObjectPool.ixx.
Referenced by GameObjectPool and guid.
inactiveGameObjects_
| protected |
List of EntityHandles for currently inactive (available) GameObjects.
Definition at line 70 of file GameObjectPool.ixx.
Referenced by acquire, addInactive, GameObjectPool, inactiveCount and release.
locked_
| protected |
True if the pool is locked and ready for acquire/release operations.
Definition at line 100 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 85 of file GameObjectPool.ixx.
Referenced by addInactive and lock.
minEntityId_
| protected |
Minimum EntityId in the pool (used for sparse array offset).
Definition at line 80 of file GameObjectPool.ixx.
Referenced by addInactive and lock.
poolSize_
| protected |
The maximum number of objects this pool manages.
Definition at line 75 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 60 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.