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")
84 const ChunkDataOffset* compOffs) {
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) {
107 dst[j].comp = comps[j];
108 dst[j].pData = &data(compOffs[j]);
109 dst[j].pItem = m_header.
cc->
find(comps[j].
id());
117 auto recs = comp_rec_view();
118 const auto recs_cnt = recs.size();
120 const auto& rec = recs[i];
121 if (!component_has_inline_data(rec.comp))
125 if (e.kind() == EntityKind::EK_Gen) {
133 call_ctor(0, *rec.pItem);
139 update_world_version_init();
142 GAIA_CLANG_WARNING_POP()
167 template <
typename T>
168 GAIA_NODISCARD GAIA_FORCEINLINE
auto view_inter_idx(uint32_t compIdx, uint32_t from, uint32_t to)
const
171 if constexpr (std::is_same_v<core::raw_t<T>,
Entity>) {
172 GAIA_ASSERT(to <= m_header.
count);
173 return {(
const uint8_t*)&m_records.
pEntities[from], to - from};
175 using TT =
typename T::type;
176 using U =
typename component_type_t<TT>::Type;
177 static_assert(!std::is_empty_v<U>,
"Attempting to get value of an empty component");
179 constexpr auto kind = entity_kind_v<TT>;
181 if constexpr (mem::is_soa_layout_v<U>) {
182 GAIA_ASSERT(from == 0);
183 GAIA_ASSERT(to == capacity());
184 return {comp_ptr(compIdx), to};
185 }
else if constexpr (kind == EntityKind::EK_Gen) {
186 GAIA_ASSERT(to <= m_header.
count);
187 return {comp_ptr(compIdx, from), to - from};
189 GAIA_ASSERT(to <= m_header.
count);
191 return {comp_ptr(compIdx), 1};
194 using U =
typename component_type_t<T>::Type;
195 static_assert(!std::is_empty_v<U>,
"Attempting to get value of an empty component");
197 constexpr auto kind = entity_kind_v<T>;
199 if constexpr (mem::is_soa_layout_v<U>) {
200 GAIA_ASSERT(from == 0);
201 GAIA_ASSERT(to == capacity());
202 return {comp_ptr(compIdx), to};
203 }
else if constexpr (kind == EntityKind::EK_Gen) {
204 GAIA_ASSERT(to <= m_header.
count);
205 return {comp_ptr(compIdx, from), to - from};
207 GAIA_ASSERT(to <= m_header.
count);
209 return {comp_ptr(compIdx), 1};
220 template <
typename T>
221 GAIA_NODISCARD GAIA_FORCEINLINE
auto view_inter(uint32_t from, uint32_t to)
const
223 if constexpr (std::is_same_v<core::raw_t<T>,
Entity>)
224 return view_inter_idx<T>(BadIndex, from, to);
226 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
227 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
228 return view_inter_idx<T>(comp_idx((
Entity)
Pair(rel, tgt)), from, to);
230 constexpr auto kind = entity_kind_v<T>;
231 const auto comp = m_header.
cc->
get<T>().entity;
232 GAIA_ASSERT(comp.kind() == kind);
233 return view_inter_idx<T>(comp_idx(comp), from, to);
245 template <
typename T,
bool WorldVersionUpdateWanted>
246 GAIA_NODISCARD GAIA_FORCEINLINE
auto view_mut_inter_idx(uint32_t compIdx, uint32_t from, uint32_t to)
248 static_assert(!std::is_same_v<core::raw_t<T>,
Entity>,
"view_mut can't be used to modify Entity");
251 using TT =
typename T::type;
252 using U =
typename component_type_t<TT>::Type;
253 static_assert(!std::is_empty_v<U>,
"view_mut can't be used to modify tag components");
255 constexpr auto kind = entity_kind_v<TT>;
258 if constexpr (WorldVersionUpdateWanted) {
259 update_world_version(compIdx);
261#if GAIA_ENABLE_SET_HOOKS
262 const auto& rec = m_records.
pRecords[compIdx];
263 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
264 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
268 if constexpr (mem::is_soa_layout_v<U>) {
269 GAIA_ASSERT(from == 0);
270 GAIA_ASSERT(to == capacity());
271 return {comp_ptr_mut(compIdx), to};
272 }
else if constexpr (kind == EntityKind::EK_Gen) {
273 GAIA_ASSERT(to <= m_header.
count);
274 return {comp_ptr_mut(compIdx, from), to - from};
276 GAIA_ASSERT(to <= m_header.
count);
278 return {comp_ptr_mut(compIdx), 1};
281 using U =
typename component_type_t<T>::Type;
282 static_assert(!std::is_empty_v<U>,
"view_mut can't be used to modify tag components");
283 constexpr auto kind = entity_kind_v<T>;
286 if constexpr (WorldVersionUpdateWanted) {
287 update_world_version(compIdx);
289#if GAIA_ENABLE_SET_HOOKS
290 const auto& rec = m_records.
pRecords[compIdx];
291 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
292 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
296 if constexpr (mem::is_soa_layout_v<U>) {
297 GAIA_ASSERT(from == 0);
298 GAIA_ASSERT(to == capacity());
299 return {comp_ptr_mut(compIdx), to};
300 }
else if constexpr (kind == EntityKind::EK_Gen) {
301 GAIA_ASSERT(to <= m_header.
count);
302 return {comp_ptr_mut(compIdx, from), to - from};
304 GAIA_ASSERT(to <= m_header.
count);
306 return {comp_ptr_mut(compIdx), 1};
311 template <
typename T,
bool WorldVersionUpdateWanted>
312 GAIA_NODISCARD GAIA_FORCEINLINE
auto view_mut_inter(uint32_t from, uint32_t to)
315 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
316 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
317 return view_mut_inter_idx<T, WorldVersionUpdateWanted>(comp_idx((
Entity)
Pair(rel, tgt)), from, to);
319 constexpr auto kind = entity_kind_v<T>;
320 const auto comp = m_header.
cc->
get<T>().entity;
321 GAIA_ASSERT(comp.kind() == kind);
322 return view_mut_inter_idx<T, WorldVersionUpdateWanted>(comp_idx(comp), from, to);
333 template <
bool WorldVersionUpdateWanted>
336 if constexpr (WorldVersionUpdateWanted) {
337 update_world_version(compIdx);
339#if GAIA_ENABLE_SET_HOOKS
340 const auto& rec = m_records.
pRecords[compIdx];
341 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
342 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
346 return comp_ptr_mut(compIdx, row);
356 update_world_version(compIdx);
358#if GAIA_ENABLE_SET_HOOKS
359 const auto& rec = m_records.
pRecords[compIdx];
360 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
361 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
374 template <
typename T>
375 GAIA_NODISCARD
decltype(
auto) comp_inter(uint16_t row)
const {
376 using U =
typename actual_type_t<T>::Type;
377 using RetValueType =
decltype(view<T>()[0]);
379 GAIA_ASSERT(row < m_header.
count);
380 if constexpr (mem::is_soa_layout_v<U>)
381 return view<T>(0, capacity())[row];
382 else if constexpr (
sizeof(RetValueType) <= 8)
383 return view<T>()[row];
385 return (
const U&)view<T>()[row];
388 template <
typename T>
389 GAIA_NODISCARD
decltype(
auto) comp_inter_idx(uint16_t row, uint32_t compIdx)
const {
390 using U =
typename actual_type_t<T>::Type;
391 using RetValueType =
decltype(view_raw<T>((
const void*)
nullptr, 1)[0]);
393 GAIA_ASSERT(row < m_header.
count);
394 if constexpr (mem::is_soa_layout_v<U>)
395 return view_raw<T>(comp_ptr(compIdx), capacity())[row];
396 else if constexpr (entity_kind_v<T> == EntityKind::EK_Gen) {
397 if constexpr (
sizeof(RetValueType) <= 8)
398 return view_raw<T>(comp_ptr(compIdx, row), 1)[0];
400 return (
const U&)view_raw<T>(comp_ptr(compIdx, row), 1)[0];
402 if constexpr (
sizeof(RetValueType) <= 8)
403 return view_raw<T>(comp_ptr(compIdx), 1)[0];
405 return (
const U&)view_raw<T>(comp_ptr(compIdx), 1)[0];
409 template <
typename T,
bool WorldVersionUpdateWanted>
410 GAIA_NODISCARD
decltype(
auto) comp_mut_idx(uint16_t row, uint32_t compIdx) {
411 using U =
typename actual_type_t<T>::Type;
413 GAIA_ASSERT(row < m_header.
capacity);
414 if constexpr (mem::is_soa_layout_v<U>)
415 return view_mut_raw<T>(comp_ptr_mut_gen<WorldVersionUpdateWanted>(compIdx, 0), capacity())[row];
416 else if constexpr (entity_kind_v<T> == EntityKind::EK_Gen)
417 return view_mut_raw<T>(comp_ptr_mut_gen<WorldVersionUpdateWanted>(compIdx, row), 1)[0];
419 return view_mut_raw<T>(comp_ptr_mut_gen<WorldVersionUpdateWanted>(compIdx, 0), 1)[0];
423 Chunk(
const Chunk& chunk) =
delete;
424 Chunk(Chunk&& chunk) =
delete;
425 Chunk& operator=(
const Chunk& chunk) =
delete;
426 Chunk& operator=(Chunk&& chunk) =
delete;
429 static constexpr uint16_t chunk_header_size() {
430 const auto dataAreaOffset =
432 MemoryBlockUsableOffset +
434 sizeof(ChunkHeader) +
sizeof(ChunkRecords);
435 static_assert(dataAreaOffset < UINT16_MAX);
436 return dataAreaOffset;
439 static constexpr uint16_t chunk_total_bytes(uint16_t dataSize) {
440 return chunk_header_size() + dataSize;
443 static constexpr uint16_t chunk_data_bytes(uint16_t totalSize) {
444 return totalSize - chunk_header_size();
452 const auto chunk_offset = (uintptr_t)&chunk;
453 const auto data_offset = (uintptr_t)&chunk.m_data[0];
454 return data_offset - chunk_offset;
461 uint32_t chunkIndex, uint16_t capacity, uint8_t cntEntities, uint8_t genEntities,
462 uint16_t dataBytes, uint32_t& worldVersion,
470 const ChunkDataOffset* compOffs) {
471 const auto totalBytes = chunk_total_bytes(dataBytes);
472#if GAIA_ECS_CHUNK_ALLOCATOR
473 auto* pChunk = (
Chunk*)ChunkAllocator::get().alloc(totalBytes);
474 (void)
new (pChunk)
Chunk(wld, cc, chunkIndex, capacity, genEntities, worldVersion);
476 GAIA_ASSERT(totalBytes <= MaxMemoryBlockSize);
477 const auto sizeType = mem_block_size_type(totalBytes);
478 const auto allocSize = mem_block_size(sizeType);
479 auto* pChunkMem = mem::AllocHelper::alloc<uint8_t>(allocSize);
480 std::memset(pChunkMem, 0, allocSize);
481 auto* pChunk =
new (pChunkMem)
Chunk(wld, cc, chunkIndex, capacity, genEntities, worldVersion);
484 pChunk->init((uint32_t)cntEntities, ids, comps, offsets, compOffs);
491 GAIA_ASSERT(pChunk !=
nullptr);
492 GAIA_ASSERT(!pChunk->
dead());
498 pChunk->call_all_dtors();
501#if GAIA_ECS_CHUNK_ALLOCATOR
502 ChunkAllocator::get().free(pChunk);
504 mem::AllocHelper::free((uint8_t*)pChunk);
509 s.save(m_header.
count);
510 if (m_header.
count == 0)
515 const uint16_t dead = m_header.
dead;
518 s.save(lifespanCountdown);
520 const auto cnt = (uint32_t)m_header.
count;
521 const auto cap = (uint32_t)m_header.
capacity;
526 GAIA_FOR(cnt) s.save(pData[i]);
531 for (
const auto& rec: comp_rec_view()) {
533 if (!component_has_inline_data(rec.comp))
536 rec.pItem->save(s, rec.pData, 0, cnt, cap);
541 void load(ser::serializer& s) {
542 uint16_t prevCount = m_header.
count;
543 s.load(m_header.
count);
544 if (m_header.
count == 0)
550 uint16_t lifespanCountdown = 0;
552 s.load(lifespanCountdown);
553 m_header.
dead = dead != 0;
556 const auto cnt = (uint32_t)m_header.
count;
557 const auto cap = (uint32_t)m_header.
capacity;
564 entity_view_mut()[i] = e;
569 call_gen_ctors(prevCount, cnt);
571 for (
const auto& rec: comp_rec_view()) {
573 if (!component_has_inline_data(rec.comp))
576 rec.pItem->load(s, rec.pData, 0, cnt, cap);
585 GAIA_ASSERT(!empty());
587#if GAIA_ASSERT_ENABLED
589 entity_view_mut()[m_header.
count - 1] = EntityBad;
598 update_world_version();
599 update_entity_order_version();
608 template <
typename T>
609 GAIA_NODISCARD
decltype(
auto)
view(uint16_t from, uint16_t to)
const {
610 using U =
typename actual_type_t<T>::Type;
613 if constexpr (mem::is_soa_layout_v<U>)
619 template <
typename T>
620 GAIA_NODISCARD
decltype(
auto) view()
const {
621 return view<T>(0, m_header.
count);
624 template <
typename T>
625 GAIA_NODISCARD
decltype(
auto) view_raw(
const void* ptr, uint32_t size)
const {
626 using U =
typename actual_type_t<T>::Type;
636 template <
typename T>
637 GAIA_NODISCARD
decltype(
auto)
view_mut(uint16_t from, uint16_t to) {
638 using U =
typename actual_type_t<T>::Type;
639 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via view_mut is forbidden");
642 if constexpr (mem::is_soa_layout_v<U>)
648 template <
typename T>
649 GAIA_NODISCARD
decltype(
auto) view_mut() {
650 return view_mut<T>(0, m_header.
count);
653 template <
typename T>
654 GAIA_NODISCARD
decltype(
auto) view_mut_raw(
void* ptr, uint32_t size)
const {
655 using U =
typename actual_type_t<T>::Type;
656 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via view_mut is forbidden");
668 template <
typename T>
669 GAIA_NODISCARD
decltype(
auto)
sview_mut(uint16_t from, uint16_t to) {
670 using U =
typename actual_type_t<T>::Type;
671 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via sview_mut is forbidden");
674 if constexpr (mem::is_soa_layout_v<U>)
680 template <
typename T>
681 GAIA_NODISCARD
decltype(
auto) sview_mut_raw(
void* ptr, uint32_t size)
const {
682 using U =
typename actual_type_t<T>::Type;
683 static_assert(!std::is_same_v<U, Entity>,
"Modifying chunk entities via sview_mut is forbidden");
688 template <
typename T>
689 GAIA_NODISCARD
decltype(
auto) sview_mut() {
690 return sview_mut<T>(0, m_header.
count);
704 static_assert(!std::is_same_v<core::raw_t<T>,
Entity>,
"mod can't be used to modify Entity");
707 using TT =
typename T::type;
708 using U =
typename component_type_t<TT>::Type;
709 static_assert(!std::is_empty_v<U>,
"mut can't be used to modify tag components");
711#if GAIA_ASSERT_ENABLED
714 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
715 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
716 const auto compIdx = comp_idx((
Entity)
Pair(rel, tgt));
719 update_world_version(compIdx);
721#if GAIA_ENABLE_SET_HOOKS
722 if constexpr (TriggerSetHooks) {
723 const auto& rec = m_records.
pRecords[compIdx];
724 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
725 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
729 using U =
typename component_type_t<T>::Type;
730 static_assert(!std::is_empty_v<U>,
"mut can't be used to modify tag components");
732#if GAIA_ASSERT_ENABLED
733 constexpr auto kind = entity_kind_v<T>;
735 const auto comp = m_header.
cc->
get<T>().entity;
736 GAIA_ASSERT(comp.kind() == kind);
737 const auto compIdx = comp_idx(comp);
740 update_world_version(compIdx);
742#if GAIA_ENABLE_SET_HOOKS
743 if constexpr (TriggerSetHooks) {
744 const auto& rec = m_records.
pRecords[compIdx];
745 if GAIA_UNLIKELY (rec.pItem->comp_hooks.func_set !=
nullptr)
746 rec.pItem->comp_hooks.func_set(*m_header.
world, rec, *
this);
759 template <
typename T>
760 GAIA_NODISCARD
decltype(
auto)
view_auto(uint16_t from, uint16_t to) {
761 using UOriginal =
typename actual_type_t<T>::TypeOriginal;
762 if constexpr (core::is_mut_v<UOriginal>)
763 return view_mut<T>(from, to);
765 return view<T>(from, to);
768 template <
typename T>
769 GAIA_NODISCARD
decltype(
auto) view_auto() {
770 return view_auto<T>(0, m_header.
count);
781 template <
typename T>
782 GAIA_NODISCARD
decltype(
auto)
sview_auto(uint16_t from, uint16_t to) {
783 using UOriginal =
typename actual_type_t<T>::TypeOriginal;
784 if constexpr (core::is_mut_v<UOriginal>)
785 return sview_mut<T>(from, to);
787 return view<T>(from, to);
790 template <
typename T>
791 GAIA_NODISCARD
decltype(
auto) sview_auto() {
792 return sview_auto<T>(0, m_header.
count);
795 GAIA_NODISCARD EntitySpan entity_view()
const {
799 GAIA_NODISCARD World& world() {
800 return *
const_cast<World*
>(m_header.
world);
803 GAIA_NODISCARD
const World& world()
const {
804 return *m_header.
world;
807 GAIA_NODISCARD EntitySpan ids_view()
const {
815 GAIA_NODISCARD uint8_t* comp_ptr_mut(uint32_t compIdx) {
816 const auto& rec = m_records.
pRecords[compIdx];
820 GAIA_NODISCARD uint8_t* comp_ptr_mut(uint32_t compIdx, uint32_t offset) {
821 const auto& rec = m_records.
pRecords[compIdx];
822 return rec.
pData + ((uintptr_t)rec.comp.size() * offset);
825 GAIA_NODISCARD
const uint8_t* comp_ptr(uint32_t compIdx)
const {
826 const auto& rec = m_records.
pRecords[compIdx];
830 GAIA_NODISCARD
const uint8_t* comp_ptr(uint32_t compIdx, uint32_t offset)
const {
831 const auto& rec = m_records.
pRecords[compIdx];
832 return rec.
pData + ((uintptr_t)rec.comp.size() * offset);
838 const auto row = m_header.
count++;
841 GAIA_ASSERT(m_header.
count != 0);
844 entity_view_mut()[row] = entity;
854 GAIA_PROF_SCOPE(Chunk::copy_entity_data);
856 auto& srcEntityContainer = recs[srcEntity];
857 auto* pSrcChunk = srcEntityContainer.pChunk;
859 auto& dstEntityContainer = recs[dstEntity];
860 auto* pDstChunk = dstEntityContainer.pChunk;
862 GAIA_ASSERT(srcEntityContainer.pArchetype == dstEntityContainer.pArchetype);
864 auto srcRecs = pSrcChunk->comp_rec_view();
868 GAIA_FOR(pSrcChunk->m_header.genEntities) {
869 const auto& rec = srcRecs[i];
870 if (!component_has_inline_data(rec.comp))
873 const auto* pSrc = (
const void*)pSrcChunk->comp_ptr_mut(i);
874 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(i);
876 pDst, pSrc, dstEntityContainer.row, srcEntityContainer.row, pDstChunk->capacity(), pSrcChunk->capacity());
887 Chunk* pSrcChunk, uint32_t srcRow,
Chunk* pDstChunk, uint32_t dstRow, uint32_t dstCount) {
888 GAIA_PROF_SCOPE(Chunk::copy_entity_data_n_same_chunk);
890 GAIA_ASSERT(pSrcChunk !=
nullptr);
891 GAIA_ASSERT(pDstChunk !=
nullptr);
892 GAIA_ASSERT(srcRow < pSrcChunk->size());
893 GAIA_ASSERT(dstRow + dstCount <= pDstChunk->size());
894 GAIA_ASSERT(pSrcChunk->ids_view().size() == pDstChunk->ids_view().size());
896 auto srcRecs = pSrcChunk->comp_rec_view();
901 const auto& rec = srcRecs[i];
902 if (!component_has_inline_data(rec.comp))
905 const auto* pSrc = (
const void*)pSrcChunk->comp_ptr(i);
906 GAIA_FOR_(dstCount, rowOffset) {
907 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(i);
908 rec.pItem->copy(pDst, pSrc, dstRow + rowOffset, srcRow, pDstChunk->
capacity(), pSrcChunk->
capacity());
920 Chunk* pSrcChunk, uint32_t srcRow,
Chunk* pDstChunk, uint32_t dstRow, uint32_t dstCount) {
921 GAIA_PROF_SCOPE(Chunk::copy_foreign_entity_data_n);
923 GAIA_ASSERT(pSrcChunk !=
nullptr);
924 GAIA_ASSERT(pDstChunk !=
nullptr);
925 GAIA_ASSERT(srcRow < pSrcChunk->size());
926 GAIA_ASSERT(dstRow + dstCount <= pDstChunk->size());
928 auto srcIds = pSrcChunk->ids_view();
929 auto dstIds = pDstChunk->ids_view();
930 auto dstRecs = pDstChunk->comp_rec_view();
935 const auto oldId = srcIds[i];
936 const auto newId = dstIds[j];
938 if (oldId == newId) {
939 const auto& rec = dstRecs[j];
940 if (component_has_inline_data(rec.comp)) {
941 auto* pSrc = (
void*)pSrcChunk->comp_ptr_mut(i);
942 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j);
943 GAIA_FOR_(dstCount, rowOffset) {
944 rec.pItem->ctor_copy(
945 pDst, pSrc, dstRow + rowOffset, srcRow, pDstChunk->
capacity(), pSrcChunk->
capacity());
954 const auto& rec = dstRecs[j];
955 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
956 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
957 rec.pItem->func_ctor(pDst, dstCount);
965 const auto& rec = dstRecs[j];
966 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
967 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
968 rec.pItem->func_ctor(pDst, dstCount);
978 GAIA_PROF_SCOPE(Chunk::move_entity_data);
980 auto& ec = recs[entity];
981 auto* pSrcChunk = ec.pChunk;
982 auto srcRecs = pSrcChunk->comp_rec_view();
986 GAIA_FOR(pSrcChunk->m_header.genEntities) {
987 const auto& rec = srcRecs[i];
988 if (!component_has_inline_data(rec.comp))
991 auto* pSrc = (
void*)pSrcChunk->comp_ptr_mut(i);
992 auto* pDst = (
void*)comp_ptr_mut(i);
993 rec.pItem->ctor_move(pDst, pSrc, row, ec.row, capacity(), pSrcChunk->capacity());
1003 GAIA_PROF_SCOPE(Chunk::copy_foreign_entity_data);
1005 GAIA_ASSERT(pSrcChunk !=
nullptr);
1006 GAIA_ASSERT(pDstChunk !=
nullptr);
1007 GAIA_ASSERT(srcRow < pSrcChunk->size());
1008 GAIA_ASSERT(dstRow < pDstChunk->size());
1010 auto srcIds = pSrcChunk->ids_view();
1011 auto dstIds = pDstChunk->ids_view();
1012 auto dstRecs = pDstChunk->comp_rec_view();
1022 const auto oldId = srcIds[i];
1023 const auto newId = dstIds[j];
1025 if (oldId == newId) {
1026 const auto& rec = dstRecs[j];
1027 if (component_has_inline_data(rec.comp)) {
1028 auto* pSrc = (
void*)pSrcChunk->comp_ptr_mut(i);
1029 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j);
1030 rec.pItem->ctor_copy(pDst, pSrc, dstRow, srcRow, pDstChunk->
capacity(), pSrcChunk->
capacity());
1039 const auto& rec = dstRecs[j];
1040 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
1041 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
1042 rec.pItem->func_ctor(pDst, 1);
1050 for (; j < pDstChunk->m_header.
genEntities; ++j) {
1051 const auto& rec = dstRecs[j];
1052 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
1053 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
1054 rec.pItem->func_ctor(pDst, 1);
1066 GAIA_PROF_SCOPE(Chunk::move_foreign_entity_data);
1068 GAIA_ASSERT(pSrcChunk !=
nullptr);
1069 GAIA_ASSERT(pDstChunk !=
nullptr);
1070 GAIA_ASSERT(srcRow < pSrcChunk->size());
1071 GAIA_ASSERT(dstRow < pDstChunk->size());
1073 auto srcIds = pSrcChunk->ids_view();
1074 auto dstIds = pDstChunk->ids_view();
1075 auto dstRecs = pDstChunk->comp_rec_view();
1085 const auto oldId = srcIds[i];
1086 const auto newId = dstIds[j];
1088 if (oldId == newId) {
1089 const auto& rec = dstRecs[j];
1090 if (component_has_inline_data(rec.comp)) {
1091 auto* pSrc = (
void*)pSrcChunk->comp_ptr_mut(i);
1092 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j);
1093 rec.pItem->ctor_move(pDst, pSrc, dstRow, srcRow, pDstChunk->
capacity(), pSrcChunk->
capacity());
1102 const auto& rec = dstRecs[j];
1103 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
1104 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
1105 rec.pItem->func_ctor(pDst, 1);
1113 for (; j < pDstChunk->m_header.
genEntities; ++j) {
1114 const auto& rec = dstRecs[j];
1115 if (rec.pItem !=
nullptr && rec.pItem->func_ctor !=
nullptr) {
1116 auto* pDst = (
void*)pDstChunk->comp_ptr_mut(j, dstRow);
1117 rec.pItem->func_ctor(pDst, 1);
1130 GAIA_PROF_SCOPE(Chunk::remove_entity_inter);
1132 const uint16_t rowA = row;
1133 const uint16_t rowB = m_header.
count - 1;
1135 GAIA_ASSERT(rowA <= rowB);
1138 if GAIA_LIKELY (rowA < rowB) {
1139 GAIA_ASSERT(m_header.
count > 1);
1141 auto ev = entity_view_mut();
1144 const auto entityB = ev[rowB];
1145 auto& ecB = recs[entityB];
1146#if GAIA_ASSERT_ENABLED
1147 const auto entityA = ev[rowA];
1148 auto& ecA = recs[entityA];
1150 GAIA_ASSERT(ecA.pArchetype == ecB.pArchetype);
1151 GAIA_ASSERT(ecA.pChunk == ecB.pChunk);
1157 auto recView = comp_rec_view();
1159 const auto& rec = recView[i];
1160 if (!component_has_inline_data(rec.comp))
1163 auto* pSrc = (
void*)comp_ptr_mut(i);
1164 rec.pItem->move(pSrc, pSrc, rowA, rowB, capacity(), capacity());
1166 pSrc = (
void*)comp_ptr_mut(i, rowB);
1167 rec.pItem->dtor(pSrc);
1172 ecB.pEntity = &ev[rowA];
1175 auto recView = comp_rec_view();
1177 const auto& rec = recView[i];
1178 if (!component_has_inline_data(rec.comp))
1181 auto* pSrc = (
void*)comp_ptr_mut(i, rowA);
1182 rec.pItem->dtor(pSrc);
1194 if GAIA_UNLIKELY (m_header.
count == 0)
1197 GAIA_PROF_SCOPE(Chunk::remove_entity);
1201 remove_entity_inter(row, recs);
1206 remove_last_entity();
1210 const uint16_t pivot = size_disabled() - 1;
1211 swap_chunk_entities(row, pivot, recs);
1213 remove_entity_inter(pivot, recs);
1216 remove_last_entity();
1229 if GAIA_UNLIKELY (m_header.
count <= 1 || rowA == rowB)
1232 GAIA_PROF_SCOPE(Chunk::swap_chunk_entities);
1235 auto ev = entity_view_mut();
1236 const auto entityA = ev[rowA];
1237 const auto entityB = ev[rowB];
1239 auto& ecA = recs[entityA];
1240 auto& ecB = recs[entityB];
1241 GAIA_ASSERT(ecA.pArchetype == ecB.pArchetype);
1242 GAIA_ASSERT(ecA.pChunk == ecB.pChunk);
1248 auto recView = comp_rec_view();
1250 const auto& rec = recView[i];
1251 if (!component_has_inline_data(rec.comp))
1254 GAIA_ASSERT(rec.pData == comp_ptr_mut(i));
1255 rec.pItem->swap(rec.pData, rec.pData, rowA, rowB, capacity(), capacity());
1261 ecA.pEntity = &ev[rowB];
1262 ecB.pEntity = &ev[rowA];
1273 if GAIA_UNLIKELY (entityA == entityB)
1276 GAIA_PROF_SCOPE(Chunk::swap_chunk_entities);
1278 auto& ecA = fetch_mut(world, entityA);
1279 auto& ecB = fetch_mut(world, entityB);
1282 GAIA_ASSERT(ecA.pArchetype == ecB.pArchetype);
1283 GAIA_ASSERT(ecA.pArchetype == ecB.pArchetype);
1285 auto* pChunkA = ecA.pChunk;
1286 auto* pChunkB = ecB.pChunk;
1289 pChunkA->entity_view_mut()[ecA.row] = entityB;
1290 pChunkB->entity_view_mut()[ecB.row] = entityA;
1293 auto recViewA = pChunkA->comp_rec_view();
1294 GAIA_FOR(pChunkA->m_header.genEntities) {
1295 const auto& recA = recViewA[i];
1296 if (!component_has_inline_data(recA.comp))
1299 auto* pDataA = pChunkA->comp_rec_view()[i].pData;
1300 auto* pDataB = pChunkB->comp_rec_view()[i].pData;
1307 pChunkA->capacity(), pChunkA->capacity()
1312 core::swap(ecA.row, ecB.row);
1313 core::swap(ecA.pChunk, ecB.pChunk);
1321 GAIA_ASSERT(row < m_header.
count &&
"Entity chunk row out of bounds!");
1325 if (!m_header.has_disabled_entities())
1331 const auto entity = entity_view()[row];
1333 recs[entity].data.dis = 0;
1337 if (!m_header.has_enabled_entities())
1343 const auto entity = entity_view()[row];
1345 recs[entity].data.dis = 1;
1354 GAIA_ASSERT(m_header.
count > 0);
1363 return m_data[offset];
1369 const uint8_t&
data(uint32_t offset)
const {
1370 return m_data[offset];
1378 if (item.
func_ctor ==
nullptr || !component_has_inline_data(item.
comp))
1381 GAIA_PROF_SCOPE(Chunk::call_ctor);
1383 const auto compIdx = comp_idx(item.
entity);
1384 auto* pSrc = (
void*)comp_ptr_mut(compIdx, entIdx);
1388 void call_gen_ctors(uint32_t entIdx, uint32_t entCnt) {
1392 GAIA_PROF_SCOPE(Chunk::call_gen_ctors);
1394 auto recs = comp_rec_view();
1396 const auto& rec = recs[i];
1397 if (!component_has_inline_data(rec.comp))
1400 const auto* pItem = rec.pItem;
1401 if (pItem ==
nullptr || pItem->func_ctor ==
nullptr)
1404 auto* pSrc = (
void*)comp_ptr_mut(i, entIdx);
1405 pItem->func_ctor(pSrc, entCnt);
1409 void call_all_dtors() {
1413 GAIA_PROF_SCOPE(Chunk::call_all_dtors);
1415 auto ids = ids_view();
1416 auto recs = comp_rec_view();
1417 const auto recs_cnt = recs.size();
1418 GAIA_FOR(recs_cnt) {
1419 const auto& rec = recs[i];
1420 if (!component_has_inline_data(rec.comp))
1423 const auto* pItem = rec.pItem;
1424 if (pItem ==
nullptr || pItem->func_dtor ==
nullptr)
1427 auto* pSrc = (
void*)comp_ptr_mut(i, 0);
1428 const auto e = ids[i];
1429 const auto cnt = (e.kind() == EntityKind::EK_Gen) ? m_header.
count : (uint16_t)1;
1430 pItem->func_dtor(pSrc, cnt);
1442 auto ids = ids_view();
1443 return core::has(ids, entity);
1449 template <
typename T>
1450 GAIA_NODISCARD
bool has()
const {
1452 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
1453 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
1456 const auto* pComp = m_header.
cc->
find<T>();
1457 return pComp !=
nullptr && has(pComp->entity);
1469 template <
typename T>
1470 decltype(
auto)
set(uint16_t row) {
1474 actual_type_t<T>::Kind == EntityKind::EK_Gen || row == 0,
1475 "Set providing a row can only be used with generic components");
1480 GAIA_ASSERT(row < m_header.
capacity);
1481 world_notify_on_set(*
const_cast<World*
>(m_header.
world), comp_entity<T>(), *
this, row, (uint16_t)(row + 1));
1482 return view_mut<T>()[row];
1489 template <
typename T>
1490 decltype(
auto)
set_idx(uint16_t row, uint32_t compIdx) {
1494 actual_type_t<T>::Kind == EntityKind::EK_Gen || row == 0,
1495 "Set providing a row can only be used with generic components");
1500 world_notify_on_set(
1502 return comp_mut_idx<T, true>(row, compIdx);
1508 template <
typename T>
1512 entity_kind_v<T> != EntityKind::EK_Gen,
1513 "Set not providing a row can only be used with non-generic components");
1519 return comp_mut_idx<T, true>(0, compIdx);
1526 template <
typename T>
1529 type.kind() == EntityKind::EK_Gen || row == 0,
1530 "Set providing a row can only be used with generic components");
1531 GAIA_ASSERT(type.kind() == entity_kind_v<T>);
1532 const uint32_t compIdx = comp_idx(type);
1537 GAIA_ASSERT(row < m_header.
capacity);
1538 world_notify_on_set(*
const_cast<World*
>(m_header.
world), type, *
this, row, (uint16_t)(row + 1));
1539 return comp_mut_idx<T, true>(row, compIdx);
1547 template <
typename T>
1548 decltype(
auto)
sset(uint16_t row) {
1550 actual_type_t<T>::Kind == EntityKind::EK_Gen || row == 0,
1551 "Set providing a row can only be used with generic components");
1553 GAIA_ASSERT(row < m_header.
capacity);
1554 return sview_mut<T>()[row];
1559 template <
typename T>
1560 decltype(
auto)
sset_idx(uint16_t row, uint32_t compIdx) {
1564 actual_type_t<T>::Kind == EntityKind::EK_Gen || row == 0,
1565 "Set providing a row can only be used with generic components");
1567 return comp_mut_idx<T, false>(row, compIdx);
1572 template <
typename T>
1576 entity_kind_v<T> != EntityKind::EK_Gen,
1577 "SSet not providing a row can only be used with non-generic components");
1579 return comp_mut_idx<T, false>(0, compIdx);
1588 template <
typename T>
1590 static_assert(core::is_raw_v<T>);
1593 type.kind() == EntityKind::EK_Gen || row == 0,
1594 "Set providing a row can only be used with generic components");
1595 GAIA_ASSERT(type.kind() == entity_kind_v<T>);
1596 const uint32_t compIdx = comp_idx(type);
1598 GAIA_ASSERT(row < m_header.
capacity);
1599 return comp_mut_idx<T, false>(row, compIdx);
1612 template <
typename T>
1613 GAIA_NODISCARD
decltype(
auto)
get(uint16_t row)
const {
1615 entity_kind_v<T> == EntityKind::EK_Gen,
"Get providing a row can only be used with generic components");
1617 return comp_inter<T>(row);
1624 template <
typename T>
1625 GAIA_NODISCARD
decltype(
auto)
get_idx(uint16_t row, uint32_t compIdx)
const {
1627 entity_kind_v<T> == EntityKind::EK_Gen,
"Get providing a row can only be used with generic components");
1629 return comp_inter_idx<T>(row, compIdx);
1637 template <
typename T>
1638 GAIA_NODISCARD
decltype(
auto)
get(uint16_t row,
Entity type)
const {
1640 type.kind() == EntityKind::EK_Gen || row == 0,
1641 "Get providing a row can only be used with generic components");
1642 GAIA_ASSERT(type.kind() == entity_kind_v<T>);
1643 GAIA_ASSERT(row < m_header.
count);
1644 const uint32_t compIdx = comp_idx(type);
1645 return comp_inter_idx<T>(row, compIdx);
1652 template <
typename T>
1653 GAIA_NODISCARD
decltype(
auto)
get()
const {
1655 entity_kind_v<T> != EntityKind::EK_Gen,
1656 "Get not providing a row can only be used with non-generic components");
1658 return comp_inter<T>(0);
1664 template <
typename T>
1665 GAIA_NODISCARD
decltype(
auto)
get_idx(uint32_t compIdx)
const {
1667 entity_kind_v<T> != EntityKind::EK_Gen,
1668 "Get not providing a row can only be used with non-generic components");
1670 return comp_inter_idx<T>(0, compIdx);
1673 template <
typename T>
1674 GAIA_NODISCARD
Entity comp_entity()
const {
1676 const auto rel = m_header.
cc->
get<
typename T::rel>().entity;
1677 const auto tgt = m_header.
cc->
get<
typename T::tgt>().entity;
1680 return m_header.
cc->
get<T>().entity;
1689 return ecs::comp_idx<ChunkHeader::MAX_COMPONENTS>(m_records.
pCompEntities, entity);
1698 return ecs::comp_idx({m_records.
pCompEntities + offset, m_header.
count - offset}, entity);
1705 m_header.
index = value;
1709 GAIA_NODISCARD uint32_t
idx()
const {
1710 return m_header.
index;
1715 return m_header.has_enabled_entities();
1720 return m_header.has_disabled_entities();
1755 return m_header.
dead == 1;
1760 GAIA_ASSERT(!dead());
1761 GAIA_ASSERT(!queued_for_deletion());
1767 GAIA_ASSERT(!dead());
1769 clear_delete_queue_index();
1775 GAIA_ASSERT(dying());
1788 constexpr float Threshold = 0.75f;
1789 return ((
float)m_header.
count / (
float)m_header.
capacity) < Threshold;
1793 GAIA_NODISCARD uint16_t
size()
const {
1794 return m_header.
count;
1799 return m_header.
count == 0;
1825 GAIA_NODISCARD
bool changed(uint32_t requiredVersion)
const {
1826 const auto* versions = m_records.
pVersions;
1827 const auto changeVersion = versions[0];
1828 return ::gaia::ecs::version_changed(changeVersion, requiredVersion);
1832 GAIA_NODISCARD
bool changed(uint32_t requiredVersion, uint32_t compIdx)
const {
1833 const auto* versions = m_records.
pVersions;
1835 const auto changeVersion = versions[compIdx + 1];
1836 return ::gaia::ecs::version_changed(changeVersion, requiredVersion);
1847 auto versions = comp_version_view_mut();
1853 world_invalidate_sorted_queries_for_entity(
1861 world_invalidate_sorted_queries(*
const_cast<World*
>(m_header.
world));
1885 " Chunk #%04u, entities:%u/%u, lifespanCountdown:%u", m_header.
index, m_header.
count, m_header.
capacity,
Array of elements of type.
Definition sarray_ext_impl.h:27
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:782
GAIA_NODISCARD uint16_t capacity() const
Returns the number of entities in the chunk.
Definition chunk.h:1813
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:1665
static void free(Chunk *pChunk)
Releases all memory allocated by pChunk.
Definition chunk.h:490
const uint8_t & data(uint32_t offset) const
Returns an immutable pointer to chunk data.
Definition chunk.h:1369
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:1490
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:1589
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:1193
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:886
GAIA_FORCEINLINE void update_world_version()
Update the version of all components.
Definition chunk.h:1865
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:1697
bool progress_death()
Updates internal lifespan.
Definition chunk.h:1774
decltype(auto) set(uint16_t row)
Sets the value of the unique component T on row in the chunk.
Definition chunk.h:1470
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:334
void die()
Marks the chunk as dead (ready to delete)
Definition chunk.h:1749
GAIA_NODISCARD uint32_t idx() const
Returns the index of this chunk in its archetype's storage.
Definition chunk.h:1709
uint8_t & data(uint32_t offset)
Returns a mutable pointer to chunk data.
Definition chunk.h:1362
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:1560
void update_versions()
Updates the version numbers for this chunk.
Definition chunk.h:596
GAIA_FORCEINLINE void update_world_version(uint32_t compIdx)
Update the version of a component at the index.
Definition chunk.h:1846
GAIA_NODISCARD bool has_disabled_entities() const
Checks is this chunk has any disabled entities.
Definition chunk.h:1719
GAIA_NODISCARD bool full() const
Checks is the full capacity of the has has been reached.
Definition chunk.h:1781
GAIA_NODISCARD bool is_semi() const
Checks is the chunk is semi-full.
Definition chunk.h:1786
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 add_entity(Entity entity)
Make.
Definition chunk.h:837
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
void start_dying()
Starts the process of dying (not yet ready to delete, can be revived)
Definition chunk.h:1759
GAIA_NODISCARD bool has() const
Checks if component T is present in the chunk.
Definition chunk.h:1450
GAIA_NODISCARD uint16_t size() const
Returns the total number of entities in the chunk (both enabled and disabled)
Definition chunk.h:1793
GAIA_FORCEINLINE void update_entity_order_version()
Updates the entity-order version after rows were added, removed, or reordered.
Definition chunk.h:1858
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:1613
GAIA_NODISCARD uint32_t delete_queue_index() const
Returns the index inside World's deferred chunk-delete queue.
Definition chunk.h:1734
static uintptr_t chunk_data_area_offset()
Returns the relative offset of m_data in Chunk.
Definition chunk.h:448
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:977
decltype(auto) set_idx(uint32_t compIdx)
Sets the value of a unique component using a pre-resolved component column.
Definition chunk.h:1509
void set_idx(uint32_t value)
Sets the index of this chunk in its archetype's storage.
Definition chunk.h:1704
void clear_delete_queue_index()
Clears the deferred chunk-delete queue index.
Definition chunk.h:1744
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:1841
GAIA_NODISCARD decltype(auto) get() const
Returns the value stored in the unique component T.
Definition chunk.h:1653
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:1832
void delete_queue_index(uint32_t idx)
Stores the index inside World's deferred chunk-delete queue.
Definition chunk.h:1739
decltype(auto) sset_idx(uint32_t compIdx)
Sets the value of a unique component using a pre-resolved component column.
Definition chunk.h:1573
static void copy_entity_data(Entity srcEntity, Entity dstEntity, EntityContainers &recs)
Copies all data associated with srcEntity into dstEntity.
Definition chunk.h:853
bool enabled(uint16_t row) const
Checks if the entity is enabled.
Definition chunk.h:1353
GAIA_NODISCARD bool dying() const
Checks is this chunk is dying.
Definition chunk.h:1724
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:1825
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:583
GAIA_NODISCARD uint16_t size_disabled() const
Return the number of entities in the chunk which are enabled.
Definition chunk.h:1808
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:1227
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:1065
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:1320
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
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:1271
GAIA_FORCEINLINE void update_world_version_init()
Update the version of all components on chunk init.
Definition chunk.h:1875
GAIA_NODISCARD bool dead() const
Checks is this chunk is dead (ready to delete)
Definition chunk.h:1754
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:1527
GAIA_NODISCARD bool has_enabled_entities() const
Checks is this chunk has any enabled entities.
Definition chunk.h:1714
void revive()
Makes a dying chunk alive again.
Definition chunk.h:1766
GAIA_NODISCARD bool has(Entity entity) const
Checks if a component/entity entity is present in the chunk.
Definition chunk.h:1441
GAIA_NODISCARD bool queued_for_deletion() const
Returns true when the chunk is currently queued for deferred deletion.
Definition chunk.h:1729
GAIA_NODISCARD bool empty() const
Checks is there are any entities in the chunk.
Definition chunk.h:1798
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:1002
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:1129
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 Component *comps, const ChunkDataOffset *compOffs)
Allocates memory for a new chunk.
Definition chunk.h:459
GAIA_NODISCARD decltype(auto) view_mut(uint16_t from, uint16_t to)
Returns a mutable entity or component view.
Definition chunk.h:637
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:351
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_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:1625
GAIA_NODISCARD uint8_t size_generic() const
Returns the total number of generic entities/components in the chunk.
Definition chunk.h:1818
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:1638
decltype(auto) sset(uint16_t row)
Sets the value of the unique component T on row in the chunk.
Definition chunk.h:1548
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:919
GAIA_FORCEINLINE void modify()
Marks the component.
Definition chunk.h:703
Cache for compile-time defined components.
Definition component_cache.h:25
GAIA_NODISCARD const ComponentCacheItem * find(detail::ComponentDescId compDescId) const noexcept
Searches for the component cache item given the compDescId.
Definition component_cache.h:355
GAIA_NODISCARD const ComponentCacheItem & get(detail::ComponentDescId compDescId) const noexcept
Returns the component cache item given the compDescId.
Definition component_cache.h:373
Wrapper for two Entities forming a relationship pair.
Definition id.h:500
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
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:25
Entity entity
Component entity.
Definition component_cache_item.h:75
Component comp
Unique component identifier.
Definition component_cache_item.h:77
FuncCtor * func_ctor
Function to call when the component needs to be constructed.
Definition component_cache_item.h:88
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:141
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