ECSTASY
All in the name
Loading...
Searching...
No Matches
ecstasy::VectorStorage< C > Class Template Reference

Linear vector to store entity components. More...

#include <VectorStorage.hpp>

Inheritance diagram for ecstasy::VectorStorage< C >:
Collaboration diagram for ecstasy::VectorStorage< C >:

Public Types

using Component = typename AStorage< C >::Component
 IsStorage constraint.
 
- Public Types inherited from ecstasy::AStorage< C >
using Component = C
 IsStorage constraint.
 
using QueryData = C &
 QueryableObject constraint.
 
using ConstQueryData = const C &
 ConstQueryableObject constraint.
 

Public Member Functions

 VectorStorage (size_t initialCapacity=0)
 Construct a new Vector Storage for a given Component type.
 
 VectorStorage (const VectorStorage &other)=delete
 Copy constructor is deleted.
 
template<typename... Args>
Componentemplace (Entity::Index index, Args &&...args)
 Emplace a new Component instance for a given entity.
 
Componentinsert (Entity::Index index, Component &&c) override final
 Insert a new Component instance associated to the given entity.
 
bool erase (Entity::Index index) override final
 Erase the Component instance associated to the given entity.
 
Componentoperator[] (Entity::Index index) noexcept override final
 Retrieve the Component instance associated to the given entity.
 
const Componentoperator[] (Entity::Index index) const noexcept override final
 Retrieve the Component instance associated to the given entity.
 
constexpr const util::BitSetgetMask () const noexcept override final
 Get the Component Mask.
 
- Public Member Functions inherited from ecstasy::AStorage< C >
virtual bool erase (Entity::Index index)=0
 Erase the Component instance associated to the given entity.
 
void erase (std::span< Entity > entities) override final
 Erase multiple Component instances associated to the given entities.
 
bool contains (Entity::Index index) const noexcept override final
 Test if the entity index match a Component instance.
 
Componentat (Entity::Index index)
 Retrieve the Component instance associated to the given entity and perform bound checking.
 
const Componentat (Entity::Index index) const
 Retrieve the Component instance associated to the given entity and perform bound checking.
 
virtual Componentoperator[] (Entity::Index index)=0
 Retrieve the Component instance associated to the given entity.
 
QueryData getQueryData (Entity::Index index)
 Retrieve the Component instance associated to the given entity.
 
virtual const Componentoperator[] (Entity::Index index) const =0
 Retrieve the const Component instance associated to the given entity.
 
ConstQueryData getQueryData (Entity::Index index) const
 Retrieve the Component instance associated to the given entity.
 
const std::type_infogetComponentTypeInfos () const noexcept override final
 Get the Component stored type infos.
 
virtual Componentinsert (Entity::Index index, Component &&c)=0
 Insert a new Component instance associated to the given entity.
 
- Public Member Functions inherited from ecstasy::IStorage
virtual ~IStorage ()=default
 
virtual constexpr const util::BitSetgetMask () const noexcept=0
 Get the Component Mask.
 
virtual void erase (std::span< Entity > entities)=0
 Erase the components attached to the given entities.
 
virtual bool contains (size_t index) const noexcept=0
 Test if the entity index match a Component instance.
 
virtual const std::type_infogetComponentTypeInfos () const noexcept=0
 Get the Component stored type infos.
 

Private Attributes

std::vector< Component_components
 Components vector.
 
util::BitSet _mask
 Mask to know if a component is set for an entity.
 

Friends

class VectorStorageTest
 

Detailed Description

template<typename C>
class ecstasy::VectorStorage< C >

Linear vector to store entity components.

Recommended for dense components. (ie. components that are attached to all entities)

Note
This storage is not recommended for sparse components as it will waste memory.
Warning
Requires default constructible (for the padding elements) and movable components (including move assignement operator).
Template Parameters
CComponent type.
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 35 of file VectorStorage.hpp.

Member Typedef Documentation

◆ Component

template<typename C >
using ecstasy::VectorStorage< C >::Component = typename AStorage<C>::Component

IsStorage constraint.

Definition at line 38 of file VectorStorage.hpp.

Constructor & Destructor Documentation

◆ VectorStorage() [1/2]

template<typename C >
ecstasy::VectorStorage< C >::VectorStorage ( size_t  initialCapacity = 0)
inline

Construct a new Vector Storage for a given Component type.

Parameters
[in]initialCapacityInitial capacity of 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 48 of file VectorStorage.hpp.

48 : _components(), _mask()
49 {
50 if (initialCapacity) {
51 _components.reserve(initialCapacity);
52 _mask.resize(initialCapacity);
53 }
54 };
util::BitSet _mask
Mask to know if a component is set for an entity.
std::vector< Component > _components
Components vector.
void resize(std::size_t size)
Changes the number of bits stored in this set.
Definition BitSet.hpp:199
T reserve(T... args)

◆ VectorStorage() [2/2]

template<typename C >
ecstasy::VectorStorage< C >::VectorStorage ( const VectorStorage< C > &  other)
delete

Copy constructor is deleted.

Parameters
[in]otherStorage to copy.
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)

Member Function Documentation

◆ emplace()

template<typename C >
template<typename... Args>
Component & ecstasy::VectorStorage< C >::emplace ( Entity::Index  index,
Args &&...  args 
)
inline

Emplace a new Component instance for a given entity.

