NcEngine
nc::ecs::ComponentPool< T > Class Template Referencefinal

Type-aware implementation for component pools. More...

#include <ComponentPool.h>

Inheritance diagram for nc::ecs::ComponentPool< T >:
nc::ecs::ComponentPoolBase nc::StableAddress

Public Types

using value_type = T
 
using iterator = std::vector< T >::iterator
 
using const_iterator = std::vector< T >::const_iterator
 
using reverse_iterator = std::vector< T >::reverse_iterator
 
- Public Types inherited from nc::ecs::ComponentPoolBase
using entity_iterator = std::span< const Entity >::iterator
 

Public Member Functions

 ComponentPool (size_t maxEntities, ComponentHandler< T > handler)
 Construct a new component pool.
 
template<class... Args>
auto Emplace (Entity entity, Args &&... args) -> T &
 Emplace a component attached to an entity.
 
auto Insert (Entity entity, T obj) -> T &
 Insert a component attached to an entity.
 
auto Get (Entity entity) -> T &
 Get a pointer to the component attached to an entity, or nullptr if one does not exist.
 
auto Get (Entity entity) const -> const T &
 Get a const pointer to the component attached to an entity, or nullptr if one does not exist.
 
auto GetParent (const T *component) const -> Entity
 Get the entity a component is attached to, or a null entity on failure.
 
auto GetComponents () noexcept -> std::span< T >
 View all components as a contiguous range.
 
auto GetComponents () const noexcept -> std::span< const T >
 View all components as a contiguous range.
 
auto Handler () noexcept -> ComponentHandler< T > &
 Get the T's ComponentHandler.
 
auto OnAdd() noexcept -> Signal< T & > &auto begin () noexcept
 Get the T's onAdd event Signal. More...
 
auto begin () const noexcept
 Get a const_iterator the the first component in the pool.
 
auto end () noexcept
 Get an iterator one past the last component in the pool.
 
auto end () const noexcept
 Get a const_iterator one past the last component in the pool.
 
auto size () const noexcept
 Get the number of components committed to the pool.
 
auto empty () const noexcept
 Check if there are no components in the pool.
 
auto operator[] (size_t pos) noexcept -> T &
 Get a reference to the component at the specified position.
 
auto operator[] (size_t pos) const noexcept -> const T &
 Get a const reference to the component at the specified position.
 
auto at (size_t pos) -> T &
 Get a reference to the component at the specified position with bounds checking.
 
auto at (size_t pos) const -> const T &
 Get a reference to the component at the specified position with bounds checking.
 
auto data () noexcept
 Get a pointer to the underlying component array.
 
auto data () const noexcept
 Get a pointer to the underlying component array.
 
auto Contains (Entity entity) const -> bool override
 Check if the pool has a component attached to an entity. More...
 
auto GetAsAnyComponent (Entity entity) -> AnyComponent override
 Get the component attached to an entity as an AnyComponent. More...
 
auto Remove (Entity entity) -> bool override
 Remove the component attached to an entity. More...
 
auto Size () const noexcept -> size_t override
 Get the number of components in the pool, excluding those still staged. More...
 
auto StagedSize () const noexcept -> size_t override
 Get the number of staged components waiting to be merged into the pool. More...
 
auto TotalSize () const noexcept -> size_t override
 Get the combined number of components in the pool and staged. More...
 
void Reserve (size_t capacity) override
 Pre-allocate space for some number of components. More...
 
auto GetEntityPool () const noexcept -> std::span< const Entity > override
 Get a span containing the entities with components in the pool. More...
 
auto GetComponentName () const noexcept -> std::string_view override
 Get the name from the component's ComponentHandler. More...
 
auto HasFactory () const noexcept -> bool override
 Check if the factory callback is set in the component's ComponentHandler. More...
 
auto HasUserData () const noexcept -> bool override
 Check if userData is set in the component's ComponentHandler. More...
 
auto HasSerialize () const noexcept -> bool override
 Check if the serialize and deserialize callbacks are set in the component's ComponentHandler. More...
 
auto HasDrawUI () const noexcept -> bool override
 Check if the drawUI callback set in the the component's ComponentHandler. More...
 
auto Id () const noexcept -> size_t override
 Get the component's unique id. More...
 
auto AddDefault (Entity entity) -> AnyComponent override
 Factory-construct a component attached to an entity. More...
 
