Skip to main content

Entity Class Template

Lightweight facade for entity component manipulation. More...

Declaration

template <typename TEntityManager> class helios::ecs::Entity<TEntityManager> { ... }

Public Member Typedefs Index

template <typename TEntityManager>
usingHandle_type = TEntityManager::Handle_type
template <typename TEntityManager>
usingComponentTypeId_type = ComponentTypeId< Handle_type >
template <typename TEntityManager>
usingComponentOpsRegistry_type = ComponentOpsRegistry< Handle_type >
template <typename TEntityManager>
usingHierarchyComponent_type = components::HierarchyComponent< Handle_type >
template <typename TEntityManager>
usingActiveComponent_type = components::Active< Handle_type >

Public Constructors Index

template <typename TEntityManager>
Entity (const Handle_type entityHandle, TEntityManager *entityManager) noexcept

Constructs a Entity wrapper. More...

Public Member Functions Index

template <typename TFunc>
voidforEachComponentTypeId (TFunc &&func) const

Returns a generator over all component type IDs attached to this entity. More...

template <typename TEntityManager>
Handle_typehandle () noexcept

Returns the underlying entity handle. More...

template <typename TEntityManager>
Handle_typehandle () const noexcept

Returns the underlying entity handle (const). More...

template <typename T, typename ... Args>
T &add (Args &&...args)

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

template <typename T, typename ... Args>
T &getOrAdd (Args &&...args)

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

template <typename T>
boolremove ()

Removes a component from this entity. More...

template <typename TEntityManager>
void *raw (const ComponentTypeId_type typeId)

Returns raw pointer to component by type ID. More...

template <typename T>
T *get ()

Returns pointer to a component. More...

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

Returns const pointer to a component. More...

template <typename T>
boolhas () const noexcept

Checks if this entity has a specific component type. More...

template <typename TEntityManager>
boolhas (ComponentTypeId_type typeId) const noexcept

Checks if this entity has a component by type ID. More...

template <typename TEntityManager>
voidenableComponent (const ComponentTypeId_type typeId)

Enables a specific component by type ID. More...

template <typename TEntityManager>
voiddisableComponent (const ComponentTypeId_type typeId)

Disables a specific component by type ID. More...

template <typename TEntityManager>
voidsetActive (const bool active)

Sets the activation state of this Entity. More...

template <typename TEntityManager>
boolisActive () const

Returns whether this Entity is active. More...

template <typename TEntityManager>
voidonRelease ()

Notifies all components that the entity is being released to a pool. More...

template <typename TEntityManager>
voidonAcquire ()

Notifies all components that the entity is being acquired from a pool. More...

Private Member Attributes Index

template <typename TEntityManager>
TEntityManager::Handle_typeentityHandle_ {0,0}

The underlying entity identifier. More...

template <typename TEntityManager>
TEntityManager *entityManager_

Non-owning pointer to the EntityManager. More...

Description

Lightweight facade for entity component manipulation.

Entity provides a type-safe, convenient interface for working with entities and their components. It wraps an EntityHandle and a pointer to the EntityManager, delegating all operations.

Definition at line 82 of file Entity.ixx.

Public Member Typedefs

ActiveComponent_type

template <typename TEntityManager>
using helios::ecs::Entity< TEntityManager >::ActiveComponent_type = components::Active<Handle_type>

ComponentOpsRegistry_type

template <typename TEntityManager>
using helios::ecs::Entity< TEntityManager >::ComponentOpsRegistry_type = ComponentOpsRegistry<Handle_type>

ComponentTypeId_type

template <typename TEntityManager>
using helios::ecs::Entity< TEntityManager >::ComponentTypeId_type = ComponentTypeId<Handle_type>

Definition at line 98 of file Entity.ixx.

Handle_type

template <typename TEntityManager>
using helios::ecs::Entity< TEntityManager >::Handle_type = TEntityManager::Handle_type

Definition at line 97 of file Entity.ixx.

97 using Handle_type = TEntityManager::Handle_type;

HierarchyComponent_type

template <typename TEntityManager>
using helios::ecs::Entity< TEntityManager >::HierarchyComponent_type = components::HierarchyComponent<Handle_type>

