Gaia-ECS v1.0.0
A simple and powerful entity component system
Loading...
Searching...
No Matches
gaia::ecs::detail::CommandBuffer< AccessContext, Optimize > Class Template Referencefinal

Buffer for deferred execution of some operations on entities. More...

#include <command_buffer.h>

Public Member Functions

 CommandBuffer (World &world)
 Creates a command buffer bound to world.
 
 CommandBuffer (CommandBuffer &&)=delete
 
 CommandBuffer (const CommandBuffer &)=delete
 
CommandBuffer & operator= (CommandBuffer &&)=delete
 
CommandBuffer & operator= (const CommandBuffer &)=delete
 
GAIA_NODISCARD Entity add ()
 Requests a new entity to be created.
 
GAIA_NODISCARD Entity copy (Entity entityFrom)
 Requests a new entity to be created by cloning an already existing entity.
 
GAIA_NODISCARD Entity instantiate (Entity prefabEntity)
 Requests a prefab to be instantiated as a normal entity. Commit replays through World::instantiate, including Prefab removal, the direct Pair(Is, prefab) edge, skipped names, OnInstantiate policies, and recursive prefab children. Non-prefab sources fall back to copy. Exact pair records are a valid source.
 
GAIA_NODISCARD Entity instantiate (Entity prefabEntity, Entity parentInstance)
 Requests a prefab to be instantiated as a normal entity parented under parentInstance. Commit replays through World::instantiate. Pair(Parent, parentInstance) is attached to the spawned root. Non-prefab sources fall back to a parented copy. Exact pair records are a valid source.
 
void instantiate_n (Entity prefabEntity, uint32_t count)
 Requests count prefab instantiations. Optimizing buffers group matching records through World::instantiate_n; plain buffers replay each instance.
 
void instantiate_n (Entity prefabEntity, Entity parentInstance, uint32_t count)
 Requests count prefab instantiations parented under parentInstance.
 
template<typename Func >
void instantiate_n (Entity prefabEntity, uint32_t count, Func func)
 Requests count prefab instantiations and records per-instance commands. The callback runs immediately while recording and receives temporary entity handles, matching add and copy. CopyIter callbacks are World-only.
 
template<typename Func >
void instantiate_n (Entity prefabEntity, Entity parentInstance, uint32_t count, Func func)
 Requests count prefab instantiations parented under parentInstance and records per-instance commands. The callback runs immediately while recording and receives temporary entity handles, matching add and copy. CopyIter callbacks are World-only.
 
template<typename T >
void add (Entity entity)
 Requests a component T to be added to entity.
 
void add (Entity entity, Entity other)
 Requests an entity other to be added to entity entity.
 
void add (Entity entity, const Pair &pair)
 Requests a relationship pair to be added to entity entity.
 
template<typename T >
void add (Entity entity, Entity object, T &&value)
 Requests a component identified by object to be added to entity. Also sets its value.
 
template<typename T >
void add (Entity entity, const Pair &pair, T &&value)
 Requests a relationship pair to be added to entity. Also sets its payload.
 
template<typename T , std::enable_if_t<!is_pair< std::remove_cv_t< std::remove_reference_t< T > > >::value, int > = 0>
void add (Entity entity, T &&value)
 Requests a component T to be added to entity. Also sets its value.
 
template<typename T >
void set (Entity entity, T &&value)
 Requests component data to be set to given values for a given entity.
 
template<typename T >
void set (Entity entity, Entity object, T &&value)
 Requests component data identified by object to be set on entity.
 
template<typename T >
void set (Entity entity, const Pair &pair, T &&value)
 Requests relationship payload identified by pair to be set on entity.
 
void del (Entity entity)
 Requests an existing entity to be removed.
 
template<typename T >
void del (Entity entity)
 Requests removal of component T from entity.
 
void del (Entity entity, Entity object)
 Requests removal of entity object from entity entity.
 
void del (Entity entity, const Pair &pair)
 Requests removal of a relationship pair from entity entity.
 
void commit ()
 Commits all queued changes. Plain buffers replay every request in recorded order, including entity creation and deletion. In optimizing buffers, entity create, copy, and instantiate records are allocated first, then component operations are merged and replayed, then entity deletions are applied. Component groups containing a delete reduce in recorded order from initial membership. Independent table-component groups can share one entity-builder commit before payload replay. Observable or policy-sensitive groups replay through World. Groups with only adds and sets merge using the last payload. Relationship operations retain their order per entity and merge only consecutive requests for the same pair. Optimizing commit does not promise global replay order across entities and components.
 

Detailed Description

template<typename AccessContext, bool Optimize>
class gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >

Buffer for deferred execution of some operations on entities.

Adding, removing, copying, or instantiating entities inside World::each can result in changes of archetypes or chunk structure. This would lead to an undefined behavior. Therefore, such operations have to be executed after the loop is done.