void Serialize (std::ostream &stream, Entity entity, const SerializationContext &ctx) override
 Serialize the component attached to an entity to a binary stream. More...
 
void Deserialize (std::istream &stream, Entity entity, const DeserializationContext &ctx) override
 Deserialize a component from a binary stream and attach it to an entity. More...
 
void ClearNonPersistent () override
 Remove all components not attached to a persistent entity. More...
 
void Clear () override
 Remove all components. More...
 
void CommitStagedComponents (const std::vector< Entity > &deleted) override
 Finalize pending changes by merging staged components and removing data for any entities deleted from the ComponentRegistry. More...
 
virtual auto Id () const noexcept -> size_t=0
 Get the component's unique id. More...
 
virtual auto Contains (Entity entity) const -> bool=0
 Check if the pool has a component attached to an entity. More...
 
virtual auto GetAsAnyComponent (Entity entity) -> AnyComponent=0
 Get the component attached to an entity as an AnyComponent. More...
 
virtual auto Remove (Entity entity) -> bool=0
 Remove the component attached to an entity. More...
 
virtual auto Size () const noexcept -> size_t=0
 Get the number of components in the pool, excluding those still staged. More...
 
virtual auto StagedSize () const noexcept -> size_t=0
 Get the number of staged components waiting to be merged into the pool. More...
 
virtual auto TotalSize () const noexcept -> size_t=0
 Get the combined number of components in the pool and staged. More...
 
virtual void Reserve (size_t capacity)=0
 Pre-allocate space for some number of components. More...
 
virtual auto GetEntityPool () const noexcept -> std::span< const Entity >=0
 Get a span containing the entities with components in the pool. More...
 
virtual auto GetComponentName () const noexcept -> std::string_view=0
 Get the name from the component's ComponentHandler. More...
 
virtual auto HasFactory () const noexcept -> bool=0
 Check if the factory callback is set in the component's ComponentHandler. More...
 
virtual auto HasUserData () const noexcept -> bool=0
 Check if userData is set in the component's ComponentHandler. More...
 
virtual auto HasSerialize () const noexcept -> bool=0
 Check if the serialize and deserialize callbacks are set in the component's ComponentHandler. More...
 
virtual auto HasDrawUI () const noexcept -> bool=0
 Check if the drawUI callback set in the the component's ComponentHandler. More...
 
virtual auto AddDefault (Entity entity) -> AnyComponent=0
 Factory-construct a component attached to an entity. More...
 
virtual void Serialize (std::ostream &stream, Entity entity, const SerializationContext &ctx)=0
 Serialize the component attached to an entity to a binary stream. More...
 
virtual void Deserialize (std::istream &stream, Entity entity, const DeserializationContext &ctx)=0
 Deserialize a component from a binary stream and attach it to an entity. More...
 
virtual void ClearNonPersistent ()=0
 Remove all components not attached to a persistent entity. More...
 
virtual void Clear ()=0
 Remove all components. More...
 
virtual void CommitStagedComponents (const std::vector< Entity > &deleted)=0
 Finalize pending changes by merging staged components and removing data for any entities deleted from the ComponentRegistry. More...
 
- Public Member Functions inherited from nc::StableAddress
 StableAddress (const StableAddress &)=delete
 
 StableAddress (StableAddress &&)=delete
 
StableAddressoperator= (const StableAddress &)=delete
 
StableAddressoperator= (StableAddress &&)=delete
 

Detailed Description

template<PooledComponent T>
class nc::ecs::ComponentPool< T >

Type-aware implementation for component pools.

Member Function Documentation

◆ AddDefault()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::AddDefault ( Entity  entity) -> AnyComponent
overridevirtual

Factory-construct a component attached to an entity.

Note
Returns a null AnyComponent if there is no registered factory handler.

Implements nc::ecs::ComponentPoolBase.

◆ begin()

template<PooledComponent T>
auto OnAdd() noexcept -> Signal< T & > & auto nc::ecs::ComponentPool< T >::begin ( )
inlinenoexcept

Get the T's onAdd event Signal.

Get the T's onCommit event Signal.

Get the T's onRemove event Signal.

Sort the components according to a predicate.

Get an iterator the the first component in the pool.

◆ Clear()

template<PooledComponent T>
void nc::ecs::ComponentPool< T >::Clear ( )
overridevirtual

Remove all components.

Note
This operation is handled automatically for pools owned by the ComponentRegistry.

Implements nc::ecs::ComponentPoolBase.

◆ ClearNonPersistent()

