co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
component.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <bitset>
5#include <cstdint>
6#include <functional>
7#include <numeric>
8#include <stdexcept>
9
13#include <co_ecs/type_meta.hpp>
14
15namespace co_ecs {
16
17namespace detail {
18
23template<typename = void, typename _id_type = std::uint64_t>
24class type_registry {
25public:
26 using id_type = _id_type;
27
32 CO_ECS_API static auto id(std::string_view type_string) -> id_type {
33 auto [iter, inserted] = get_id_map().emplace(type_string, get_next_id());
34 if (inserted) {
35 get_next_id()++;
36 }
37 auto type_id = iter->second;
38 return type_id;
39 }
40
44 CO_ECS_API static auto get_id_map() -> hash_map<std::string_view, id_type>& {
46 return id_map;
47 }
48
52 CO_ECS_API static auto get_next_id() -> id_type& {
53 static id_type next_id{};
54 return next_id;
55 }
56};
57
62template<typename Base = void, typename _id_type = std::uint64_t>
63struct type_id {
64 using id_type = _id_type;
65
66 template<typename T>
67 inline static const id_type value = type_registry<Base, id_type>::id(type_name<T>());
68};
69
70} // namespace detail
71
73using component_id_t = std::uint32_t;
74
76constexpr auto invalid_component_id = std::numeric_limits<component_id_t>::max();
77
79using component_id = detail::type_id<struct _component_family_t, component_id_t>;
80
85template<typename T>
86concept component =
87 std::is_class_v<T> && std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_assignable_v<T>;
88
93template<typename T>
94concept component_reference = std::is_reference_v<T> && component<std::remove_cvref_t<T>>;
95
99template<component_reference T>
100using decay_component_t = std::decay_t<T>;
101
105template<component_reference T>
107 constexpr static bool value = std::is_const_v<std::remove_reference_t<T>>;
108};
109
113template<component_reference T>
115
119template<component_reference T>
121 constexpr static bool value = !std::is_const_v<std::remove_reference_t<T>>;
122};
123
127template<component_reference T>
129
133template<component_reference... Args>
135 constexpr static bool value = std::conjunction_v<const_component_reference<Args>...>;
136};
137
141template<component_reference... Args>
143
146public:
151 template<component T>
152 static auto of() noexcept -> component_meta {
153 return component_meta{
154 component_id::value<T>,
155 type_meta::of<T>(),
156 };
157 }
158
163 constexpr auto operator<=>(const component_meta& rhs) const noexcept {
164 return id <=> rhs.id;
165 }
166
172 constexpr auto operator==(const component_meta& rhs) const noexcept -> bool {
173 return id == rhs.id;
174 }
175
178};
179
182public:
183 using storage_type = detail::dynamic_bitset<>;
184
189 template<component... Args>
190 static auto create() -> component_set {
192 (..., s.insert<Args>());
193 return s;
194 }
195
199 template<component T>
200 void insert() {
201 insert(component_id::value<T>);
202 }
203
207 template<component T>
208 void erase() {
209 erase(component_id::value<T>);
210 }
211
217 template<component T>
218 [[nodiscard]] auto contains() const -> bool {
219 return contains(component_id::value<T>);
220 }
221
226 _bitset.set(id);
227 }
228
233 _bitset.set(id, false);
234 }
235
241 [[nodiscard]] auto contains(component_id_t id) const -> bool {
242 return _bitset.test(id);
243 }
244
245 void clear() noexcept {
246 _bitset.clear();
247 }
248
254 auto operator==(const component_set& rhs) const noexcept -> bool {
255 return _bitset == rhs._bitset;
256 }
257
258private:
260 storage_type _bitset{};
261};
262
265public:
270 auto operator()(const component_set& set) const -> std::size_t {
271 return std::hash<typename component_set::storage_type>()(set._bitset);
272 }
273};
274
277public:
278 using size_type = std::size_t;
279 using storage_type = std::vector<component_meta>;
280 using value_type = typename storage_type::value_type;
281 using const_iterator = typename storage_type::const_iterator;
282
287 template<component... Args>
288 static auto create() -> component_meta_set {
290 s._components_meta.reserve(sizeof...(Args));
291 (..., s.insert<Args>());
292 return s;
293 }
294
298 template<component T>
299 constexpr void insert() {
300 insert(component_meta::of<T>());
301 }
302
306 template<component T>
307 constexpr void erase() {
308 erase(component_id::value<T>);
309 }
310
316 template<component T>
317 [[nodiscard]] auto contains() const -> bool {
318 return contains(component_id::value<T>);
319 }
320
324 void insert(const value_type& meta) {
325 if (contains(meta.id)) {
326 return;
327 }
328 _component_set.insert(meta.id);
329 _components_meta.emplace_back(meta);
330 }
331
336 if (!contains(id)) {
337 return;
338 }
339 _component_set.erase(id);
340 std::erase_if(_components_meta, [id](const auto& meta) { return meta.id == id; });
341 }
342
348 [[nodiscard]] auto contains(component_id_t id) const -> bool {
349 return _component_set.contains(id);
350 }
351
355 [[nodiscard]] auto size() const noexcept -> size_type {
356 return _components_meta.size();
357 }
358
362 [[nodiscard]] auto begin() const noexcept -> const_iterator {
363 return _components_meta.begin();
364 }
365
369 [[nodiscard]] auto end() const noexcept -> const_iterator {
370 return _components_meta.end();
371 }
372
376 [[nodiscard]] auto cbegin() const noexcept -> const_iterator {
377 return begin();
378 }
379
383 [[nodiscard]] auto cend() const noexcept -> const_iterator {
384 return end();
385 }
386
392 auto operator==(const component_meta_set& rhs) const noexcept -> bool {
393 return _component_set == rhs._component_set;
394 }
395
399 [[nodiscard]] auto ids() const noexcept -> const component_set& {
400 return _component_set;
401 }
402
403private:
404 component_set _component_set;
405 storage_type _components_meta;
406};
407
408} // namespace co_ecs
Component set holds a set of components metadata.
Definition component.hpp:276
auto cend() const noexcept -> const_iterator
Return const iterator to the end of the set.
Definition component.hpp:383
typename storage_type::const_iterator const_iterator
Definition component.hpp:281
auto end() const noexcept -> const_iterator
Return const iterator to the end of the set.
Definition component.hpp:369
constexpr void insert()
Insert component of type T.
Definition component.hpp:299
auto cbegin() const noexcept -> const_iterator
Return const iterator to beginning of the set.
Definition component.hpp:376
typename storage_type::value_type value_type
Definition component.hpp:280
auto operator==(const component_meta_set &rhs) const noexcept -> bool
Equality operator.
Definition component.hpp:392
constexpr void erase()
Erase component of type T.
Definition component.hpp:307
void erase(component_id_t id)
Erases component from the set.
Definition component.hpp:335
std::vector< component_meta > storage_type
Definition component.hpp:279
auto ids() const noexcept -> const component_set &
Return a bitset of components.
Definition component.hpp:399
auto size() const noexcept -> size_type
Returns how many components in the set.
Definition component.hpp:355
auto begin() const noexcept -> const_iterator
Return const iterator to beginning of the set.
Definition component.hpp:362
auto contains(component_id_t id) const -> bool
Check if component is present in the set.
Definition component.hpp:348
static auto create() -> component_meta_set
Construct component set from given component types.
Definition component.hpp:288
auto contains() const -> bool
Check if component of type T is present in the set.
Definition component.hpp:317
std::size_t size_type
Definition component.hpp:278
void insert(const value_type &meta)
Inserts component into the set.
Definition component.hpp:324
Component set hasher.
Definition component.hpp:264
auto operator()(const component_set &set) const -> std::size_t
Hash component set.
Definition component.hpp:270
Component set holds a set of component IDs.
Definition component.hpp:181
auto contains() const -> bool
Check if component of type T is present in the set.
Definition component.hpp:218
void clear() noexcept
Definition component.hpp:245
detail::dynamic_bitset<> storage_type
Definition component.hpp:183
void insert()
Insert component of type T.
Definition component.hpp:200
auto contains(component_id_t id) const -> bool
Check if component is present in the set.
Definition component.hpp:241
void insert(component_id_t id)
Inserts component into the set.
Definition component.hpp:225
void erase(component_id_t id)
Erases component from the set.
Definition component.hpp:232
void erase()
Erase component of type T.
Definition component.hpp:208
static auto create() -> component_set
Construct component set from given component types.
Definition component.hpp:190
auto operator==(const component_set &rhs) const noexcept -> bool
Equality operator.
Definition component.hpp:254
Component reference concept. It should be a reference or const reference to C, where C satisfies comp...
Definition component.hpp:94
Component concept. The component must be a struct/class that can be move constructed and move assigna...
Definition component.hpp:86
#define CO_ECS_API
Definition macro.hpp:41
hash_table< K, T, true, Hash, KeyEqual, Allocator > hash_map
Hash map.
Definition hash_map.hpp:19
Definition archetype.hpp:11
constexpr bool const_component_references_v
Returns true when all Args are const references.
Definition component.hpp:142
constexpr bool mutable_component_reference_v
Returns true for non-const component references.
Definition component.hpp:128
constexpr bool const_component_reference_v
Returns true for const component references.
Definition component.hpp:114
std::decay_t< T > decay_component_t
Decay component; converts component_reference to component by removing cv-qualifiers and reference.
Definition component.hpp:100
detail::type_id< struct _component_family_t, component_id_t > component_id
Type for family used to generated component IDs.
Definition component.hpp:79
constexpr auto invalid_component_id
Invalid component ID.
Definition component.hpp:76
std::uint32_t component_id_t
Type for component ID.
Definition component.hpp:73
Component metadata. Stores an ID, size, alignment, destructor, etc.
Definition component.hpp:145
static auto of() noexcept -> component_meta
Constructs component_meta for type T.
Definition component.hpp:152
const type_meta * type
Definition component.hpp:177
constexpr auto operator<=>(const component_meta &rhs) const noexcept
Spaceship operator.
Definition component.hpp:163
component_id_t id
Definition component.hpp:176
constexpr auto operator==(const component_meta &rhs) const noexcept -> bool
Equality operator.
Definition component.hpp:172
Struct to determine const-ness of component reference type.
Definition component.hpp:106
static constexpr bool value
Definition component.hpp:107
Struct to determine whether all component references are const.
Definition component.hpp:134
static constexpr bool value
Definition component.hpp:135
Struct to determine mutability of component reference type.
Definition component.hpp:120
static constexpr bool value
Definition component.hpp:121
Type meta information.
Definition type_meta.hpp:29