2#include "gaia/config/config.h"
3#include "gaia/config/profiler.h"
11#include "gaia/cnt/sarray_ext.h"
12#include "gaia/core/utility.h"
13#include "gaia/ecs/archetype_common.h"
14#include "gaia/ecs/chunk_allocator.h"
15#include "gaia/ecs/chunk_header.h"
16#include "gaia/ecs/common.h"
17#include "gaia/ecs/component.h"
18#include "gaia/ecs/component_cache.h"
19#include "gaia/ecs/component_desc.h"
20#include "gaia/ecs/entity_container.h"
21#include "gaia/ecs/id.h"
22#include "gaia/mem/data_layout_policy.h"
23#include "gaia/mem/mem_alloc.h"
24#include "gaia/ser/ser_binary.h"
25#include "gaia/ser/ser_rt.h"
31 void world_invalidate_sorted_queries_for_entity(World& world, Entity entity);
32 void world_invalidate_sorted_queries(World& world);
33 void world_notify_on_set(World& world, Entity term, Chunk& chunk, uint16_t from, uint16_t to);
59 GAIA_MSVC_WARNING_PUSH()
60 GAIA_MSVC_WARNING_DISABLE(26495)
67 uint32_t chunkIndex, uint16_t capacity, uint8_t genEntities,
68 uint32_t& worldVersion):
69 m_header(wld, cc, chunkIndex, capacity, genEntities, worldVersion) {
76 GAIA_MSVC_WARNING_POP()
78 GAIA_CLANG_WARNING_PUSH()
80 GAIA_CLANG_WARNING_DISABLE(
"-Wcast-align")
91 if (cntEntities > 0) {
97 for (; j < cntEntities; ++j)
99 for (; j < ChunkHeader::MAX_COMPONENTS; ++j)
104 if (cntEntities > 0) {
106 GAIA_FOR_(cntEntities, j) {
108 pItems[j] ==
nullptr ?
Component(IdentifierIdBad, 0, 0, 0, DataStorageType::Table) : pItems[j]->
comp;
109 dst[j].pData = &data(compOffs[j]);
110 dst[j].pItem = pItems[j];
118 auto recs = comp_rec_view();
119 const auto recs_cnt = recs.size();
121 const auto& rec = recs[i];
122 if (!component_has_inline_data(rec.comp))
126 if (e.kind() == EntityKind::EK_Gen) {
134 call_ctor(0, *rec.pItem);
140 update_world_version_init();
143 GAIA_CLANG_WARNING_POP()
168 template <
typename T>
169 GAIA_NODISCARD GAIA_FORCEINLINE
auto view_inter_idx(uint32_t compIdx, uint32_t from, uint32_t to)
const
172 if constexpr (std::is_same_v<core::raw_t<T>,
Entity>) {
173 GAIA_ASSERT(to <= m_header.
count);
174 return {(
const uint8_t*)&m_records.
pEntities[from], to - from};
176 using TT =
typename T::type;
177 using U =
typename component_type_t<TT>::Type;
178 static_assert(!std::is_empty_v<U>,
"Attempting to get value of an empty component");
180 constexpr auto kind = entity_kind_v<TT>;
182 if constexpr (mem::is_soa_layout_v<U>) {
183 GAIA_ASSERT(from == 0);
184 GAIA_ASSERT(to == capacity());
185 return {comp_ptr(compIdx), to};
186 }
else if constexpr (kind == EntityKind::EK_Gen) {
187 GAIA_ASSERT(to <= m_header.
count);
188 return {comp_ptr(compIdx, from), to - from};
190 GAIA_ASSERT(to <= m_header.
count);
192 return {comp_ptr(compIdx), 1};
195 using U =
typename component_type_t<T>::Type;
196 static_assert(!std::is_empty_v<U>,
"Attempting to get value of an empty component");
198 constexpr auto kind = entity_kind_v<T>;
200 if constexpr (mem::is_soa_layout_v<U>) {
201 GAIA_ASSERT(from == 0);
202 GAIA_ASSERT(to == capacity());
203 return {comp_ptr(compIdx), to};
204 }
else if constexpr (kind == EntityKind::EK_Gen) {
205 GAIA_ASSERT(to <= m_header.
count);
206 return {comp_ptr(compIdx, from), to - from};
208 GAIA_ASSERT(to <= m_header.
count);
210 return {comp_ptr(compIdx), 1};
221 template <
typename T>
222 GAIA_NODISCARD GAIA_FORCEINLINE
auto view_inter(uint32_t from, uint32_t to)
const
224 if constexpr (std::is_same_v<core::raw_t<T>,
Entity>)
225 return view_inter_idx<T>(
BadIndex, from, to);
227 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
228 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
229 return view_inter_idx<T>(comp_idx((
Entity)
Pair(rel, tgt)), from, to);
231 const auto comp = m_header.
cc->
get<T>().entity;
232#if GAIA_ASSERT_ENABLED
233 constexpr auto kind = entity_kind_v<T>;
234 GAIA_ASSERT(comp.kind() == kind);
236 return view_inter_idx<T>(comp_idx(comp), from, to);
248 template <
typename T,
bool WorldVersionUpdateWanted>
249 GAIA_NODISCARD GAIA_FORCEINLINE
auto view_mut_inter_idx(uint32_t compIdx, uint32_t from, uint32_t to)
251 static_assert(!std::is_same_v<core::raw_t<T>,
Entity>,
"view_mut can't be used to modify Entity");
254 using TT =
typename T::type;
255 using U =
typename component_type_t<TT>::Type;
256 static_assert(!std::is_empty_v<U>,
"view_mut can't be used to modify tag components");
258 constexpr auto kind = entity_kind_v<TT>;
261 if constexpr (WorldVersionUpdateWanted) {
262 update_world_version(compIdx);
264#if GAIA_ENABLE_SET_HOOKS
265 const auto& rec = m_records.
pRecords[compIdx];
266 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
267 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
271 if constexpr (mem::is_soa_layout_v<U>) {
272 GAIA_ASSERT(from == 0);
273 GAIA_ASSERT(to == capacity());
274 return {comp_ptr_mut(compIdx), to};
275 }
else if constexpr (kind == EntityKind::EK_Gen) {
276 GAIA_ASSERT(to <= m_header.
count);
277 return {comp_ptr_mut(compIdx, from), to - from};
279 GAIA_ASSERT(to <= m_header.
count);
281 return {comp_ptr_mut(compIdx), 1};
284 using U =
typename component_type_t<T>::Type;
285 static_assert(!std::is_empty_v<U>,
"view_mut can't be used to modify tag components");
286 constexpr auto kind = entity_kind_v<T>;
289 if constexpr (WorldVersionUpdateWanted) {
290 update_world_version(compIdx);
292#if GAIA_ENABLE_SET_HOOKS
293 const auto& rec = m_records.
pRecords[compIdx];
294 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
295 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
299 if constexpr (mem::is_soa_layout_v<U>) {
300 GAIA_ASSERT(from == 0);
301 GAIA_ASSERT(to == capacity());
302 return {comp_ptr_mut(compIdx), to};
303 }
else if constexpr (kind == EntityKind::EK_Gen) {
304 GAIA_ASSERT(to <= m_header.
count);
305 return {comp_ptr_mut(compIdx, from), to - from};
307 GAIA_ASSERT(to <= m_header.
count);
309 return {comp_ptr_mut(compIdx), 1};
314 template <
typename T,
bool WorldVersionUpdateWanted>
315 GAIA_NODISCARD GAIA_FORCEINLINE
auto view_mut_inter(uint32_t from, uint32_t to)
318 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
319 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
320 return view_mut_inter_idx<T, WorldVersionUpdateWanted>(comp_idx((
Entity)
Pair(rel, tgt)), from, to);
322 const auto comp = m_header.
cc->
get<T>().entity;
323#if GAIA_ASSERT_ENABLED
324 constexpr auto kind = entity_kind_v<T>;
325 GAIA_ASSERT(comp.kind() == kind);
327 return view_mut_inter_idx<T, WorldVersionUpdateWanted>(comp_idx(comp), from, to);
338 template <
bool WorldVersionUpdateWanted>
341 if constexpr (WorldVersionUpdateWanted) {
342 update_world_version(compIdx);
344#if GAIA_ENABLE_SET_HOOKS
345 const auto& rec = m_records.
pRecords[compIdx];
346 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
347 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
351 return comp_ptr_mut(compIdx, row);
361 update_world_version(compIdx);
363#if GAIA_ENABLE_SET_HOOKS
364 const auto& rec = m_records.
pRecords[compIdx];
365 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
366 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
379 template <
typename T>
380 GAIA_NODISCARD
decltype(
auto) comp_inter(uint16_t row)
const {
381 using U =
typename actual_type_t<T>::Type;
382 using RetValueType =
decltype(view<T>()[0]);
384 GAIA_ASSERT(row < m_header.
count);
385 if constexpr (mem::is_soa_layout_v<U>)
386 return view<T>(0, capacity())[row];
387 else if constexpr (
sizeof(RetValueType) <= 8)
388 return view<T>()[row];
390 return (
const U&)view<T>()[row];
393 template <
typename T>
394 GAIA_NODISCARD
decltype(
auto) comp_inter_idx(uint16_t row, uint32_t compIdx)
const {
395 using U =
typename actual_type_t<T>::Type;
396 using RetValueType =
decltype(view_raw<T>((
const void*)
nullptr, 1)[0]);
398 GAIA_ASSERT(row < m_header.
count);
399 if constexpr (mem::is_soa_layout_v<U>)
400 return view_raw<T>(comp_ptr(compIdx), capacity())[row];
401 else if constexpr (entity_kind_v<T> == EntityKind::EK_Gen) {
402 if constexpr (
sizeof(RetValueType) <= 8)
403 return view_raw<T>(comp_ptr(compIdx, row), 1)[0];
405 return (
const U&)view_raw<T>(comp_ptr(compIdx, row), 1)[0];
407 if constexpr (
sizeof(RetValueType) <= 8)
408 return view_raw<T>(comp_ptr(compIdx), 1)[0];
410 return (
const U&)view_raw<T>(comp_ptr(compIdx), 1)[0];
414 template <
typename T,
bool WorldVersionUpdateWanted>
415 GAIA_NODISCARD
decltype(
auto) comp_mut_idx(uint16_t row, uint32_t compIdx) {
416 using U =
typename actual_type_t<T>::Type;
418 GAIA_ASSERT(row < m_header.
capacity);
419 if constexpr (mem::is_soa_layout_v<U>)
420 return view_mut_raw<T>(comp_ptr_mut_gen<WorldVersionUpdateWanted>(compIdx, 0), capacity())[row];
421 else if constexpr (entity_kind_v<T> == EntityKind::EK_Gen)
422 return view_mut_raw<T>(comp_ptr_mut_gen<WorldVersionUpdateWanted>(compIdx, row), 1)[0];
424 return view_mut_raw<T>(comp_ptr_mut_gen<WorldVersionUpdateWanted>(compIdx, 0), 1)[0];
428 Chunk(
const Chunk& chunk) =
delete;
429 Chunk(Chunk&& chunk) =
delete;
430 Chunk& operator=(
const Chunk& chunk) =
delete;
431 Chunk& operator=(Chunk&& chunk) =
delete;
434 static constexpr uint16_t chunk_header_size() {
435 const auto dataAreaOffset =
437 MemoryBlockUsableOffset +
439 sizeof(ChunkHeader) +
sizeof(ChunkRecords);
440 static_assert(dataAreaOffset % MemoryBlockAlignment == 0);
441 static_assert(dataAreaOffset < UINT16_MAX);
442 return dataAreaOffset;
445 static constexpr uint16_t chunk_total_bytes(uint16_t dataSize) {
446 return chunk_header_size() + dataSize;
449 static constexpr uint16_t chunk_data_bytes(uint16_t totalSize) {
450 return totalSize - chunk_header_size();
458 const auto chunk_offset = (uintptr_t)&chunk;
459 const auto data_offset = (uintptr_t)&chunk.m_data[0];
460 return data_offset - chunk_offset;
467 uint32_t chunkIndex, uint16_t capacity, uint8_t cntEntities, uint8_t genEntities,
468 uint16_t dataBytes, uint32_t& worldVersion,
476 const ChunkDataOffset* compOffs) {
477 const auto totalBytes = chunk_total_bytes(dataBytes);
478#if GAIA_ECS_CHUNK_ALLOCATOR
479 auto* pChunk = (
Chunk*)ChunkAllocator::get().alloc(totalBytes);
480 (void)
new (pChunk)
Chunk(wld, cc, chunkIndex, capacity, genEntities, worldVersion);
482 GAIA_ASSERT(totalBytes <= MaxMemoryBlockSize);
483 const auto sizeType = mem_block_size_type(totalBytes);
484 const auto allocSize = mem_block_size(sizeType);
485 auto* pChunkMem = mem::AllocHelper::alloc<uint8_t>(allocSize);
486 std::memset(pChunkMem, 0, allocSize);
487 auto* pChunk =
new (pChunkMem)
Chunk(wld, cc, chunkIndex, capacity, genEntities, worldVersion);
490 pChunk->init((uint32_t)cntEntities, ids, pItems, offsets, compOffs);
497 GAIA_ASSERT(pChunk !=
nullptr);
498 GAIA_ASSERT(!pChunk->
dead());
504 pChunk->call_all_dtors();
507#if GAIA_ECS_CHUNK_ALLOCATOR
508 ChunkAllocator::get().free(pChunk);
510 mem::AllocHelper::free((uint8_t*)pChunk);
515 s.save(m_header.
count);
516 if (m_header.
count == 0)
521 const uint16_t dead = m_header.
dead;
524 s.save(lifespanCountdown);
526 const auto cnt = (uint32_t)m_header.
count;
527 const auto cap = (uint32_t)m_header.
capacity;
532 GAIA_FOR(cnt) s.save(pData[i]);
537 for (
const auto& rec: comp_rec_view()) {
539 if (!component_has_inline_data(rec.comp))
542 rec.pItem->save(s, rec.pData, 0, cnt, cap);
547 void load(ser::serializer& s) {
548 uint16_t prevCount = m_header.
count;
549 s.load(m_header.
count);
550 if (m_header.
count == 0)
556 uint16_t lifespanCountdown = 0;
558 s.load(lifespanCountdown);
559 m_header.
dead = dead != 0;
562 const auto cnt = (uint32_t)m_header.
count;
563 const auto cap = (uint32_t)m_header.
capacity;
570 entity_view_mut()[i] = e;
575 call_gen_ctors(prevCount, cnt);
577 for (
const auto& rec: comp_rec_view()) {
579 if (!component_has_inline_data(rec.comp))
582 rec.pItem->load(s, rec.pData, 0, cnt, cap);
591 GAIA_ASSERT(!empty());
593#if GAIA_ASSERT_ENABLED
595 entity_view_mut()[m_header.
count - 1] = EntityBad;
604 update_world_version();
605 update_entity_order_version();
614 template <
typename T>
615 GAIA_NODISCARD
decltype(
auto)
view(uint16_t from, uint16_t to)
const {
616 using U =
typename actual_type_t<T>::Type;
619 if constexpr (mem::is_soa_layout_v<U>)
625 template <
typename T>
626 GAIA_NODISCARD
decltype(
auto) view()
const {
627 return view<T>(0, m_header.
count);
630 template <
typename T>
631 GAIA_NODISCARD
decltype(
auto) view_raw(
const void* ptr, uint32_t size)
const {
632 using U =
typename actual_type_t<T>::Type;
642 template <
typename T>
643 GAIA_NODISCARD
decltype(
auto)
view_mut(uint16_t from, uint16_t to) {
644 using U =
typename actual_type_t<T>::Type;
645 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via view_mut is forbidden");
648 if constexpr (mem::is_soa_layout_v<U>)
654 template <
typename T>
655 GAIA_NODISCARD
decltype(
auto) view_mut() {
656 return view_mut<T>(0, m_header.
count);
659 template <
typename T>
660 GAIA_NODISCARD
decltype(
auto) view_mut_raw(
void* ptr, uint32_t size)
const {
661 using U =
typename actual_type_t<T>::Type;
662 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via view_mut is forbidden");
674 template <
typename T>
675 GAIA_NODISCARD
decltype(
auto)
sview_mut(uint16_t from, uint16_t to) {
676 using U =
typename actual_type_t<T>::Type;
677 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via sview_mut is forbidden");
680 if constexpr (mem::is_soa_layout_v<U>)
686 template <
typename T>
687 GAIA_NODISCARD
decltype(
auto) sview_mut_raw(
void* ptr, uint32_t size)
const {
688 using U =
typename actual_type_t<T>::Type;
689 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via sview_mut is forbidden");
694 template <
typename T>
695 GAIA_NODISCARD
decltype(
auto) sview_mut() {
696 return sview_mut<T>(0, m_header.
count);
710 static_assert(!std::is_same_v<core::raw_t<T>,
Entity>,
"mod can't be used to modify Entity");
713 using TT =
typename T::type;
714 using U =
typename component_type_t<TT>::Type;
715 static_assert(!std::is_empty_v<U>,
"mut can't be used to modify tag components");
717#if GAIA_ASSERT_ENABLED
720 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
721 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
722 const auto compIdx = comp_idx((
Entity)
Pair(rel, tgt));
725 update_world_version(compIdx);
727#if GAIA_ENABLE_SET_HOOKS
728 if constexpr (TriggerSetHooks) {
729 const auto& rec = m_records.
pRecords[compIdx];
730 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
731 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
735 using U =
typename component_type_t<T>::Type;
736 static_assert(!std::is_empty_v<U>,
"mut can't be used to modify tag components");
738#if GAIA_ASSERT_ENABLED
739 constexpr auto kind = entity_kind_v<T>;
741 const auto comp = m_header.
cc->
get<T>().entity;
742 GAIA_ASSERT(comp.kind() == kind);
743 const auto compIdx = comp_idx(comp);
746 update_world_version(compIdx);
748#if GAIA_ENABLE_SET_HOOKS
749 if constexpr (TriggerSetHooks) {
750 const auto& rec = m_records.
pRecords[compIdx];
751 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
752 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
765 template <
typename T>
766 GAIA_NODISCARD
decltype(
auto)
view_auto(uint16_t from, uint16_t to) {
767 using UOriginal =
typename actual_type_t<T>::TypeOriginal;
768 if constexpr (core::is_mut_v<UOriginal>)
769 return view_mut<T>(from, to);
771 return view<T>(from, to);
774 template <
typename T>
775 GAIA_NODISCARD
decltype(
auto) view_auto() {
776 return view_auto<T>(0, m_header.
count);
787 template <
typename T>
788 GAIA_NODISCARD
decltype(
auto)
sview_auto(uint16_t from, uint16_t to) {
789 using UOriginal =
typename actual_type_t<T>::TypeOriginal;
790 if constexpr (core::is_mut_v<UOriginal>)
791 return sview_mut<T>(from, to);
793 return view<T>(from, to);
796 template <
typename T>
797 GAIA_NODISCARD
decltype(
auto) sview_auto() {
798 return sview_auto<T>(0, m_header.
count);
801 GAIA_NODISCARD EntitySpan entity_view()
const {
805 GAIA_NODISCARD World& world() {
806 return *
const_cast<World*
>(m_header.
world);
809 GAIA_NODISCARD
const World& world()
const {
810 return *m_header.
world;
813 GAIA_NODISCARD EntitySpan ids_view()
const {
821 GAIA_NODISCARD uint8_t* comp_ptr_mut(uint32_t compIdx) {
822 const auto& rec = m_records.
pRecords[compIdx];
826 GAIA_NODISCARD uint8_t* comp_ptr_mut(uint32_t compIdx, uint32_t offset) {
827 const auto& rec = m_records.
pRecords[compIdx];
828 return rec.
pData + ((uintptr_t)rec.comp.size() * offset);
831 GAIA_NODISCARD
const uint8_t* comp_ptr(uint32_t compIdx)
const {
832 const auto& rec = m_records.
pRecords[compIdx];
836 GAIA_NODISCARD
const uint8_t* comp_ptr(uint32_t compIdx, uint32_t offset)
const {
837 const auto& rec = m_records.
pRecords[compIdx];
838 return rec.
pData + ((uintptr_t)rec.comp.size() * offset);
844 const auto row = m_header.
count++;
847 GAIA_ASSERT(m_header.
count != 0);
850 entity_view_mut()[row] = entity;
860 GAIA_PROF_SCOPE(Chunk::copy_entity_data);
862 auto& srcEntityContainer = recs[srcEntity];
863 auto* pSrcChunk = srcEntityContainer.pChunk;
865 auto& dstEntityContainer = recs[dstEntity];
866 auto* pDstChunk = dstEntityContainer.pChunk;
868 GAIA_ASSERT(srcEntityContainer.pArchetype == dstEntityContainer.pArchetype);
870 auto srcRecs = pSrcChunk->comp_rec_view();
874 GAIA_FOR(pSrcChunk->m_header.genEntities) {
875 const auto& rec = srcRecs[i];
876 if (!component_has_inline_data(rec.comp))
879 const auto* pSrc = (
const void*)pSrcChunk->comp_ptr_mut(i);
880 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(i);
882 pDst, pSrc, dstEntityContainer.row, srcEntityContainer.row, pDstChunk->capacity(), pSrcChunk->capacity());
893 Chunk* pSrcChunk, uint32_t srcRow,
Chunk* pDstChunk, uint32_t dstRow, uint32_t dstCount) {
894 GAIA_PROF_SCOPE(Chunk::copy_entity_data_n_same_chunk);
896 GAIA_ASSERT(pSrcChunk !=
nullptr);
897 GAIA_ASSERT(pDstChunk !=
nullptr);
898 GAIA_ASSERT(srcRow < pSrcChunk->size());
899 GAIA_ASSERT(dstRow + dstCount <= pDstChunk->size());
900 GAIA_ASSERT(pSrcChunk->ids_view().size() == pDstChunk->ids_view().size());
902 auto srcRecs = pSrcChunk->comp_rec_view();
907 const auto& rec = srcRecs[i];
908 if (!component_has_inline_data(rec.comp))
911 const auto* pSrc = (
const void*)pSrcChunk->comp_ptr(i);
912 GAIA_FOR_(dstCount, rowOffset) {
913 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(i);
914 rec.pItem->copy(pDst, pSrc, dstRow + rowOffset, srcRow, pDstChunk->
capacity(), pSrcChunk->
capacity());
926 Chunk* pSrcChunk, uint32_t srcRow,
Chunk* pDstChunk, uint32_t dstRow, uint32_t dstCount) {
927 GAIA_PROF_SCOPE(Chunk::copy_foreign_entity_data_n);
929 GAIA_ASSERT(pSrcChunk !=
nullptr);
930 GAIA_ASSERT(pDstChunk !=
nullptr);
931 GAIA_ASSERT(srcRow < pSrcChunk->size());
932 GAIA_ASSERT(dstRow + dstCount <= pDstChunk->size());
934 auto srcIds = pSrcChunk->ids_view();
935 auto dstIds = pDstChunk->ids_view();
936 auto dstRecs = pDstChunk->comp_rec_view();
941 const auto oldId = srcIds[i];
942 const auto newId = dstIds[j];
944 if (oldId == newId) {
945 const auto& rec = dstRecs[j];
946 if (component_has_inline_data(rec.comp)) {
947 auto* pSrc = (
void*)pSrcChunk->comp_ptr_mut(i);
948 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j);
949 GAIA_FOR_(dstCount, rowOffset) {
950 rec.pItem->ctor_copy(
951 pDst, pSrc, dstRow + rowOffset, srcRow, pDstChunk->
capacity(), pSrcChunk->
capacity());
960 const auto& rec = dstRecs[j];
961 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
962 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
963 rec.pItem->func_ctor(pDst, dstCount);
971 const auto& rec = dstRecs[j];
972 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
973 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
974 rec.pItem->func_ctor(pDst, dstCount);
984 GAIA_PROF_SCOPE(Chunk::move_entity_data);
986 auto& ec = recs[entity];
987 auto* pSrcChunk = ec.pChunk;
988 auto srcRecs = pSrcChunk->comp_rec_view();
992 GAIA_FOR(pSrcChunk->m_header.genEntities) {
993 const auto& rec = srcRecs[i];
994 if (!component_has_inline_data(rec.comp))
997 auto* pSrc = (
void*)pSrcChunk->comp_ptr_mut(i);
998 auto* pDst = (
void*)comp_ptr_mut(i);
999 rec.pItem->ctor_move(pDst, pSrc, row, ec.row, capacity(), pSrcChunk->capacity());
1009 GAIA_PROF_SCOPE(Chunk::copy_foreign_entity_data);
1011 GAIA_ASSERT(pSrcChunk !=
nullptr);
1012 GAIA_ASSERT(pDstChunk !=
nullptr);
1013 GAIA_ASSERT(srcRow < pSrcChunk->size());
1014 GAIA_ASSERT(dstRow < pDstChunk->size());
1016 auto srcIds = pSrcChunk->ids_view();
1017 auto dstIds = pDstChunk->ids_view();
1018 auto dstRecs = pDstChunk->comp_rec_view();
1028 const auto oldId = srcIds[i];
1029 const auto newId = dstIds[j];
1031 if (oldId == newId) {
1032 const auto& rec = dstRecs[j];
1033 if (component_has_inline_data(rec.comp)) {
1034 auto* pSrc = (
void*)pSrcChunk->comp_ptr_mut(i);
1035 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j);
1036 rec.pItem->ctor_copy(pDst, pSrc, dstRow, srcRow, pDstChunk->
capacity(), pSrcChunk->
capacity());
1045 const auto& rec = dstRecs[j];
1046 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
1047 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
1048 rec.pItem->func_ctor(pDst, 1);
1056 for (; j < pDstChunk->m_header.
genEntities; ++j) {
1057 const auto& rec = dstRecs[j];
1058 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
1059 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
1060 rec.pItem->func_ctor(pDst, 1);
1072 GAIA_PROF_SCOPE(Chunk::move_foreign_entity_data);
1074 GAIA_ASSERT(pSrcChunk !=
nullptr);
1075 GAIA_ASSERT(pDstChunk !=
nullptr);
1076 GAIA_ASSERT(srcRow < pSrcChunk->size());
1077 GAIA_ASSERT(dstRow < pDstChunk->size());
1079 auto srcIds = pSrcChunk->ids_view();
1080 auto dstIds = pDstChunk->ids_view();
1081 auto dstRecs = pDstChunk->comp_rec_view();
1091 const auto oldId = srcIds[i];
1092 const auto newId = dstIds[j];
1094 if (oldId == newId) {
1095 const auto& rec = dstRecs[j];
1096 if (component_has_inline_data(rec.comp)) {
1097 auto* pSrc = (
void*)pSrcChunk->comp_ptr_mut(i);
1098 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j);
1099 rec.pItem->ctor_move(pDst, pSrc, dstRow, srcRow, pDstChunk->
capacity(), pSrcChunk->
capacity());
1108 const auto& rec = dstRecs[j];
1109 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
1110 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
1111 rec.pItem->func_ctor(pDst, 1);
1119 for (; j < pDstChunk->m_header.
genEntities; ++j) {
1120 const auto& rec = dstRecs[j];
1121 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
1122 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
1123 rec.pItem->func_ctor(pDst, 1);
1136 GAIA_PROF_SCOPE(Chunk::remove_entity_inter);
1138 const uint16_t rowA = row;
1139 const uint16_t rowB = m_header.
count - 1;
1141 GAIA_ASSERT(rowA <= rowB);
1144 if GAIA_LIKELY (rowA < rowB) {
1145 GAIA_ASSERT(m_header.
count > 1);
1147 auto ev = entity_view_mut();
1150 const auto entityB = ev[rowB];
1151 auto& ecB = recs[entityB];
1152#if GAIA_ASSERT_ENABLED
1153 const auto entityA = ev[rowA];
1154 auto& ecA = recs[entityA];
1156 GAIA_ASSERT(ecA.pArchetype == ecB.pArchetype);
1157 GAIA_ASSERT(ecA.pChunk == ecB.pChunk);
1163 auto recView = comp_rec_view();
1165 const auto& rec = recView[i];
1166 if (!component_has_inline_data(rec.comp))
1169 auto* pSrc = (
void*)comp_ptr_mut(i);
1170 rec.pItem->move(pSrc, pSrc, rowA, rowB, capacity(), capacity());
1172 pSrc = (
void*)comp_ptr_mut(i, rowB);
1173 rec.pItem->dtor(pSrc);
1178 ecB.pEntity = &ev[rowA];
1181 auto recView = comp_rec_view();
1183 const auto& rec = recView[i];
1184 if (!component_has_inline_data(rec.comp))
1187 auto* pSrc = (
void*)comp_ptr_mut(i, rowA);
1188 rec.pItem->dtor(pSrc);
1200 if GAIA_UNLIKELY (m_header.
count == 0)
1203 GAIA_PROF_SCOPE(Chunk::remove_entity);
1207 remove_entity_inter(row, recs);
1212 remove_last_entity();
1216 const uint16_t pivot = size_disabled() - 1;
1217 swap_chunk_entities(row, pivot, recs);
1219 remove_entity_inter(pivot, recs);
1222 remove_last_entity();
1235 if GAIA_UNLIKELY (m_header.
count <= 1 || rowA == rowB)
1238 GAIA_PROF_SCOPE(Chunk::swap_chunk_entities);
1241 auto ev = entity_view_mut();
1242 const auto entityA = ev[rowA];
1243 const auto entityB = ev[rowB];
1245 auto& ecA = recs[entityA];
1246 auto& ecB = recs[entityB];
1247 GAIA_ASSERT(ecA.pArchetype == ecB.pArchetype);
1248 GAIA_ASSERT(ecA.pChunk == ecB.pChunk);
1254 auto recView = comp_rec_view();
1256 const auto& rec = recView[i];
1257 if (!component_has_inline_data(rec.comp))
1260 GAIA_ASSERT(rec.pData == comp_ptr_mut(i));
1261 rec.pItem->swap(rec.pData, rec.pData, rowA, rowB, capacity(), capacity());
1267 ecA.pEntity = &ev[rowB];
1268 ecB.pEntity = &ev[rowA];
1279 if GAIA_UNLIKELY (entityA == entityB)
1282 GAIA_PROF_SCOPE(Chunk::swap_chunk_entities);
1284 auto& ecA = fetch_mut(world, entityA);
1285 auto& ecB = fetch_mut(world, entityB);
1288 GAIA_ASSERT(ecA.pArchetype == ecB.pArchetype);
1289 GAIA_ASSERT(ecA.pArchetype == ecB.pArchetype);
1291 auto* pChunkA = ecA.pChunk;
1292 auto* pChunkB = ecB.pChunk;
1295 pChunkA->entity_view_mut()[ecA.row] = entityB;
1296 pChunkB->entity_view_mut()[ecB.row] = entityA;
1299 auto recViewA = pChunkA->comp_rec_view();
1300 GAIA_FOR(pChunkA->m_header.genEntities) {
1301 const auto& recA = recViewA[i];
1302 if (!component_has_inline_data(recA.comp))
1305 auto* pDataA = pChunkA->comp_rec_view()[i].pData;
1306 auto* pDataB = pChunkB->comp_rec_view()[i].pData;
1313 pChunkA->capacity(), pChunkA->capacity()
1318 core::swap(ecA.row, ecB.row);
1319 core::swap(ecA.pChunk, ecB.pChunk);
1327 GAIA_ASSERT(row < m_header.
count &&
"Entity chunk row out of bounds!");
1331 if (!m_header.has_disabled_entities())
1337 const auto entity = entity_view()[row];
1339 recs[entity].data.dis = 0;
1343 if (!m_header.has_enabled_entities())
1349 const auto entity = entity_view()[row];
1351 recs[entity].data.dis = 1;
1360 GAIA_ASSERT(m_header.
count > 0);
1369 return m_data[offset];
1375 const uint8_t&
data(uint32_t offset)
const {
1376 return m_data[offset];
1384 if (item.
func_ctor ==
nullptr || !component_has_inline_data(item.
comp))
1387 GAIA_PROF_SCOPE(Chunk::call_ctor);
1389 const auto compIdx = comp_idx(item.
entity);
1390 auto* pSrc = (
void*)comp_ptr_mut(compIdx, entIdx);
1394 void call_gen_ctors(uint32_t entIdx, uint32_t entCnt) {
1398 GAIA_PROF_SCOPE(Chunk::call_gen_ctors);
1400 auto recs = comp_rec_view();
1402 const auto& rec = recs[i];
1403 if (!component_has_inline_data(rec.comp))
1406 const auto* pItem = rec.pItem;
1407 if (pItem ==
nullptr || pItem->func_ctor ==
nullptr)
1410 auto* pSrc = (
void*)comp_ptr_mut(i, entIdx);
1411 pItem->func_ctor(pSrc, entCnt);
1415 void call_all_dtors() {
1419 GAIA_PROF_SCOPE(Chunk::call_all_dtors);
1421 auto ids = ids_view();
1422 auto recs = comp_rec_view();
1423 const auto recs_cnt = recs.size();
1424 GAIA_FOR(recs_cnt) {
1425 const auto& rec = recs[i];
1426 if (!component_has_inline_data(rec.comp))
1429 const auto* pItem = rec.pItem;
1430 if (pItem ==
nullptr || pItem->func_dtor ==
nullptr)
1433 auto* pSrc = (
void*)comp_ptr_mut(i, 0);
1434 const auto e = ids[i];
1435 const auto cnt = (e.kind() == EntityKind::EK_Gen) ? m_header.
count : (uint16_t)1;
1436 pItem->func_dtor(pSrc, cnt);
1448 auto ids = ids_view();
1449 return core::has(ids, entity);
1455 template <
typename T>
1456 GAIA_NODISCARD
bool has()
const {
1458 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
1459 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
1462 const auto* pComp = m_header.
cc->
find<T>();
1463 return pComp !=
nullptr && has(pComp->entity);
1475 template <
typename T>
1476 decltype(
auto)
set(uint16_t row) {
1480 actual_type_t<T>::Kind == EntityKind::EK_Gen || row == 0,
1481 "Set providing a row can only be used with generic components");
1486 GAIA_ASSERT(row < m_header.
capacity);
1487 world_notify_on_set(*
const_cast<World*
>(m_header.
world), comp_entity<T>(), *
this, row, (uint16_t)(row + 1));
1488 return view_mut<T>()[row];
1495 template <
typename T>
1496 decltype(
auto)
set_idx(uint16_t row, uint32_t compIdx) {
1500 actual_type_t<T>::Kind == EntityKind::EK_Gen || row == 0,
1501 "Set providing a row can only be used with generic components");
1506 world_notify_on_set(
1508 return comp_mut_idx<T, true>(row, compIdx);
1514 template <
typename T>
1518 entity_kind_v<T> != EntityKind::EK_Gen,
1519 "Set not providing a row can only be used with non-generic components");
1525 return comp_mut_idx<T, true>(0, compIdx);
1532 template <
typename T>
1535 type.kind() == EntityKind::EK_Gen || row == 0,
1536 "Set providing a row can only be used with generic components");
1537 GAIA_ASSERT(type.kind() == entity_kind_v<T>);
1538 const uint32_t compIdx = comp_idx(type);
1543 GAIA_ASSERT(row < m_header.
capacity);
1544 world_notify_on_set(*
const_cast<World*
>(m_header.
world), type, *
this, row, (uint16_t)(row + 1));
1545 return comp_mut_idx<T, true>(row, compIdx);
1553 template <
typename T>
1554 decltype(
auto)
sset(uint16_t row) {
1556 actual_type_t<T>::Kind == EntityKind::EK_Gen || row == 0,
1557 "Set providing a row can only be used with generic components");
1559 GAIA_ASSERT(row < m_header.
capacity);
1560 return sview_mut<T>()[row];
1565 template <
typename T>
1566 decltype(
auto)
sset_idx(uint16_t row, uint32_t compIdx) {
1570 actual_type_t<T>::Kind == EntityKind::EK_Gen || row == 0,
1571 "Set providing a row can only be used with generic components");
1573 return comp_mut_idx<T, false>(row, compIdx);
1578 template <
typename T>
1582 entity_kind_v<T> != EntityKind::EK_Gen,
1583 "SSet not providing a row can only be used with non-generic components");
1585 return comp_mut_idx<T, false>(0, compIdx);
1594 template <
typename T>
1596 static_assert(core::is_raw_v<T>);
1599 type.kind() == EntityKind::EK_Gen || row == 0,
1600 "Set providing a row can only be used with generic components");
1601 GAIA_ASSERT(type.kind() == entity_kind_v<T>);
1602 const uint32_t compIdx = comp_idx(type);
1604 GAIA_ASSERT(row < m_header.
capacity);
1605 return comp_mut_idx<T, false>(row, compIdx);
1618 template <
typename T>
1619 GAIA_NODISCARD
decltype(
auto)
get(uint16_t row)
const {
1621 entity_kind_v<T> == EntityKind::EK_Gen,
"Get providing a row can only be used with generic components");
1623 return comp_inter<T>(row);
1630 template <
typename T>
1631 GAIA_NODISCARD
decltype(
auto)
get_idx(uint16_t row, uint32_t compIdx)
const {
1633 entity_kind_v<T> == EntityKind::EK_Gen,
"Get providing a row can only be used with generic components");
1635 return comp_inter_idx<T>(row, compIdx);
1643 template <
typename T>
1644 GAIA_NODISCARD
decltype(
auto)
get(uint16_t row,
Entity type)
const {
1646 type.kind() == EntityKind::EK_Gen || row == 0,
1647 "Get providing a row can only be used with generic components");
1648 GAIA_ASSERT(type.kind() == entity_kind_v<T>);
1649 GAIA_ASSERT(row < m_header.
count);
1650 const uint32_t compIdx = comp_idx(type);
1651 return comp_inter_idx<T>(row, compIdx);
1658 template <
typename T>
1659 GAIA_NODISCARD
decltype(
auto)
get()
const {
1661 entity_kind_v<T> != EntityKind::EK_Gen,
1662 "Get not providing a row can only be used with non-generic components");
1664 return comp_inter<T>(0);
1670 template <
typename T>
1671 GAIA_NODISCARD
decltype(
auto)
get_idx(uint32_t compIdx)
const {
1673 entity_kind_v<T> != EntityKind::EK_Gen,
1674 "Get not providing a row can only be used with non-generic components");
1676 return comp_inter_idx<T>(0, compIdx);
1679 template <
typename T>
1680 GAIA_NODISCARD
Entity comp_entity()
const {
1682 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
1683 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
1686 return m_header.
cc->
get<T>().entity;
1695 return ecs::comp_idx<ChunkHeader::MAX_COMPONENTS>(m_records.
pCompEntities, entity);
1704 return ecs::comp_idx({m_records.
pCompEntities + offset, m_header.
count - offset}, entity);
1711 m_header.
index = value;
1715 GAIA_NODISCARD uint32_t
idx()
const {
1716 return m_header.
index;
1721 return m_header.has_enabled_entities();
1726 return m_header.has_disabled_entities();
1761 return m_header.
dead == 1;
1766 GAIA_ASSERT(!dead());
1767 GAIA_ASSERT(!queued_for_deletion());
1773 GAIA_ASSERT(!dead());
1775 clear_delete_queue_index();
1781 GAIA_ASSERT(dying());
1794 constexpr float Threshold = 0.75f;
1795 return ((
float)m_header.
count / (
float)m_header.
capacity) < Threshold;
1799 GAIA_NODISCARD uint16_t
size()
const {
1800 return m_header.
count;
1805 return m_header.
count == 0;
1831 GAIA_NODISCARD
bool changed(uint32_t requiredVersion)
const {
1832 const auto* versions = m_records.
pVersions;
1833 const auto changeVersion = versions[0];
1834 return ::gaia::ecs::version_changed(changeVersion, requiredVersion);
1838 GAIA_NODISCARD
bool changed(uint32_t requiredVersion, uint32_t compIdx)
const {
1839 const auto* versions = m_records.
pVersions;
1841 const auto changeVersion = versions[compIdx + 1];
1842 return ::gaia::ecs::version_changed(changeVersion, requiredVersion);
1853 auto versions = comp_version_view_mut();
1859 world_invalidate_sorted_queries_for_entity(
1867 world_invalidate_sorted_queries(*
const_cast<World*
>(m_header.
world));
1891 " Chunk #%04u, entities:%u/%u, lifespanCountdown:%u", m_header.
index, m_header.
count, m_header.
capacity,
Array with variable size of elements of type.
Definition darray_impl.h:25
Definition span_impl.h:99
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:788
GAIA_NODISCARD uint16_t capacity() const
Returns the number of entities in the chunk.
Definition chunk.h:1819
GAIA_NODISCARD decltype(auto) get_idx(uint32_t compIdx) const
Returns the value stored in the unique component T using a pre-resolved component column.
Definition chunk.h:1671
static void free(Chunk *pChunk)
Releases all memory allocated by pChunk.
Definition chunk.h:496
const uint8_t & data(uint32_t offset) const
Returns an immutable pointer to chunk data.
Definition chunk.h:1375
decltype(auto) set_idx(uint16_t row, uint32_t compIdx)
Sets the value of a generic component using a pre-resolved component column.
Definition chunk.h:1496
decltype(auto) sset(uint16_t row, Entity type)
Sets the value of a generic entity type at the position row in the chunk.
Definition chunk.h:1595
void remove_entity(uint16_t row, EntityContainers &recs)
Tries to remove the entity at row row. Removal is done via swapping with last entity in chunk....
Definition chunk.h:1199
static void copy_entity_data_n_same_chunk(Chunk *pSrcChunk, uint32_t srcRow, Chunk *pDstChunk, uint32_t dstRow, uint32_t dstCount)
Copies all data associated with srcRow into dstCount consecutive rows in the same-archetype chunk.
Definition chunk.h:892
GAIA_FORCEINLINE void update_world_version()
Update the version of all components.
Definition chunk.h:1871
GAIA_NODISCARD uint32_t comp_idx(Entity entity, uint32_t offset) const
Returns the internal index of a component based on the provided entity.
Definition chunk.h:1703
bool progress_death()
Updates internal lifespan.
Definition chunk.h:1780
decltype(auto) set(uint16_t row)
Sets the value of the unique component T on row in the chunk.
Definition chunk.h:1476
GAIA_NODISCARD GAIA_FORCEINLINE auto comp_ptr_mut_gen(uint32_t compIdx, uint32_t row)
Returns a read-write span of the component data. Also updates the world version for the component.
Definition chunk.h:339
void die()
Marks the chunk as dead (ready to delete)
Definition chunk.h:1755
GAIA_NODISCARD uint32_t idx() const
Returns the index of this chunk in its archetype's storage.
Definition chunk.h:1715
uint8_t & data(uint32_t offset)
Returns a mutable pointer to chunk data.
Definition chunk.h:1368
decltype(auto) sset_idx(uint16_t row, uint32_t compIdx)
Sets the value of a generic component using a pre-resolved component column.
Definition chunk.h:1566
void update_versions()
Updates the version numbers for this chunk.
Definition chunk.h:602
GAIA_FORCEINLINE void update_world_version(uint32_t compIdx)
Update the version of a component at the index.
Definition chunk.h:1852
GAIA_NODISCARD bool has_disabled_entities() const
Checks is this chunk has any disabled entities.
Definition chunk.h:1725
GAIA_NODISCARD bool full() const
Checks is the full capacity of the has has been reached.
Definition chunk.h:1787
GAIA_NODISCARD bool is_semi() const
Checks is the chunk is semi-full.
Definition chunk.h:1792
GAIA_NODISCARD decltype(auto) view(uint16_t from, uint16_t to) const
Returns a read-only entity or component view.
Definition chunk.h:615
GAIA_NODISCARD uint16_t add_entity(Entity entity)
Make.
Definition chunk.h:843
GAIA_NODISCARD uint16_t size_enabled() const
Return the number of entities in the chunk which are enabled.
Definition chunk.h:1809
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:766
void start_dying()
Starts the process of dying (not yet ready to delete, can be revived)
Definition chunk.h:1765
GAIA_NODISCARD bool has() const
Checks if component T is present in the chunk.
Definition chunk.h:1456
GAIA_NODISCARD uint16_t size() const
Returns the total number of entities in the chunk (both enabled and disabled)
Definition chunk.h:1799
GAIA_FORCEINLINE void update_entity_order_version()
Updates the entity-order version after rows were added, removed, or reordered.
Definition chunk.h:1864
GAIA_NODISCARD decltype(auto) get(uint16_t row) const
Returns the value stored in the generic component T on row in the chunk.
Definition chunk.h:1619
GAIA_NODISCARD uint32_t delete_queue_index() const
Returns the index inside World's deferred chunk-delete queue.
Definition chunk.h:1740
static uintptr_t chunk_data_area_offset()
Returns the relative offset of m_data in Chunk.
Definition chunk.h:454
void move_entity_data(Entity entity, uint16_t row, EntityContainers &recs)
Moves all data associated with entity into the chunk so that it is stored at the row row.
Definition chunk.h:983
decltype(auto) set_idx(uint32_t compIdx)
Sets the value of a unique component using a pre-resolved component column.
Definition chunk.h:1515
void set_idx(uint32_t value)
Sets the index of this chunk in its archetype's storage.
Definition chunk.h:1710
void clear_delete_queue_index()
Clears the deferred chunk-delete queue index.
Definition chunk.h:1750
GAIA_NODISCARD bool entity_order_changed(uint32_t requiredVersion) const
Returns true if entity order changed since requiredVersion. This is narrower than changed(requiredVer...
Definition chunk.h:1847
GAIA_NODISCARD decltype(auto) get() const
Returns the value stored in the unique component T.
Definition chunk.h:1659
GAIA_NODISCARD bool changed(uint32_t requiredVersion, uint32_t compIdx) const
Returns true if the provided version is newer than the one stored internally.
Definition chunk.h:1838
void delete_queue_index(uint32_t idx)
Stores the index inside World's deferred chunk-delete queue.
Definition chunk.h:1745
decltype(auto) sset_idx(uint32_t compIdx)
Sets the value of a unique component using a pre-resolved component column.
Definition chunk.h:1579
static void copy_entity_data(Entity srcEntity, Entity dstEntity, EntityContainers &recs)
Copies all data associated with srcEntity into dstEntity.
Definition chunk.h:859
bool enabled(uint16_t row) const
Checks if the entity is enabled.
Definition chunk.h:1359
GAIA_NODISCARD bool dying() const
Checks is this chunk is dying.
Definition chunk.h:1730
GAIA_NODISCARD bool changed(uint32_t requiredVersion) const
Returns true if the provided version is newer than the one stored internally. Use when checking if th...
Definition chunk.h:1831
void remove_last_entity()
Remove the last entity from a chunk. If as a result the chunk becomes empty it is scheduled for delet...
Definition chunk.h:589
GAIA_NODISCARD uint16_t size_disabled() const
Return the number of entities in the chunk which are enabled.
Definition chunk.h:1814
void swap_chunk_entities(uint16_t rowA, uint16_t rowB, EntityContainers &recs)
Tries to swap the entity at row rowA with the one at the row rowB. When swapping, all data associated...
Definition chunk.h:1233
static void move_foreign_entity_data(Chunk *pSrcChunk, uint32_t srcRow, Chunk *pDstChunk, uint32_t dstRow)
Moves all data associated with entity into the chunk so that it is stored at the row row.
Definition chunk.h:1071
void enable_entity(uint16_t row, bool enableEntity, EntityContainers &recs)
Enables or disables the entity on a given row in the chunk.
Definition chunk.h:1326
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
static void swap_chunk_entities(World &world, Entity entityA, Entity entityB)
Tries to swap entityA with entityB. When swapping, all data associated with the two entities is swapp...
Definition chunk.h:1277
GAIA_FORCEINLINE void update_world_version_init()
Update the version of all components on chunk init.
Definition chunk.h:1881
GAIA_NODISCARD bool dead() const
Checks is this chunk is dead (ready to delete)
Definition chunk.h:1760
decltype(auto) set(uint16_t row, Entity type)
Sets the value of a generic entity type at the position row in the chunk.
Definition chunk.h:1533
GAIA_NODISCARD bool has_enabled_entities() const
Checks is this chunk has any enabled entities.
Definition chunk.h:1720
void revive()
Makes a dying chunk alive again.
Definition chunk.h:1772
GAIA_NODISCARD bool has(Entity entity) const
Checks if a component/entity entity is present in the chunk.
Definition chunk.h:1447
GAIA_NODISCARD bool queued_for_deletion() const
Returns true when the chunk is currently queued for deferred deletion.
Definition chunk.h:1735
GAIA_NODISCARD bool empty() const
Checks is there are any entities in the chunk.
Definition chunk.h:1804
static void copy_foreign_entity_data(Chunk *pSrcChunk, uint32_t srcRow, Chunk *pDstChunk, uint32_t dstRow)
Copies all data associated with entity into the chunk so that it is stored at the row row.
Definition chunk.h:1008
void remove_entity_inter(uint16_t row, EntityContainers &recs)
Tries to remove the entity at row. Removal is done via swapping with last entity in chunk....
Definition chunk.h:1135
GAIA_NODISCARD decltype(auto) view_mut(uint16_t from, uint16_t to)
Returns a mutable entity or component view.
Definition chunk.h:643
void finish_write(uint32_t compIdx, uint16_t from, uint16_t to)
Finishes a raw write over a chunk range by updating versions, running set hooks once,...
Definition chunk.h:356
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:675
GAIA_NODISCARD decltype(auto) get_idx(uint16_t row, uint32_t compIdx) const
Returns the value stored in the generic component T using a pre-resolved component column.
Definition chunk.h:1631
GAIA_NODISCARD uint8_t size_generic() const
Returns the total number of generic entities/components in the chunk.
Definition chunk.h:1824
GAIA_NODISCARD decltype(auto) get(uint16_t row, Entity type) const
Returns the value stored in the generic component type on row in the chunk.
Definition chunk.h:1644
static Chunk * create(const World &wld, const ComponentCache &cc, uint32_t chunkIndex, uint16_t capacity, uint8_t cntEntities, uint8_t genEntities, uint16_t dataBytes, uint32_t &worldVersion, const ChunkDataOffsets &offsets, const Entity *ids, const ComponentCacheItem *const *pItems, const ChunkDataOffset *compOffs)
Allocates memory for a new chunk.
Definition chunk.h:465
decltype(auto) sset(uint16_t row)
Sets the value of the unique component T on row in the chunk.
Definition chunk.h:1554
static void copy_foreign_entity_data_n(Chunk *pSrcChunk, uint32_t srcRow, Chunk *pDstChunk, uint32_t dstRow, uint32_t dstCount)
Copies all data associated with srcRow into dstCount consecutive rows in a foreign chunk.
Definition chunk.h:925
GAIA_FORCEINLINE void modify()
Marks the component.
Definition chunk.h:709
Cache for compile-time defined components.
Definition component_cache.h:24
GAIA_NODISCARD const ComponentCacheItem & get(Entity entity) const noexcept
Returns the component cache item.
Definition component_cache.h:408
GAIA_NODISCARD const ComponentCacheItem * find(Entity entity) const noexcept
Searches for the component cache item.
Definition component_cache.h:389
Wrapper for two Entities forming a relationship pair.
Definition id.h:529
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
Strict weak ordering functor using operator<.
Definition utility.h:1368
Definition chunk_header.h:19
ChunkDataVersionOffset firstByte_Versions
Byte at which the first version number is located.
Definition chunk_header.h:21
ChunkDataOffset firstByte_EntityData
Byte at which the first entity is located.
Definition chunk_header.h:27
ChunkDataOffset firstByte_Records
Byte at which the first component id is located.
Definition chunk_header.h:25
ChunkDataOffset firstByte_CompEntities
Byte at which the first entity id is located.
Definition chunk_header.h:23
Definition chunk_header.h:39
ComponentRecord * pRecords
Pointer to the array of component records.
Definition chunk_header.h:45
Entity * pCompEntities
Pointer to where (component) entities are stored.
Definition chunk_header.h:43
Entity * pEntities
Pointer to the array of entities.
Definition chunk_header.h:47
ComponentVersion * pVersions
Pointer to where component versions are stored.
Definition chunk_header.h:41
Definition component_cache_item.h:26
Entity entity
Component entity.
Definition component_cache_item.h:84
Component comp
Unique component identifier.
Definition component_cache_item.h:86
FuncCtor * func_ctor
Function to call when the component needs to be constructed.
Definition component_cache_item.h:97
Definition chunk_header.h:30
uint8_t * pData
Pointer to where the first instance of the component is stored.
Definition chunk_header.h:34
Definition entity_container.h:142
Definition data_layout_policy.h:90
Definition data_layout_policy.h:92
Runtime serializer type-erased handle. Traversal logic is shared with compile-time serialization,...
Definition ser_rt.h:79