Template Parameters
AccessContextAccess guard. AccessContextST for single-threaded recording, AccessContextMT when multiple threads record into the same buffer.
OptimizeMerge and batch operations when true; replay each request in recorded order when false.

Constructor & Destructor Documentation

◆ CommandBuffer()

template<typename AccessContext , bool Optimize>
gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::CommandBuffer ( World &  world)
inlineexplicit

Creates a command buffer bound to world.

Parameters
worldWorld that receives the recorded operations on commit.

Member Function Documentation

◆ add() [1/7]

template<typename AccessContext , bool Optimize>
GAIA_NODISCARD Entity gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::add ( )
inline

Requests a new entity to be created.

Returns
Entity that will be created. The id is not usable right away. It will be filled with proper data after commit().

◆ add() [2/7]

template<typename AccessContext , bool Optimize>
template<typename T >
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::add ( Entity  entity)
inline

Requests a component T to be added to entity.

Template Parameters
TComponent type.
Parameters
entityDestination entity.
Warning
Component T should be registered in the world before calling this function while the world is locked for iteration. Registering a new component type is a structural change.

◆ add() [3/7]

template<typename AccessContext , bool Optimize>
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::add ( Entity  entity,
const Pair &  pair 
)
inline

Requests a relationship pair to be added to entity entity.

Parameters
entityDestination entity.
pairRelationship pair to add to entity.

◆ add() [4/7]

template<typename AccessContext , bool Optimize>
template<typename T >
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::add ( Entity  entity,
const Pair &  pair,
T &&  value 
)
inline

Requests a relationship pair to be added to entity. Also sets its payload.

Template Parameters
TPayload type stored on pair.
Parameters
entityDestination entity or exact pair record.
pairRelationship pair to attach.
valuePayload value.
Warning
Component T should be registered in the world before calling this function while the world is locked for iteration. Registering a new component type is a structural change. If used in concurrent environment, race conditions may occur otherwise.

◆ add() [5/7]

template<typename AccessContext , bool Optimize>
template<typename T >
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::add ( Entity  entity,
Entity  object,
T &&  value 
)
inline

Requests a component identified by object to be added to entity. Also sets its value.

Template Parameters
TPayload type stored on object.
Parameters
entityDestination entity or exact pair record.
objectComponent entity or exact relationship pair.
valuePayload value.
Warning
Component T should be registered in the world before calling this function while the world is locked for iteration. Registering a new component type is a structural change. If used in concurrent environment, race conditions may occur otherwise.

◆ add() [6/7]

template<typename AccessContext , bool Optimize>
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::add ( Entity  entity,
Entity  other 
)
inline

Requests an entity other to be added to entity entity.

Parameters
entityDestination entity.
otherEntity to add to entity.

◆ add() [7/7]

template<typename AccessContext , bool Optimize>
template<typename T , std::enable_if_t<!is_pair< std::remove_cv_t< std::remove_reference_t< T > > >::value, int > = 0>
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::add ( Entity  entity,
T &&  value 
)
inline

Requests a component T to be added to entity. Also sets its value.

Template Parameters
TComponent type.
Parameters
entityDestination entity.
valueComponent value.
Warning
Component T should be registered in the world before calling this function while the world is locked for iteration. Registering a new component type is a structural change. If used in concurrent environment, race conditions may occur otherwise.

◆ copy()

template<typename AccessContext , bool Optimize>
GAIA_NODISCARD Entity gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::copy ( Entity  entityFrom)
inline

Requests a new entity to be created by cloning an already existing entity.

Parameters
entityFromEntity to clone. Temporary entities from this buffer are resolved on commit.
Returns
Entity that will be created. The id is not usable right away. It will be filled with proper data after commit().

◆ del() [1/4]

template<typename AccessContext , bool Optimize>
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::del ( Entity  entity)
inline

Requests an existing entity to be removed.

Parameters
entityEntity to remove.

◆ del() [2/4]

template<typename AccessContext , bool Optimize>
template<typename T >
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::del ( Entity  entity)
inline

Requests removal of component T from entity.

Template Parameters
TComponent type.
Parameters
entitySource entity.
Warning
Component T must be registered in the world before calling this function. Calling del without a previous add of the component doesn't make sense.

◆ del() [3/4]

template<typename AccessContext , bool Optimize>
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::del ( Entity  entity,
const Pair &  pair 
)
inline

Requests removal of a relationship pair from entity entity.

Parameters
entitySource entity.
pairRelationship pair to remove from entity.

◆ del() [4/4]

template<typename AccessContext , bool Optimize>
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::del ( Entity  entity,
Entity  object 
)
inline

Requests removal of entity object from entity entity.

Parameters
entitySource entity.
objectEntity to remove.

◆ instantiate() [1/2]

template<typename AccessContext , bool Optimize>
GAIA_NODISCARD Entity gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::instantiate ( Entity  prefabEntity)
inline