Public Constructors

Entity()

template <typename TEntityManager>
helios::ecs::Entity< TEntityManager >::Entity (const Handle_type entityHandle, TEntityManager * entityManager)
inline explicit noexcept

Constructs a Entity wrapper.

Parameters
entityHandle

The entity handle to wrap.

entityManager

Pointer to the EntityManager. Must not be null.

Definition at line 109 of file Entity.ixx.

109 explicit Entity(
110 const Handle_type entityHandle,
111 TEntityManager* entityManager
112
113 ) noexcept : entityHandle_(entityHandle), entityManager_(entityManager) {
114 assert(entityManager_ != nullptr && "EntityManager must not be null.");
115 };

Public Member Functions

add()

template <typename T, typename ... Args>
T & helios::ecs::Entity< TEntityManager >::add (Args &&... args)
inline

Constructs and attaches a component to this entity.

If the entity is active, calls onActivate() on the new component. If inactive, calls onDeactivate().

Template Parameters
T

The component type to add.

Args

Constructor argument types.

Parameters
args

Arguments forwarded to the component constructor.

Returns

Reference to the newly created component.

Definition at line 159 of file Entity.ixx.

159 T& add(Args&& ...args) {
160
161 bool active = true;
162
163 if (entityManager_->template has<ActiveComponent_type>(entityHandle_)) {
164 active = true;
165 } else {
166 active = false;
167 }
168
169 auto* cmp = entityManager_->template emplace<T>(entityHandle_, std::forward<Args>(args)...);
170
171 auto typeId = ComponentTypeId_type::template id<T>();
172 const auto ops = ComponentOpsRegistry_type::ops(typeId);
173 void* raw = entityManager_->raw(entityHandle_, typeId);
174
175 if (active && ops.onActivate) {
176 ops.onActivate(raw);
177 } else if (!active && ops.onDeactivate) {
178 ops.onDeactivate(raw);
179 }
180
181 return *cmp;
182 }

References helios::ecs::ComponentOpsRegistry< THandle >::ops and helios::ecs::Entity< TEntityManager >::raw.

disableComponent()

template <typename TEntityManager>
void helios::ecs::Entity< TEntityManager >::disableComponent (const ComponentTypeId_type typeId)
inline

Disables a specific component by type ID.

Parameters
typeId

The component type identifier.

Definition at line 286 of file Entity.ixx.

287 entityManager_->disable(entityHandle_, typeId);
288 }

enableComponent()

template <typename TEntityManager>
void helios::ecs::Entity< TEntityManager >::enableComponent (const ComponentTypeId_type typeId)
inline

Enables a specific component by type ID.

Parameters
typeId

The component type identifier.

Definition at line 277 of file Entity.ixx.

278 entityManager_->enable(entityHandle_, typeId);
279 }

forEachComponentTypeId()

template <typename TFunc>
void helios::ecs::Entity< TEntityManager >::forEachComponentTypeId (TFunc && func)
inline

Returns a generator over all component type IDs attached to this entity.

Returns

Generator yielding ComponentTypeId for each attached component.

Definition at line 123 of file Entity.ixx.

123 void forEachComponentTypeId(TFunc&& func) const {
124 entityManager_->forEachComponentTypeId(entityHandle_, std::forward<TFunc>(func));
125 }

Referenced by helios::ecs::Entity< TEntityManager >::onAcquire, helios::ecs::Entity< TEntityManager >::onRelease and helios::ecs::Entity< TEntityManager >::setActive.

get()

template <typename T>
T * helios::ecs::Entity< TEntityManager >::get ()
inline

Returns pointer to a component.

Template Parameters
T

The component type.

Returns

Pointer to the component, or nullptr if not attached.

Definition at line 233 of file Entity.ixx.

233 T* get() {
234 return entityManager_->template get<T>(entityHandle_);
235 }

get()

template <typename T>
const T * helios::ecs::Entity< TEntityManager >::get ()
inline

Returns const pointer to a component.

Template Parameters
T

The component type.

Returns

Const pointer to the component, or nullptr if not attached.

Definition at line 245 of file Entity.ixx.

245 const T* get() const {
246 return entityManager_->template get<T>(entityHandle_);
247 }

getOrAdd()

template <typename T, typename ... Args>
T & helios::ecs::Entity< TEntityManager >::getOrAdd (Args &&... args)
inline

Returns existing component or creates a new one.

Template Parameters
T

The component type.

Args

Constructor argument types.

Parameters
args

Arguments forwarded to the constructor if creating.

Returns

Reference to the existing or newly created component.

Definition at line 195 of file Entity.ixx.

195 T& getOrAdd(Args&& ...args) {
196 if (entityManager_->template has<T>(entityHandle_)) {
197 return *entityManager_->template get<T>(entityHandle_);
198 }
199 return add<T>(std::forward<Args>(args)...);
200 }

handle()

template <typename TEntityManager>
Handle_type helios::ecs::Entity< TEntityManager >::handle ()
inline noexcept

Returns the underlying entity handle.

Returns

The EntityHandle for this Entity.

Definition at line 132 of file Entity.ixx.

132 [[nodiscard]] Handle_type handle() noexcept {
133 return entityHandle_;
134 }

handle()

template <typename TEntityManager>
Handle_type helios::ecs::Entity< TEntityManager >::handle ()
inline noexcept

Returns the underlying entity handle (const).

Returns

The EntityHandle for this Entity.

Definition at line 141 of file Entity.ixx.

141 [[nodiscard]] Handle_type handle() const noexcept {
142 return entityHandle_;
143 }

has()

template <typename T>
bool helios::ecs::Entity< TEntityManager >::has ()
inline noexcept

Checks if this entity has a specific component type.

Template Parameters
T

The component type to check.

Returns

True if the component is attached, false otherwise.

Definition at line 257 of file Entity.ixx.

257 bool has() const noexcept{
258 return entityManager_->template has<T>(entityHandle_);
259 }

has()

template <typename TEntityManager>
bool helios::ecs::Entity< TEntityManager >::has (ComponentTypeId_type typeId)
inline noexcept

Checks if this entity has a component by type ID.

Parameters
typeId

The component type identifier.

Returns

True if the component is attached, false otherwise.

Definition at line 268 of file Entity.ixx.

268 bool has(ComponentTypeId_type typeId) const noexcept {
269 return entityManager_->has(entityHandle_, typeId);
270 }

isActive()

template <typename TEntityManager>
bool helios::ecs::Entity< TEntityManager >::isActive ()
inline

Returns whether this Entity is active.

Returns

True if the entity has the Active tag component.

Definition at line 353 of file Entity.ixx.

353 [[nodiscard]] bool isActive() const {
354 return entityManager_->template has<ActiveComponent_type>(entityHandle_);
355 }

Referenced by helios::ecs::Entity< TEntityManager >::setActive.

onAcquire()

template <typename TEntityManager>
void helios::ecs::Entity< TEntityManager >::onAcquire ()
inline

Notifies all components that the entity is being acquired from a pool.

Iterates through all attached components and calls onAcquire() on those that implement it.

Definition at line 385 of file Entity.ixx.

385 void onAcquire() {
387 [&](const ComponentTypeId_type typeId) {
388 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
389
390 if (ops.onAcquire) {
391 void* raw = entityManager_->raw(entityHandle_, typeId);
392 ops.onAcquire(raw);
393 }
394 }
395 );
396 }

References helios::ecs::Entity< TEntityManager >::forEachComponentTypeId, helios::ecs::ComponentOpsRegistry< THandle >::ops and helios::ecs::Entity< TEntityManager >::raw.

onRelease()

template <typename TEntityManager>
void helios::ecs::Entity< TEntityManager >::onRelease ()
inline

Notifies all components that the entity is being released to a pool.

Iterates through all attached components and calls onRelease() on those that implement it.

Definition at line 363 of file Entity.ixx.

363 void onRelease() {
364
366 [&](const ComponentTypeId_type typeId) {
367 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
368
369 if (ops.onRelease) {
370 void* raw = entityManager_->raw(entityHandle_, typeId);
371 ops.onRelease(raw);
372 }
373 }
374 );
375
376
377 }

References helios::ecs::Entity< TEntityManager >::forEachComponentTypeId, helios::ecs::ComponentOpsRegistry< THandle >::ops and helios::ecs::Entity< TEntityManager >::raw.

raw()

template <typename TEntityManager>
void * helios::ecs::Entity< TEntityManager >::raw (const ComponentTypeId_type typeId)
inline

Returns raw pointer to component by type ID.

Parameters
typeId

The component type identifier.

Returns

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

Definition at line 221 of file Entity.ixx.

221 void* raw(const ComponentTypeId_type typeId) {
222 return entityManager_->raw(entityHandle_, typeId);
223 }

Referenced by helios::ecs::Entity< TEntityManager >::add, helios::ecs::Entity< TEntityManager >::onAcquire, helios::ecs::Entity< TEntityManager >::onRelease and helios::ecs::Entity< TEntityManager >::setActive.

remove()

template <typename T>
bool helios::ecs::Entity< TEntityManager >::remove ()
inline

Removes a component from this entity.

Template Parameters
T

The component type to remove.

Returns

True if the component was removed, false if not present.

Definition at line 210 of file Entity.ixx.

210 bool remove() {
211 return entityManager_->template remove<T>(entityHandle_);
212 }

setActive()

template <typename TEntityManager>
void helios::ecs::Entity< TEntityManager >::setActive (const bool active)
inline

Sets the activation state of this Entity.

When deactivated:

  • The Active tag component is removed
  • onDeactivate() is called on components that support it
  • If a HierarchyComponent is present, it is marked dirty for propagation

When activated:

  • The Inactive tag component is removed
  • An Active tag component is added
  • onActivate() is called on components that support it
  • If a HierarchyComponent is present, it is marked dirty for propagation
info

Does not call enable()/disable() on components.

Parameters
active

True to activate, false to deactivate.

See Also

HierarchyComponent_type

See Also

HierarchyPropagationSystem

Definition at line 311 of file Entity.ixx.

311 void setActive(const bool active) {
312 bool isActive = entityManager_->template has<ActiveComponent_type>(entityHandle_);
313 bool isInActive = !isActive;
314
315 if (!isActive && active) {
316 auto* hc = entityManager_->template get<HierarchyComponent_type>(entityHandle_);
317 if (hc) {
318 hc->markDirty();
319 }
320
321 entityManager_->template emplaceOrGet<ActiveComponent_type>(entityHandle_);
322 }
323
324 if (!isInActive && !active) {
325 auto* hc = entityManager_->template get<HierarchyComponent_type>(entityHandle_);
326 if (hc) {
327 hc->markDirty();
328 }
329
330 entityManager_->template remove<ActiveComponent_type>(entityHandle_);
331 }
332
334 [&](const ComponentTypeId_type typeId) {
335 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
336
337 if (active && ops.onActivate) {
338 void* raw = entityManager_->raw(entityHandle_, typeId);
339 ops.onActivate(raw);
340 } else if (!active && ops.onDeactivate) {
341 void* raw = entityManager_->raw(entityHandle_, typeId);
342 ops.onDeactivate(raw);
343 }
344 }
345 );
346 }

References helios::ecs::Entity< TEntityManager >::forEachComponentTypeId, helios::ecs::Entity< TEntityManager >::isActive, helios::ecs::ComponentOpsRegistry< THandle >::ops and helios::ecs::Entity< TEntityManager >::raw.

Private Member Attributes

entityHandle_

template <typename TEntityManager>
TEntityManager::Handle_type helios::ecs::Entity< TEntityManager >::entityHandle_ {0,0}

The underlying entity identifier.

Definition at line 87 of file Entity.ixx.

87 TEntityManager::Handle_type entityHandle_{0,0};

entityManager_

template <typename TEntityManager>
TEntityManager* helios::ecs::Entity< TEntityManager >::entityManager_

Non-owning pointer to the EntityManager.

Definition at line 92 of file Entity.ixx.

92 TEntityManager* entityManager_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.