template<PooledComponent T>
void nc::ecs::ComponentPool< T >::ClearNonPersistent ( )
overridevirtual

Remove all components not attached to a persistent entity.

Note
This operation is handled automatically for pools owned by the ComponentRegistry.

Implements nc::ecs::ComponentPoolBase.

◆ CommitStagedComponents()

template<PooledComponent T>
void nc::ecs::ComponentPool< T >::CommitStagedComponents ( const std::vector< Entity > &  deleted)
overridevirtual

Finalize pending changes by merging staged components and removing data for any entities deleted from the ComponentRegistry.

Note
This operation is handled automatically for pools owned by the ComponentRegistry.

Implements nc::ecs::ComponentPoolBase.

◆ Contains()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::Contains ( Entity  entity) const -> bool
overridevirtual

Check if the pool has a component attached to an entity.

Implements nc::ecs::ComponentPoolBase.

◆ Deserialize()

template<PooledComponent T>
void nc::ecs::ComponentPool< T >::Deserialize ( std::istream &  stream,
Entity  entity,
const DeserializationContext ctx 
)
overridevirtual

Deserialize a component from a binary stream and attach it to an entity.

Implements nc::ecs::ComponentPoolBase.

◆ GetAsAnyComponent()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::GetAsAnyComponent ( Entity  entity) -> AnyComponent
overridevirtual

Get the component attached to an entity as an AnyComponent.

Note
Returns a null object if no component exists.

Implements nc::ecs::ComponentPoolBase.

◆ GetComponentName()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::GetComponentName ( ) const -> std::string_view
inlineoverridevirtualnoexcept

Get the name from the component's ComponentHandler.

Implements nc::ecs::ComponentPoolBase.

◆ GetEntityPool()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::GetEntityPool ( ) const -> std::span< const Entity >
inlineoverridevirtualnoexcept

Get a span containing the entities with components in the pool.

Implements nc::ecs::ComponentPoolBase.

◆ HasDrawUI()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::HasDrawUI ( ) const -> bool
inlineoverridevirtualnoexcept

Check if the drawUI callback set in the the component's ComponentHandler.

Implements nc::ecs::ComponentPoolBase.

◆ HasFactory()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::HasFactory ( ) const -> bool
inlineoverridevirtualnoexcept

Check if the factory callback is set in the component's ComponentHandler.

Implements nc::ecs::ComponentPoolBase.

◆ HasSerialize()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::HasSerialize ( ) const -> bool
inlineoverridevirtualnoexcept

Check if the serialize and deserialize callbacks are set in the component's ComponentHandler.

Implements nc::ecs::ComponentPoolBase.

◆ HasUserData()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::HasUserData ( ) const -> bool
inlineoverridevirtualnoexcept

Check if userData is set in the component's ComponentHandler.

Implements nc::ecs::ComponentPoolBase.

◆ Id()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::Id ( ) const -> size_t
inlineoverridevirtualnoexcept

Get the component's unique id.

Implements nc::ecs::ComponentPoolBase.

◆ Remove()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::Remove ( Entity  entity) -> bool
overridevirtual

Remove the component attached to an entity.

Implements nc::ecs::ComponentPoolBase.

◆ Reserve()

template<PooledComponent T>
void nc::ecs::ComponentPool< T >::Reserve ( size_t  capacity)
overridevirtual

Pre-allocate space for some number of components.

Implements nc::ecs::ComponentPoolBase.

◆ Serialize()

template<PooledComponent T>
void nc::ecs::ComponentPool< T >::Serialize ( std::ostream &  stream,
Entity  entity,
const SerializationContext ctx 
)
overridevirtual

Serialize the component attached to an entity to a binary stream.

Implements nc::ecs::ComponentPoolBase.

◆ Size()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::Size ( ) const -> size_t
inlineoverridevirtualnoexcept

Get the number of components in the pool, excluding those still staged.

Implements nc::ecs::ComponentPoolBase.

◆ StagedSize()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::StagedSize ( ) const -> size_t
inlineoverridevirtualnoexcept

Get the number of staged components waiting to be merged into the pool.

Implements nc::ecs::ComponentPoolBase.

◆ TotalSize()

template<PooledComponent T>
auto nc::ecs::ComponentPool< T >::TotalSize ( ) const -> size_t
inlineoverridevirtualnoexcept

Get the combined number of components in the pool and staged.

Implements nc::ecs::ComponentPoolBase.


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