Skip to main content

EntityManager Class Template

Manages entities and their associated components. More...

Declaration

template <typename THandle, typename TEntityRegistry, size_t TCapacity> class helios::ecs::EntityManager<THandle, TEntityRegistry, TCapacity> { ... }

Public Member Typedefs Index

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
usingEntityRegistry_type = TEntityRegistry

The EntityRegistry type used for handle allocation and versioning. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
usingHandle_type = THandle

The entity handle type managed by this instance. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
usingStrongId_type = Handle_type::StrongId_type

Strong identifier type derived from the handle. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
usingComponentTypeId_type = ComponentTypeId< Handle_type >

ComponentTypeId specialisation bound to THandle. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
usingComponentOpsRegistry_type = ComponentOpsRegistry< Handle_type >

ComponentOpsRegistry specialisation bound to THandle. More...

Public Constructors Index

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
EntityManager (const EntityManager &)=delete

Non-copyable: copying an EntityManager is explicitly disabled. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
EntityManager (EntityManager &&) noexcept=default

Move constructor. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
EntityManager (const size_t capacity=TCapacity)

Constructs an EntityManager with the given capacity. More...

Public Operators Index

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
EntityManager &operator= (const EntityManager &)=delete
template <typename THandle, typename TEntityRegistry, size_t TCapacity>
EntityManager &operator= (EntityManager &&) noexcept=default

Move-assignment operator. More...

Public Member Functions Index

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
Handle_typecreate (StrongId_type strongId=StrongId_type{})

Creates a new entity. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
boolisValid (const Handle_type handle) const noexcept

Checks if an entity handle is valid. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
boolisValid (const EntityId entityId) const noexcept

Checks if an entity ID is valid. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
booldestroy (const Handle_type handle)

Destroys an entity and invalidates its handle. More...

template <typename T>
T *get (const Handle_type handle) const

Retrieves a component for the given entity. More...

template <typename T>
auto sparseSet () -> SparseSet< T > *

Returns the SparseSet for a component type. More...

template <typename T>
auto sparseSet () const -> const SparseSet< T > *

Returns the SparseSet for a component type (const). More...

template <typename T>
boolhas (const Handle_type handle) const

Checks whether an entity has a specific component. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
boolhas (const Handle_type handle, const ComponentTypeId_type typeId) const

Checks whether an entity has a component by type ID. More...

template <typename TComponent>
auto ensureSparseSet () -> SparseSet< TComponent > *

Ensures that a SparseSet for TComponent exists, creating it if necessary. More...

template <typename T, typename... Args>
T *emplace (const Handle_type handle, Args &&...args)

Constructs and attaches a component to an entity. More...

template <typename T, typename... Args>
T *emplaceOrGet (const Handle_type handle, Args &&...args)

Returns existing component or creates a new one. More...

template <typename T>
boolremove (const Handle_type &handle)

Removes a specific component from an entity. More...

template <typename TComponent>
boolmanagesDirty ()

Returns true if the specified ComponentType is managed by this EntityManager. More...

template <typename TComponent>
voidtrackDirty ()

Tracks this ComponentType with DirtyComponentSpec. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
voidclearAllDirtySets ()

Clears all dirty sets that have been registered via trackDirty(). More...

template <typename... T>
voidclearDirtySet ()

Checks if the SparseSet for DirtyComponentSpec<T> exists and clears it. More...

template <typename TFunc>
voidforEachComponentTypeId (const Handle_type handle, TFunc &&func) const

Invokes func for every component type ID attached to an entity. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
voidclone (const Handle_type source, const Handle_type target)

Clones all components from source entity to target entity. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
void *raw (const Handle_type handle, const ComponentTypeId_type typeId) const

Returns raw void pointer to a component. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
Handle_typehandle (const EntityId entityId) const

Reconstructs an EntityHandle from an EntityId. More...

Private Member Attributes Index

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
std::vector< size_t >registeredDirtySets_

