2#include "gaia/config/config.h"
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"
21 void world_finish_write(World& world, Entity term, Entity entity);
23 decltype(
auto) world_query_entity_arg_by_id_raw(World& world, Entity entity, Entity
id);
25 decltype(
auto) world_query_entity_arg_by_id_cached_const(
26 World& world, Entity entity, Entity
id,
const Archetype*& pLastArchetype, Entity& cachedOwner,
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);
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);
37 Entity world_query_arg_id(World& world);
40 enum class Constraints : uint8_t { EnabledOnly, DisabledOnly, AcceptAll };
45 Chunk* pChunk =
nullptr;
55 const U* pData =
nullptr;
58 GAIA_NODISCARD
decltype(
auto)
operator[](
size_t idx)
const {
59 GAIA_ASSERT(idx < cnt);
63 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
67 GAIA_NODISCARD
const U* data()
const noexcept {
76 const Entity* pEntities =
nullptr;
77 World* pWorld =
nullptr;
80 mutable const Archetype* pLastArchetype =
nullptr;
81 mutable Entity cachedOwner = EntityBad;
82 mutable bool cachedDirect =
false;
84 GAIA_NODISCARD
decltype(
auto)
operator[](
size_t idx)
const {
85 GAIA_ASSERT(idx < cnt);
86 return world_query_entity_arg_by_id_cached_const<const U&>(
87 *pWorld, pEntities[idx],
id, pLastArchetype, cachedOwner, cachedDirect);
90 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
94 GAIA_NODISCARD
const U* data()
const noexcept {
101 template <
typename U>
106 GAIA_NODISCARD
decltype(
auto)
operator[](
size_t idx) {
107 GAIA_ASSERT(idx < cnt);
111 GAIA_NODISCARD
decltype(
auto)
operator[](
size_t idx)
const {
112 GAIA_ASSERT(idx < cnt);
113 return static_cast<const U&
>(pData[idx]);
116 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
120 GAIA_NODISCARD U* data()
noexcept {
124 GAIA_NODISCARD
const U* data()
const noexcept {
131 template <
typename U,
bool WriteIm>
133 const Entity* pEntities =
nullptr;
134 World* pWorld =
nullptr;
138 GAIA_NODISCARD
decltype(
auto)
operator[](
size_t idx) {
139 GAIA_ASSERT(idx < cnt);
140 if constexpr (WriteIm)
141 return world_query_entity_arg_by_id<U&>(*pWorld, pEntities[idx],
id);
143 return world_query_entity_arg_by_id_raw<U&>(*pWorld, pEntities[idx],
id);
146 GAIA_NODISCARD
decltype(
auto)
operator[](
size_t idx)
const {
147 GAIA_ASSERT(idx < cnt);
148 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx],
id);
151 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
155 GAIA_NODISCARD U* data()
noexcept {
159 GAIA_NODISCARD
const U* data()
const noexcept {
166 template <
typename U>
168 const U* pData =
nullptr;
169 const Entity* pEntities =
nullptr;
170 World* pWorld =
nullptr;
173 const U* pDataInherited =
nullptr;
174 mutable const Archetype* pLastArchetype =
nullptr;
175 mutable Entity cachedOwner = EntityBad;
176 mutable bool cachedDirect =
false;
179 return {pData,
nullptr,
nullptr, EntityBad, cnt,
nullptr,
nullptr, EntityBad,
false};
183 return {
nullptr, pEntities, pWorld, id, cnt,
nullptr,
nullptr, EntityBad,
false};
187 return {
nullptr,
nullptr,
nullptr, EntityBad, cnt, pDataInherited,
nullptr, EntityBad,
false};
192 uint32_t compIdx = BadIndex;
193 const U* pDataInherited =
nullptr;
195 world_init_query_entity_arg_by_id_chunk_stable_const<U>(
196 *pWorld, *pChunk, pEntities,
id, direct, compIdx, pDataInherited);
197 const U* pData =
nullptr;
199 pData =
reinterpret_cast<const U*
>(pChunk->comp_ptr(compIdx, rowBase));
201 return {pData,
nullptr, pWorld, id, cnt, pDataInherited,
nullptr, EntityBad,
false};
204 GAIA_NODISCARD
decltype(
auto)
operator[](
size_t idx)
const {
205 GAIA_ASSERT(idx < cnt);
206 if (pData !=
nullptr)
208 if (pDataInherited !=
nullptr) {
209 GAIA_ASSERT(pDataInherited !=
nullptr);
210 return *pDataInherited;
213 return world_query_entity_arg_by_id_cached_const<const U&>(
214 *pWorld, pEntities[idx],
id, pLastArchetype, cachedOwner, cachedDirect);
217 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
221 GAIA_NODISCARD
const U* data()
const noexcept {
228 template <
typename U>
231 const Entity* pEntities =
nullptr;
232 World* pWorld =
nullptr;
238 return {pData,
nullptr,
nullptr, EntityBad, cnt,
true};
242 entity(
const Entity* pEntities,
World* pWorld,
Entity id, uint32_t cnt,
bool writeIm =
true) {
243 return {
nullptr, pEntities, pWorld, id, cnt, writeIm};
246 GAIA_NODISCARD
decltype(
auto)
operator[](
size_t idx) {
247 GAIA_ASSERT(idx < cnt);
248 if (pData !=
nullptr)
256 GAIA_NODISCARD
decltype(
auto)
operator[](
size_t idx)
const {
257 GAIA_ASSERT(idx < cnt);
258 if (pData !=
nullptr)
259 return static_cast<const U&
>(pData[idx]);
261 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx],
id);
264 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
268 GAIA_NODISCARD U* data()
noexcept {
272 GAIA_NODISCARD
const U* data()
const noexcept {
279 template <
typename U>
283 uint8_t* pData =
nullptr;
284 uint32_t dataSize = 0;
287 GAIA_NODISCARD
operator U()
const {
299 template <
typename U,
bool WriteIm>
301 const Entity* pEntities =
nullptr;
302 World* pWorld =
nullptr;
306 GAIA_NODISCARD
operator U()
const {
307 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx],
id);
311 if constexpr (WriteIm)
312 world_query_entity_arg_by_id<U&>(*pWorld, pEntities[idx],
id) = value;
314 world_query_entity_arg_by_id_raw<U&>(*pWorld, pEntities[idx],
id) = value;
321 template <
typename U,
size_t Item>
324 using value_type =
typename view_policy::template data_view_policy_idx_info<Item>::const_value_type;
326 const value_type* pData =
nullptr;
329 GAIA_NODISCARD value_type operator[](
size_t idx)
const {
330 GAIA_ASSERT(idx < cnt);
334 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
341 template <
typename U,
size_t Item>
344 using value_type =
typename view_policy::template data_view_policy_idx_info<Item>::const_value_type;
346 const Entity* pEntities =
nullptr;
347 World* pWorld =
nullptr;
351 GAIA_NODISCARD value_type operator[](
size_t idx)
const {
352 GAIA_ASSERT(idx < cnt);
353 const U value = world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx],
id);
354 return std::get<Item>(meta::struct_to_tuple(value));
357 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
364 template <
typename U,
size_t Item>
367 using value_type =
typename view_policy::template data_view_policy_idx_info<Item>::value_type;
369 value_type* pData =
nullptr;
372 GAIA_NODISCARD value_type& operator[](
size_t idx) {
373 GAIA_ASSERT(idx < cnt);
377 GAIA_NODISCARD
const value_type& operator[](
size_t idx)
const {
378 GAIA_ASSERT(idx < cnt);
382 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
389 template <
typename U,
size_t Item,
bool WriteIm>
392 using value_type =
typename view_policy::template data_view_policy_idx_info<Item>::value_type;
396 const Entity* pEntities =
nullptr;
397 World* pWorld =
nullptr;
401 GAIA_NODISCARD
operator value_type()
const {
402 const U value = world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx],
id);
403 return std::get<Item>(meta::struct_to_tuple(value));
407 auto data = world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx],
id);
408 auto tuple = meta::struct_to_tuple(data);
409 std::get<Item>(tuple) = value;
410 if constexpr (WriteIm)
411 world_query_entity_arg_by_id<U&>(*pWorld, pEntities[idx],
id) = meta::tuple_to_struct<U>(GAIA_MOV(tuple));
413 world_query_entity_arg_by_id_raw<U&>(*pWorld, pEntities[idx],
id) =
414 meta::tuple_to_struct<U>(GAIA_MOV(tuple));
418 template <
typename V>
420 const value_type current =
operator value_type();
421 return operator=(current + GAIA_FWD(value));
424 template <
typename V>
426 const value_type current =
operator value_type();
427 return operator=(current - GAIA_FWD(value));
430 template <
typename V>
432 const value_type current =
operator value_type();
433 return operator=(current * GAIA_FWD(value));
436 template <
typename V>
438 const value_type current =
operator value_type();
439 return operator=(current / GAIA_FWD(value));
443 const Entity* pEntities =
nullptr;
444 World* pWorld =
nullptr;
448 GAIA_NODISCARD
ElementProxy operator[](
size_t idx)
const {
449 GAIA_ASSERT(idx < cnt);
450 return ElementProxy{pEntities, pWorld, id, (uint32_t)idx};
453 GAIA_NODISCARD
constexpr size_t size() const noexcept {
460 template <
typename U>
465 const uint8_t* pData =
nullptr;
466 uint32_t dataSize = 0;
467 uint32_t idxBase = 0;
470 GAIA_NODISCARD U operator[](
size_t idx)
const {
471 GAIA_ASSERT(idx < cnt);
475 template <
size_t Item>
476 GAIA_NODISCARD
auto get()
const {
481 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
488 template <
typename U>
490 const Entity* pEntities =
nullptr;
491 World* pWorld =
nullptr;
495 GAIA_NODISCARD U operator[](
size_t idx)
const {
496 GAIA_ASSERT(idx < cnt);
497 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx],
id);
500 template <
size_t Item>
501 GAIA_NODISCARD
auto get()
const {
505 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
512 template <
typename U>
518 uint8_t* pData =
nullptr;
519 uint32_t dataSize = 0;
520 uint32_t idxBase = 0;
523 GAIA_NODISCARD
auto operator[](
size_t idx) {
524 GAIA_ASSERT(idx < cnt);
528 GAIA_NODISCARD U operator[](
size_t idx)
const {
529 GAIA_ASSERT(idx < cnt);
533 template <
size_t Item>
534 GAIA_NODISCARD
auto get()
const {
539 template <
size_t Item>
540 GAIA_NODISCARD
auto set() {
545 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
552 template <
typename U,
bool WriteIm>
554 const Entity* pEntities =
nullptr;
555 World* pWorld =
nullptr;
559 GAIA_NODISCARD
auto operator[](
size_t idx) {
560 GAIA_ASSERT(idx < cnt);
564 GAIA_NODISCARD U operator[](
size_t idx)
const {
565 GAIA_ASSERT(idx < cnt);
566 return world_query_entity_arg_by_id<const U&>(*pWorld, pEntities[idx],
id);
569 template <
size_t Item>
570 GAIA_NODISCARD
auto get()
const {
574 template <
size_t Item>
575 GAIA_NODISCARD
auto set() {
579 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
586 template <
typename U>
588 uint8_t* pData =
nullptr;
589 uint32_t dataSize = 0;
590 const Entity* pEntities =
nullptr;
591 World* pWorld =
nullptr;
596 GAIA_NODISCARD
operator U()
const {
597 if (pData !=
nullptr)
606 if (pData !=
nullptr) {
618 template <
typename U,
size_t Item>
620 using value_type =
typename SoATermFieldReadProxyPointer<U, Item>::value_type;
622 const value_type* pData =
nullptr;
623 const Entity* pEntities =
nullptr;
624 World* pWorld =
nullptr;
628 GAIA_NODISCARD value_type operator[](
size_t idx)
const {
629 GAIA_ASSERT(idx < cnt);
630 if (pData !=
nullptr)
636 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
643 template <
typename U,
size_t Item>
645 using value_type =
typename SoATermFieldWriteProxyPointer<U, Item>::value_type;
647 value_type* pData =
nullptr;
648 const Entity* pEntities =
nullptr;
649 World* pWorld =
nullptr;
657 value_type* pData =
nullptr;
658 const Entity* pEntities =
nullptr;
659 World* pWorld =
nullptr;
664 GAIA_NODISCARD
operator value_type()
const {
665 if (pData !=
nullptr)
674 if (pData !=
nullptr) {
686 template <
typename V>
688 const value_type current =
operator value_type();
689 return operator=(current + GAIA_FWD(value));
692 template <
typename V>
694 const value_type current =
operator value_type();
695 return operator=(current - GAIA_FWD(value));
698 template <
typename V>
700 const value_type current =
operator value_type();
701 return operator=(current * GAIA_FWD(value));
704 template <
typename V>
706 const value_type current =
operator value_type();
707 return operator=(current / GAIA_FWD(value));
711 GAIA_NODISCARD
ElementProxy operator[](
size_t idx)
const {
712 GAIA_ASSERT(idx < cnt);
713 return ElementProxy{pData, pEntities, pWorld, id, (uint32_t)idx, writeIm};
716 GAIA_NODISCARD
constexpr size_t size() const noexcept {
723 template <
typename U>
725 const uint8_t* pData =
nullptr;
726 uint32_t dataSize = 0;
727 const Entity* pEntities =
nullptr;
728 World* pWorld =
nullptr;
730 uint32_t idxBase = 0;
733 GAIA_NODISCARD U operator[](
size_t idx)
const {
734 GAIA_ASSERT(idx < cnt);
735 if (pData !=
nullptr)
741 template <
size_t Item>
742 GAIA_NODISCARD
auto get()
const {
743 if (pData !=
nullptr) {
752 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
759 template <
typename U>
761 uint8_t* pData =
nullptr;
762 uint32_t dataSize = 0;
763 const Entity* pEntities =
nullptr;
764 World* pWorld =
nullptr;
766 uint32_t idxBase = 0;
770 GAIA_NODISCARD
auto operator[](
size_t idx) {
771 GAIA_ASSERT(idx < cnt);
775 GAIA_NODISCARD U operator[](
size_t idx)
const {
776 GAIA_ASSERT(idx < cnt);
777 if (pData !=
nullptr)
783 template <
size_t Item>
784 GAIA_NODISCARD
auto get()
const {
785 if (pData !=
nullptr) {
794 template <
size_t Item>
795 GAIA_NODISCARD
auto set() {
796 if (pData !=
nullptr) {
810 GAIA_NODISCARD
constexpr size_t size()
const noexcept {
815 template <Constra
ints IterConstra
int>
836 uint8_t m_touchedCompCnt = 0;
839 uint8_t m_touchedTermCnt = 0;
842 bool m_entitySnapshotValid =
false;
858 void set_world(const
World* pWorld) {
859 GAIA_ASSERT(pWorld !=
nullptr);
863 GAIA_NODISCARD
World* world() {
868 GAIA_NODISCARD
const World* world()
const {
873 void set_archetype(
const Archetype* pArchetype) {
874 GAIA_ASSERT(pArchetype !=
nullptr);
883 GAIA_NODISCARD
const Archetype* archetype()
const {
888 void set_chunk(Chunk* pChunk) {
889 GAIA_ASSERT(pChunk !=
nullptr);
891 m_entitySnapshotValid =
false;
893 if constexpr (IterConstraint == Constraints::EnabledOnly)
898 if constexpr (IterConstraint == Constraints::DisabledOnly)
904 void set_chunk(Chunk* pChunk, uint16_t
from, uint16_t
to) {
905 if (
from == 0 &&
to == 0) {
910 GAIA_ASSERT(pChunk !=
nullptr);
912 m_entitySnapshotValid =
false;
917 GAIA_NODISCARD
const Chunk* chunk()
const {
922 void set_comp_indices(
const uint8_t* pCompIndices) {
926 void set_inherited_data(InheritedTermDataView inheritedData) {
930 void set_term_ids(
const Entity* pTermIds) {
934 GAIA_NODISCARD
const uint8_t* comp_indices()
const {
938 GAIA_NODISCARD InheritedTermDataView inherited_data()
const {
942 GAIA_NODISCARD
const Entity* term_ids()
const {
946 void set_write_im(
bool value) {
948 clear_touched_writes();
951 GAIA_NODISCARD
bool write_im()
const {
955 void clear_touched_writes() {
956 m_touchedCompCnt = 0;
957 m_touchedTermCnt = 0;
960 GAIA_NODISCARD
const Entity* entity_snapshot() {
961 if (!m_entitySnapshotValid) {
962 const auto cnt =
size();
965 const auto entities =
m_pChunk->entity_view();
970 m_entitySnapshotValid =
true;
976 GAIA_NODISCARD
auto touched_comp_indices()
const {
980 GAIA_NODISCARD
auto touched_terms()
const {
984 GAIA_NODISCARD
auto entity_rows() {
985 if (m_entitySnapshotValid)
991 template <
typename U>
992 GAIA_NODISCARD
auto entity_view_set(Entity termId,
bool writeIm) {
993 return EntityTermViewSet<U>::entity(entity_snapshot(), world(), termId,
size(), writeIm);
996 template <
typename U>
997 GAIA_NODISCARD
auto entity_soa_view_set(Entity termId,
bool writeIm) {
998 return SoATermViewSet<U>{
nullptr, 0, entity_snapshot(), world(), termId, 0,
size(), writeIm};
1001 void set_group_id(GroupId groupId) {
1005 GAIA_NODISCARD GroupId group_id()
const {
1009 GAIA_NODISCARD CommandBufferST& cmd_buffer_st()
const {
1010 auto* pWorld =
const_cast<World*
>(
m_pWorld);
1011 return cmd_buffer_st_get(*pWorld);
1014 GAIA_NODISCARD CommandBufferMT& cmd_buffer_mt()
const {
1015 auto* pWorld =
const_cast<World*
>(
m_pWorld);
1016 return cmd_buffer_mt_get(*pWorld);
1019 template <
typename T>
1020 GAIA_NODISCARD
bool uses_out_of_line_component()
const {
1021 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
1022 if constexpr (std::is_same_v<Arg, Entity>)
1025 using FT =
typename component_type_t<Arg>::TypeFull;
1026 if constexpr (is_pair<FT>::value || mem::is_soa_layout_v<Arg>)
1029 const auto* pItem = comp_cache(*world()).template find<FT>();
1032 return pItem !=
nullptr && world_is_out_of_line_component(*world(), pItem->entity);
1037 void touch_comp_idx(uint8_t compIdx) {
1038 GAIA_ASSERT(compIdx != 0xFF);
1039 GAIA_FOR(m_touchedCompCnt) {
1048 void touch_term(Entity term) {
1049 GAIA_ASSERT(term != EntityBad);
1050 GAIA_FOR(m_touchedTermCnt) {
1059 template <
typename T>
1060 void touch_term_by_type() {
1061 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
1062 if constexpr (std::is_same_v<Arg, Entity>)
1065 const auto term = world_query_arg_id<Arg>(*world());
1066 if (world_is_out_of_line_component(*world(), term))
1073 template <
typename T>
1074 GAIA_NODISCARD Entity fallback_term_id(uint32_t termIdx)
const {
1077 if (termId != EntityBad)
1081 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
1082 if constexpr (std::is_same_v<Arg, Entity>)
1085 return world_query_arg_id<Arg>(*
const_cast<World*
>(world()));
1094 template <
typename T>
1096 using U =
typename actual_type_t<T>::Type;
1097 if constexpr (std::is_same_v<U, Entity> || mem::is_soa_layout_v<U>)
1101 if (uses_out_of_line_component<T>()) {
1102 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
1103 using FT =
typename component_type_t<Arg>::TypeFull;
1104 id = comp_cache(*world()).template get<FT>().entity;
1107 if (
id != EntityBad)
1111 auto* pData =
reinterpret_cast<const U*
>(
m_pChunk->template view<T>(
from(),
to()).
data());
1123 template <
typename T>
1125 using U =
typename actual_type_t<T>::Type;
1127 if constexpr (mem::is_soa_layout_v<U>) {
1129 if (compIdx == 0xFF) {
1130 const auto* pEntities =
m_pChunk->entity_view().data() +
from();
1131 const auto termId = fallback_term_id<T>(termIdx);
1132 GAIA_ASSERT(termId != EntityBad);
1136 GAIA_ASSERT(compIdx < m_pChunk->ids_view().
size());
1140 const auto termId = fallback_term_id<T>(termIdx);
1142 if (termId != EntityBad && world_is_out_of_line_component(*world(), termId))
1146 if (compIdx == 0xFF) {
1147 GAIA_ASSERT(termId != EntityBad);
1149 const auto* pInheritedValue =
reinterpret_cast<const U*
>(
m_inheritedData[termIdx]);
1150 if (pInheritedValue !=
nullptr)
1153 if (world_term_uses_inherit_policy(*world(), termId)) {
1161 GAIA_ASSERT(compIdx < m_pChunk->ids_view().
size());
1163 auto* pData =
reinterpret_cast<const U*
>(
m_pChunk->comp_ptr_mut(compIdx,
from()));
1176 template <
typename T>
1178 using U =
typename actual_type_t<T>::Type;
1179 if constexpr (std::is_same_v<U, Entity>)
1181 else if constexpr (mem::is_soa_layout_v<U>) {
1185 auto* pData =
reinterpret_cast<const U*
>(
m_pChunk->template view<T>(
from(),
to()).
data());
1198 template <
typename T>
1199 GAIA_NODISCARD
auto view(uint32_t termIdx)
const {
1200 using U =
typename actual_type_t<T>::Type;
1202 GAIA_ASSERT(compIdx != 0xFF);
1204 if constexpr (mem::is_soa_layout_v<U>)
1207 auto* pData =
reinterpret_cast<const U*
>(
m_pChunk->comp_ptr(compIdx,
from()));
1218 template <
typename T>
1220 using U =
typename actual_type_t<T>::Type;
1221 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via view_mut is forbidden");
1222 if constexpr (mem::is_soa_layout_v<U>) {
1223 touch_term_by_type<T>();
1229 if (uses_out_of_line_component<T>()) {
1230 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
1231 using FT =
typename component_type_t<Arg>::TypeFull;
1232 id = comp_cache(*world()).template get<FT>().entity;
1235 if (
id != EntityBad) {
1237 return entity_view_set<U>(
id,
m_writeIm);
1240 touch_term_by_type<T>();
1254 template <
typename T>
1256 using U =
typename actual_type_t<T>::Type;
1257 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via view_mut is forbidden");
1258 touch_term_by_type<T>();
1259 if constexpr (mem::is_soa_layout_v<U>) {
1278 template <
typename T>
1280 using U =
typename actual_type_t<T>::Type;
1281 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via view_mut is forbidden");
1283 GAIA_ASSERT(compIdx != 0xFF);
1284 touch_comp_idx(compIdx);
1286 if constexpr (mem::is_soa_layout_v<U>) {
1293 auto* pData =
reinterpret_cast<U*
>(
m_pChunk->comp_ptr_mut(compIdx,
from()));
1306 template <
typename T>
1308 using U =
typename actual_type_t<T>::Type;
1310 if constexpr (mem::is_soa_layout_v<U>) {
1312 if (compIdx == 0xFF) {
1313 const auto termId = fallback_term_id<T>(termIdx);
1314 GAIA_ASSERT(termId != EntityBad);
1316 return entity_soa_view_set<U>(termId,
m_writeIm);
1319 GAIA_ASSERT(compIdx < m_pChunk->comp_rec_view().
size());
1320 touch_comp_idx(compIdx);
1332 const auto termId = fallback_term_id<T>(termIdx);
1334 if (termId != EntityBad && world_is_out_of_line_component(*world(), termId)) {
1336 return entity_view_set<U>(termId,
m_writeIm);
1339 if (compIdx == 0xFF) {
1340 GAIA_ASSERT(termId != EntityBad);
1342 return entity_view_set<U>(termId,
m_writeIm);
1344 GAIA_ASSERT(compIdx < m_pChunk->comp_rec_view().
size());
1346 touch_comp_idx(compIdx);
1350 auto* pData =
reinterpret_cast<U*
>(
m_pChunk->comp_ptr_mut(compIdx,
from()));
1362 template <
typename T>
1364 using U =
typename actual_type_t<T>::Type;
1365 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via sview_mut is forbidden");
1366 if constexpr (mem::is_soa_layout_v<U>)
1370 if (uses_out_of_line_component<T>()) {
1371 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
1372 using FT =
typename component_type_t<Arg>::TypeFull;
1373 id = comp_cache(*world()).template get<FT>().entity;
1376 if (
id != EntityBad)
1377 return entity_view_set<U>(
id,
false);
1379 auto* pData =
reinterpret_cast<U*
>(
m_pChunk->template sview_mut<T>(
from(),
to()).
data());
1390 template <
typename T>
1392 using U =
typename actual_type_t<T>::Type;
1393 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via sview_mut is forbidden");
1394 if constexpr (mem::is_soa_layout_v<U>) {
1398 auto* pData =
reinterpret_cast<U*
>(
m_pChunk->template sview_mut<T>(
from(),
to()).
data());
1411 template <
typename T>
1413 using U =
typename actual_type_t<T>::Type;
1414 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via sview_mut is forbidden");
1416 GAIA_ASSERT(compIdx != 0xFF);
1418 if constexpr (mem::is_soa_layout_v<U>)
1421 auto* pData =
reinterpret_cast<U*
>(
m_pChunk->comp_ptr_mut(compIdx,
from()));
1434 template <
typename T>
1436 using U =
typename actual_type_t<T>::Type;
1438 if constexpr (mem::is_soa_layout_v<U>) {
1440 if (compIdx == 0xFF) {
1441 const auto termId = fallback_term_id<T>(termIdx);
1442 GAIA_ASSERT(termId != EntityBad);
1443 return entity_soa_view_set<U>(termId,
false);
1446 GAIA_ASSERT(compIdx < m_pChunk->ids_view().
size());
1456 const auto termId = fallback_term_id<T>(termIdx);
1458 if (termId != EntityBad && world_is_out_of_line_component(*world(), termId))
1459 return entity_view_set<U>(termId,
false);
1461 if (compIdx == 0xFF) {
1462 GAIA_ASSERT(termId != EntityBad);
1463 return entity_view_set<U>(termId,
false);
1465 GAIA_ASSERT(compIdx < m_pChunk->ids_view().
size());
1467 auto* pData =
reinterpret_cast<U*
>(
m_pChunk->comp_ptr_mut(compIdx,
from()));
1475 template <
typename T,
bool TriggerHooks>
1487 template <
typename T>
1489 using UOriginal =
typename actual_type_t<T>::TypeOriginal;
1490 if constexpr (core::is_mut_v<UOriginal>)
1491 return view_mut<T>();
1502 template <
typename T>
1504 using UOriginal =
typename actual_type_t<T>::TypeOriginal;
1505 if constexpr (core::is_mut_v<UOriginal>)
1506 return view_mut_any<T>();
1508 return view_any<T>();
1518 template <
typename T>
1520 using UOriginal =
typename actual_type_t<T>::TypeOriginal;
1521 if constexpr (core::is_mut_v<UOriginal>)
1522 return sview_mut_any<T>();
1524 return view_any<T>();
1535 template <
typename T>
1537 using UOriginal =
typename actual_type_t<T>::TypeOriginal;
1538 if constexpr (core::is_mut_v<UOriginal>)
1539 return sview_mut<T>();
1546 GAIA_NODISCARD
bool enabled(uint32_t index)
const {
1547 const auto row = (uint16_t)(
from() + index);
1568 template <
typename T>
1569 GAIA_NODISCARD
bool has()
const {
1573 GAIA_NODISCARD
static uint16_t start_index(
Chunk* pChunk)
noexcept {
1574 if constexpr (IterConstraint == Constraints::EnabledOnly)
1575 return pChunk->size_disabled();
1580 GAIA_NODISCARD
static uint16_t end_index(
Chunk* pChunk)
noexcept {
1581 if constexpr (IterConstraint == Constraints::DisabledOnly)
1582 return pChunk->size_enabled();
1584 return pChunk->size();
1587 GAIA_NODISCARD
static uint16_t
size(Chunk* pChunk)
noexcept {
1588 if constexpr (IterConstraint == Constraints::EnabledOnly)
1589 return pChunk->size_enabled();
1590 else if constexpr (IterConstraint == Constraints::DisabledOnly)
1591 return pChunk->size_disabled();
1593 return pChunk->size();
1597 GAIA_NODISCARD uint16_t
size() const noexcept {
1598 return (uint16_t)(
to() -
from());
1607 GAIA_NODISCARD uint16_t
row_end() const noexcept {
1614 template <
typename T>
1616 using U =
typename actual_type_t<T>::Type;
1618 if constexpr (mem::is_soa_layout_v<U>)
1619 return idx +
from();
1626 template <
typename T>
1628 (void)
sizeof(
typename actual_type_t<T>::Type);
1634 GAIA_NODISCARD uint16_t
from() const noexcept {
1639 GAIA_NODISCARD uint16_t
to() const noexcept {
1693 void set_range(uint16_t from, uint16_t cnt) {
1694 GAIA_ASSERT(from < m_pChunk->size());
1695 GAIA_ASSERT(from + cnt <= m_pChunk->size());
1700 void set_world(
const World* pWorld) {
1701 GAIA_ASSERT(pWorld !=
nullptr);
1705 GAIA_NODISCARD World* world() {
1706 GAIA_ASSERT(m_pWorld !=
nullptr);
1707 return const_cast<World*
>(m_pWorld);
1710 GAIA_NODISCARD
const World* world()
const {
1711 GAIA_ASSERT(m_pWorld !=
nullptr);
1715 void set_archetype(
const Archetype* pArchetype) {
1716 GAIA_ASSERT(pArchetype !=
nullptr);
1717 m_pArchetype = pArchetype;
1725 GAIA_NODISCARD
const Archetype* archetype()
const {
1726 GAIA_ASSERT(m_pArchetype !=
nullptr);
1727 return m_pArchetype;
1730 void set_chunk(Chunk* pChunk) {
1731 GAIA_ASSERT(pChunk !=
nullptr);
1735 GAIA_NODISCARD
const Chunk* chunk()
const {
1736 GAIA_ASSERT(m_pChunk !=
nullptr);
1740 GAIA_NODISCARD CommandBufferST& cmd_buffer_st()
const {
1741 auto* pWorld =
const_cast<World*
>(m_pWorld);
1742 return cmd_buffer_st_get(*pWorld);
1745 GAIA_NODISCARD CommandBufferMT& cmd_buffer_mt()
const {
1746 auto* pWorld =
const_cast<World*
>(m_pWorld);
1747 return cmd_buffer_mt_get(*pWorld);
1754 template <
typename T>
1756 return m_pChunk->
view<T>(from(), to());
1763 template <
typename T>
1765 return m_pChunk->
view_mut<T>(from(), to());
1773 template <
typename T>
1775 return m_pChunk->
sview_mut<T>(from(), to());
1781 template <
typename T,
bool TriggerHooks>
1783 m_pChunk->
modify<T, TriggerHooks>();
1791 template <
typename T>
1793 return m_pChunk->
view_auto<T>(from(), to());
1802 template <
typename T>
1804 return m_pChunk->
sview_auto<T>(from(), to());
1809 GAIA_NODISCARD
bool enabled(uint32_t index)
const {
1810 const auto row = (uint16_t)(from() + index);
1811 return m_pChunk->
enabled(row);
1818 return m_pChunk->
has(entity);
1831 template <
typename T>
1832 GAIA_NODISCARD
bool has()
const {
1833 return m_pChunk->
has<T>();
1837 GAIA_NODISCARD uint16_t
size() const noexcept {
1843 GAIA_NODISCARD uint16_t from() const noexcept {
1848 GAIA_NODISCARD uint16_t to() const noexcept {
1849 return m_from + m_cnt;
Definition span_impl.h:99
Definition archetype.h:83
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:782
GAIA_NODISCARD uint16_t capacity() const
Returns the number of entities in the chunk.
Definition chunk.h:1813
uint8_t & data(uint32_t offset)
Returns a mutable pointer to chunk data.
Definition chunk.h:1362
GAIA_FORCEINLINE void update_world_version(uint32_t compIdx)
Update the version of a component at the index.
Definition chunk.h:1846
GAIA_NODISCARD decltype(auto) view(uint16_t from, uint16_t to) const
Returns a read-only entity or component view.
Definition chunk.h:609
GAIA_NODISCARD uint16_t size_enabled() const
Return the number of entities in the chunk which are enabled.
Definition chunk.h:1803
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:760
GAIA_NODISCARD uint16_t size() const
Returns the total number of entities in the chunk (both enabled and disabled)
Definition chunk.h:1793
bool enabled(uint16_t row) const
Checks if the entity is enabled.
Definition chunk.h:1353
GAIA_NODISCARD uint16_t size_disabled() const
Return the number of entities in the chunk which are enabled.
Definition chunk.h:1808
GAIA_NODISCARD uint32_t comp_idx(Entity entity) const
Returns the internal index of a component based on the provided entity.
Definition chunk.h:1688
GAIA_NODISCARD bool has(Entity entity) const
Checks if a component/entity entity is present in the chunk.
Definition chunk.h:1441
GAIA_NODISCARD decltype(auto) view_mut(uint16_t from, uint16_t to)
Returns a mutable entity or component view.
Definition chunk.h:637
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:669
GAIA_FORCEINLINE void modify()
Marks the component.
Definition chunk.h:703
Iterator used when copying entities.
Definition chunk_iterator.h:1667
uint16_t m_from
Row of the first entity we iterate from.
Definition chunk_iterator.h:1678
GAIA_NODISCARD auto view() const
Returns a read-only entity or component view.
Definition chunk_iterator.h:1755
GAIA_NODISCARD auto sview_auto()
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk_iterator.h:1803
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:1774
GAIA_NODISCARD bool has() const
Checks if component T is present in the chunk.
Definition chunk_iterator.h:1832
GAIA_NODISCARD auto view_mut()
Returns a mutable entity or component view.
Definition chunk_iterator.h:1764
GAIA_NODISCARD bool has(Pair pair) const
Checks if relationship pair pair is present in the chunk.
Definition chunk_iterator.h:1824
uint16_t m_cnt
The number of entities accessible via the iterator.
Definition chunk_iterator.h:1680
GAIA_NODISCARD auto view_auto()
Returns either a mutable or immutable entity/component view based on the requested type....
Definition chunk_iterator.h:1792
GAIA_NODISCARD bool has(Entity entity) const
Checks if entity entity is present in the chunk.
Definition chunk_iterator.h:1817
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:1782
GAIA_NODISCARD uint16_t size() const noexcept
Returns the number of entities accessible via the iterator.
Definition chunk_iterator.h:1837
GAIA_NODISCARD bool enabled(uint32_t index) const
Checks if the entity at the current iterator index is enabled.
Definition chunk_iterator.h:1809
Iterator for iterating both enabled and disabled entities. Disabled entities always precede enabled o...
Definition chunk_iterator.h:1652
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:1661
GAIA_NODISCARD uint16_t size_enabled() const noexcept
Returns the number of enabled entities accessible via the iterator.
Definition chunk_iterator.h:1655
Iterator for iterating disabled entities.
Definition chunk_iterator.h:1648
Iterator for iterating enabled entities.
Definition chunk_iterator.h:1646
Definition chunk_iterator.h:816
GAIA_NODISCARD uint16_t to() const noexcept
Returns the ending index of the iterator (one past the last valid index)
Definition chunk_iterator.h:1639
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:1536
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:1615
Chunk * m_pChunk
Chunk currently associated with the iterator.
Definition chunk_iterator.h:825
GAIA_NODISCARD auto view() const
Returns a read-only entity or component view for the owned chunk-backed fast path....
Definition chunk_iterator.h:1177
Entity m_touchedTerms[ChunkHeader::MAX_COMPONENTS]
Entity-backed terms that were exposed as mutable during the current callback.
Definition chunk_iterator.h:838
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:1391
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:1124
GAIA_NODISCARD uint16_t size() const noexcept
Returns the number of entities accessible via the iterator.
Definition chunk_iterator.h:1597
uint32_t acc_index_direct(uint32_t idx) const noexcept
Returns the local row index for direct iterator views. Direct views already encode the iterator row b...
Definition chunk_iterator.h:1627
const Archetype * m_pArchetype
Currently iterated archetype.
Definition chunk_iterator.h:823
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:1503
GAIA_NODISCARD uint16_t row_begin() const noexcept
Returns the first row covered by the iterator in the current chunk.
Definition chunk_iterator.h:1602
const uint8_t * m_pCompIndices
ChunkHeader::MAX_COMPONENTS values for component indices mapping for the parent archetype.
Definition chunk_iterator.h:827
GAIA_NODISCARD auto view_mut_any()
Returns a mutable entity or component view that can resolve non-direct storage. This is the fallback ...
Definition chunk_iterator.h:1219
bool m_writeIm
Whether mutable access should finish writes immediately or defer them until the callback returns.
Definition chunk_iterator.h:833
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:1476
GAIA_NODISCARD bool enabled(uint32_t index) const
Checks if the entity at the current iterator index is enabled.
Definition chunk_iterator.h:1546
GAIA_NODISCARD auto sview_mut_any()
Returns a mutable component view that can resolve non-direct storage. This is the fallback accessor f...
Definition chunk_iterator.h:1363
const World * m_pWorld
World pointer.
Definition chunk_iterator.h:821
uint16_t m_from
Row of the first entity we iterate from.
Definition chunk_iterator.h:844
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:1412
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:1488
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:1095
InheritedTermDataView m_inheritedData
Optional inherited term data view for exact semantic self-source terms.
Definition chunk_iterator.h:829
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:1607
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:1519
GAIA_NODISCARD bool has() const
Checks if component T is present in the chunk.
Definition chunk_iterator.h:1569
GroupId m_groupId
GroupId. 0 if not set.
Definition chunk_iterator.h:848
GAIA_NODISCARD bool has(Pair pair) const
Checks if relationship pair pair is present in the chunk.
Definition chunk_iterator.h:1561
GAIA_NODISCARD bool has(Entity entity) const
Checks if entity entity is present in the chunk.
Definition chunk_iterator.h:1554
Entity m_entitySnapshot[ChunkHeader::MAX_CHUNK_ENTITIES]
Stable copy of the currently iterated entity rows for mutable entity-backed views.
Definition chunk_iterator.h:841
uint8_t m_touchedCompIndices[ChunkHeader::MAX_COMPONENTS]
Chunk-backed columns that were exposed as mutable during the current callback.
Definition chunk_iterator.h:835
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:1279
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:1199
GAIA_NODISCARD uint16_t from() const noexcept
Returns the starting index of the iterator.
Definition chunk_iterator.h:1634
const Entity * m_pTermIdMapping
Optional per-term ids used by one-row direct iterators when a term resolves semantically.
Definition chunk_iterator.h:831
GAIA_NODISCARD auto sview_mut_any(uint32_t termIdx)
Returns a mutable component view for a query-term index that can resolve non-direct storage....
Definition chunk_iterator.h:1435
GAIA_NODISCARD auto view_mut_any(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:1307
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:1255
uint16_t m_to
Row of the last entity we iterate to.
Definition chunk_iterator.h:846
Wrapper for two Entities forming a relationship pair.
Definition id.h:500
Wrapper for two types forming a relationship pair. Depending on what types are used to form a pair it...
Definition id.h:218
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition bit_utils.h:11
Definition chunk_iterator.h:43
Read-only term view for entity-backed AoS data resolved through the world store. Used when the querie...
Definition chunk_iterator.h:75
Read-only term view for chunk-backed AoS data. Used by direct iteration paths that can guarantee cont...
Definition chunk_iterator.h:54
Read-only AoS term view fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:167
Mutable term view for entity-backed AoS data resolved through the world store. Used when writes targe...
Definition chunk_iterator.h:132
Mutable term view for chunk-backed AoS data. Exposes direct references to contiguous component storag...
Definition chunk_iterator.h:102
Mutable AoS term view fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:229
Read-only field proxy for a single SoA member in entity-backed storage. Resolves the whole value via ...
Definition chunk_iterator.h:342
Read-only field proxy for a single SoA member in chunk-backed storage. Exposes a contiguous field spa...
Definition chunk_iterator.h:322
Read-only SoA field proxy fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:619
Proxy representing one writable field element in entity-backed storage.
Definition chunk_iterator.h:395
Mutable field proxy for a single SoA member in entity-backed storage. Updates reconstruct the whole v...
Definition chunk_iterator.h:390
Mutable field proxy for a single SoA member in chunk-backed storage. Returns raw references so arithm...
Definition chunk_iterator.h:365
Proxy representing a single writable SoA field element in the runtime fallback path only....
Definition chunk_iterator.h:656
Mutable SoA field proxy fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:644
Writable row proxy for entity-backed SoA data. Reads and writes the full structured value through the...
Definition chunk_iterator.h:300
Writable row proxy for chunk-backed SoA data. Reads and writes the full structured value directly fro...
Definition chunk_iterator.h:280
Writable SoA row proxy fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:587
Read-only SoA term view for entity-backed storage only. Used when the queried SoA payload lives out o...
Definition chunk_iterator.h:489
Read-only SoA term view for chunk-backed storage only. Used by direct iteration paths once the term h...
Definition chunk_iterator.h:461
Read-only SoA term view fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:724
Mutable SoA term view for entity-backed storage only. Reads and writes rows and fields through the wo...
Definition chunk_iterator.h:553
Mutable SoA term view for chunk-backed storage only. Provides direct row and field access without car...
Definition chunk_iterator.h:513
Mutable SoA term view fallback for APIs where storage mode is known only at runtime....
Definition chunk_iterator.h:760
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