Skip to main content

EntityRegistry.ixx File

Generic, policy-based entity registry for managing entity lifecycles. More...

Included Headers

Namespaces Index

namespacehelios
namespaceecs

Classes Index

classEntityRegistry<TDomainTag, TLookupStrategy, TAllowRemoval, TCapacity>

Generic registry for creating and managing strongly-typed entity handles. More...

Description

Generic, policy-based entity registry for managing entity lifecycles.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
8#include <vector>
9#include <cassert>
10#include <cstddef>
11
12export module helios.ecs.EntityRegistry;
13
14import helios.ecs.types.StrongId;
15import helios.ecs.types.TypeDefs;
16import helios.ecs.concepts.IsStrongIdLike;
17import helios.ecs.types.TypeDefs;
18import helios.ecs.types.EntityHandle;
19import helios.ecs.strategies.LinearLookupStrategy;
20import helios.ecs.strategies.HashedLookupStrategy;
21
22import helios.ecs.concepts;
23
24using namespace helios::ecs::types;
25using namespace helios::ecs::types;
26using namespace helios::ecs::strategies;
27export namespace helios::ecs {
28
29
69 template<
70 typename TDomainTag,
71 typename TLookupStrategy = HashedLookupStrategy,
72 bool TAllowRemoval = true,
73 size_t TCapacity = DEFAULT_ENTITY_MANAGER_CAPACITY
74 >
75 requires helios::ecs::concepts::IsStrongIdCollisionResolverLike<TLookupStrategy>
77
78
82 std::vector<EntityId> freeList_;
83
84
91 std::vector<VersionId> versions_;
92
93
97 std::vector<StrongId_t> strongIds_;
98
102 TLookupStrategy lookupStrategy_;
103
107 size_t strongIdCounter_ = 0;
108
109 public:
110
119 explicit EntityRegistry(const size_t capacity = TCapacity)
120 : lookupStrategy_(capacity) {
121 versions_.reserve(capacity);
122 strongIds_.reserve(capacity);
123 freeList_.reserve(capacity);
124 }
125
126
140
141 EntityId idx;
143
144 if (freeList_.empty()) {
145 idx = static_cast<EntityId>(versions_.size());
147 versions_.push_back(version);
148 strongIds_.push_back(StrongId_t{});
149 } else {
150 idx = freeList_.back();
151 freeList_.pop_back();
152
153 // version was already incremented in destroy
154 version = versions_[idx];
155 }
156
157 if (!strongId.isValid()) {
158 strongId = StrongId<TDomainTag>(++strongIdCounter_);
159 }
160 assert(strongId.isValid() && "EntityRegistry: invalid strongId");
161
162 assert(!lookupStrategy_.has(strongId.value()) && "EntityRegistry: strongId collision");
163
164 strongIds_[idx] = strongId.value();
165
166 const bool added = lookupStrategy_.add(strongId.value());
167
168 assert(added && "EntityRegistry: failed to add strongId to lookupStrategy");
169
170 return {idx, version, strongId};
171
172 }
173
181 [[nodiscard]] VersionId version(const EntityId entityId) const {
182 if (entityId >= static_cast<EntityId>(versions_.size())) {
183 return InvalidVersion;
184 }
185 return versions_[entityId];
186 }
187
196 [[nodiscard]] StrongId<TDomainTag> strongId(const EntityId entityId) const {
197 if (entityId >= static_cast<EntityId>(strongIds_.size())) {
198 return StrongId<TDomainTag>{};
199 }
200 return static_cast<StrongId<TDomainTag>>(strongIds_[entityId]);
201 }
202
203
215 [[nodiscard]] bool isValid(const EntityHandle<TDomainTag> handle) const noexcept {
216 const auto index = handle.entityId;
217
218 if (index >= static_cast<EntityId>(versions_.size())) {
219 return false;
220 }
221
222 return handle.strongId.value() == strongIds_[index] &&
223 handle.versionId == versions_[index] &&
224 handle.strongId.isValid();
225 }
226
227
242 bool destroy(const EntityHandle<TDomainTag> handle) {
243
244 if constexpr (!TAllowRemoval) {
245 assert(false && "EntityRegistry: Entity removal is not allowed");
246 return false;
247 }
248
249 if (!isValid(handle)) {
250 return false;
251 }
252
253 const auto index = handle.entityId;
254
255 versions_[index] += 1;
256 strongIds_[index] = 0;
257
258 const bool removed = lookupStrategy_.remove(handle.strongId.value());
259 assert(removed && "EntityRegistry: failed to remove strongId from lookupStrategy");
260
261 freeList_.push_back(index);
262
263 return true;
264 }
265
266 };
267
268
269
270}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.