Note
Overwrite the previous component if any.
Warning
This method may resize the internal vector, which will probably invalidate references to the components.
Template Parameters
ArgsType of the arguments to forward to the component constructor.
Parameters
[in]indexEntity index.
[in]argsArgs to forward to the component constructor.
Returns
Component& Newly created 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 84 of file VectorStorage.hpp.

85 {
86 // Component at index already existing
87 if (_components.size() > index)
88 _components[index] = std::move(Component(std::forward<Args>(args)...));
89 else {
90 _mask.resize(std::max(_mask.size(), index + 1));
91 // Padding elements
92 if (_components.size() < index)
93 _components.resize(index);
94 // New component
95 _components.emplace_back(std::forward<Args>(args)...);
96 }
97 _mask[index] = true;
98 return _components[index];
99 }
typename AStorage< C >::Component Component
IsStorage constraint.
constexpr std::size_t size() const noexcept
Definition BitSet.hpp:87
T emplace_back(T... args)
T max(T... args)
T resize(T... args)
T size(T... args)

◆ erase()

template<typename C >
bool ecstasy::VectorStorage< C >::erase ( Entity::Index  index)
inlinefinaloverridevirtual

Erase the Component instance associated to the given entity.

Note
Does nothing if the index doesn't match with any component (ie if the entity doesn't have a component Component)
Parameters
[in]indexIndex of the entity.
Returns
bool True if the component was erased, false otherwise.
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)
Note
Unset the flag in the mask but effectively delete the component only if it's the last one. Otherwise, it becomes a padding element.

Implements ecstasy::AStorage< C >.

Definition at line 127 of file VectorStorage.hpp.

128 {
129 if (index >= _components.size()) [[unlikely]]
130 return false;
131 size_t effectiveSize;
132
133 _mask[index] = false;
134 effectiveSize = _mask.lastSet() + 1;
135
136 // Last component, we can safely remove it and the padding elements until the last set bit
137 // The size is never lower than 1 because 0 is returned if no bit is set (unsigned number)
138 if (effectiveSize < _mask.size()) {
139 _mask.resize(effectiveSize);
140 _components.resize(effectiveSize);
141 }
142 return true;
143 }
BIT_SET_CONSTEXPR std::size_t lastSet() const
Finds the last set bit in the set.
Definition BitSet.hpp:247

◆ getMask()

template<typename C >
constexpr const util::BitSet & ecstasy::VectorStorage< C >::getMask ( ) const
inlineconstexprfinaloverridevirtualnoexcept

Get the Component Mask.

Note
Each bit set to true mean the entity at the bit index has a component in the storage.
Warning
The mask might be smaller than the entity count.
Returns
const util::BitSet& Component mask.
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-20)

Implements ecstasy::IStorage.

Definition at line 158 of file VectorStorage.hpp.

159 {
160 return _mask;
161 }

◆ insert()

template<typename C >
Component & ecstasy::VectorStorage< C >::insert ( Entity::Index  index,
Component &&  c 
)
inlinefinaloverridevirtual

Insert a new Component instance associated to the given entity.

Parameters
[in]indexIndex of the entity.
[in]cComponent instance to insert.
Returns
Component& Reference to the inserted 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 (2024-06-25)

Implements ecstasy::AStorage< C >.

Definition at line 102 of file VectorStorage.hpp.

103 {
104 if constexpr (!std::movable<Component>)
105 throw std::runtime_error("VectorStorage: Component is not movable");
106 else {
107 // Component at index already existing
108 if (_components.size() > index)
109 _components[index] = std::move(c);
110 else {
111 _mask.resize(std::max(_mask.size(), index + 1));
112 // Padding elements
113 if (_components.size() < index)
114 _components.resize(index);
115 // New component
116 _components.push_back(std::move(c));
117 }
118 _mask[index] = true;
119 return _components[index];
120 }
121 }
T push_back(T... args)

◆ operator[]() [1/2]

template<typename C >
const Component & ecstasy::VectorStorage< C >::operator[] ( Entity::Index  index) const
inlinefinaloverridevirtualnoexcept

Retrieve the Component instance associated to the given entity.

Warning
This function may not perform bound checking. For VectorStorage or MarkerStorage it is noexcept but not for MapStorage for example.
Parameters
[in]indexIndex of the entity.
Returns
Component& Reference to the associated 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)

Implements ecstasy::AStorage< C >.

Definition at line 152 of file VectorStorage.hpp.

153 {
154 return _components[index];
155 }

◆ operator[]() [2/2]

template<typename C >
Component & ecstasy::VectorStorage< C >::operator[] ( Entity::Index  index)
inlinefinaloverridevirtualnoexcept

Retrieve the Component instance associated to the given entity.

Warning
This function may not perform bound checking. For VectorStorage or MarkerStorage it is noexcept but not for MapStorage for example.
Parameters
[in]indexIndex of the entity.
Returns
Component& Reference to the associated 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)

Implements ecstasy::AStorage< C >.

Definition at line 146 of file VectorStorage.hpp.

147 {
148 return _components[index];
149 }

Friends And Related Symbol Documentation

◆ VectorStorageTest

template<typename C >
friend class VectorStorageTest
friend

Definition at line 169 of file VectorStorage.hpp.

Member Data Documentation

◆ _components

template<typename C >
std::vector<Component> ecstasy::VectorStorage< C >::_components
private

Components vector.

Definition at line 165 of file VectorStorage.hpp.

◆ _mask

template<typename C >
util::BitSet ecstasy::VectorStorage< C >::_mask
private

Mask to know if a component is set for an entity.

Definition at line 167 of file VectorStorage.hpp.


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