Type IDs of all dirty sets registered via trackDirty(), used by clearAllDirtySets(). More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
std::vector< std::unique_ptr< SparseSetBase > >components_

Component storage indexed by type ID. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
EntityRegistry_typeregistry_

Entity registry owned by this EntityManager. More...

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
const size_tcapacity_

Initial capacity for the underlying sparse sets. More...

Description

Manages entities and their associated components.

EntityManager provides a unified interface for creating entities and attaching/retrieving components. It delegates handle management to an EntityRegistry and stores component data in type-specific SparseSet containers.

Definition at line 78 of file EntityManager.ixx.

Public Member Typedefs

ComponentOpsRegistry_type

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
using helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::ComponentOpsRegistry_type = ComponentOpsRegistry<Handle_type>

ComponentOpsRegistry specialisation bound to THandle.

Definition at line 92 of file EntityManager.ixx.

ComponentTypeId_type

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
using helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::ComponentTypeId_type = ComponentTypeId<Handle_type>

ComponentTypeId specialisation bound to THandle.

Definition at line 90 of file EntityManager.ixx.

EntityRegistry_type

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
using helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::EntityRegistry_type = TEntityRegistry

The EntityRegistry type used for handle allocation and versioning.

Definition at line 84 of file EntityManager.ixx.

84 using EntityRegistry_type = TEntityRegistry;

Handle_type

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
using helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::Handle_type = THandle

The entity handle type managed by this instance.

Definition at line 86 of file EntityManager.ixx.

86 using Handle_type = THandle;

StrongId_type

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
using helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::StrongId_type = Handle_type::StrongId_type

Strong identifier type derived from the handle.

Definition at line 88 of file EntityManager.ixx.

88 using StrongId_type = Handle_type::StrongId_type;

Public Constructors

EntityManager()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::EntityManager (const EntityManager &)
delete

Non-copyable: copying an EntityManager is explicitly disabled.

Definition at line 97 of file EntityManager.ixx.

EntityManager()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::EntityManager (EntityManager &&)
noexcept default

Move constructor.

Definition at line 103 of file EntityManager.ixx.

EntityManager()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::EntityManager (const size_t capacity=TCapacity)
inline explicit

Constructs an EntityManager with the given capacity.

Creates an internally owned EntityRegistry initialized with the specified capacity.

Parameters
capacity

Initial capacity for the registry and sparse sets. Defaults to TCapacity.

Definition at line 120 of file EntityManager.ixx.

120 explicit EntityManager(const size_t capacity = TCapacity)
121 : registry_(EntityRegistry_type{capacity}), capacity_(capacity) {
122 int i = 1;
123 }

Public Operators

operator=()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
EntityManager & helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::operator= (const EntityManager &)
delete

Definition at line 98 of file EntityManager.ixx.

operator=()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
EntityManager & helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::operator= (EntityManager &&)
noexcept default

Move-assignment operator.

Definition at line 108 of file EntityManager.ixx.

Public Member Functions

clearAllDirtySets()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
void helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::clearAllDirtySets ()
inline

Clears all dirty sets that have been registered via trackDirty().

Definition at line 496 of file EntityManager.ixx.

497
498 for (const auto id : registeredDirtySets_) {
499 if (id < components_.size() && components_[id]) {
500 components_[id]->clear();
501 }
502 }
503 }

clearDirtySet()

template <typename... T>
void helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::clearDirtySet ()
inline

Checks if the SparseSet for DirtyComponentSpec<T> exists and clears it.

Accepts a variadic list of component types; all matching dirty sets are cleared.

Template Parameters
T

Component types whose dirty sets should be cleared.

Definition at line 513 of file EntityManager.ixx.

514 ([this] {
515 const auto typeId = ComponentTypeId_type::template id<DirtyComponentSpec<T>>().value();
516 if (typeId < components_.size() && components_[typeId]) {
517 components_[typeId]->clear();
518 }
519 }(), ...);
520 }

clone()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
void helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::clone (const Handle_type source, const Handle_type target)
inline

