ECSTASY
All in the name
Loading...
Searching...
No Matches
MapStorage.hpp
Go to the documentation of this file.
1
11
12#ifndef ECSTASY_STORAGE_MAPSTORAGE_HPP_
13#define ECSTASY_STORAGE_MAPSTORAGE_HPP_
14
15#include <unordered_map>
16
17#include "AStorage.hpp"
18
19namespace ecstasy
20{
30 template <typename C>
31 class MapStorage : public AStorage<C> {
32 public:
40 MapStorage() = default;
41
50 MapStorage(const MapStorage &other) = delete;
51
67 template <typename... Args>
68 Component &emplace(Entity::Index index, Args &&...args)
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 }
74
76 Component &insert(Entity::Index index, Component &&c) override final
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 }
86
88 bool erase(Entity::Index index) override final
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 }
99
101 [[nodiscard]] Component &operator[](Entity::Index index) override final
102 {
103 return _components.at(index);
104 }
105
107 [[nodiscard]] const Component &operator[](Entity::Index index) const override final
108 {
109 return _components.at(index);
110 }
111
120 [[nodiscard]] size_t size() const noexcept
121 {
122 return _components.size();
123 }
124
126 [[nodiscard]] constexpr const util::BitSet &getMask() const noexcept override final
127 {
128 return _mask;
129 }
130
131 private:
136 };
137} // namespace ecstasy
138
139#endif /* !ECSTASY_STORAGE_MAPSTORAGE_HPP_ */
T at(T... args)
Abstract class for all components storage.
Definition AStorage.hpp:34
C Component
IsStorage constraint.
Definition AStorage.hpp:37
size_t Index
The entity identifier type.
Definition Entity.hpp:38
Associative Map to store entity components Recommended for sparse components.
bool erase(Entity::Index index) override final
Erase the Component instance associated to the given entity.
size_t size() const noexcept
Get the number of Component instances.
Component & operator[](Entity::Index index) override final
Retrieve the Component instance associated to the given entity.
MapStorage()=default
Construct a new Map Storage for a given Component type.
MapStorage(const MapStorage &other)=delete
Copy constructor is deleted.
Component & insert(Entity::Index index, Component &&c) override final
Insert a new Component instance associated to the given entity.
typename AStorage< C >::Component Component
util::BitSet _mask
Component mask.
Component & emplace(Entity::Index index, Args &&...args)
Emplace a new Component instance for a given entity.
const Component & operator[](Entity::Index index) const override final
Retrieve the Component instance associated to the given entity.
constexpr const util::BitSet & getMask() const noexcept override final
Get the Component Mask.
std::unordered_map< Entity::Index, Component > _components
Components map.
Mimics the API of std::bitset but with the dynamic properties of std::vector<bool>
Definition BitSet.hpp:35
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 end(T... args)
T erase(T... args)
T find(T... args)
T make_pair(T... args)
T max(T... args)
Namespace containing all symbols specific to ecstasy.
Definition ecstasy.hpp:30
T size(T... args)