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 void world_finish_write(World& world, Entity term, Entity entity);
22 template <typename T>
23 decltype(auto) world_query_entity_arg_by_id_raw(World& world, Entity entity, Entity id);
24 template <typename T>
25 decltype(auto) world_query_entity_arg_by_id_cached_const(
26 World& world, Entity entity, Entity id, const Archetype*& pLastArchetype, Entity& cachedOwner,
27 bool& cachedDirect);
28 template <typename T>
29 void world_init_query_entity_arg_by_id_chunk_stable_const(
30 World& world, const Chunk& chunk, const Entity* pEntities, Entity id, bool& direct, uint32_t& compIdx,
31 const std::remove_cv_t<std::remove_reference_t<T>>*& pDataInherited);
32 template <typename T>
33 const std::remove_cv_t<std::remove_reference_t<T>>*
34 world_query_inherited_arg_data_const(World& world, Entity owner, Entity id);
35 bool world_term_uses_inherit_policy(const World& world, Entity term);
36 template <typename T>
37 Entity world_query_arg_id(World& world);
38
40 enum class Constraints : uint8_t { EnabledOnly, DisabledOnly, AcceptAll };
41
42 namespace detail {
43 class ChunkIterImpl;
44
45 struct IterTermDesc {
46 Entity termId = EntityBad;
47 bool isEntity = false;
48 bool isOutOfLine = false;
49 };
50
52 template <typename T>
53 static IterTermDesc term_desc(const ChunkIterImpl& self);
54 template <typename T>
55 static auto view_any(const ChunkIterImpl& self);
56 template <typename T>
57 static auto view_any(ChunkIterImpl& self, uint32_t termIdx);
58 template <typename T>
59 static auto view(const ChunkIterImpl& self);
60 template <typename T>
61 static auto view(const ChunkIterImpl& self, uint32_t termIdx);
62 template <typename T>
63 static auto view_any_mut(ChunkIterImpl& self);
64 template <typename T>
65 static auto view_mut(ChunkIterImpl& self);
66 template <typename T>
67 static auto view_mut(ChunkIterImpl& self, uint32_t termIdx);
68 template <typename T>
69 static auto view_any_mut(ChunkIterImpl& self, uint32_t termIdx);
70 template <typename T>
71 static auto sview_any_mut(ChunkIterImpl& self);
72 template <typename T>
73 static auto sview_mut(ChunkIterImpl& self);
74 template <typename T>
75 static auto sview_mut(ChunkIterImpl& self, uint32_t termIdx);
76 template <typename T>
77 static auto sview_any_mut(ChunkIterImpl& self, uint32_t termIdx);
78 };
79
80 struct BfsChunkRun {
81 const Archetype* pArchetype = nullptr;
82 Chunk* pChunk = nullptr;
83 uint16_t from = 0;
84 uint16_t to = 0;
85 uint32_t offset = 0;
86 };
87
90 template <typename U>
92 const U* pData = nullptr;
93 uint32_t cnt = 0;
94
95 GAIA_NODISCARD decltype(auto) operator[](size_t idx) const {
96 GAIA_ASSERT(idx < cnt);
97 return pData[idx];
98 }
99
100 GAIA_NODISCARD constexpr size_t size() const noexcept {
101 return cnt;
102 }
103
104 GAIA_NODISCARD const U* data() const noexcept {
105 return pData;
106 }
107 };
108
111 template <typename U>
113 const Entity* pEntities = nullptr;
114 World* pWorld = nullptr;
115 Entity id = EntityBad;
116 uint32_t cnt = 0;
117 mutable const Archetype* pLastArchetype = nullptr;
118 mutable Entity cachedOwner = EntityBad;
119 mutable bool cachedDirect = false;
120
121 GAIA_NODISCARD decltype(auto) operator[](size_t idx) const {
122 GAIA_ASSERT(idx < cnt);
123 return world_query_entity_arg_by_id_cached_const<const U&>(
124 *pWorld, pEntities[idx], id, pLastArchetype, cachedOwner, cachedDirect);
125 }
126
127 GAIA_NODISCARD constexpr size_t size() const noexcept {
128 return cnt;
129 }
130
131 GAIA_NODISCARD const U* data() const noexcept {
132 return nullptr;
133 }
134 };
135
138 template <typename U>
140 U* pData = nullptr;
141 uint32_t cnt = 0;
142
143 GAIA_NODISCARD decltype(auto) operator[](size_t idx) {
144 GAIA_ASSERT(idx < cnt);
145 return pData[idx];
146 }
147
148 GAIA_NODISCARD decltype(auto) operator[](size_t idx) const {
149 GAIA_ASSERT(idx < cnt);
150 return static_cast<const U&>(pData[idx]);
151 }
152
153 GAIA_NODISCARD constexpr size_t size() const noexcept {
154 return cnt;
155 }
156
157 GAIA_NODISCARD U* data() noexcept {
158 return pData;
159 }
160
161 GAIA_NODISCARD const U* data() const noexcept {
162 return pData;
163 }
164 };
165
168 template <typename U, bool WriteIm>
170 const Entity* pEntities = nullptr;
171 World* pWorld = nullptr;
172 Entity id = EntityBad;
173 uint32_t cnt = 0;
174
175 GAIA_NODISCARD decltype(auto) operator[](size_t idx) {
176 GAIA_ASSERT(idx < cnt);
177 if constexpr (WriteIm)
178 return world_query_entity_arg_by_id<U&>(*pWorld, pEntities[idx], id);
179 else
180 return world_query_entity_arg_by_id_raw<U&>(*pWorld, pEntities[idx], id);
181 }
182
183 GAIA_NODISCARD decltype(auto) operator[](size_t idx) const {
184 GAIA_ASSERT(idx < cnt);
185 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx], id);
186 }
187
188 GAIA_NODISCARD constexpr size_t size() const noexcept {
189 return cnt;
190 }
191
192 GAIA_NODISCARD U* data() noexcept {
193 return nullptr;
194 }
195
196 GAIA_NODISCARD const U* data() const noexcept {
197 return nullptr;
198 }
199 };
200
203 template <typename U>
205 const U* pData = nullptr;
206 const Entity* pEntities = nullptr;
207 World* pWorld = nullptr;
208 Entity id = EntityBad;
209 uint32_t cnt = 0;
210 const U* pDataInherited = nullptr;
211 mutable const Archetype* pLastArchetype = nullptr;
212 mutable Entity cachedOwner = EntityBad;
213 mutable bool cachedDirect = false;
214
215 static EntityTermViewGet pointer(const U* pData, uint32_t cnt) {
216 return {pData, nullptr, nullptr, EntityBad, cnt, nullptr, nullptr, EntityBad, false};
217 }
218
219 static EntityTermViewGet entity(const Entity* pEntities, World* pWorld, Entity id, uint32_t cnt) {
220 return {nullptr, pEntities, pWorld, id, cnt, nullptr, nullptr, EntityBad, false};
221 }
222
223 static EntityTermViewGet inherited(const U* pDataInherited, uint32_t cnt) {
224 return {nullptr, nullptr, nullptr, EntityBad, cnt, pDataInherited, nullptr, EntityBad, false};
225 }
226
227 static EntityTermViewGet entity_chunk_stable(
228 const Entity* pEntities, const Chunk* pChunk, World* pWorld, Entity id, uint16_t rowBase, uint32_t cnt) {
229 uint32_t compIdx = BadIndex;
230 const U* pDataInherited = nullptr;
231 bool direct = false;
232 world_init_query_entity_arg_by_id_chunk_stable_const<U>(
233 *pWorld, *pChunk, pEntities, id, direct, compIdx, pDataInherited);
234 const U* pData = nullptr;
235 if (direct)
236 pData = reinterpret_cast<const U*>(pChunk->comp_ptr(compIdx, rowBase));
237
238 return {pData, nullptr, pWorld, id, cnt, pDataInherited, nullptr, EntityBad, false};
239 }
240
241 GAIA_NODISCARD decltype(auto) operator[](size_t idx) const {
242 GAIA_ASSERT(idx < cnt);
243 if (pData != nullptr)
244 return pData[idx];
245 if (pDataInherited != nullptr) {
246 GAIA_ASSERT(pDataInherited != nullptr);
247 return *pDataInherited;
248 }
249
250 return world_query_entity_arg_by_id_cached_const<const U&>(
251 *pWorld, pEntities[idx], id, pLastArchetype, cachedOwner, cachedDirect);
252 }
253
254 GAIA_NODISCARD constexpr size_t size() const noexcept {
255 return cnt;
256 }
257
258 GAIA_NODISCARD const U* data() const noexcept {
259 return pData;
260 }
261 };
262
265 template <typename U>
267 U* pData = nullptr;
268 const Entity* pEntities = nullptr;
269 World* pWorld = nullptr;
270 Entity id = EntityBad;
271 uint32_t cnt = 0;
272 bool writeIm = true;
273
274 static EntityTermViewSet pointer(U* pData, uint32_t cnt) {
275 return {pData, nullptr, nullptr, EntityBad, cnt, true};
276 }
277
278 static EntityTermViewSet
279 entity(const Entity* pEntities, World* pWorld, Entity id, uint32_t cnt, bool writeIm = true) {
280 return {nullptr, pEntities, pWorld, id, cnt, writeIm};
281 }
282
283 GAIA_NODISCARD decltype(auto) operator[](size_t idx) {
284 GAIA_ASSERT(idx < cnt);
285 if (pData != nullptr)
286 return pData[idx];
287
288 if (writeIm)
289 return EntityTermViewSetEntity<U, true>{pEntities, pWorld, id, cnt}[idx];
290 return EntityTermViewSetEntity<U, false>{pEntities, pWorld, id, cnt}[idx];
291 }
292
293 GAIA_NODISCARD decltype(auto) operator[](size_t idx) const {
294 GAIA_ASSERT(idx < cnt);
295 if (pData != nullptr)
296 return static_cast<const U&>(pData[idx]);
297
298 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx], id);
299 }
300
301 GAIA_NODISCARD constexpr size_t size() const noexcept {
302 return cnt;
303 }
304
305 GAIA_NODISCARD U* data() noexcept {
306 return pData;
307 }
308
309 GAIA_NODISCARD const U* data() const noexcept {
310 return pData;
311 }
312 };
313
316 template <typename U>
319
320 uint8_t* pData = nullptr;
321 uint32_t dataSize = 0;
322 uint32_t idx = 0;
323
324 GAIA_NODISCARD operator U() const {
325 return raw_view_policy::get(std::span<const uint8_t>{pData, dataSize}, idx);
326 }
327
328 SoATermRowWriteProxyPointer& operator=(const U& value) {
329 raw_view_policy::set(std::span<uint8_t>{pData, dataSize}, idx) = value;
330 return *this;
331 }
332 };
333
336 template <typename U, bool WriteIm>
338 const Entity* pEntities = nullptr;
339 World* pWorld = nullptr;
340 Entity id = EntityBad;
341 uint32_t idx = 0;
342
343 GAIA_NODISCARD operator U() const {
344 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx], id);
345 }
346
347 SoATermRowWriteProxyEntity& operator=(const U& value) {
348 if constexpr (WriteIm)
349 world_query_entity_arg_by_id<U&>(*pWorld, pEntities[idx], id) = value;
350 else
351 world_query_entity_arg_by_id_raw<U&>(*pWorld, pEntities[idx], id) = value;
352 return *this;
353 }
354 };
355
358 template <typename U, size_t Item>
361 using value_type = typename view_policy::template data_view_policy_idx_info<Item>::const_value_type;
362
363 const value_type* pData = nullptr;
364 uint32_t cnt = 0;
365
366 GAIA_NODISCARD value_type operator[](size_t idx) const {
367 GAIA_ASSERT(idx < cnt);
368 return pData[idx];
369 }
370
371 GAIA_NODISCARD constexpr size_t size() const noexcept {
372 return cnt;
373 }
374 };
375
378 template <typename U, size_t Item>
381 using value_type = typename view_policy::template data_view_policy_idx_info<Item>::const_value_type;
382
383 const Entity* pEntities = nullptr;
384 World* pWorld = nullptr;
385 Entity id = EntityBad;
386 uint32_t cnt = 0;
387
388 GAIA_NODISCARD value_type operator[](size_t idx) const {
389 GAIA_ASSERT(idx < cnt);
390 const U value = world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx], id);
391 return std::get<Item>(meta::struct_to_tuple(value));
392 }
393
394 GAIA_NODISCARD constexpr size_t size() const noexcept {
395 return cnt;
396 }
397 };
398
401 template <typename U, size_t Item>
404 using value_type = typename view_policy::template data_view_policy_idx_info<Item>::value_type;
405
406 value_type* pData = nullptr;
407 uint32_t cnt = 0;
408
409 GAIA_NODISCARD value_type& operator[](size_t idx) {
410 GAIA_ASSERT(idx < cnt);
411 return pData[idx];
412 }
413
414 GAIA_NODISCARD const value_type& operator[](size_t idx) const {
415 GAIA_ASSERT(idx < cnt);
416 return pData[idx];
417 }
418
419 GAIA_NODISCARD constexpr size_t size() const noexcept {
420 return cnt;
421 }
422 };
423
426 template <typename U, size_t Item, bool WriteIm>
429 using value_type = typename view_policy::template data_view_policy_idx_info<Item>::value_type;
430
433 const Entity* pEntities = nullptr;
434 World* pWorld = nullptr;
435 Entity id = EntityBad;
436 uint32_t idx = 0;
437
438 GAIA_NODISCARD operator value_type() const {
439 const U value = world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx], id);
440 return std::get<Item>(meta::struct_to_tuple(value));
441 }
442
443 ElementProxy& operator=(const value_type& value) {
444 auto data = world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx], id);
445 auto tuple = meta::struct_to_tuple(data);
446 std::get<Item>(tuple) = value;
447 if constexpr (WriteIm)
448 world_query_entity_arg_by_id<U&>(*pWorld, pEntities[idx], id) = meta::tuple_to_struct<U>(GAIA_MOV(tuple));
449 else
450 world_query_entity_arg_by_id_raw<U&>(*pWorld, pEntities[idx], id) =
451 meta::tuple_to_struct<U>(GAIA_MOV(tuple));
452 return *this;
453 }
454
455 template <typename V>
456 ElementProxy& operator+=(V&& value) {
457 const value_type current = operator value_type();
458 return operator=(current + GAIA_FWD(value));
459 }
460
461 template <typename V>
462 ElementProxy& operator-=(V&& value) {
463 const value_type current = operator value_type();
464 return operator=(current - GAIA_FWD(value));
465 }
466
467 template <typename V>
468 ElementProxy& operator*=(V&& value) {
469 const value_type current = operator value_type();
470 return operator=(current * GAIA_FWD(value));
471 }
472
473 template <typename V>
474 ElementProxy& operator/=(V&& value) {
475 const value_type current = operator value_type();
476 return operator=(current / GAIA_FWD(value));
477 }
478 };
479
480 const Entity* pEntities = nullptr;
481 World* pWorld = nullptr;
482 Entity id = EntityBad;
483 uint32_t cnt = 0;
484
485 GAIA_NODISCARD ElementProxy operator[](size_t idx) const {
486 GAIA_ASSERT(idx < cnt);
487 return ElementProxy{pEntities, pWorld, id, (uint32_t)idx};
488 }
489
490 GAIA_NODISCARD constexpr size_t size() const noexcept {
491 return cnt;
492 }
493 };
494
497 template <typename U>
501
502 const uint8_t* pData = nullptr;
503 uint32_t dataSize = 0;
504 uint32_t idxBase = 0;
505 uint32_t cnt = 0;
506
507 GAIA_NODISCARD U operator[](size_t idx) const {
508 GAIA_ASSERT(idx < cnt);
509 return raw_view_policy::get(std::span<const uint8_t>{pData, dataSize}, idxBase + idx);
510 }
511
512 template <size_t Item>
513 GAIA_NODISCARD auto get() const {
514 const auto field = read_view_policy{std::span<const uint8_t>{pData, dataSize}}.template get<Item>();
515 return SoATermFieldReadProxyPointer<U, Item>{field.data() + idxBase, cnt};
516 }
517
518 GAIA_NODISCARD constexpr size_t size() const noexcept {
519 return cnt;
520 }
521 };
522
525 template <typename U>
527 const Entity* pEntities = nullptr;
528 World* pWorld = nullptr;
529 Entity id = EntityBad;
530 uint32_t cnt = 0;
531
532 GAIA_NODISCARD U operator[](size_t idx) const {
533 GAIA_ASSERT(idx < cnt);
534 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx], id);
535 }
536
537 template <size_t Item>
538 GAIA_NODISCARD auto get() const {
539 return SoATermFieldReadProxyEntity<U, Item>{pEntities, pWorld, id, cnt};
540 }
541
542 GAIA_NODISCARD constexpr size_t size() const noexcept {
543 return cnt;
544 }
545 };
546
549 template <typename U>
554
555 uint8_t* pData = nullptr;
556 uint32_t dataSize = 0;
557 uint32_t idxBase = 0;
558 uint32_t cnt = 0;
559
560 GAIA_NODISCARD auto operator[](size_t idx) {
561 GAIA_ASSERT(idx < cnt);
562 return SoATermRowWriteProxyPointer<U>{pData, dataSize, idxBase + (uint32_t)idx};
563 }
564
565 GAIA_NODISCARD U operator[](size_t idx) const {
566 GAIA_ASSERT(idx < cnt);
567 return raw_view_policy::get(std::span<const uint8_t>{pData, dataSize}, idxBase + idx);
568 }
569
570 template <size_t Item>
571 GAIA_NODISCARD auto get() const {
572 const auto field = read_view_policy{std::span<const uint8_t>{pData, dataSize}}.template get<Item>();
573 return SoATermFieldReadProxyPointer<U, Item>{field.data() + idxBase, cnt};
574 }
575
576 template <size_t Item>
577 GAIA_NODISCARD auto set() {
578 auto field = write_view_policy{std::span<uint8_t>{pData, dataSize}}.template set<Item>();
579 return SoATermFieldWriteProxyPointer<U, Item>{field.data() + idxBase, cnt};
580 }
581
582 GAIA_NODISCARD constexpr size_t size() const noexcept {
583 return cnt;
584 }
585 };
586
589 template <typename U, bool WriteIm>
591 const Entity* pEntities = nullptr;
592 World* pWorld = nullptr;
593 Entity id = EntityBad;
594 uint32_t cnt = 0;
595
596 GAIA_NODISCARD auto operator[](size_t idx) {
597 GAIA_ASSERT(idx < cnt);
598 return SoATermRowWriteProxyEntity<U, WriteIm>{pEntities, pWorld, id, (uint32_t)idx};
599 }
600
601 GAIA_NODISCARD U operator[](size_t idx) const {
602 GAIA_ASSERT(idx < cnt);
603 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx], id);
604 }
605
606 template <size_t Item>
607 GAIA_NODISCARD auto get() const {
608 return SoATermFieldReadProxyEntity<U, Item>{pEntities, pWorld, id, cnt};
609 }
610
611 template <size_t Item>
612 GAIA_NODISCARD auto set() {
613 return SoATermFieldWriteProxyEntity<U, Item, WriteIm>{pEntities, pWorld, id, cnt};
614 }
615
616 GAIA_NODISCARD constexpr size_t size() const noexcept {
617 return cnt;
618 }
619 };
620
623 template <typename U>
625 uint8_t* pData = nullptr;
626 uint32_t dataSize = 0;
627 const Entity* pEntities = nullptr;
628 World* pWorld = nullptr;
629 Entity id = EntityBad;
630 uint32_t idx = 0;
631 bool writeIm = true;
632
633 GAIA_NODISCARD operator U() const {
634 if (pData != nullptr)
635 return (U)SoATermRowWriteProxyPointer<U>{pData, dataSize, idx};
636
637 if (writeIm)
638 return (U)SoATermRowWriteProxyEntity<U, true>{pEntities, pWorld, id, idx};
639 return (U)SoATermRowWriteProxyEntity<U, false>{pEntities, pWorld, id, idx};
640 }
641
642 SoATermRowWriteProxy& operator=(const U& value) {
643 if (pData != nullptr) {
644 SoATermRowWriteProxyPointer<U>{pData, dataSize, idx} = value;
645 } else if (writeIm)
646 SoATermRowWriteProxyEntity<U, true>{pEntities, pWorld, id, idx} = value;
647 else
648 SoATermRowWriteProxyEntity<U, false>{pEntities, pWorld, id, idx} = value;
649 return *this;
650 }
651 };
652
655 template <typename U, size_t Item>
657 using value_type = typename SoATermFieldReadProxyPointer<U, Item>::value_type;
658
659 const value_type* pData = nullptr;
660 const Entity* pEntities = nullptr;
661 World* pWorld = nullptr;
662 Entity id = EntityBad;
663 uint32_t cnt = 0;
664
665 GAIA_NODISCARD value_type operator[](size_t idx) const {
666 GAIA_ASSERT(idx < cnt);
667 if (pData != nullptr)
668 return SoATermFieldReadProxyPointer<U, Item>{pData, cnt}[idx];
669
670 return SoATermFieldReadProxyEntity<U, Item>{pEntities, pWorld, id, cnt}[idx];
671 }
672
673 GAIA_NODISCARD constexpr size_t size() const noexcept {
674 return cnt;
675 }
676 };
677
680 template <typename U, size_t Item>
682 using value_type = typename SoATermFieldWriteProxyPointer<U, Item>::value_type;
683
684 value_type* pData = nullptr;
685 const Entity* pEntities = nullptr;
686 World* pWorld = nullptr;
687 Entity id = EntityBad;
688 uint32_t cnt = 0;
689 bool writeIm = true;
690
694 value_type* pData = nullptr;
695 const Entity* pEntities = nullptr;
696 World* pWorld = nullptr;
697 Entity id = EntityBad;
698 uint32_t idx = 0;
699 bool writeIm = true;
700
701 GAIA_NODISCARD operator value_type() const {
702 if (pData != nullptr)
703 return SoATermFieldWriteProxyPointer<U, Item>{pData, idx + 1}[idx];
704
705 if (writeIm)
706 return SoATermFieldWriteProxyEntity<U, Item, true>{pEntities, pWorld, id, idx + 1}[idx];
707 return SoATermFieldWriteProxyEntity<U, Item, false>{pEntities, pWorld, id, idx + 1}[idx];
708 }
709
710 ElementProxy& operator=(const value_type& value) {
711 if (pData != nullptr) {
712 SoATermFieldWriteProxyPointer<U, Item>{pData, idx + 1}[idx] = value;
713 return *this;
714 }
715
716 if (writeIm)
717 SoATermFieldWriteProxyEntity<U, Item, true>{pEntities, pWorld, id, idx + 1}[idx] = value;
718 else
719 SoATermFieldWriteProxyEntity<U, Item, false>{pEntities, pWorld, id, idx + 1}[idx] = value;
720 return *this;
721 }
722
723 template <typename V>
724 ElementProxy& operator+=(V&& value) {
725 const value_type current = operator value_type();
726 return operator=(current + GAIA_FWD(value));
727 }
728
729 template <typename V>
730 ElementProxy& operator-=(V&& value) {
731 const value_type current = operator value_type();
732 return operator=(current - GAIA_FWD(value));
733 }
734
735 template <typename V>
736 ElementProxy& operator*=(V&& value) {
737 const value_type current = operator value_type();
738 return operator=(current * GAIA_FWD(value));
739 }
740
741 template <typename V>
742 ElementProxy& operator/=(V&& value) {
743 const value_type current = operator value_type();
744 return operator=(current / GAIA_FWD(value));
745 }
746 };
747
748 GAIA_NODISCARD ElementProxy operator[](size_t idx) const {
749 GAIA_ASSERT(idx < cnt);
750 return ElementProxy{pData, pEntities, pWorld, id, (uint32_t)idx, writeIm};
751 }
752
753 GAIA_NODISCARD constexpr size_t size() const noexcept {
754 return cnt;
755 }
756 };
757
760 template <typename U>
762 const uint8_t* pData = nullptr;
763 uint32_t dataSize = 0;
764 const Entity* pEntities = nullptr;
765 World* pWorld = nullptr;
766 Entity id = EntityBad;
767 uint32_t idxBase = 0;
768 uint32_t cnt = 0;
769
770 GAIA_NODISCARD U operator[](size_t idx) const {
771 GAIA_ASSERT(idx < cnt);
772 if (pData != nullptr)
773 return SoATermViewGetPointer<U>{pData, dataSize, idxBase, cnt}[idx];
774
775 return SoATermViewGetEntity<U>{pEntities, pWorld, id, cnt}[idx];
776 }
777
778 template <size_t Item>
779 GAIA_NODISCARD auto get() const {
780 if (pData != nullptr) {
781 const auto field = SoATermViewGetPointer<U>{pData, dataSize, idxBase, cnt}.template get<Item>();
782 return SoATermFieldReadProxy<U, Item>{field.pData, nullptr, pWorld, id, cnt};
783 }
784
785 const auto field = SoATermViewGetEntity<U>{pEntities, pWorld, id, cnt}.template get<Item>();
786 return SoATermFieldReadProxy<U, Item>{nullptr, field.pEntities, field.pWorld, field.id, field.cnt};
787 }
788
789 GAIA_NODISCARD constexpr size_t size() const noexcept {
790 return cnt;
791 }
792 };
793
796 template <typename U>
798 uint8_t* pData = nullptr;
799 uint32_t dataSize = 0;
800 const Entity* pEntities = nullptr;
801 World* pWorld = nullptr;
802 Entity id = EntityBad;
803 uint32_t idxBase = 0;
804 uint32_t cnt = 0;
805 bool writeIm = true;
806
807 GAIA_NODISCARD auto operator[](size_t idx) {
808 GAIA_ASSERT(idx < cnt);
809 return SoATermRowWriteProxy<U>{pData, dataSize, pEntities, pWorld, id, idxBase + (uint32_t)idx, writeIm};
810 }
811
812 GAIA_NODISCARD U operator[](size_t idx) const {
813 GAIA_ASSERT(idx < cnt);
814 if (pData != nullptr)
815 return SoATermViewSetPointer<U>{pData, dataSize, idxBase, cnt}[idx];
816
817 return SoATermViewSetEntity<U, true>{pEntities, pWorld, id, cnt}[idx];
818 }
819
820 template <size_t Item>
821 GAIA_NODISCARD auto get() const {
822 if (pData != nullptr) {
823 const auto field = SoATermViewSetPointer<U>{pData, dataSize, idxBase, cnt}.template get<Item>();
824 return SoATermFieldReadProxy<U, Item>{field.pData, nullptr, pWorld, id, cnt};
825 }
826
827 const auto field = SoATermViewSetEntity<U, true>{pEntities, pWorld, id, cnt}.template get<Item>();
828 return SoATermFieldReadProxy<U, Item>{nullptr, field.pEntities, field.pWorld, field.id, field.cnt};
829 }
830
831 template <size_t Item>
832 GAIA_NODISCARD auto set() {
833 if (pData != nullptr) {
834 auto field = SoATermViewSetPointer<U>{pData, dataSize, idxBase, cnt}.template set<Item>();
835 return SoATermFieldWriteProxy<U, Item>{field.pData, nullptr, pWorld, id, cnt};
836 }
837
838 if (writeIm) {
839 const auto field = SoATermViewSetEntity<U, true>{pEntities, pWorld, id, cnt}.template set<Item>();
840 return SoATermFieldWriteProxy<U, Item>{nullptr, field.pEntities, field.pWorld, field.id, field.cnt, true};
841 }
842
843 const auto field = SoATermViewSetEntity<U, false>{pEntities, pWorld, id, cnt}.template set<Item>();
844 return SoATermFieldWriteProxy<U, Item>{nullptr, field.pEntities, field.pWorld, field.id, field.cnt, false};
845 }
846
847 GAIA_NODISCARD constexpr size_t size() const noexcept {
848 return cnt;
849 }
850 };
851
853 friend struct ChunkIterTypedOps;
854
855 protected:
857
859 const World* m_pWorld = nullptr;
861 const Archetype* m_pArchetype = nullptr;
863 Chunk* m_pChunk = nullptr;
865 const uint8_t* m_pCompIndices = nullptr;
869 const Entity* m_pTermIdMapping = nullptr;
871 bool m_writeIm = true;
873 Constraints m_constraints = Constraints::EnabledOnly;
876 uint8_t m_touchedCompCnt = 0;
879 uint8_t m_touchedTermCnt = 0;
882 bool m_entitySnapshotValid = false;
884 uint16_t m_from;
886 uint16_t m_to;
888 GroupId m_groupId = 0;
890 void* m_pCtx = nullptr;
891
892 public:
893 ChunkIterImpl() = default;
894 ~ChunkIterImpl() = default;
895 ChunkIterImpl(ChunkIterImpl&&) noexcept = default;
896 ChunkIterImpl& operator=(ChunkIterImpl&&) noexcept = default;
897 ChunkIterImpl(const ChunkIterImpl&) = delete;
898 ChunkIterImpl& operator=(const ChunkIterImpl&) = delete;
899
900 void set_world(const World* pWorld) {
901 GAIA_ASSERT(pWorld != nullptr);
902 m_pWorld = pWorld;
903 }
904
905 GAIA_NODISCARD World* world() {
906 GAIA_ASSERT(m_pWorld != nullptr);
907 return const_cast<World*>(m_pWorld);
908 }
909
910 GAIA_NODISCARD const World* world() const {
911 GAIA_ASSERT(m_pWorld != nullptr);
912 return m_pWorld;
913 }
914
915 void set_archetype(const Archetype* pArchetype) {
916 GAIA_ASSERT(pArchetype != nullptr);
917 m_pArchetype = pArchetype;
918 }
919
920 // GAIA_NODISCARD Archetype* archetype() {
921 // GAIA_ASSERT(m_pArchetype != nullptr);
922 // return m_pArchetype;
923 // }
924
925 GAIA_NODISCARD const Archetype* archetype() const {
926 GAIA_ASSERT(m_pArchetype != nullptr);
927 return m_pArchetype;
928 }
929
930 void set_chunk(Chunk* pChunk) {
931 GAIA_ASSERT(pChunk != nullptr);
932 m_pChunk = pChunk;
933 m_entitySnapshotValid = false;
934 m_from = start_index(m_pChunk, m_constraints);
935 m_to = end_index(m_pChunk, m_constraints);
936 }
937
938 void set_chunk(Chunk* pChunk, uint16_t from, uint16_t to) {
939 if (from == 0 && to == 0) {
940 set_chunk(pChunk);
941 return;
942 }
943
944 GAIA_ASSERT(pChunk != nullptr);
945 m_pChunk = pChunk;
946 m_entitySnapshotValid = false;
947 m_from = from;
948 m_to = to;
949 }
950
951 GAIA_NODISCARD const Chunk* chunk() const {
952 GAIA_ASSERT(m_pChunk != nullptr);
953 return m_pChunk;
954 }
955
956 void set_comp_indices(const uint8_t* pCompIndices) {
957 m_pCompIndices = pCompIndices;
958 }
959
960 void set_inherited_data(InheritedTermDataView inheritedData) {
961 m_inheritedData = inheritedData;
962 }
963
964 void set_term_ids(const Entity* pTermIds) {
965 m_pTermIdMapping = pTermIds;
966 }
967
968 GAIA_NODISCARD const uint8_t* comp_indices() const {
969 return m_pCompIndices;
970 }
971
972 GAIA_NODISCARD InheritedTermDataView inherited_data() const {
973 return m_inheritedData;
974 }
975
976 GAIA_NODISCARD const Entity* term_ids() const {
977 return m_pTermIdMapping;
978 }
979
980 void set_write_im(bool value) {
981 m_writeIm = value;
982 clear_touched_writes();
983 }
984
985 void set_constraints(Constraints constraints) {
986 m_constraints = constraints;
987 if (m_pChunk != nullptr) {
988 m_from = start_index(m_pChunk, m_constraints);
989 m_to = end_index(m_pChunk, m_constraints);
990 m_entitySnapshotValid = false;
991 }
992 }
993
998 void init_query_state(const World* pWorld, Constraints constraints, bool writeIm) {
999 set_world(pWorld);
1000 m_constraints = constraints;
1001 m_writeIm = writeIm;
1002 clear_touched_writes();
1003 }
1004
1012 const Archetype* pArchetype, const uint8_t* pCompIndices, Chunk* pChunk, uint16_t from, uint16_t to) {
1013 GAIA_ASSERT(pArchetype != nullptr);
1014 GAIA_ASSERT(pChunk != nullptr);
1015 if (m_pArchetype != pArchetype)
1016 m_pArchetype = pArchetype;
1017 if (m_pCompIndices != pCompIndices)
1018 m_pCompIndices = pCompIndices;
1019 m_pChunk = pChunk;
1020 m_entitySnapshotValid = false;
1021 m_from = from;
1022 m_to = to;
1023 }
1024
1025 GAIA_NODISCARD bool write_im() const {
1026 return m_writeIm;
1027 }
1028
1029 GAIA_NODISCARD Constraints constraints() const {
1030 return m_constraints;
1031 }
1032
1033 void clear_touched_writes() {
1034 m_touchedCompCnt = 0;
1035 m_touchedTermCnt = 0;
1036 }
1037
1038 GAIA_NODISCARD const Entity* entity_snapshot() {
1039 if (!m_entitySnapshotValid) {
1040 const auto cnt = size();
1041 GAIA_ASSERT(cnt <= ChunkHeader::MAX_CHUNK_ENTITIES);
1042
1043 const auto entities = m_pChunk->entity_view();
1044 GAIA_FOR(cnt) {
1045 m_entitySnapshot[i] = entities[from() + i];
1046 }
1047
1048 m_entitySnapshotValid = true;
1049 }
1050
1051 return m_entitySnapshot;
1052 }
1053
1054 GAIA_NODISCARD auto touched_comp_indices() const {
1055 return std::span<const uint8_t>{m_touchedCompIndices, m_touchedCompCnt};
1056 }
1057
1058 GAIA_NODISCARD auto touched_terms() const {
1059 return std::span<const Entity>{m_touchedTerms, m_touchedTermCnt};
1060 }
1061
1062 GAIA_NODISCARD auto entity_rows() {
1063 if (m_entitySnapshotValid)
1065
1066 return std::span<const Entity>{m_pChunk->entity_view().data() + from(), size()};
1067 }
1068
1069 template <typename U>
1070 GAIA_NODISCARD auto entity_view_set(Entity termId, bool writeIm) {
1071 return EntityTermViewSet<U>::entity(entity_snapshot(), world(), termId, size(), writeIm);
1072 }
1073
1074 template <typename U>
1075 GAIA_NODISCARD auto entity_soa_view_set(Entity termId, bool writeIm) {
1076 return SoATermViewSet<U>{nullptr, 0, entity_snapshot(), world(), termId, 0, size(), writeIm};
1077 }
1078
1079 void set_group_id(GroupId groupId) {
1080 m_groupId = groupId;
1081 }
1082
1083 GAIA_NODISCARD GroupId group_id() const {
1084 return m_groupId;
1085 }
1086
1092
1095 void ctx(void* pCtx) {
1096 m_pCtx = pCtx;
1097 }
1098
1101 GAIA_NODISCARD void* ctx() const {
1102 return m_pCtx;
1103 }
1105
1106 GAIA_NODISCARD CommandBufferST& cmd_buffer_st() const {
1107 auto* pWorld = const_cast<World*>(m_pWorld);
1108 return cmd_buffer_st_get(*pWorld);
1109 }
1110
1111 GAIA_NODISCARD CommandBufferMT& cmd_buffer_mt() const {
1112 auto* pWorld = const_cast<World*>(m_pWorld);
1113 return cmd_buffer_mt_get(*pWorld);
1114 }
1115
1116 GAIA_NODISCARD IterTermDesc resolved_term_desc(uint32_t termIdx, IterTermDesc desc) const {
1117 if (m_pTermIdMapping != nullptr) {
1118 const auto mappedTermId = m_pTermIdMapping[termIdx];
1119 if (mappedTermId != EntityBad)
1120 desc.termId = mappedTermId;
1121 }
1122
1123 if (!desc.isEntity && desc.termId != EntityBad)
1124 desc.isOutOfLine = world_is_out_of_line_component(*world(), desc.termId);
1125
1126 return desc;
1127 }
1128
1129 void touch_comp_idx(uint8_t compIdx) {
1130 GAIA_ASSERT(compIdx != 0xFF);
1131 GAIA_FOR(m_touchedCompCnt) {
1132 if (m_touchedCompIndices[i] == compIdx)
1133 return;
1134 }
1135
1136 GAIA_ASSERT(m_touchedCompCnt < ChunkHeader::MAX_COMPONENTS);
1137 m_touchedCompIndices[m_touchedCompCnt++] = compIdx;
1138 }
1139
1140 void touch_term(Entity term) {
1141 GAIA_ASSERT(term != EntityBad);
1142 GAIA_FOR(m_touchedTermCnt) {
1143 if (m_touchedTerms[i] == term)
1144 return;
1145 }
1146
1147 GAIA_ASSERT(m_touchedTermCnt < ChunkHeader::MAX_COMPONENTS);
1148 m_touchedTerms[m_touchedTermCnt++] = term;
1149 }
1150
1151 void touch_term_desc(const IterTermDesc& desc) {
1152 if (desc.isEntity)
1153 return;
1154 if (desc.isOutOfLine)
1155 touch_term(desc.termId);
1156 else
1157 touch_comp_idx((uint8_t)m_pChunk->comp_idx(desc.termId));
1158 }
1159
1166 template <typename T>
1167 GAIA_NODISCARD auto view_any() const {
1168 return ChunkIterTypedOps::template view_any<T>(*this);
1169 }
1170
1178 template <typename T>
1179 GAIA_NODISCARD auto view_any(uint32_t termIdx) {
1180 return ChunkIterTypedOps::template view_any<T>(*this, termIdx);
1181 }
1182
1191 template <typename T>
1192 GAIA_NODISCARD auto view() const {
1193 return ChunkIterTypedOps::template view<T>(*this);
1194 }
1195
1204 template <typename T>
1205 GAIA_NODISCARD auto view(uint32_t termIdx) const {
1206 return ChunkIterTypedOps::template view<T>(*this, termIdx);
1207 }
1208
1215 template <typename T>
1216 GAIA_NODISCARD auto view_any_mut() {
1217 return ChunkIterTypedOps::template view_any_mut<T>(*this);
1218 }
1219
1226 template <typename T>
1227 GAIA_NODISCARD auto view_mut() {
1228 return ChunkIterTypedOps::template view_mut<T>(*this);
1229 }
1230
1239 template <typename T>
1240 GAIA_NODISCARD auto view_mut(uint32_t termIdx) {
1241 return ChunkIterTypedOps::template view_mut<T>(*this, termIdx);
1242 }
1243
1252 template <typename T>
1253 GAIA_NODISCARD auto view_any_mut(uint32_t termIdx) {
1254 return ChunkIterTypedOps::template view_any_mut<T>(*this, termIdx);
1255 }
1256
1264 template <typename T>
1265 GAIA_NODISCARD auto sview_any_mut() {
1266 return ChunkIterTypedOps::template sview_any_mut<T>(*this);
1267 }
1268
1275 template <typename T>
1276 GAIA_NODISCARD auto sview_mut() {
1277 return ChunkIterTypedOps::template sview_mut<T>(*this);
1278 }
1279
1288 template <typename T>
1289 GAIA_NODISCARD auto sview_mut(uint32_t termIdx) {
1290 return ChunkIterTypedOps::template sview_mut<T>(*this, termIdx);
1291 }
1292
1301 template <typename T>
1302 GAIA_NODISCARD auto sview_any_mut(uint32_t termIdx) {
1303 return ChunkIterTypedOps::template sview_any_mut<T>(*this, termIdx);
1304 }
1305
1309 template <typename T, bool TriggerHooks>
1310 void modify() {
1311 m_pChunk->template modify<T, TriggerHooks>();
1312 }
1313
1321 template <typename T>
1322 GAIA_NODISCARD auto view_auto() {
1323 using UOriginal = typename actual_type_t<T>::TypeOriginal;
1324 if constexpr (core::is_mut_v<UOriginal>)
1325 return view_mut<T>();
1326 else
1327 return view<T>();
1328 }
1329
1336 template <typename T>
1337 GAIA_NODISCARD auto view_auto_any() {
1338 using UOriginal = typename actual_type_t<T>::TypeOriginal;
1339 if constexpr (core::is_mut_v<UOriginal>)
1340 return view_any_mut<T>();
1341 else
1342 return view_any<T>();
1343 }
1344
1352 template <typename T>
1353 GAIA_NODISCARD auto sview_auto_any() {
1354 using UOriginal = typename actual_type_t<T>::TypeOriginal;
1355 if constexpr (core::is_mut_v<UOriginal>)
1356 return sview_any_mut<T>();
1357 else
1358 return view_any<T>();
1359 }
1360
1369 template <typename T>
1370 GAIA_NODISCARD auto sview_auto() {
1371 using UOriginal = typename actual_type_t<T>::TypeOriginal;
1372 if constexpr (core::is_mut_v<UOriginal>)
1373 return sview_mut<T>();
1374 else
1375 return view<T>();
1376 }
1377
1380 GAIA_NODISCARD bool enabled(uint32_t index) const {
1381 const auto row = (uint16_t)(from() + index);
1382 return m_pChunk->enabled(row);
1383 }
1384
1388 GAIA_NODISCARD bool has(Entity entity) const {
1389 return m_pChunk->has(entity);
1390 }
1391
1395 GAIA_NODISCARD bool has(Pair pair) const {
1396 return m_pChunk->has((Entity)pair);
1397 }
1398
1402 template <typename T>
1403 GAIA_NODISCARD bool has() const {
1404 return m_pChunk->template has<T>();
1405 }
1406
1407 GAIA_NODISCARD static uint16_t start_index(Chunk* pChunk, Constraints constraints) noexcept {
1408 if (constraints == Constraints::EnabledOnly)
1409 return pChunk->size_disabled();
1410 return 0;
1411 }
1412
1413 GAIA_NODISCARD static uint16_t end_index(Chunk* pChunk, Constraints constraints) noexcept {
1414 if (constraints == Constraints::DisabledOnly)
1415 return pChunk->size_disabled();
1416 return pChunk->size();
1417 }
1418
1419 GAIA_NODISCARD static uint16_t size(Chunk* pChunk, Constraints constraints) noexcept {
1420 if (constraints == Constraints::EnabledOnly)
1421 return pChunk->size_enabled();
1422 if (constraints == Constraints::DisabledOnly)
1423 return pChunk->size_disabled();
1424 return pChunk->size();
1425 }
1426
1428 GAIA_NODISCARD uint16_t size() const noexcept {
1429 return (uint16_t)(to() - from());
1430 }
1431
1433 GAIA_NODISCARD uint16_t row_begin() const noexcept {
1434 return from();
1435 }
1436
1438 GAIA_NODISCARD uint16_t row_end() const noexcept {
1439 return to();
1440 }
1441
1445 template <typename T>
1446 uint32_t acc_index(uint32_t idx) const noexcept {
1447 using U = typename actual_type_t<T>::Type;
1448 if constexpr (mem::is_soa_layout_v<U>)
1449 return idx + from();
1450 else
1451 return idx;
1452 }
1453
1454 protected:
1456 GAIA_NODISCARD uint16_t from() const noexcept {
1457 return m_from;
1458 }
1459
1461 GAIA_NODISCARD uint16_t to() const noexcept {
1462 return m_to;
1463 }
1464 };
1465 } // namespace detail
1466
1469 class GAIA_API Iter: public detail::ChunkIterImpl {
1470 public:
1471 static constexpr Constraints ConstraintMode = Constraints::EnabledOnly;
1472
1473 using detail::ChunkIterImpl::size;
1474
1475 Iter() {
1476 set_constraints(ConstraintMode);
1477 }
1478
1479 GAIA_NODISCARD static uint16_t start_index(Chunk* pChunk) noexcept {
1480 return detail::ChunkIterImpl::start_index(pChunk, ConstraintMode);
1481 }
1482
1483 GAIA_NODISCARD static uint16_t end_index(Chunk* pChunk) noexcept {
1484 return detail::ChunkIterImpl::end_index(pChunk, ConstraintMode);
1485 }
1486
1487 GAIA_NODISCARD static uint16_t size(Chunk* pChunk) noexcept {
1488 return detail::ChunkIterImpl::size(pChunk, ConstraintMode);
1489 }
1490
1492 GAIA_NODISCARD uint16_t size_enabled() const noexcept {
1493 return m_pChunk->size_enabled();
1494 }
1495
1498 GAIA_NODISCARD uint16_t size_disabled() const noexcept {
1499 return m_pChunk->size_disabled();
1500 }
1501 };
1502
1504 class GAIA_API CopyIter final {
1505 protected:
1507
1509 const World* m_pWorld = nullptr;
1511 const Archetype* m_pArchetype = nullptr;
1513 Chunk* m_pChunk = nullptr;
1515 uint16_t m_from;
1517 uint16_t m_cnt;
1518
1519 public:
1520 CopyIter() = default;
1521 ~CopyIter() = default;
1522 CopyIter(CopyIter&&) noexcept = default;
1523 CopyIter& operator=(CopyIter&&) noexcept = default;
1524 CopyIter(const CopyIter&) = delete;
1525 CopyIter& operator=(const CopyIter&) = delete;
1526
1530 void set_range(uint16_t from, uint16_t cnt) {
1531 GAIA_ASSERT(from < m_pChunk->size());
1532 GAIA_ASSERT(from + cnt <= m_pChunk->size());
1533 m_from = from;
1534 m_cnt = cnt;
1535 }
1536
1537 void set_world(const World* pWorld) {
1538 GAIA_ASSERT(pWorld != nullptr);
1539 m_pWorld = pWorld;
1540 }
1541
1542 GAIA_NODISCARD World* world() {
1543 GAIA_ASSERT(m_pWorld != nullptr);
1544 return const_cast<World*>(m_pWorld);
1545 }
1546
1547 GAIA_NODISCARD const World* world() const {
1548 GAIA_ASSERT(m_pWorld != nullptr);
1549 return m_pWorld;
1550 }
1551
1552 void set_archetype(const Archetype* pArchetype) {
1553 GAIA_ASSERT(pArchetype != nullptr);
1554 m_pArchetype = pArchetype;
1555 }
1556
1557 // GAIA_NODISCARD Archetype* archetype() {
1558 // GAIA_ASSERT(m_pArchetype != nullptr);
1559 // return m_pArchetype;
1560 // }
1561
1562 GAIA_NODISCARD const Archetype* archetype() const {
1563 GAIA_ASSERT(m_pArchetype != nullptr);
1564 return m_pArchetype;
1565 }
1566
1567 void set_chunk(Chunk* pChunk) {
1568 GAIA_ASSERT(pChunk != nullptr);
1569 m_pChunk = pChunk;
1570 }
1571
1572 GAIA_NODISCARD const Chunk* chunk() const {
1573 GAIA_ASSERT(m_pChunk != nullptr);
1574 return m_pChunk;
1575 }
1576
1577 GAIA_NODISCARD CommandBufferST& cmd_buffer_st() const {
1578 auto* pWorld = const_cast<World*>(m_pWorld);
1579 return cmd_buffer_st_get(*pWorld);
1580 }
1581
1582 GAIA_NODISCARD CommandBufferMT& cmd_buffer_mt() const {
1583 auto* pWorld = const_cast<World*>(m_pWorld);
1584 return cmd_buffer_mt_get(*pWorld);
1585 }
1586
1591 template <typename T>
1592 GAIA_NODISCARD auto view() const {
1593 return m_pChunk->template view<T>(from(), to());
1594 }
1595
1600 template <typename T>
1601 GAIA_NODISCARD auto view_mut() {
1602 return m_pChunk->template view_mut<T>(from(), to());
1603 }
1604
1610 template <typename T>
1611 GAIA_NODISCARD auto sview_mut() {
1612 return m_pChunk->template sview_mut<T>(from(), to());
1613 }
1614
1618 template <typename T, bool TriggerHooks>
1619 void modify() {
1620 m_pChunk->template modify<T, TriggerHooks>();
1621 }
1622
1628 template <typename T>
1629 GAIA_NODISCARD auto view_auto() {
1630 return m_pChunk->template view_auto<T>(from(), to());
1631 }
1632
1639 template <typename T>
1640 GAIA_NODISCARD auto sview_auto() {
1641 return m_pChunk->template sview_auto<T>(from(), to());
1642 }
1643
1646 GAIA_NODISCARD bool enabled(uint32_t index) const {
1647 const auto row = (uint16_t)(from() + index);
1648 return m_pChunk->enabled(row);
1649 }
1650
1654 GAIA_NODISCARD bool has(Entity entity) const {
1655 return m_pChunk->has(entity);
1656 }
1657
1661 GAIA_NODISCARD bool has(Pair pair) const {
1662 return m_pChunk->has((Entity)pair);
1663 }
1664
1668 template <typename T>
1669 GAIA_NODISCARD bool has() const {
1670 return m_pChunk->template has<T>();
1671 }
1672
1674 GAIA_NODISCARD uint16_t size() const noexcept {
1675 return m_cnt;
1676 }
1677
1678 private:
1680 GAIA_NODISCARD uint16_t from() const noexcept {
1681 return m_from;
1682 }
1683
1685 GAIA_NODISCARD uint16_t to() const noexcept {
1686 return m_from + m_cnt;
1687 }
1688 };
1689 } // namespace ecs
1690} // namespace gaia
1691
1692#include "gaia/ecs/chunk_iterator_typed.inl"
Definition span_impl.h:99
Definition archetype.h:83
Definition chunk.h:35
GAIA_NODISCARD uint16_t size_enabled() const
Return the number of entities in the chunk which are enabled.
Definition chunk.h:1809
bool enabled(uint16_t row) const
Checks if the entity is enabled.
Definition chunk.h:1359
GAIA_NODISCARD uint16_t size_disabled() const
Return the number of entities in the chunk which are enabled.
Definition chunk.h:1814
GAIA_NODISCARD uint32_t comp_idx(Entity entity) const
Returns the internal index of a component based on the provided entity.
Definition chunk.h:1694
GAIA_NODISCARD bool has(Entity entity) const
Checks if a component/entity entity is present in the chunk.
Definition chunk.h:1447
Iterator used when copying entities.
Definition chunk_iterator.h:1504
uint16_t m_from
Row of the first entity we iterate from.
Definition chunk_iterator.h:1515
GAIA_NODISCARD auto view() const
Returns a read-only entity or component view.
Definition chunk_iterator.h:1592
GAIA_NODISCARD auto sview_auto()
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk_iterator.h:1640
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:1611
GAIA_NODISCARD bool has() const
Checks if component T is present in the chunk.
Definition chunk_iterator.h:1669
GAIA_NODISCARD auto view_mut()
Returns a mutable entity or component view.
Definition chunk_iterator.h:1601
GAIA_NODISCARD bool has(Pair pair) const
Checks if relationship pair pair is present in the chunk.
Definition chunk_iterator.h:1661
uint16_t m_cnt
The number of entities accessible via the iterator.
Definition chunk_iterator.h:1517
GAIA_NODISCARD auto view_auto()
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk_iterator.h:1629
GAIA_NODISCARD bool has(Entity entity) const
Checks if entity entity is present in the chunk.
Definition chunk_iterator.h:1654
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:1619
GAIA_NODISCARD uint16_t size() const noexcept
Returns the number of entities accessible via the iterator.
Definition chunk_iterator.h:1674
GAIA_NODISCARD bool enabled(uint32_t index) const
Checks if the entity at the current iterator index is enabled.
Definition chunk_iterator.h:1646
Iterator for iterating entity subsets selected by Constraints. Disabled entities always precede enabl...
Definition chunk_iterator.h:1469
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:1498
GAIA_NODISCARD uint16_t size_enabled() const noexcept
Returns the number of enabled entities accessible via the iterator.
Definition chunk_iterator.h:1492
Definition world.h:174
Definition chunk_iterator.h:852
GAIA_NODISCARD auto view_mut(uint32_t termIdx)
Returns a mutable entity or component view for a query-term owned chunk-backed term....
Definition chunk_iterator.h:1240
GAIA_NODISCARD auto sview_any_mut()
Returns a mutable component view that can resolve non-direct storage. This is the fallback accessor f...
Definition chunk_iterator.h:1265
GAIA_NODISCARD uint16_t to() const noexcept
Returns the ending index of the iterator (one past the last valid index)
Definition chunk_iterator.h:1461
void init_query_state(const World *pWorld, Constraints constraints, bool writeIm)
Initializes stable query execution state stored by the iterator.
Definition chunk_iterator.h:998
GAIA_NODISCARD auto view_any_mut(uint32_t termIdx)
Returns a mutable entity or component view for a query-term index that can resolve non-direct storage...
Definition chunk_iterator.h:1253
Entity m_entitySnapshot[ChunkHeader::MAX_CHUNK_ENTITIES]
Stable copy of the currently iterated entity rows for mutable entity-backed views.
Definition chunk_iterator.h:881
GAIA_NODISCARD auto sview_mut()
Returns a mutable component view for the owned chunk-backed fast path. Doesn't update the world versi...
Definition chunk_iterator.h:1276
Entity m_touchedTerms[ChunkHeader::MAX_COMPONENTS]
Entity-backed terms that were exposed as mutable during the current callback.
Definition chunk_iterator.h:878
GAIA_NODISCARD uint16_t row_end() const noexcept
Returns one-past-the-end row covered by the iterator in the current chunk.
Definition chunk_iterator.h:1438
void ctx(void *pCtx)
Sets the user-owned context pointer visible through ctx().
Definition chunk_iterator.h:1095
GAIA_NODISCARD uint16_t from() const noexcept
Returns the starting index of the iterator.
Definition chunk_iterator.h:1456
GAIA_NODISCARD void * ctx() const
Returns the user-owned context pointer supplied by the caller driving this iteration.
Definition chunk_iterator.h:1101
GroupId m_groupId
GroupId. 0 if not set.
Definition chunk_iterator.h:888
GAIA_NODISCARD auto view_auto()
Returns either a mutable or immutable entity/component view for the owned chunk-backed fast path....
Definition chunk_iterator.h:1322
GAIA_NODISCARD uint16_t row_begin() const noexcept
Returns the first row covered by the iterator in the current chunk.
Definition chunk_iterator.h:1433
GAIA_NODISCARD auto view() const
Returns a read-only entity or component view for the owned chunk-backed fast path....
Definition chunk_iterator.h:1192
GAIA_NODISCARD auto sview_any_mut(uint32_t termIdx)
Returns a mutable component view for a query-term index that can resolve non-direct storage....
Definition chunk_iterator.h:1302
const Entity * m_pTermIdMapping
Optional per-term ids used by one-row direct iterators when a term resolves semantically.
Definition chunk_iterator.h:869
GAIA_NODISCARD auto sview_auto()
Returns either a mutable or immutable entity/component view for the owned chunk-backed fast path....
Definition chunk_iterator.h:1370
GAIA_NODISCARD auto sview_auto_any()
Returns either a mutable or immutable entity/component view that can resolve non-direct storage....
Definition chunk_iterator.h:1353
const World * m_pWorld
World pointer.
Definition chunk_iterator.h:859
void set_query_chunk(const Archetype *pArchetype, const uint8_t *pCompIndices, Chunk *pChunk, uint16_t from, uint16_t to)
Binds the iterator to the next query chunk and keeps archetype/mapping state on the iterator.
Definition chunk_iterator.h:1011
GAIA_NODISCARD auto view_any_mut()
Returns a mutable entity or component view that can resolve non-direct storage. This is the fallback ...
Definition chunk_iterator.h:1216
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:1446
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:1310
GAIA_NODISCARD auto view_any() const
Returns a read-only entity or component view that can resolve non-direct storage. This is the fallbac...
Definition chunk_iterator.h:1167
uint16_t m_from
Row of the first entity we iterate from.
Definition chunk_iterator.h:884
GAIA_NODISCARD bool has() const
Checks if component T is present in the chunk.
Definition chunk_iterator.h:1403
GAIA_NODISCARD bool has(Entity entity) const
Checks if entity entity is present in the chunk.
Definition chunk_iterator.h:1388
const uint8_t * m_pCompIndices
ChunkHeader::MAX_COMPONENTS values for component indices mapping for the parent archetype.
Definition chunk_iterator.h:865
Constraints m_constraints
Which entity subset the iterator currently exposes from the chunk.
Definition chunk_iterator.h:873
void * m_pCtx
User-owned pointer supplied by the caller driving this iteration.
Definition chunk_iterator.h:890
InheritedTermDataView m_inheritedData
Optional inherited term data view for exact semantic self-source terms.
Definition chunk_iterator.h:867
GAIA_NODISCARD auto view_auto_any()
Returns either a mutable or immutable entity/component view that can resolve non-direct storage....
Definition chunk_iterator.h:1337
GAIA_NODISCARD auto view_mut()
Returns a mutable entity or component view for the owned chunk-backed fast path. Updates world versio...
Definition chunk_iterator.h:1227
const Archetype * m_pArchetype
Currently iterated archetype.
Definition chunk_iterator.h:861
GAIA_NODISCARD auto view(uint32_t termIdx) const
Returns a read-only entity or component view for a query-term owned chunk-backed term....
Definition chunk_iterator.h:1205
uint8_t m_touchedCompIndices[ChunkHeader::MAX_COMPONENTS]
Chunk-backed columns that were exposed as mutable during the current callback.
Definition chunk_iterator.h:875
GAIA_NODISCARD uint16_t size() const noexcept
Returns the number of entities accessible via the iterator.
Definition chunk_iterator.h:1428
GAIA_NODISCARD auto sview_mut(uint32_t termIdx)
Returns a mutable component view for a query-term owned chunk-backed term. Doesn't update the world v...
Definition chunk_iterator.h:1289
GAIA_NODISCARD bool enabled(uint32_t index) const
Checks if the entity at the current iterator index is enabled.
Definition chunk_iterator.h:1380
uint16_t m_to
Row of the last entity we iterate to.
Definition chunk_iterator.h:886
Chunk * m_pChunk
Chunk currently associated with the iterator.
Definition chunk_iterator.h:863
GAIA_NODISCARD auto view_any(uint32_t termIdx)
Returns a read-only entity or component view for a query-term index that can resolve non-direct stora...
Definition chunk_iterator.h:1179
GAIA_NODISCARD bool has(Pair pair) const
Checks if relationship pair pair is present in the chunk.
Definition chunk_iterator.h:1395
bool m_writeIm
Whether mutable access should finish writes immediately or defer them until the callback returns.
Definition chunk_iterator.h:871
Buffer for deferred execution of some operations on entities.
Definition command_buffer.h:45
Wrapper for two Entities forming a relationship pair.
Definition id.h:529
Wrapper for two types forming a relationship pair. Depending on what types are used to form a pair it...
Definition id.h:224
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
constexpr uint32_t BadIndex
Sentinel index value returned by helpers when a lookup fails.
Definition utility.h:20
Definition bit_utils.h:11
static constexpr uint32_t MAX_COMPONENTS
Maximum number of components on archetype.
Definition chunk_header.h:53
static constexpr uint16_t MAX_CHUNK_ENTITIES
Maximum number of entities per chunk. Defined as sizeof(big_chunk) / sizeof(entity)
Definition chunk_header.h:57
Definition id.h:247
Definition chunk_iterator.h:80
Definition chunk_iterator.h:51
Read-only term view for entity-backed AoS data resolved through the world store. Used when the querie...
Definition chunk_iterator.h:112
Read-only term view for chunk-backed AoS data. Used by direct iteration paths that can guarantee cont...
Definition chunk_iterator.h:91
Read-only AoS term view fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:204
Mutable term view for entity-backed AoS data resolved through the world store. Used when writes targe...
Definition chunk_iterator.h:169
Mutable term view for chunk-backed AoS data. Exposes direct references to contiguous component storag...
Definition chunk_iterator.h:139
Mutable AoS term view fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:266
Definition chunk_iterator.h:45
Read-only field proxy for a single SoA member in entity-backed storage. Resolves the whole value via ...
Definition chunk_iterator.h:379
Read-only field proxy for a single SoA member in chunk-backed storage. Exposes a contiguous field spa...
Definition chunk_iterator.h:359
Read-only SoA field proxy fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:656
Proxy representing one writable field element in entity-backed storage.
Definition chunk_iterator.h:432
Mutable field proxy for a single SoA member in entity-backed storage. Updates reconstruct the whole v...
Definition chunk_iterator.h:427
Mutable field proxy for a single SoA member in chunk-backed storage. Returns raw references so arithm...
Definition chunk_iterator.h:402
Proxy representing a single writable SoA field element in the runtime fallback path only....
Definition chunk_iterator.h:693
Mutable SoA field proxy fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:681
Writable row proxy for entity-backed SoA data. Reads and writes the full structured value through the...
Definition chunk_iterator.h:337
Writable row proxy for chunk-backed SoA data. Reads and writes the full structured value directly fro...
Definition chunk_iterator.h:317
Writable SoA row proxy fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:624
Read-only SoA term view for entity-backed storage only. Used when the queried SoA payload lives out o...
Definition chunk_iterator.h:526
Read-only SoA term view for chunk-backed storage only. Used by direct iteration paths once the term h...
Definition chunk_iterator.h:498
Read-only SoA term view fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:761
Mutable SoA term view for entity-backed storage only. Reads and writes rows and fields through the wo...
Definition chunk_iterator.h:590
Mutable SoA term view for chunk-backed storage only. Provides direct row and field access without car...
Definition chunk_iterator.h:550
Mutable SoA term view fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:797
Definition data_layout_policy.h:432
Definition data_layout_policy.h:482
View policy for accessing and storing data in the SoA way. Good for SIMD processing.
Definition data_layout_policy.h:243