Clones all components from source entity to target entity.

Iterates through all components on the source and copies them to the target using the registered clone function. Skips components that already exist on the target.

Parameters
source

The entity to clone from.

target

The entity to clone to.

Definition at line 556 of file EntityManager.ixx.

556 void clone(const Handle_type source, const Handle_type target) {
557
558 if (!registry_.isValid(source)) {
559 return;
560 }
561
563 [&](const ComponentTypeId_type typeId) {
564 if (!has(target, typeId)) {
565
566 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
567 const void* sourceCmp = raw(source, typeId);
568
569 if (sourceCmp && ops.clone) {
570 ops.clone(this, sourceCmp, &target);
571 }
572 }
573 }
574 );
575
576 }

References helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::forEachComponentTypeId, helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::has, helios::ecs::ComponentOpsRegistry< THandle >::ops and helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::raw.

create()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
Handle_type helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::create (StrongId_type strongId=StrongId_type{})
inline

Creates a new entity.

Delegates to the underlying EntityRegistry to allocate a new handle.

Returns

A valid EntityHandle for the newly created entity.

See Also

EntityRegistry::create

Definition at line 134 of file EntityManager.ixx.

134 [[nodiscard]] Handle_type create(StrongId_type strongId = StrongId_type{}) {
135 return registry_.create(strongId);
136 }

destroy()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
bool helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::destroy (const Handle_type handle)
inline

Destroys an entity and invalidates its handle.

Increments the entity's version in the registry, making all existing handles to this entity stale. Does automatically remove its components from storage.

Parameters
handle

The handle of the entity to destroy.

Returns

true if the entity was destroyed, false if already invalid.

Definition at line 171 of file EntityManager.ixx.

171 [[nodiscard]] bool destroy(const Handle_type handle) {
172
173 if (!registry_.isValid(handle)) {
174 return false;
175 }
176
177 for (size_t i = 0; i < components_.size(); i++) {
178
179 if (!components_[i]) {
180 continue;
181 }
182 const auto typeId = ComponentTypeId_type{i};
183 if (void* rawCmp = raw(handle, typeId)) {
184 if (const auto& ops = ComponentOpsRegistry_type::ops(typeId); ops.onRemove) {
185 ops.onRemove(rawCmp);
186 }
187 }
188
189 components_[i]->remove(handle.entityId);
190 }
191
192 registry_.destroy(handle);
193
194 return true;
195 }

References helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle, helios::ecs::types::ComponentOps::onRemove, helios::ecs::ComponentOpsRegistry< THandle >::ops and helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::raw.

emplace()

template <typename T, typename... Args>
T * helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::emplace (const Handle_type handle, Args &&... args)
inline

Constructs and attaches a component to an entity.

If the component type has not been registered yet, a new SparseSet is created. The component is constructed in-place with the provided arguments. Returns nullptr if the component already exists or if the handle was invalid.

Template Parameters
T

The component type to emplace.

Args

Constructor argument types.

Parameters
handle

The entity to attach the component to.

args

Arguments forwarded to the component constructor.

Returns

Pointer to the newly created component, or nullptr if the handle is invalid.

Definition at line 350 of file EntityManager.ixx.

350 T* emplace(const Handle_type handle, Args&& ...args) {
351
352 if (!registry_.isValid(handle)) {
353 return nullptr;
354 }
355
356 const auto entityId = handle.entityId;
357
358 const auto typeId = ComponentTypeId_type::template id<T>().value();
359
360 auto* sparseSet = ensureSparseSet<T>();
361
362 if (sparseSet->contains(entityId)) {
363 return nullptr;
364 }
365
366 return sparseSet->emplace(entityId, std::forward<Args>(args)...);
367 }

References helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle and helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::sparseSet.

emplaceOrGet()

template <typename T, typename... Args>
T * helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::emplaceOrGet (const Handle_type handle, Args &&... args)
inline

Returns existing component or creates a new one.

Template Parameters
T

The component type.

Args

