CoriEngine
Loading...
Searching...
No Matches
Cori::World::DisposableEntityPool< PoolSize > Class Template Reference

Creates a pool with disposable entities. More...

#include <DisposableEntityPool.hpp>

Public Member Functions

 DisposableEntityPool ()
void Init (SceneHandle scene, std::function< Entity(SceneHandle &)> &&compositor, std::function< void(Entity &)> &&resetter)
 Initializes the pool.
std::expected< std::pair< Entity, uint16_t >, Core::PossibleErrors< Core::CoriError< uint16_t >, Core::CoriError<> > > GetFreeEntity ()
 Finds a free entity in a pool (if any) in O(1) time. After retrieving the entity will be considered busy and will no longer be retrievable until you call FreeIndex on this entity index.
std::expected< Entity, Core::PossibleErrors< Core::CoriError< uint16_t >, Core::CoriError<> > > GetEntityAtIndex (const uint16_t index)
 Retries the entity at a particular index, regardless if it's free or not.
void FreeIndex (const uint16_t slot)
 Frees the entity at the given index, a resetter will be run for the entity at the specified index, the entity will be considered free after calling this.
uint16_t Capacity () const
 Returns the total capacity of the pool, how many entities can it fit in total.
uint16_t Size () const
 Returns the amount of free entities currently present in the pool.
void FreeAll ()
 Frees all indexes at once.

Detailed Description

template<uint16_t PoolSize>
class Cori::World::DisposableEntityPool< PoolSize >

Creates a pool with disposable entities.

It is usefully when you need some entity to perform some task, but you don't want to keep track of it, so basically fire and forget style.
Without this class you would need to:
1: Create the entity
2: Add components that you need to it
3: Do the work you need to do
4: Delete the entity
This is far from optimal, DisposableEntityPool lets you create and define your entities once and then reuse them.

Template Parameters
PoolSizeDesired size of the pool.

Definition at line 20 of file DisposableEntityPool.hpp.

Constructor & Destructor Documentation

◆ DisposableEntityPool()

template<uint16_t PoolSize>
Cori::World::DisposableEntityPool< PoolSize >::DisposableEntityPool ( )
inline

Definition at line 22 of file DisposableEntityPool.hpp.

Member Function Documentation

◆ Capacity()

template<uint16_t PoolSize>
uint16_t Cori::World::DisposableEntityPool< PoolSize >::Capacity ( ) const
inlinenodiscard

Returns the total capacity of the pool, how many entities can it fit in total.

Returns
Pool capacity.

Definition at line 102 of file DisposableEntityPool.hpp.

◆ FreeAll()

template<uint16_t PoolSize>
void Cori::World::DisposableEntityPool< PoolSize >::FreeAll ( )
inline

Frees all indexes at once.

Definition at line 117 of file DisposableEntityPool.hpp.

Here is the call graph for this function:

◆ FreeIndex()

template<uint16_t PoolSize>
void Cori::World::DisposableEntityPool< PoolSize >::FreeIndex ( const uint16_t slot)
inline

Frees the entity at the given index, a resetter will be run for the entity at the specified index, the entity will be considered free after calling this.

Parameters
slotSlot or index to free.

Definition at line 88 of file DisposableEntityPool.hpp.

Here is the call graph for this function:

◆ GetEntityAtIndex()

template<uint16_t PoolSize>
std::expected< Entity, Core::PossibleErrors< Core::CoriError< uint16_t >, Core::CoriError<> > > Cori::World::DisposableEntityPool< PoolSize >::GetEntityAtIndex ( const uint16_t index)
inline

Retries the entity at a particular index, regardless if it's free or not.

Parameters
indexIndex to retrieve an entity from.
Returns
Expected object with entity on success, on failure returns PossibleErrors object.

Definition at line 72 of file DisposableEntityPool.hpp.

◆ GetFreeEntity()

template<uint16_t PoolSize>
std::expected< std::pair< Entity, uint16_t >, Core::PossibleErrors< Core::CoriError< uint16_t >, Core::CoriError<> > > Cori::World::DisposableEntityPool< PoolSize >::GetFreeEntity ( )
inline

Finds a free entity in a pool (if any) in O(1) time. After retrieving the entity will be considered busy and will no longer be retrievable until you call FreeIndex on this entity index.

Returns
Expected object with a pair on Entity and its internal index (used to free the entity later) on success, on failure returns PossibleErrors object.

Definition at line 49 of file DisposableEntityPool.hpp.

Here is the call graph for this function:

◆ Init()

template<uint16_t PoolSize>
void Cori::World::DisposableEntityPool< PoolSize >::Init ( SceneHandle scene,
std::function< Entity(SceneHandle &)> && compositor,
std::function< void(Entity &)> && resetter )
inline

Initializes the pool.

Parameters
sceneScene that pool will live in.
compositorEntity compositor, you need to define how to create the entity here, you're given scene handle and expected to return Entity, this functor will be used to create the entities.
resetterLets you define how to clean entities after they've been used, you're given the entity that is being freed, wll be run for every entity that you're freeing with FreeIndex.

Definition at line 33 of file DisposableEntityPool.hpp.

Here is the call graph for this function:

◆ Size()

template<uint16_t PoolSize>
uint16_t Cori::World::DisposableEntityPool< PoolSize >::Size ( ) const
inlinenodiscard

Returns the amount of free entities currently present in the pool.

Returns
Pool size.

Definition at line 110 of file DisposableEntityPool.hpp.


The documentation for this class was generated from the following file: