ECSTASY
All in the name
Loading...
Searching...
No Matches
ecstasy::Entity Class Reference

Encapsulate an index to an entity. More...

#include <Entity.hpp>

Inheritance diagram for ecstasy::Entity:

Public Types

using Index = size_t
 The entity identifier type.
 
using Generation = size_t
 The entity generation type.
 

Public Member Functions

constexpr Index getIndex () const noexcept
 Get the entity identifier,.
 
constexpr Generation getGeneration () const noexcept
 Get the Generation of the entity.
 
constexpr auto operator<=> (Entity const &other) const noexcept
 Compare two entities using only their identifier.
 
constexpr bool operator== (Entity const &other) const noexcept
 Compare two entities using only their identifier.
 
template<IsStorage S, typename... Args>
S::Component & add (S &storage, Args &&...args)
 Add a component to the entity.
 
template<IsStorage S>
S::Component & operator[] (S &storage)
 If the entity already has an instance of component C, returns it.
 
template<IsStorage S>
const S::Component & operator[] (S &storage) const
 Try to fetch the instance of component C associated to the current entity.
 
template<IsStorage S>
const S::Component & get (S &storage) const
 Try to fetch the instance of component C associated to the current entity.
 
template<IsStorage S>
S::Component & get (S &storage)
 Try to fetch the instance of component C associated to the current entity.
 
template<IsStorage S>
bool has (S &storage) const
 Test if the entity has an associated component in the storage S.
 

Protected Member Functions

constexpr Entity (Index index, Generation generation) noexcept
 The entity structure may only be constructed by builders.
 

Protected Attributes

Index _index
 The entity identifier.
 
Generation _generation
 The entity generation.
 

Friends

class ::ecstasy::Entities
 The 'Entities' Resource is the only class able to create entities.
 

Detailed Description

Encapsulate an index to an entity.

Note
The entity may not exist (ie an id doesn't mean a living entity)
This class is trivially copiable because it doesn't store the entity components.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 35 of file Entity.hpp.

Member Typedef Documentation

◆ Generation

The entity generation type.

Definition at line 40 of file Entity.hpp.

◆ Index

using ecstasy::Entity::Index = size_t

The entity identifier type.

Definition at line 38 of file Entity.hpp.

Constructor & Destructor Documentation

◆ Entity()

constexpr ecstasy::Entity::Entity ( Index  index,
Generation  generation 
)
inlineconstexprprotectednoexcept

The entity structure may only be constructed by builders.

Parameters
[in]indexThe entity identifier.
[in]generationThe entity generation.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 236 of file Entity.hpp.

236: _index(index), _generation(generation){};
Index _index
The entity identifier.
Definition Entity.hpp:223
Generation _generation
The entity generation.
Definition Entity.hpp:225

Member Function Documentation

◆ add()

template<IsStorage S, typename... Args>
S::Component & ecstasy::Entity::add ( S &  storage,
Args &&...  args 
)
inline

Add a component to the entity.

Template Parameters
CComponent type.
ArgsType of the arguments to forward to the component constructor.
Parameters
[in]storageStorage for type C.
[in]argsArguments to forward to the component constructor.
Returns
C& Reference to the newly created component.
Exceptions
std::logic_errorIf the component was already present.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-19)

Definition at line 111 of file Entity.hpp.

112 {
113 if (storage.contains(_index)) [[unlikely]]
114 throw std::logic_error(std::string("Trying to add twice the component ")
115 + typeid(typename S::Component).name() + " on the same entity.");
116 return storage.emplace(_index, std::forward<Args>(args)...);
117 }

◆ get() [1/2]

template<IsStorage S>
S::Component & ecstasy::Entity::get ( S &  storage)
inline

Try to fetch the instance of component C associated to the current entity.

Template Parameters
CType of the component to retrieve.
Parameters
[in]storageStorage for type C.
Returns
C& Reference to the entity instance of C associated to the entity.
Exceptions
std::out_of_rangeIf no associated instance found.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-19)

Definition at line 198 of file Entity.hpp.

199 {
200 return storage[_index];
201 }

◆ get() [2/2]

template<IsStorage S>
const S::Component & ecstasy::Entity::get ( S &  storage) const
inline

Try to fetch the instance of component C associated to the current entity.

Template Parameters
CType of the component to retrieve.
Parameters
[in]storageStorage for type C.
Returns
const C& Const reference to the entity instance of C associated to the entity.
Exceptions
std::out_of_rangeIf no associated instance found.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-19)

Definition at line 178 of file Entity.hpp.

179 {
180 return storage[_index];
181 }

◆ getGeneration()

constexpr Generation ecstasy::Entity::getGeneration ( ) const
inlineconstexprnoexcept

Get the Generation of the entity.

Alive entities always have a generation number strictly positive. If two entities have the same id, it mean the one with the lowest generation is outdated and no longer exists.

Note
A generation greater than 0 doesn't mean the entity is always alive.
Returns
Generation The generation of this entity.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 67 of file Entity.hpp.

68 {
69 return _generation;
70 }

◆ getIndex()

constexpr Index ecstasy::Entity::getIndex ( ) const
inlineconstexprnoexcept

Get the entity identifier,.

Returns
Index entity index (identifier)
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 50 of file Entity.hpp.

51 {
52 return _index;
53 }

◆ has()

template<IsStorage S>
bool ecstasy::Entity::has ( S &  storage) const
inline

Test if the entity has an associated component in the storage S.

Template Parameters
SStorage of a component.
Parameters
[in]storageStorage for the component type searched.
Returns
bool True if the entity has an associated entry in the storage.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-19)

Definition at line 216 of file Entity.hpp.

217 {
218 return storage.contains(_index);
219 }

◆ operator<=>()

constexpr auto ecstasy::Entity::operator<=> ( Entity const &  other) const
inlineconstexprnoexcept

Compare two entities using only their identifier.

Used to sort entities.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 78 of file Entity.hpp.

79 {
80 return this->_index <=> other._index;
81 }

◆ operator==()

constexpr bool ecstasy::Entity::operator== ( Entity const &  other) const
inlineconstexprnoexcept

Compare two entities using only their identifier.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 89 of file Entity.hpp.

90 {
91 return this->_index == other._index;
92 }

◆ operator[]() [1/2]

template<IsStorage S>
S::Component & ecstasy::Entity::operator[] ( S &  storage)
inline

If the entity already has an instance of component C, returns it.

If it doesn't, create a new instance of C for this entity and return it.

Note
You should use add to create new components with constructor parameters and get to retrieve them.
Template Parameters
CType of the component to retrieve.
Parameters
[in]storageStorage for type C.
Returns
C& Reference to the entity instance of C associated to the entity. May be a new component.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-19)

Definition at line 136 of file Entity.hpp.

137 {
138 if (!storage.contains(_index)) [[unlikely]]
139 return storage.emplace(_index);
140 return storage[_index];
141 }

◆ operator[]() [2/2]

template<IsStorage S>
const S::Component & ecstasy::Entity::operator[] ( S &  storage) const
inline

Try to fetch the instance of component C associated to the current entity.

Template Parameters
CType of the component to retrieve.
Parameters
[in]storageStorage for type C.
Returns
const C& Reference to the entity instance of C associated to the entity.
Exceptions
std::out_of_rangeIf no associated instance found.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-19)

Definition at line 158 of file Entity.hpp.

159 {
160 return storage[_index];
161 }

Friends And Related Symbol Documentation

◆ ::ecstasy::Entities

friend class ::ecstasy::Entities
friend

The 'Entities' Resource is the only class able to create entities.

Definition at line 239 of file Entity.hpp.

Member Data Documentation

◆ _generation

Generation ecstasy::Entity::_generation
protected

The entity generation.

Definition at line 225 of file Entity.hpp.

◆ _index

Index ecstasy::Entity::_index
protected

The entity identifier.

Definition at line 223 of file Entity.hpp.


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