Requests a prefab to be instantiated as a normal entity. Commit replays through World::instantiate, including Prefab removal, the direct Pair(Is, prefab) edge, skipped names, OnInstantiate policies, and recursive prefab children. Non-prefab sources fall back to copy. Exact pair records are a valid source.

Parameters
prefabEntityPrefab entity or exact pair record to instantiate.
Returns
Temporary entity filled with the spawned root instance after commit().
Warning
The returned entity is not usable until commit(). Prefab children are not exposed as temporaries; look them up with World::find_prefab_instance after commit.

◆ instantiate() [2/2]

template<typename AccessContext , bool Optimize>
GAIA_NODISCARD Entity gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::instantiate ( Entity  prefabEntity,
Entity  parentInstance 
)
inline

Requests a prefab to be instantiated as a normal entity parented under parentInstance. Commit replays through World::instantiate. Pair(Parent, parentInstance) is attached to the spawned root. Non-prefab sources fall back to a parented copy. Exact pair records are a valid source.

Parameters
prefabEntityPrefab entity or exact pair record to instantiate.
parentInstanceEntity receiving the spawned root through Parent, or EntityBad for an unparented root.
Returns
Temporary entity filled with the spawned root instance after commit().
Warning
The returned entity is not usable until commit(). Prefab children are not exposed as temporaries; look them up with World::find_prefab_instance after commit.

◆ instantiate_n() [1/4]

template<typename AccessContext , bool Optimize>
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::instantiate_n ( Entity  prefabEntity,
Entity  parentInstance,
uint32_t  count 
)
inline

Requests count prefab instantiations parented under parentInstance.

Parameters
prefabEntityPrefab entity to instantiate.
parentInstanceEntity receiving each spawned root through Parent.
countNumber of root instances to spawn. Zero is a no-op.

◆ instantiate_n() [2/4]

template<typename AccessContext , bool Optimize>
template<typename Func >
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::instantiate_n ( Entity  prefabEntity,
Entity  parentInstance,
uint32_t  count,
Func  func 
)
inline

Requests count prefab instantiations parented under parentInstance and records per-instance commands. The callback runs immediately while recording and receives temporary entity handles, matching add and copy. CopyIter callbacks are World-only.

Template Parameters
FuncCallback type. Must be invocable as void(Entity).
Parameters
prefabEntityPrefab entity to instantiate.
parentInstanceEntity receiving each spawned root through Parent, or EntityBad.
countNumber of root instances to spawn. Zero is a no-op.
funcFunctor invoked with each temporary root handle while recording.

◆ instantiate_n() [3/4]

template<typename AccessContext , bool Optimize>
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::instantiate_n ( Entity  prefabEntity,
uint32_t  count 
)
inline

Requests count prefab instantiations. Optimizing buffers group matching records through World::instantiate_n; plain buffers replay each instance.

Parameters
prefabEntityPrefab entity to instantiate.
countNumber of root instances to spawn. Zero is a no-op.

◆ instantiate_n() [4/4]

template<typename AccessContext , bool Optimize>
template<typename Func >
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::instantiate_n ( Entity  prefabEntity,
uint32_t  count,
Func  func 
)
inline

Requests count prefab instantiations and records per-instance commands. The callback runs immediately while recording and receives temporary entity handles, matching add and copy. CopyIter callbacks are World-only.

Template Parameters
FuncCallback type. Must be invocable as void(Entity).
Parameters
prefabEntityPrefab entity to instantiate.
countNumber of root instances to spawn. Zero is a no-op.
funcFunctor invoked with each temporary root handle while recording.

◆ set() [1/3]

template<typename AccessContext , bool Optimize>
template<typename T >
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::set ( Entity  entity,
const Pair &  pair,
T &&  value 
)
inline

Requests relationship payload identified by pair to be set on entity.

Template Parameters
TPayload type stored on pair.
Parameters
entityDestination entity or exact pair record.
pairRelationship pair whose payload is written.
valuePayload value.
Warning
Component T must be registered in the world before calling this function. Calling set without a previous add of the component doesn't make sense.

◆ set() [2/3]

template<typename AccessContext , bool Optimize>
template<typename T >
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::set ( Entity  entity,
Entity  object,
T &&  value 
)
inline

Requests component data identified by object to be set on entity.

Template Parameters
TPayload type stored on object.
Parameters
entityDestination entity or exact pair record.
objectComponent entity or exact relationship pair.
valuePayload value.
Warning
Component T must be registered in the world before calling this function. Calling set without a previous add of the component doesn't make sense.

◆ set() [3/3]

template<typename AccessContext , bool Optimize>
template<typename T >
void gaia::ecs::detail::CommandBuffer< AccessContext, Optimize >::set ( Entity  entity,
T &&  value 
)
inline

Requests component data to be set to given values for a given entity.

Template Parameters
TComponent type.
Parameters
entityDestination entity.
valueComponent value.
Warning
Component T must be registered in the world before calling this function. Calling set without a previous add of the component doesn't make sense.

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