Constructor argument types.

Parameters
handle

The entity.

args

Arguments forwarded to the constructor if creating.

Returns

Pointer to the existing or newly created component, or nullptr if the handle is invalid.

Definition at line 382 of file EntityManager.ixx.

382 T* emplaceOrGet(const Handle_type handle, Args&& ...args) {
383
384 if (!registry_.isValid(handle)) {
385 return nullptr;
386 }
387
388 auto* raw = emplace<T>(handle, std::forward<Args>(args)...);
389
390 if (!raw) {
391 return get<T>(handle);
392 }
393
394 return raw;
395 }

References helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle and helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::raw.

ensureSparseSet()

template <typename TComponent>
SparseSet< TComponent > * helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::ensureSparseSet ()
inline

Ensures that a SparseSet for TComponent exists, creating it if necessary.

Resizes the component storage vector and allocates a new SparseSet when the type has not been seen before. Safe to call multiple times.

Template Parameters
TComponent

Component type whose storage should be guaranteed.

Returns

Non-owning pointer to the (potentially newly created) SparseSet.

Definition at line 317 of file EntityManager.ixx.

318
319 const auto typeId = ComponentTypeId_type::template id<TComponent>().value();
320
321 if (typeId >= components_.size()) {
322 components_.resize(typeId + 1);
323 }
324
325 if (!components_[typeId]) {
326 components_[typeId] = std::make_unique<SparseSet<TComponent>>(capacity_);
327 }
328
329 return static_cast<SparseSet<TComponent>*>(components_[typeId].get());
330 }

forEachComponentTypeId()

template <typename TFunc>
void helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::forEachComponentTypeId (const Handle_type handle, TFunc && func)
inline

Invokes func for every component type ID attached to an entity.

Iterates over all allocated component slots and calls func(ComponentTypeId) for each type whose SparseSet contains the given entity. Does nothing if the handle is invalid.

Template Parameters
TFunc

Callable with signature void(ComponentTypeId_type).

Parameters
handle

The entity to query.

func

Callback invoked for each attached component type.

Definition at line 534 of file EntityManager.ixx.

534 void forEachComponentTypeId(const Handle_type handle, TFunc&& func) const {
535 if (!registry_.isValid(handle)) {
536 return;
537 }
538
539 for (size_t i = 0; i < components_.size(); i++) {
540 if (components_[i] && components_[i]->contains(handle.entityId)) {
541 std::forward<TFunc>(func)(ComponentTypeId_type{i});
542 }
543 }
544 }

Reference helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle.

Referenced by helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::clone.

get()

template <typename T>
T * helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::get (const Handle_type handle)
inline

Retrieves a component for the given entity.

Template Parameters
T

The component type to retrieve.

Parameters
handle

The entity whose component to retrieve.

Returns

Pointer to the component, or nullptr if the entity is invalid or does not have the requested component.

Definition at line 208 of file EntityManager.ixx.

208 [[nodiscard]] T* get(const Handle_type handle) const {
209 if (!has<T>(handle)) {
210 return nullptr;
211 }
212
213 const auto entityId = handle.entityId;
214 const auto typeId = ComponentTypeId_type::template id<T>().value();
215
216 auto* sparseSet = static_cast<SparseSet<T>*>(components_[typeId].get());
217
218 return sparseSet->get(entityId);
219 }

References helios::ecs::SparseSet< T >::get, helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle and helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::sparseSet.

handle()

has()

template <typename T>
bool helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::has (const Handle_type handle)
inline

Checks whether an entity has a specific component.

Template Parameters
T

The component type to check for.

Parameters
handle

The entity to query.

Returns

true if the entity has the component, false if the handle is invalid or the component is not attached.

Definition at line 270 of file EntityManager.ixx.

270 [[nodiscard]] bool has(const Handle_type handle) const {
271 if (!registry_.isValid(handle)) {
272 return false;
273 }
274
275 const auto typeId = ComponentTypeId_type::template id<T>().value();
276
277 if (typeId < components_.size() && components_[typeId]) {
278 return components_[typeId]->contains(handle.entityId);
279 }
280
281 return false;
282 }

