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

Associative Map to store entity components Recommended for sparse components. More...

#include <MapStorage.hpp>

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

Public Types

using Component = typename AStorage< C >::Component
 
- 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

 MapStorage ()=default
 Construct a new Map Storage for a given Component type.
 
 MapStorage (const MapStorage &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) override final
 Retrieve the Component instance associated to the given entity.
 
const Componentoperator[] (Entity::Index index) const override final
 Retrieve the Component instance associated to the given entity.
 
size_t size () const noexcept
 Get the number of Component instances.
 
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::unordered_map< Entity::Index, Component_components
 Components map.
 
util::BitSet _mask
 Component mask.
 

Detailed Description

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

Associative Map to store entity components Recommended for sparse components.

(ie. components that are not attached to all entities)

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 31 of file MapStorage.hpp.

Member Typedef Documentation

◆ Component

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

Definition at line 33 of file MapStorage.hpp.

Constructor & Destructor Documentation

◆ MapStorage() [1/2]

template<typename C >
ecstasy::MapStorage< C >::MapStorage ( )
default

Construct a new Map Storage for a given Component 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)

◆ MapStorage() [2/2]

template<typename C >
ecstasy::MapStorage< C >::MapStorage ( const MapStorage< 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::MapStorage< C >::emplace ( Entity::Index  index,
Args &&...  args 
)
inline

Emplace a new Component instance for a given entity.

Note
No check is done to see if the entity already has the component.
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 68 of file MapStorage.hpp.

69 {
70 _mask.resize(std::max(_mask.size(), index + 1));
71 _mask[index] = true;
72 return _components.emplace(std::make_pair(index, Component(std::forward<Args>(args)...))).first->second;
73 }
typename AStorage< C >::Component Component
util::BitSet _mask
Component mask.
std::unordered_map< Entity::Index, Component > _components
Components map.
void resize(std::size_t size)
Changes the number of bits stored in this set.
Definition BitSet.hpp:199
constexpr std::size_t size() const noexcept
Definition BitSet.hpp:87
T emplace(T... args)
T make_pair(T... args)
T max(T... args)

◆ erase()

template<typename C >
bool ecstasy::MapStorage< 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)

Implements ecstasy::AStorage< C >.

Definition at line 88 of file MapStorage.hpp.

89 {
90 auto it = _components.find(index);
91
92 if (it != _components.end()) [[likely]] {
93 _components.erase(index);
94 _mask[index] = false;
95 return true;
96 }
97 return false;
98 }
T end(T... args)
T erase(T... args)
T find(T... args)

◆ getMask()

template<typename C >
constexpr const util::BitSet & ecstasy::MapStorage< 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 126 of file MapStorage.hpp.

127 {
128 return _mask;
129 }

◆ insert()

template<typename C >
Component & ecstasy::MapStorage< 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 76 of file MapStorage.hpp.

77 {
78 if constexpr (!std::movable<Component>)
79 throw std::runtime_error("MapStorage: Component is not movable");
80 else {
81 _mask.resize(std::max(_mask.size(), index + 1));
82 _mask[index] = true;
83 return _components.emplace(std::make_pair(index, std::move(c))).first->second;
84 }
85 }

◆ operator[]() [1/2]

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

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 107 of file MapStorage.hpp.

108 {
109 return _components.at(index);
110 }
T at(T... args)

◆ operator[]() [2/2]

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

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 101 of file MapStorage.hpp.

102 {
103 return _components.at(index);
104 }

◆ size()

template<typename C >
size_t ecstasy::MapStorage< C >::size ( ) const
inlinenoexcept

Get the number of Component instances.

Returns
size_t number of Component instances.
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 120 of file MapStorage.hpp.

121 {
122 return _components.size();
123 }
T size(T... args)

Member Data Documentation

◆ _components

template<typename C >
std::unordered_map<Entity::Index, Component> ecstasy::MapStorage< C >::_components
private

Components map.

Definition at line 133 of file MapStorage.hpp.

◆ _mask

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

Component mask.

Definition at line 135 of file MapStorage.hpp.


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