stellarlib 0.1.0
Loading...
Searching...
No Matches
archetype.hpp
1/* clang-format off */
2
3/*
4 stellarlib
5 Copyright (C) 2025-2026 Domán Zana
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22*/
23
24#ifndef STELLARLIB_ECS_ARCHETYPE_HPP
25#define STELLARLIB_ECS_ARCHETYPE_HPP
26
27#include <stellarlib/ecs/sparse_storage.hpp>
28#include <stellarlib/ext/memory.hpp>
29
30#include <cstdint>
31#include <type_traits>
32
33/**
34 * @brief Entity component system
35 */
36namespace stellarlib::ecs
37{
38/**
39 * @brief Represents the archetype of an entity
40 */
41class archetype final : ext::vector_allocator<std::uintmax_t>
42{
43public:
44 /**
45 * @brief Retrieves an archetype for a set of component types
46 * @tparam T Component types
47 * @return Archetype for a set of component types
48 */
49 template <typename ...T>
50 [[nodiscard]]
51 static constexpr auto of() noexcept
52 -> std::conditional_t<static_cast<bool>(sizeof...(T)), const archetype &, archetype>
53 {
54 if constexpr (sizeof...(T)) {
55 static const auto cache{[] [[nodiscard]] noexcept -> archetype {
57
58 for (const auto id : internal::sparse_storage::ids<T...>()) {
59 archetype.insert(id);
60 }
61
62 return archetype;
63 }()};
64
65 return cache;
66 }
67 else {
68 return archetype{};
69 }
70 }
71
72 /**
73 * @brief Default constructor
74 */
75 [[nodiscard]]
76 archetype() noexcept;
77
78 /**
79 * @brief Copy constructor
80 * @param other Other instance
81 */
82 [[nodiscard]]
83 archetype(const archetype &other) noexcept;
84
85 /**
86 * @brief Move constructor
87 * @param other Other instance
88 */
89 [[nodiscard]]
90 archetype(archetype &&other) noexcept;
91
92 /**
93 * @brief Copy assignment operator
94 * @param other Other instance
95 * @return Current instance
96 */
97 auto operator=(const archetype &other) noexcept
98 -> archetype &;
99
100 /**
101 * @brief Move assignment operator
102 * @param other Other instance
103 * @return Current instance
104 */
105 auto operator=(archetype &&other) noexcept
106 -> archetype &;
107
108 /**
109 * @brief Destructor
110 */
111 ~archetype() noexcept;
112
113 /**
114 * @brief Inserts a component ID into the archetype
115 * @param id Component ID
116 */
117 void insert(std::uintmax_t id) noexcept;
118
119 /**
120 * @brief Inserts all component IDs that exist in another instance into the archetype
121 * @param other Other instance
122 */
123 void insert(const archetype &other) noexcept;
124
125 /**
126 * @brief Evaluates whether the archetype contains a component ID
127 * @param id Component ID
128 * @return Whether the archetype contains the component ID
129 */
130 [[nodiscard]]
131 auto contains(std::uintmax_t id) const noexcept
132 -> bool;
133
134 /**
135 * @brief Comparison operator
136 * @param other Other instance
137 * @return Result of the comparison
138 */
139 [[nodiscard]]
140 auto operator==(const archetype &other) const noexcept
141 -> bool;
142
143 /**
144 * @brief Evaluates whether the current instance is the subtype of an other instance
145 * @param other Other instance
146 * @return Whether the current instance is the subtype of the other instance
147 */
148 [[nodiscard]]
149 auto operator<=(const archetype &other) const noexcept
150 -> bool;
151
152 /**
153 * @brief Evaluates whether the current instance is the supertype of an other instance
154 * @param other Other instance
155 * @return Whether the current instance is the supertype of the other instance
156 */
157 [[nodiscard]]
158 auto operator>=(const archetype &other) const noexcept
159 -> bool;
160
161 /**
162 * @brief Removes a component ID from the archetype
163 * @param id Component ID
164 */
165 void erase(std::uintmax_t id) noexcept;
166
167 /**
168 * @brief Removes all component IDs that exist in another instance from the archetype
169 * @param other Other instance
170 */
171 void erase(const archetype &other) noexcept;
172
173 /**
174 * @brief Removes all component IDs from the archetype
175 */
176 void clear() noexcept;
177
178private:
179 std::uintmax_t *_begin{};
180 std::uintmax_t *_end{};
181 std::uintmax_t _size{};
182 std::uintmax_t _capacity{};
183};
184}
185
186#endif
static constexpr auto of() noexcept -> std::conditional_t< static_cast< bool >(sizeof...(T)), const archetype &, archetype >
Retrieves an archetype for a set of component types.
Definition archetype.hpp:51
void erase(std::uintmax_t id) noexcept
Removes a component ID from the archetype.
void clear() noexcept
Removes all component IDs from the archetype.
auto contains(std::uintmax_t id) const noexcept -> bool
Evaluates whether the archetype contains a component ID.
archetype() noexcept
Default constructor.
void insert(std::uintmax_t id) noexcept
Inserts a component ID into the archetype.
Linear memory allocator optimized for vectors.
Definition memory.hpp:50
Entity component system.
Definition any_set.hpp:28