Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
chunk_iterator.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cinttypes>
5#include <cstdint>
6#include <type_traits>
7
8#include "gaia/ecs/api.h"
9#include "gaia/ecs/archetype.h"
10#include "gaia/ecs/chunk.h"
11#include "gaia/ecs/command_buffer_fwd.h"
12#include "gaia/ecs/component.h"
13#include "gaia/ecs/component_cache_item.h"
14#include "gaia/ecs/id.h"
15#include "gaia/ecs/query_common.h"
16#include "gaia/mem/data_layout_policy.h"
17
18namespace gaia {
19 namespace ecs {
20 class World;
21
23 enum class Constraints : uint8_t { EnabledOnly, DisabledOnly, AcceptAll };
24
25 namespace detail {
26 template <Constraints IterConstraint>
28 protected:
30
32 const World* m_pWorld = nullptr;
34 const Archetype* m_pArchetype = nullptr;
36 Chunk* m_pChunk = nullptr;
38 const uint8_t* m_pCompIdxMapping = nullptr;
40 uint16_t m_from;
42 uint16_t m_to;
44 GroupId m_groupId = 0;
45
46 public:
47 ChunkIterImpl() = default;
48 ~ChunkIterImpl() = default;
49 ChunkIterImpl(ChunkIterImpl&&) noexcept = default;
50 ChunkIterImpl& operator=(ChunkIterImpl&&) noexcept = default;
51 ChunkIterImpl(const ChunkIterImpl&) = delete;
52 ChunkIterImpl& operator=(const ChunkIterImpl&) = delete;
53
54 void set_world(const World* pWorld) {
55 GAIA_ASSERT(pWorld != nullptr);
56 m_pWorld = pWorld;
57 }
58
59 GAIA_NODISCARD World* world() {
60 GAIA_ASSERT(m_pWorld != nullptr);
61 return const_cast<World*>(m_pWorld);
62 }
63
64 GAIA_NODISCARD const World* world() const {
65 GAIA_ASSERT(m_pWorld != nullptr);
66 return m_pWorld;
67 }
68
69 void set_archetype(const Archetype* pArchetype) {
70 GAIA_ASSERT(pArchetype != nullptr);
71 m_pArchetype = pArchetype;
72 }
73
74 // GAIA_NODISCARD Archetype* archetype() {
75 // GAIA_ASSERT(m_pArchetype != nullptr);
76 // return m_pArchetype;
77 // }
78
79 GAIA_NODISCARD const Archetype* archetype() const {
80 GAIA_ASSERT(m_pArchetype != nullptr);
81 return m_pArchetype;
82 }
83
84 void set_chunk(Chunk* pChunk) {
85 GAIA_ASSERT(pChunk != nullptr);
86 m_pChunk = pChunk;
87
88 if constexpr (IterConstraint == Constraints::EnabledOnly)
90 else
91 m_from = 0;
92
93 if constexpr (IterConstraint == Constraints::DisabledOnly)
95 else
96 m_to = m_pChunk->size();
97 }
98
99 void set_chunk(Chunk* pChunk, uint16_t from, uint16_t to) {
100 if (from == 0 && to == 0) {
101 set_chunk(pChunk);
102 return;
103 }
104
105 GAIA_ASSERT(pChunk != nullptr);
106 m_pChunk = pChunk;
107 m_from = from;
108 m_to = to;
109 }
110
111 GAIA_NODISCARD const Chunk* chunk() const {
112 GAIA_ASSERT(m_pChunk != nullptr);
113 return m_pChunk;
114 }
115
116 void set_remapping_indices(const uint8_t* pCompIndicesMapping) {
117 m_pCompIdxMapping = pCompIndicesMapping;
118 }
119
120 void set_group_id(GroupId groupId) {
121 m_groupId = groupId;
122 }
123
124 GAIA_NODISCARD GroupId group_id() const {
125 return m_groupId;
126 }
127
128 GAIA_NODISCARD CommandBufferST& cmd_buffer_st() const {
129 auto* pWorld = const_cast<World*>(m_pWorld);
130 return cmd_buffer_st_get(*pWorld);
131 }
132
133 GAIA_NODISCARD CommandBufferMT& cmd_buffer_mt() const {
134 auto* pWorld = const_cast<World*>(m_pWorld);
135 return cmd_buffer_mt_get(*pWorld);
136 }
137
142 template <typename T>
143 GAIA_NODISCARD auto view() const {
144 return m_pChunk->view<T>(from(), to());
145 }
146
147 template <typename T>
148 GAIA_NODISCARD auto view(uint32_t termIdx) {
149 using U = typename actual_type_t<T>::Type;
150
151 const auto compIdx = m_pCompIdxMapping[termIdx];
152 GAIA_ASSERT(compIdx < m_pChunk->ids_view().size());
153
154 if constexpr (mem::is_soa_layout_v<U>) {
155 auto* pData = m_pChunk->comp_ptr_mut(compIdx);
156 return m_pChunk->view_raw<T>(pData, m_pChunk->capacity());
157 } else {
158 auto* pData = m_pChunk->comp_ptr_mut(compIdx, from());
159 return m_pChunk->view_raw<T>(pData, to() - from());
160 }
161 }
162
167 template <typename T>
168 GAIA_NODISCARD auto view_mut() {
169 return m_pChunk->view_mut<T>(from(), to());
170 }
171
172 template <typename T>
173 GAIA_NODISCARD auto view_mut(uint32_t termIdx) {
174 using U = typename actual_type_t<T>::Type;
175
176 const auto compIdx = m_pCompIdxMapping[termIdx];
177 GAIA_ASSERT(compIdx < m_pChunk->comp_rec_view().size());
178
180
181 if constexpr (mem::is_soa_layout_v<U>) {
182 auto* pData = m_pChunk->comp_ptr_mut(compIdx);
183 return m_pChunk->view_mut_raw<T>(pData, m_pChunk->capacity());
184 } else {
185 auto* pData = m_pChunk->comp_ptr_mut(compIdx, from());
186 return m_pChunk->view_mut_raw<T>(pData, to() - from());
187 }
188 }
189
195 template <typename T>
196 GAIA_NODISCARD auto sview_mut() {
197 return m_pChunk->sview_mut<T>(from(), to());
198 }
199
200 template <typename T>
201 GAIA_NODISCARD auto sview_mut(uint32_t termIdx) {
202 using U = typename actual_type_t<T>::Type;
203
204 const auto compIdx = m_pCompIdxMapping[termIdx];
205 GAIA_ASSERT(compIdx < m_pChunk->ids_view().size());
206
207 if constexpr (mem::is_soa_layout_v<U>) {
208 auto* pData = m_pChunk->comp_ptr_mut(compIdx);
209 return m_pChunk->view_mut_raw<T>(pData, m_pChunk->capacity());
210 } else {
211 auto* pData = m_pChunk->comp_ptr_mut(compIdx, from());
212 return m_pChunk->view_mut_raw<T>(pData, to() - from());
213 }
214 }
215
219 template <typename T, bool TriggerHooks>
220 void modify() {
221 m_pChunk->modify<T, TriggerHooks>();
222 }
223
229 template <typename T>
230 GAIA_NODISCARD auto view_auto() {
231 return m_pChunk->view_auto<T>(from(), to());
232 }
233
240 template <typename T>
241 GAIA_NODISCARD auto sview_auto() {
242 return m_pChunk->sview_auto<T>(from(), to());
243 }
244
247 GAIA_NODISCARD bool enabled(uint32_t index) const {
248 const auto row = (uint16_t)(from() + index);
249 return m_pChunk->enabled(row);
250 }
251
255 GAIA_NODISCARD bool has(Entity entity) const {
256 return m_pChunk->has(entity);
257 }
258
262 GAIA_NODISCARD bool has(Pair pair) const {
263 return m_pChunk->has((Entity)pair);
264 }
265
269 template <typename T>
270 GAIA_NODISCARD bool has() const {
271 return m_pChunk->has<T>();
272 }
273
274 GAIA_NODISCARD static uint16_t start_index(Chunk* pChunk) noexcept {
275 if constexpr (IterConstraint == Constraints::EnabledOnly)
276 return pChunk->size_disabled();
277 else
278 return 0;
279 }
280
281 GAIA_NODISCARD static uint16_t end_index(Chunk* pChunk) noexcept {
282 if constexpr (IterConstraint == Constraints::DisabledOnly)
283 return pChunk->size_enabled();
284 else
285 return pChunk->size();
286 }
287
288 GAIA_NODISCARD static uint16_t size(Chunk* pChunk) noexcept {
289 if constexpr (IterConstraint == Constraints::EnabledOnly)
290 return pChunk->size_enabled();
291 else if constexpr (IterConstraint == Constraints::DisabledOnly)
292 return pChunk->size_disabled();
293 else
294 return pChunk->size();
295 }
296
298 GAIA_NODISCARD uint16_t size() const noexcept {
299 return size(m_pChunk);
300 }
301
305 template <typename T>
306 uint32_t acc_index(uint32_t idx) const noexcept {
307 using U = typename actual_type_t<T>::Type;
308
309 if constexpr (mem::is_soa_layout_v<U>)
310 return idx + from();
311 else
312 return idx;
313 }
314
315 protected:
317 GAIA_NODISCARD uint16_t from() const noexcept {
318 return m_from;
319 }
320
322 GAIA_NODISCARD uint16_t to() const noexcept {
323 return m_to;
324 }
325 };
326 } // namespace detail
327
329 class GAIA_API Iter final: public detail::ChunkIterImpl<Constraints::EnabledOnly> {};
331 class GAIA_API IterDisabled final: public detail::ChunkIterImpl<Constraints::DisabledOnly> {};
332
335 class GAIA_API IterAll final: public detail::ChunkIterImpl<Constraints::AcceptAll> {
336 public:
338 GAIA_NODISCARD uint16_t size_enabled() const noexcept {
339 return m_pChunk->size_enabled();
340 }
341
344 GAIA_NODISCARD uint16_t size_disabled() const noexcept {
345 return m_pChunk->size_disabled();
346 }
347 };
348
350 class GAIA_API CopyIter final {
351 protected:
353
355 const World* m_pWorld = nullptr;
357 const Archetype* m_pArchetype = nullptr;
359 Chunk* m_pChunk = nullptr;
361 uint16_t m_from;
363 uint16_t m_cnt;
364
365 public:
366 CopyIter() = default;
367 ~CopyIter() = default;
368 CopyIter(CopyIter&&) noexcept = default;
369 CopyIter& operator=(CopyIter&&) noexcept = default;
370 CopyIter(const CopyIter&) = delete;
371 CopyIter& operator=(const CopyIter&) = delete;
372
376 void set_range(uint16_t from, uint16_t cnt) {
377 GAIA_ASSERT(from < m_pChunk->size());
378 GAIA_ASSERT(from + cnt <= m_pChunk->size());
379 m_from = from;
380 m_cnt = cnt;
381 }
382
383 void set_world(const World* pWorld) {
384 GAIA_ASSERT(pWorld != nullptr);
385 m_pWorld = pWorld;
386 }
387
388 GAIA_NODISCARD World* world() {
389 GAIA_ASSERT(m_pWorld != nullptr);
390 return const_cast<World*>(m_pWorld);
391 }
392
393 GAIA_NODISCARD const World* world() const {
394 GAIA_ASSERT(m_pWorld != nullptr);
395 return m_pWorld;
396 }
397
398 void set_archetype(const Archetype* pArchetype) {
399 GAIA_ASSERT(pArchetype != nullptr);
400 m_pArchetype = pArchetype;
401 }
402
403 // GAIA_NODISCARD Archetype* archetype() {
404 // GAIA_ASSERT(m_pArchetype != nullptr);
405 // return m_pArchetype;
406 // }
407
408 GAIA_NODISCARD const Archetype* archetype() const {
409 GAIA_ASSERT(m_pArchetype != nullptr);
410 return m_pArchetype;
411 }
412
413 void set_chunk(Chunk* pChunk) {
414 GAIA_ASSERT(pChunk != nullptr);
415 m_pChunk = pChunk;
416 }
417
418 GAIA_NODISCARD const Chunk* chunk() const {
419 GAIA_ASSERT(m_pChunk != nullptr);
420 return m_pChunk;
421 }
422
423 GAIA_NODISCARD CommandBufferST& cmd_buffer_st() const {
424 auto* pWorld = const_cast<World*>(m_pWorld);
425 return cmd_buffer_st_get(*pWorld);
426 }
427
428 GAIA_NODISCARD CommandBufferMT& cmd_buffer_mt() const {
429 auto* pWorld = const_cast<World*>(m_pWorld);
430 return cmd_buffer_mt_get(*pWorld);
431 }
432
437 template <typename T>
438 GAIA_NODISCARD auto view() const {
439 return m_pChunk->view<T>(from(), to());
440 }
441
446 template <typename T>
447 GAIA_NODISCARD auto view_mut() {
448 return m_pChunk->view_mut<T>(from(), to());
449 }
450
456 template <typename T>
457 GAIA_NODISCARD auto sview_mut() {
458 return m_pChunk->sview_mut<T>(from(), to());
459 }
460
464 template <typename T, bool TriggerHooks>
465 void modify() {
466 m_pChunk->modify<T, TriggerHooks>();
467 }
468
474 template <typename T>
475 GAIA_NODISCARD auto view_auto() {
476 return m_pChunk->view_auto<T>(from(), to());
477 }
478
485 template <typename T>
486 GAIA_NODISCARD auto sview_auto() {
487 return m_pChunk->sview_auto<T>(from(), to());
488 }
489
492 GAIA_NODISCARD bool enabled(uint32_t index) const {
493 const auto row = (uint16_t)(from() + index);
494 return m_pChunk->enabled(row);
495 }
496
500 GAIA_NODISCARD bool has(Entity entity) const {
501 return m_pChunk->has(entity);
502 }
503
507 GAIA_NODISCARD bool has(Pair pair) const {
508 return m_pChunk->has((Entity)pair);
509 }
510
514 template <typename T>
515 GAIA_NODISCARD bool has() const {
516 return m_pChunk->has<T>();
517 }
518
520 GAIA_NODISCARD uint16_t size() const noexcept {
521 return m_cnt;
522 }
523
524 private:
526 GAIA_NODISCARD uint16_t from() const noexcept {
527 return m_from;
528 }
529
531 GAIA_NODISCARD uint16_t to() const noexcept {
532 return m_from + m_cnt;
533 }
534 };
535 } // namespace ecs
536} // namespace gaia
Definition archetype.h:82
Definition chunk.h:29
GAIA_NODISCARD decltype(auto) sview_auto(uint16_t from, uint16_t to)
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk.h:692
GAIA_NODISCARD uint16_t capacity() const
Returns the number of entities in the chunk.
Definition chunk.h:1491
GAIA_FORCEINLINE void update_world_version(uint32_t compIdx)
Update the version of a component at the index.
Definition chunk.h:1518
GAIA_NODISCARD decltype(auto) view(uint16_t from, uint16_t to) const
Returns a read-only entity or component view.
Definition chunk.h:519
GAIA_NODISCARD uint16_t size_enabled() const
Return the number of entities in the chunk which are enabled.
Definition chunk.h:1481
GAIA_NODISCARD decltype(auto) view_auto(uint16_t from, uint16_t to)
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk.h:670
GAIA_NODISCARD uint16_t size() const
Returns the total number of entities in the chunk (both enabled and disabled)
Definition chunk.h:1471
bool enabled(uint16_t row) const
Checks if the entity is enabled.
Definition chunk.h:1159
GAIA_NODISCARD uint16_t size_disabled() const
Return the number of entities in the chunk which are enabled.
Definition chunk.h:1486
GAIA_NODISCARD bool has(Entity entity) const
Checks if a component/entity entity is present in the chunk.
Definition chunk.h:1243
GAIA_NODISCARD decltype(auto) view_mut(uint16_t from, uint16_t to)
Returns a mutable entity or component view.
Definition chunk.h:547
GAIA_NODISCARD decltype(auto) sview_mut(uint16_t from, uint16_t to)
Returns a mutable component view. Doesn't update the world version when the access is acquired.
Definition chunk.h:579
GAIA_FORCEINLINE void modify()
Marks the component.
Definition chunk.h:613
Iterator used when copying entities.
Definition chunk_iterator.h:350
uint16_t m_from
Row of the first entity we iterate from.
Definition chunk_iterator.h:361
GAIA_NODISCARD auto view() const
Returns a read-only entity or component view.
Definition chunk_iterator.h:438
GAIA_NODISCARD auto sview_auto()
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk_iterator.h:486
GAIA_NODISCARD auto sview_mut()
Returns a mutable component view. Doesn't update the world version when the access is acquired.
Definition chunk_iterator.h:457
GAIA_NODISCARD bool has() const
Checks if component T is present in the chunk.
Definition chunk_iterator.h:515
GAIA_NODISCARD auto view_mut()
Returns a mutable entity or component view.
Definition chunk_iterator.h:447
GAIA_NODISCARD bool has(Pair pair) const
Checks if relationship pair pair is present in the chunk.
Definition chunk_iterator.h:507
uint16_t m_cnt
The number of entities accessible via the iterator.
Definition chunk_iterator.h:363
GAIA_NODISCARD auto view_auto()
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk_iterator.h:475
GAIA_NODISCARD bool has(Entity entity) const
Checks if entity entity is present in the chunk.
Definition chunk_iterator.h:500
void modify()
Marks the component T as modified. Best used with sview to manually trigger an update at user's whim....
Definition chunk_iterator.h:465
GAIA_NODISCARD uint16_t size() const noexcept
Returns the number of entities accessible via the iterator.
Definition chunk_iterator.h:520
GAIA_NODISCARD bool enabled(uint32_t index) const
Checks if the entity at the current iterator index is enabled.
Definition chunk_iterator.h:492
Iterator for iterating both enabled and disabled entities. Disabled entities always precede enabled o...
Definition chunk_iterator.h:335
GAIA_NODISCARD uint16_t size_disabled() const noexcept
Returns the number of disabled entities accessible via the iterator. Can be read also as "the index o...
Definition chunk_iterator.h:344
GAIA_NODISCARD uint16_t size_enabled() const noexcept
Returns the number of enabled entities accessible via the iterator.
Definition chunk_iterator.h:338
Iterator for iterating disabled entities.
Definition chunk_iterator.h:331
Iterator for iterating enabled entities.
Definition chunk_iterator.h:329
Definition world.h:48
Definition chunk_iterator.h:27
GAIA_NODISCARD uint16_t to() const noexcept
Returns the ending index of the iterator (one past the last valid index)
Definition chunk_iterator.h:322
GAIA_NODISCARD auto sview_auto()
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk_iterator.h:241
uint32_t acc_index(uint32_t idx) const noexcept
Returns the absolute index that should be used to access an item in the chunk. AoS indices map direct...
Definition chunk_iterator.h:306
Chunk * m_pChunk
Chunk currently associated with the iterator.
Definition chunk_iterator.h:36
GAIA_NODISCARD auto view() const
Returns a read-only entity or component view.
Definition chunk_iterator.h:143
GAIA_NODISCARD auto sview_mut()
Returns a mutable component view. Doesn't update the world version when the access is acquired.
Definition chunk_iterator.h:196
GAIA_NODISCARD uint16_t size() const noexcept
Returns the number of entities accessible via the iterator.
Definition chunk_iterator.h:298
const Archetype * m_pArchetype
Currently iterated archetype.
Definition chunk_iterator.h:34
const uint8_t * m_pCompIdxMapping
ChunkHeader::MAX_COMPONENTS values for component indices mapping for the parent archetype.
Definition chunk_iterator.h:38
void modify()
Marks the component T as modified. Best used with sview to manually trigger an update at user's whim....
Definition chunk_iterator.h:220
GAIA_NODISCARD bool enabled(uint32_t index) const
Checks if the entity at the current iterator index is enabled.
Definition chunk_iterator.h:247
const World * m_pWorld
World pointer.
Definition chunk_iterator.h:32
uint16_t m_from
Row of the first entity we iterate from.
Definition chunk_iterator.h:40
GAIA_NODISCARD auto view_auto()
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk_iterator.h:230
GAIA_NODISCARD bool has() const
Checks if component T is present in the chunk.
Definition chunk_iterator.h:270
GroupId m_groupId
GroupId. 0 if not set.
Definition chunk_iterator.h:44
GAIA_NODISCARD bool has(Pair pair) const
Checks if relationship pair pair is present in the chunk.
Definition chunk_iterator.h:262
GAIA_NODISCARD bool has(Entity entity) const
Checks if entity entity is present in the chunk.
Definition chunk_iterator.h:255
GAIA_NODISCARD uint16_t from() const noexcept
Returns the starting index of the iterator.
Definition chunk_iterator.h:317
GAIA_NODISCARD auto view_mut()
Returns a mutable entity or component view.
Definition chunk_iterator.h:168
uint16_t m_to
Row of the last entity we iterate to.
Definition chunk_iterator.h:42
Wrapper for two Entities forming a relationship pair.
Definition id.h:395
Wrapper for two types forming a relationship pair. Depending on what types are used to form a pair it...
Definition id.h:202
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition bit_utils.h:11
Definition id.h:225