Reference helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle.

Referenced by helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::clone and helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::raw.

has()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
bool helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::has (const Handle_type handle, const ComponentTypeId_type typeId)
inline

Checks whether an entity has a component by type ID.

Parameters
handle

The entity to query.

typeId

The component type identifier.

Returns

true if the entity has the component, false otherwise.

Definition at line 292 of file EntityManager.ixx.

292 [[nodiscard]] bool has(const Handle_type handle,
293 const ComponentTypeId_type typeId) const {
294 if (!registry_.isValid(handle)) {
295 return false;
296 }
297
298 const auto tvalue = typeId.value();
299
300 if (tvalue < components_.size() && components_[tvalue]) {
301 return components_[tvalue]->contains(handle.entityId);
302 }
303
304 return false;
305 }

References helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle and helios::ecs::types::ComponentTypeId< THandle >::value.

isValid()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
bool helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::isValid (const Handle_type handle)
inline noexcept

Checks if an entity handle is valid.

Parameters
handle

The handle to validate.

Returns

true if the handle refers to a living entity.

Definition at line 145 of file EntityManager.ixx.

145 [[nodiscard]] bool isValid(const Handle_type handle) const noexcept {
146 return registry_.isValid(handle);
147 }

Reference helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle.

isValid()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
bool helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::isValid (const EntityId entityId)
inline noexcept

Checks if an entity ID is valid.

Parameters
entityId

The entity ID to validate.

Returns

true if the entity ID refers to a living entity.

Definition at line 156 of file EntityManager.ixx.

156 [[nodiscard]] bool isValid(const EntityId entityId) const noexcept {
157 return registry_.isValid(handle(entityId));
158 }

Reference helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle.

managesDirty()

template <typename TComponent>
bool helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::managesDirty ()
inline

Returns true if the specified ComponentType is managed by this EntityManager.

Template Parameters
TComponent

The component to check.

Returns

true if this component is managed by this Entitymanager.

Definition at line 443 of file EntityManager.ixx.

443 [[nodiscard]] bool managesDirty() {
444
445 auto typeId = ComponentTypeId_type::template id<DirtyComponentSpec<TComponent>>().value();
446
447 return typeId < components_.size() && components_[typeId];
448 }

raw()

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
void * helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::raw (const Handle_type handle, const ComponentTypeId_type typeId)
inline

Returns raw void pointer to a component.

Parameters
handle

The entity.

typeId

The component type identifier.

Returns

Raw pointer to the component, or nullptr if not found.

Definition at line 586 of file EntityManager.ixx.

586 [[nodiscard]] void* raw(const Handle_type handle, const ComponentTypeId_type typeId ) const {
587 if (!has(handle, typeId)) {
588 return nullptr;
589 }
590
591 return components_[typeId.value()]->raw(handle.entityId);
592 }

References helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle, helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::has and helios::ecs::types::ComponentTypeId< THandle >::value.

Referenced by helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::clone, helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::destroy, helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::emplaceOrGet and helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::remove.

remove()

template <typename T>
bool helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::remove (const Handle_type & handle)
inline

Removes a specific component from an entity.

Unlike destroy(), this only removes a single component type while keeping the entity and other components intact.

Template Parameters
T

The component type to remove.

Parameters
handle

The entity whose component to remove.

Returns

true if the component was removed, false if the handle was invalid, the component was not attached, or removal was blocked by onRemove.

See Also

destroy

See Also

SparseSet::remove

Definition at line 415 of file EntityManager.ixx.

415 [[nodiscard]] bool remove(const Handle_type& handle) {
416
417 if (!has<T>(handle)) {
418 return false;
419 }
420
421 const auto typeId = ComponentTypeId_type::template id<T>();
422 void* rawCmp = raw(handle, typeId);
423 if (!rawCmp) {
424 return false;
425 }
426 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
427
428 if (ops.onRemove && !ops.onRemove(rawCmp)) {
429 return false;
430 }
431
432 return components_[typeId.value()]->remove(handle.entityId);
433 }

References helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::handle, helios::ecs::ComponentOpsRegistry< THandle >::ops and helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::raw.

sparseSet()

template <typename T>
SparseSet< T > * helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::sparseSet ()
inline

Returns the SparseSet for a component type.

Template Parameters
T

The component type.

Returns

Pointer to the SparseSet, or nullptr if the type has no storage.

Definition at line 229 of file EntityManager.ixx.

229 [[nodiscard]] SparseSet<T>* sparseSet() {
230
231 const auto typeId = ComponentTypeId_type::template id<T>().value();
232
233 if (typeId >= components_.size() || !components_[typeId]) {
234 return nullptr;
235 }
236
237 return static_cast<SparseSet<T>*>(components_[typeId].get());
238 }

Referenced by helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::emplace and helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::get.

sparseSet()

template <typename T>
const SparseSet< T > * helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::sparseSet ()
inline

Returns the SparseSet for a component type (const).

Template Parameters
T

The component type.

Returns

Const pointer to the SparseSet, or nullptr if the type has no storage.

Definition at line 248 of file EntityManager.ixx.

248 [[nodiscard]] const SparseSet<T>* sparseSet() const {
249
250 const auto typeId = ComponentTypeId_type::template id<T>().value();
251
252 if (typeId >= components_.size()) {
253 return nullptr;
254 }
255
256 return static_cast<SparseSet<T>*>(components_[typeId].get());
257 }

trackDirty()

template <typename TComponent>
void helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::trackDirty ()
inline

Tracks this ComponentType with DirtyComponentSpec.

Template Parameters
TComponent

The Component-type to track.

See Also

clearAllDirtySets()

Definition at line 458 of file EntityManager.ixx.

458 void trackDirty() {
459 auto typeId = ComponentTypeId_type::template id<DirtyComponentSpec<TComponent>>().value();
460
461 if (typeId >= components_.size()) {
462 components_.resize(typeId + 1);
463 }
464
465 // not calling ensureSparseSet() since we need to make sure registeredDirtySets
466 // is pushed once with typeId
467 if (!components_[typeId]) {
468 components_[typeId] = std::make_unique<SparseSet<DirtyComponentSpec<TComponent>>>(capacity_);
469 registeredDirtySets_.push_back(typeId);
470 }
471 #if HELIOS_DEBUG
472 else {
473 bool found = false;
474 for (auto id : registeredDirtySets_) {
475 if (id == typeId) {
476 found = true;
477 break;
478 }
479 }
480 if (!found) {
481 assert(false && "typeId found, but was missing in registeredDirtySets_");
482 //registeredDirtySets_.push_back(typeId);
483 }
484
485 }
486 #endif
487
488
489 }

Private Member Attributes

capacity_

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
const size_t helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::capacity_

Initial capacity for the underlying sparse sets.

Definition at line 626 of file EntityManager.ixx.

626 const size_t capacity_;

components_

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
std::vector<std::unique_ptr<SparseSetBase> > helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::components_

Component storage indexed by type ID.

Definition at line 616 of file EntityManager.ixx.

616 std::vector<std::unique_ptr<SparseSetBase>> components_;

registeredDirtySets_

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
std::vector<size_t> helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::registeredDirtySets_

Type IDs of all dirty sets registered via trackDirty(), used by clearAllDirtySets().

Definition at line 608 of file EntityManager.ixx.

608 std::vector<size_t> registeredDirtySets_;

registry_

template <typename THandle, typename TEntityRegistry, size_t TCapacity>
EntityRegistry_type helios::ecs::EntityManager< THandle, TEntityRegistry, TCapacity >::registry_

Entity registry owned by this EntityManager.

Definition at line 621 of file EntityManager.ixx.

621 EntityRegistry_type registry_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.