4#ifndef GREM_EXECUTION_ENTITY_REGISTRY_HPP
5#define GREM_EXECUTION_ENTITY_REGISTRY_HPP
23#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
41template <
typename... KnownComponents>
44template <
typename... ComponentsAndExclusions>
49template <
typename... ComponentsAndExclusions>
50struct is_entity_range<
Entities<ComponentsAndExclusions...>> : std::true_type {};
53template <
typename... ComponentsAndExclusions>
54struct entity_range_components_and_exclusions<
Entities<ComponentsAndExclusions...>> {
55 using type = meta::TypeList<ComponentsAndExclusions...>;
59template <
typename Component>
64template <
typename Component>
65struct is_component_pool<
ComponentPool<Component>> : std::true_type {};
98 : value((uint64_t{flags} << 48) | (uint64_t{generation} << 32) | uint64_t{index}) {}
189template <
typename ComponentOrExclusion>
190struct entities_extract_components {
191 static_assert(component<ComponentOrExclusion>,
192 "The template arguments to grem::execution::Entities may only contain valid component types and/or exclusions of valid component types. See the definition of the "
193 "grem::execution::component concept for what constitutes a valid component type.");
196template <component Component>
197struct entities_extract_components<Component> {
204template <component Component>
205struct entities_extract_components<const Component> {
206 using MutableComponents = meta::TypeList<>;
207 using ImmutableComponents = meta::TypeList<Component>;
208 using IncludedComponents = meta::TypeList<const Component>;
209 using ExcludedComponents = meta::TypeList<>;
212template <component Component>
213struct entities_extract_components<Exclude<Component>> {
214 using MutableComponents = meta::TypeList<>;
215 using ImmutableComponents = meta::TypeList<>;
216 using IncludedComponents = meta::TypeList<>;
217 using ExcludedComponents = meta::TypeList<Component>;
221concept empty_component = std::is_empty_v<std::remove_cv_t<T>>;
223template <empty_component Component>
224inline constinit Component DUMMY_COMPONENT{};
226template <
typename... Components>
227inline constexpr size_t NON_EMPTY_COMPONENT_COUNT = (
static_cast<size_t>(!empty_component<Components>) + ... + 0);
229template <
typename... Components>
230static constexpr auto NON_EMPTY_COMPONENT_INDEX_MAP = [] {
231 Array<size_t,
sizeof...(Components)> result{};
232 [&]<
size_t... Indices>(std::index_sequence<Indices...>) {
234 ((result[Indices] = (empty_component<Components>) ?
size_t{} : i++), ...);
235 }(std::make_index_sequence<
sizeof...(Components)>{});
239template <
typename FirstComponent,
typename... OtherComponents>
240struct first_non_empty_component : first_non_empty_component<OtherComponents...> {};
242template <
typename FirstComponent>
243struct first_non_empty_component<FirstComponent> {
244 using type = FirstComponent;
247template <
typename FirstComponent,
typename... OtherComponents>
248requires(!empty_component<FirstComponent>)
struct first_non_empty_component<FirstComponent, OtherComponents...> {
249 using type = FirstComponent;
252template <
typename List>
253struct non_empty_component_count_of_type_list;
255template <
typename... Ts>
256struct non_empty_component_count_of_type_list<meta::TypeList<Ts...>> : std::integral_constant<size_t, NON_EMPTY_COMPONENT_COUNT<Ts...>> {};
258template <
typename List>
259struct first_non_empty_component_of_type_list;
261template <
typename... Ts>
262struct first_non_empty_component_of_type_list<meta::TypeList<Ts...>> : first_non_empty_component<Ts...> {};
264template <
typename... ComponentsAndExclusions>
265concept entities_is_single_component =
266 (meta::type_list_size_v<typename detail::entities_extract_components<ComponentsAndExclusions>::IncludedComponents> + ...) == 1 &&
267 (meta::type_list_empty_v<typename detail::entities_extract_components<ComponentsAndExclusions>::ExcludedComponents> && ...);
269struct EmptyComponentPointer {};
271#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
272struct IteratorValidity {
273 SharedPointer<Atomic<size_t>> validGeneration{};
276 constexpr IteratorValidity() noexcept = default;
278 IteratorValidity(SharedPointer<Atomic<
size_t>> validGeneration) noexcept
279 : validGeneration(std::
move(validGeneration)) {
280 if (this->validGeneration) {
281 iteratorGeneration = this->validGeneration->load();
285 [[nodiscard]]
bool operator==(
const IteratorValidity&)
const noexcept =
default;
287 void assertValid()
const {
288 [[maybe_unused]]
const bool iteratorValidity = validGeneration && iteratorGeneration == validGeneration->load();
291Error: Detected use of invalidated iterator to grem::execution::Entities.
292Note: Iterators may be invalidated by calls to createEntity(), destroyEntity(), clear(), addComponent(), etc. on the EntityRegistry.
293Tip: If entities need to be destroyed as a result of iteration, either use erase_if(), or build a list of EntityIDs while iterating and destroy them afterwards.
294Tip: If new entities need to be created as a result of iteration, build a list of configurations while iterating, and create entities from them afterwards.
295Tip: If only one entity needs to be created/destroyed as a result of searching the entities, create/destroy it and then immediately break from the loop.
302struct MultiComponentSentinel {};
304template <
typename IncludedComponentsList,
typename ExcludedComponentsList>
305class MultiComponentIterator;
307template <
typename... IncludedComponents,
typename... ExcludedComponents>
308class MultiComponentIterator<meta::TypeList<IncludedComponents...>, meta::TypeList<ExcludedComponents...>> {
310 static constexpr size_t EXCLUDED_COMPONENT_COUNT =
sizeof...(ExcludedComponents);
311 static constexpr size_t INCLUDED_COMPONENT_COUNT =
sizeof...(IncludedComponents);
312 static constexpr size_t NON_EMPTY_INCLUDED_COMPONENT_COUNT = NON_EMPTY_COMPONENT_COUNT<IncludedComponents...>;
313 static constexpr auto NON_EMPTY_INCLUDED_COMPONENT_INDEX_MAP = NON_EMPTY_COMPONENT_INDEX_MAP<IncludedComponents...>;
316 using difference_type = ptrdiff_t;
317 using value_type =
Tuple<
const EntityID, IncludedComponents...>;
318 using reference =
Tuple<
const EntityID, IncludedComponents&...>;
319 using iterator_category = std::forward_iterator_tag;
331#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
332 GREM_ALWAYS_INLINE constexpr MultiComponentIterator(
const EntityID* candidateEntity,
const EntityID* candidateEntitiesEnd,
333 const Array<const EntityID::Index*, EXCLUDED_COMPONENT_COUNT>& excludedIndexArrays,
const Array<const EntityID::Index*, INCLUDED_COMPONENT_COUNT>& componentIndexArrays,
334 const Array<void*, NON_EMPTY_INCLUDED_COMPONENT_COUNT>& componentArrays,
Span<const EntitySlot> slots) noexcept
335 : candidateEntity(candidateEntity)
336 , candidateEntitiesEnd(candidateEntitiesEnd)
337 , excludedIndexArrays(excludedIndexArrays)
338 , componentIndexArrays(componentIndexArrays)
339 , componentArrays(componentArrays)
344 GREM_ALWAYS_INLINE constexpr MultiComponentIterator(
const EntityID* candidateEntity,
const EntityID* candidateEntitiesEnd,
345 const Array<const EntityID::Index*, EXCLUDED_COMPONENT_COUNT>& excludedIndexArrays,
const Array<const EntityID::Index*, INCLUDED_COMPONENT_COUNT>& componentIndexArrays,
348 const Array<IteratorValidity,
max(INCLUDED_COMPONENT_COUNT,
size_t{1})>& componentIteratorValidity)
noexcept
349 : candidateEntity(candidateEntity)
350 , candidateEntitiesEnd(candidateEntitiesEnd)
351 , excludedIndexArrays(excludedIndexArrays)
352 , componentIndexArrays(componentIndexArrays)
353 , componentArrays(componentArrays)
355 , excludedIteratorValidity(excludedIteratorValidity)
356 , componentIteratorValidity(componentIteratorValidity) {
362#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
365 GREM_ASSERT(containsEntity(excludedIndexArrays, componentIndexArrays, slots, *candidateEntity));
366 return dereference(componentIndexArrays, componentArrays, *candidateEntity);
370 return pointer{**
this};
374#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
383 MultiComponentIterator old = *
this;
389#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
390 GREM_ASSERT(excludedIteratorValidity == other.excludedIteratorValidity);
391 GREM_ASSERT(componentIteratorValidity == other.componentIteratorValidity);
393 return candidateEntity == other.candidateEntity;
397 return candidateEntity == candidateEntitiesEnd;
401 template <
typename... ComponentsAndExclusions>
402 friend class grem::execution::Entities;
404 template <
typename Component,
size_t I>
405 [[nodiscard]]
GREM_ALWAYS_INLINE static Component& getComponentUnsafe(
const Array<const EntityID::Index*, INCLUDED_COMPONENT_COUNT>& componentIndexArrays,
406 const Array<void*, NON_EMPTY_INCLUDED_COMPONENT_COUNT>& componentArrays,
size_t slotIndex) {
407 if constexpr (empty_component<Component>) {
408 return DUMMY_COMPONENT<Component>;
410 return static_cast<Component*
>(componentArrays[NON_EMPTY_INCLUDED_COMPONENT_INDEX_MAP[
I]])[componentIndexArrays[I][slotIndex]];
414 [[nodiscard]]
GREM_ALWAYS_INLINE static bool passesFilter(
const Array<const EntityID::Index*, EXCLUDED_COMPONENT_COUNT>& excludedIndexArrays,
415 const Array<const EntityID::Index*, INCLUDED_COMPONENT_COUNT>& componentIndexArrays, EntityID
id) {
416 const size_t slotIndex =
id.getIndex();
417 const bool notExcluded = [&]<
size_t... Indices>(std::index_sequence<Indices...>) ->
bool {
418 return ((!excludedIndexArrays[Indices] || excludedIndexArrays[Indices][slotIndex] ==
EntityID::INVALID_INDEX) && ... &&
true);
419 }(std::make_index_sequence<EXCLUDED_COMPONENT_COUNT>{});
423 const bool included = [&]<
size_t... Indices>(std::index_sequence<Indices...>) ->
bool {
425 }(std::make_index_sequence<INCLUDED_COMPONENT_COUNT>{});
429 [[nodiscard]]
GREM_ALWAYS_INLINE static bool containsEntity(
const Array<const EntityID::Index*, EXCLUDED_COMPONENT_COUNT>& excludedIndexArrays,
430 const Array<const EntityID::Index*, INCLUDED_COMPONENT_COUNT>& componentIndexArrays,
Span<const EntitySlot> slots, EntityID
id) {
431 return id.getIndex() < slots.size() && (
id.getGeneration() & 1) != 0 && slots[
id.getIndex()].generation ==
id.getGeneration() &&
432 passesFilter(excludedIndexArrays, componentIndexArrays,
id);
435 [[nodiscard]]
GREM_ALWAYS_INLINE static reference dereference(
const Array<const EntityID::Index*, INCLUDED_COMPONENT_COUNT>& componentIndexArrays,
436 const Array<void*, NON_EMPTY_INCLUDED_COMPONENT_COUNT>& componentArrays, EntityID
id) {
437 return [&]<
size_t... Indices>(std::index_sequence<Indices...>) -> reference {
438 const size_t slotIndex =
id.getIndex();
439 return reference{id, getComponentUnsafe<IncludedComponents, Indices>(componentIndexArrays, componentArrays, slotIndex)...};
440 }(std::make_index_sequence<INCLUDED_COMPONENT_COUNT>{});
444 while (candidateEntity != candidateEntitiesEnd && !passesFilter(excludedIndexArrays, componentIndexArrays, *candidateEntity)) {
450 const EntityID* candidateEntitiesEnd;
451 [[no_unique_address]] Array<const EntityID::Index*, EXCLUDED_COMPONENT_COUNT> excludedIndexArrays;
452 [[no_unique_address]] Array<const EntityID::Index*, INCLUDED_COMPONENT_COUNT> componentIndexArrays;
453 [[no_unique_address]] Array<void*, NON_EMPTY_INCLUDED_COMPONENT_COUNT> componentArrays;
455#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
457 Array<IteratorValidity,
max(INCLUDED_COMPONENT_COUNT,
size_t{1})> componentIteratorValidity{};
459 void assertValid()
const {
460 for (
size_t i = 0; i < EXCLUDED_COMPONENT_COUNT; ++i) {
461 if (excludedIndexArrays[i]) {
462 excludedIteratorValidity[i].assertValid();
465 for (
const IteratorValidity& validity : componentIteratorValidity) {
466 validity.assertValid();
472struct SingleComponentSentinel {
476template <
typename Component>
477class SingleComponentIterator {
479 using difference_type = ptrdiff_t;
480 using value_type = Tuple<const EntityID, Component>;
481 using reference = Tuple<const EntityID, Component&>;
482 using iterator_category = std::random_access_iterator_tag;
494#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
495 GREM_ALWAYS_INLINE constexpr SingleComponentIterator(
const EntityID* entity, Component* component,
const EntityID::Index* componentIndexArray,
void* componentArray,
498 , component(component)
499 , componentIndexArray(componentIndexArray)
500 , componentArray(componentArray)
503 GREM_ALWAYS_INLINE constexpr SingleComponentIterator(
const EntityID* entity, Component* component,
const EntityID::Index* componentIndexArray,
void* componentArray,
505 const IteratorValidity& validity)
noexcept
507 , component(component)
508 , componentIndexArray(componentIndexArray)
509 , componentArray(componentArray)
511 , validity(validity) {}
514 GREM_ALWAYS_INLINE constexpr operator SingleComponentIterator<const Component>() const noexcept {
515#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
516 return SingleComponentIterator<const Component>{entity, component, componentIndexArray, componentArray, slots};
518 return SingleComponentIterator<const Component>{entity, component, componentIndexArray, componentArray, slots, validity};
523#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
526 return reference{*entity, *component};
530 return pointer{**
this};
533 [[nodiscard]]
GREM_ALWAYS_INLINE constexpr reference operator[](difference_type n)
const {
538#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
547#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
556 SingleComponentIterator old = *
this;
562 SingleComponentIterator old = *
this;
568#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
579#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
589 [[nodiscard]]
GREM_ALWAYS_INLINE constexpr bool operator==(
const SingleComponentSentinel& sentinel)
const {
590 return entity == sentinel.entitiesEnd;
593 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr SingleComponentIterator operator+(SingleComponentIterator a, difference_type b) {
597 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr SingleComponentIterator operator+(difference_type a, SingleComponentIterator b) {
601 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr SingleComponentIterator operator-(SingleComponentIterator a, difference_type b) {
605 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr difference_type operator-(SingleComponentIterator a, SingleComponentIterator b) {
606#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
609 GREM_ASSERT(a.entity - b.entity == a.component - b.component);
610 return static_cast<difference_type
>(a.entity - b.entity);
613 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr bool operator==(SingleComponentIterator a, SingleComponentIterator b) {
614#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
617 GREM_ASSERT(a.entity - b.entity == a.component - b.component);
618 return a.entity == b.entity;
621 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr auto operator<=>(SingleComponentIterator a, SingleComponentIterator b) {
622#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
625 GREM_ASSERT(a.entity - b.entity == a.component - b.component);
626 return a.entity <=> b.entity;
630 template <
typename... ComponentsAndExclusions>
631 friend class Entities;
634 Component* component;
636 void* componentArray;
638#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
639 IteratorValidity validity{};
641 void assertValid()
const {
642 validity.assertValid();
647template <empty_component Component>
648class SingleComponentIterator<Component> {
650 using difference_type = ptrdiff_t;
651 using value_type = Tuple<const EntityID, Component>;
652 using reference = Tuple<const EntityID, Component&>;
653 using iterator_category = std::random_access_iterator_tag;
665#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
669 , componentIndexArray(componentIndexArray)
673 const IteratorValidity& validity)
noexcept
675 , componentIndexArray(componentIndexArray)
677 , validity(validity) {}
680 GREM_ALWAYS_INLINE constexpr operator SingleComponentIterator<const Component>() const noexcept {
681#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
682 return SingleComponentIterator<const Component>{entity,
nullptr, componentIndexArray,
nullptr, slots};
684 return SingleComponentIterator<const Component>{entity,
nullptr, componentIndexArray,
nullptr, slots, validity};
689#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
692 return reference{*entity, DUMMY_COMPONENT<Component>};
696 return pointer{**
this};
699 [[nodiscard]]
GREM_ALWAYS_INLINE constexpr reference operator[](difference_type n)
const {
704#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
712#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
720 SingleComponentIterator old = *
this;
726 SingleComponentIterator old = *
this;
732#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
742#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
751 [[nodiscard]]
GREM_ALWAYS_INLINE constexpr bool operator==(
const SingleComponentSentinel& sentinel)
const {
752 return entity == sentinel.entitiesEnd;
755 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr SingleComponentIterator operator+(SingleComponentIterator a, difference_type b) {
759 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr SingleComponentIterator operator+(difference_type a, SingleComponentIterator b) {
763 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr SingleComponentIterator operator-(SingleComponentIterator a, difference_type b) {
767 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr difference_type operator-(SingleComponentIterator a, SingleComponentIterator b) {
768#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
771 return static_cast<difference_type
>(a.entity - b.entity);
774 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr bool operator==(SingleComponentIterator a, SingleComponentIterator b) {
775#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
778 return a.entity == b.entity;
781 [[nodiscard]]
GREM_ALWAYS_INLINE friend constexpr auto operator<=>(SingleComponentIterator a, SingleComponentIterator b) {
782#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
785 return a.entity <=> b.entity;
789 template <
typename... ComponentsAndExclusions>
790 friend class Entities;
795#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
796 IteratorValidity validity{};
798 void assertValid()
const {
799 validity.assertValid();
814template <
typename... ComponentsAndExclusions>
832 static constexpr size_t NON_EMPTY_INCLUDED_COMPONENT_COUNT = detail::non_empty_component_count_of_type_list<IncludedComponents>::value;
835 using iterator = detail::MultiComponentIterator<IncludedComponents, ExcludedComponents>;
856 : candidateEntities(first.candidateEntities.data(), static_cast<
size_t>(last.candidateEntities.data() - first.candidateEntities.data()))
857 , excludedIndexArrays(first.excludedIndexArrays)
858 , componentIndexArrays(first.componentIndexArrays)
859 , componentArrays(first.componentArrays)
860 , slots(first.slots) {
861#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
862 GREM_ASSERT(first.excludedIteratorValidity == last.excludedIteratorValidity);
863 GREM_ASSERT(first.componentIteratorValidity == last.componentIteratorValidity);
864 excludedIteratorValidity = first.excludedIteratorValidity;
865 componentIteratorValidity = first.componentIteratorValidity;
870#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
871 return iterator{candidateEntities.data(), candidateEntities.data() + candidateEntities.size(), excludedIndexArrays, componentIndexArrays, componentArrays, slots};
873 return iterator{candidateEntities.data(), candidateEntities.data() + candidateEntities.size(), excludedIndexArrays, componentIndexArrays, componentArrays, slots,
874 excludedIteratorValidity, componentIteratorValidity};
889 return candidateEntities.size();
901 for (
const EntityID::Index*
const componentIndexArray : componentIndexArrays) {
902 if (!componentIndexArray) {
906#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
909 return iterator::containsEntity(excludedIndexArrays, componentIndexArrays, slots,
id);
913#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
916 GREM_ASSERT(iterator::containsEntity(excludedIndexArrays, componentIndexArrays, slots,
id));
917 return iterator::dereference(componentIndexArrays, componentArrays,
id);
931 template <component T>
949 template <component T>
974 template <component T,
typename U>
978 return std::forward<U>(defaultValue);
995 template <component T>
1016 GREM_ASSERT((slots[slotIndex].generation & 1) != 0);
1017 return EntityID{slotIndex, slots[slotIndex].generation, slots[slotIndex].active.flags};
1037 template <component T>
1041 static_assert(CONTAINS_CONST_COMPONENT || CONTAINS_NON_CONST_COMPONENT,
"The specified component type must be included in the entity range type.");
1042#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1045 if constexpr (CONTAINS_CONST_COMPONENT) {
1046 return iterator::template getComponentUnsafe<const T, meta::type_list_index_v<IncludedComponents, const T>>(componentIndexArrays, componentArrays, slotIndex);
1048 return iterator::template getComponentUnsafe<T, meta::type_list_index_v<IncludedComponents, T>>(componentIndexArrays, componentArrays, slotIndex);
1053 template <
typename... KnownComponents>
1056#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
1060 : candidateEntities(candidateEntities)
1061 , excludedIndexArrays(excludedIndexArrays)
1062 , componentIndexArrays(componentIndexArrays)
1063 , componentArrays(componentArrays)
1069 const Array<detail::IteratorValidity,
max(INCLUDED_COMPONENT_COUNT,
size_t{1})>& componentIteratorValidity)
noexcept
1070 : candidateEntities(candidateEntities)
1071 , excludedIndexArrays(excludedIndexArrays)
1072 , componentIndexArrays(componentIndexArrays)
1073 , componentArrays(componentArrays)
1075 , excludedIteratorValidity(excludedIteratorValidity)
1076 , componentIteratorValidity(componentIteratorValidity) {}
1084 [[no_unique_address]] Array<const EntityID::Index*, EXCLUDED_COMPONENT_COUNT> excludedIndexArrays{};
1085 [[no_unique_address]] Array<const EntityID::Index*, INCLUDED_COMPONENT_COUNT> componentIndexArrays{};
1086 [[no_unique_address]] Array<void*, NON_EMPTY_INCLUDED_COMPONENT_COUNT> componentArrays{};
1087 Span<const detail::EntitySlot> slots{};
1088#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1089 Array<detail::IteratorValidity, EXCLUDED_COMPONENT_COUNT> excludedIteratorValidity{};
1090 Array<detail::IteratorValidity,
max(INCLUDED_COMPONENT_COUNT,
size_t{1})> componentIteratorValidity{};
1092 void assertValid()
const {
1093 for (
size_t i = 0; i < EXCLUDED_COMPONENT_COUNT; ++i) {
1094 if (excludedIndexArrays[i]) {
1095 excludedIteratorValidity[i].assertValid();
1098 for (
const detail::IteratorValidity& validity : componentIteratorValidity) {
1099 validity.assertValid();
1113template <
typename... ComponentsAndExclusions>
1129 using Component =
typename detail::first_non_empty_component_of_type_list<IncludedComponents>::type;
1131 static constexpr bool IS_COMPONENT_EMPTY = detail::empty_component<Component>;
1134 using iterator = detail::SingleComponentIterator<Component>;
1155 : candidateEntities(first.entity, static_cast<
size_t>(last.entity - first.entity))
1156 , componentIndexArray(first.componentIndexArray)
1157 , slots(first.slots) {
1158#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1160 validity = first.validity;
1173 : candidateEntities(first.entity, static_cast<size_t>(last.entity - first.entity))
1175 , componentIndexArray(first.componentIndexArray)
1176 , componentArray(first.componentArray)
1177 , slots(first.slots) {
1178#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1180 validity = first.validity;
1186 void* componentArrayElement =
nullptr;
1187 if constexpr (!IS_COMPONENT_EMPTY) {
1189 componentArrayElement = componentArray;
1191#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
1192 return iterator{candidateEntities.data(),
component, componentIndexArray, componentArrayElement, slots};
1194 return iterator{candidateEntities.data(),
component, componentIndexArray, componentArrayElement, slots, validity};
1199 return sentinel{candidateEntities.data() + candidateEntities.size()};
1203 return candidateComponents;
1207 return candidateEntities.size();
1211 return candidateEntities.empty();
1221 return candidateEntities.size();
1233 if (!componentIndexArray) {
1236#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1239 return id.getIndex() < slots.size() && (
id.getGeneration() & 1) != 0 && slots[
id.getIndex()].generation ==
id.getGeneration() &&
1259 template <component T>
1277 template <component T>
1302 template <component T,
typename U>
1306 return std::forward<U>(defaultValue);
1323 template <component T>
1344 GREM_ASSERT((slots[slotIndex].generation & 1) != 0);
1345 return EntityID{slotIndex, slots[slotIndex].generation, slots[slotIndex].active.flags};
1365 template <component T>
1368#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1371 if constexpr (IS_COMPONENT_EMPTY) {
1372 return detail::DUMMY_COMPONENT<Component>;
1374 return static_cast<Component*
>(componentArray)[componentIndexArray[slotIndex]];
1379 template <
typename... KnownComponents>
1382#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
1385 : candidateEntities(candidateEntities)
1386 , componentIndexArray(componentIndexArrays[0])
1391 requires(!IS_COMPONENT_EMPTY)
1392 : candidateEntities(candidateEntities)
1393 , candidateComponents(static_cast<Component*>(componentArrays[0]))
1394 , componentIndexArray(componentIndexArrays[0])
1395 , componentArray(componentArrays[0])
1401 : candidateEntities(candidateEntities)
1402 , componentIndexArray(componentIndexArrays[0])
1404 , validity(componentIteratorValidity[0]) {
1405 if (!candidateEntities.empty()) {
1406 validity.assertValid();
1413 : candidateEntities(candidateEntities)
1414 , candidateComponents(static_cast<Component*>(componentArrays[0]))
1415 , componentIndexArray(componentIndexArrays[0])
1416 , componentArray(componentArrays[0])
1418 , validity(componentIteratorValidity[0]) {
1419 if (!candidateEntities.empty()) {
1420 validity.assertValid();
1426 candidateEntities = candidateEntities.subspan(begin, end - begin);
1427 if constexpr (!IS_COMPONENT_EMPTY) {
1428 candidateComponents += begin;
1433 [[no_unique_address]] std::conditional_t<IS_COMPONENT_EMPTY, detail::EmptyComponentPointer, Component*> candidateComponents{};
1434 [[no_unique_address]]
const EntityID::Index* componentIndexArray =
nullptr;
1435 [[no_unique_address]] std::conditional_t<IS_COMPONENT_EMPTY, detail::EmptyComponentPointer, void*> componentArray{};
1436 Span<const detail::EntitySlot> slots{};
1437#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1438 detail::IteratorValidity validity{};
1440 void assertValid()
const {
1441 validity.assertValid();
1448struct ComponentStorage {
1454 template <component T>
1456 if constexpr (!detail::empty_component<T>) {
1457 T*
const components =
static_cast<T*
>(array);
1458 std::destroy_n(components,
static_cast<size_t>(
count));
1460 std::allocator<T>{}.deallocate(components,
static_cast<size_t>(capacity));
1465 template <component T>
1467 if constexpr (!detail::empty_component<T>) {
1468 T*
const components =
static_cast<T*
>(array);
1469 if (destinationIndex != sourceIndex) {
1470 components[destinationIndex] = std::move(components[sourceIndex]);
1472 std::destroy_at(components + sourceIndex);
1476 template <component T>
1479 if constexpr (!copyable<T>) {
1480 if (inputCount > 0) {
1483 T*
const outputComponents =
static_cast<T*
>(output);
1484 std::destroy_n(outputComponents,
static_cast<size_t>(outputCount));
1486 }
else if constexpr (!detail::empty_component<T>) {
1487 T*
const outputComponents =
static_cast<T*
>(output);
1488 const T*
const inputComponents =
static_cast<const T*
>(input);
1489 if (outputCount >= inputCount) {
1490 std::copy_n(inputComponents,
static_cast<size_t>(inputCount), outputComponents);
1491 std::destroy_n(outputComponents + inputCount,
static_cast<size_t>(outputCount - inputCount));
1492 }
else if (outputCapacity >= inputCount) {
1493 std::copy_n(inputComponents,
static_cast<size_t>(outputCount), outputComponents);
1494 std::uninitialized_copy_n(inputComponents + outputCount,
static_cast<size_t>(inputCount - outputCount), outputComponents + outputCount);
1496 T*
const newArray = std::allocator<T>{}.allocate(
static_cast<size_t>(inputCapacity));
1498 std::uninitialized_copy_n(inputComponents,
static_cast<size_t>(inputCount), newArray);
1500 std::allocator<T>{}.deallocate(newArray,
static_cast<size_t>(inputCapacity));
1511 Buffer<EntityID::Index> indices{};
1512 Buffer<EntityID> entityIDs{};
1514 void* array =
nullptr;
1515 Destroy destroy =
nullptr;
1516 DestructiveMove destructiveMove =
nullptr;
1517 CopyOrClone copyOrClone =
nullptr;
1518#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1519 SharedPointer<Atomic<size_t>> currentIteratorGeneration = SharedPointer<Atomic<size_t>>::create();
1524 template <component T>
1526 : indices(slotCount,
EntityID::INVALID_INDEX)
1527 , destroy(destroyImplementation<
T>)
1528 , destructiveMove(destructiveMoveImplementation<
T>)
1529 , copyOrClone(copyOrCloneImplementation<
T>) {}
1532 : indices(slotCount,
EntityID::INVALID_INDEX)
1533 , destroy(other.destroy)
1534 , destructiveMove(other.destructiveMove)
1535 , copyOrClone(other.copyOrClone) {}
1539 destroy(array,
static_cast<EntityID::Index>(entityIDs.size()), componentCapacity);
1541#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1542 currentIteratorGeneration->fetch_add(1);
1551 *
this = std::move(other);
1554 ComponentStorage& operator=(
const ComponentStorage& other) {
1555 if (
this == &other) {
1558#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1559 currentIteratorGeneration->fetch_add(1);
1561 if (destroy != other.destroy) {
1563 destroy(array,
static_cast<EntityID::Index>(entityIDs.size()), componentCapacity);
1567 componentCapacity = 0;
1569 destroy = other.destroy;
1570 destructiveMove = other.destructiveMove;
1571 copyOrClone = other.copyOrClone;
1573 if (other.copyOrClone) {
1578 void*
const newArray = other.copyOrClone(array,
count, capacity, other.array, otherCount, otherCapacity);
1581 destroy(array,
count, capacity);
1584 componentCapacity = other.componentCapacity;
1587 entityIDs = other.entityIDs;
1588 indices = other.indices;
1590 destroy(array, otherCount, 0);
1598 ComponentStorage& operator=(ComponentStorage&& other)
noexcept {
1599 if (
this == &other) {
1603 destroy(array,
static_cast<EntityID::Index>(entityIDs.size()), componentCapacity);
1605 indices = std::move(other.indices);
1606 entityIDs = std::move(other.entityIDs);
1607 componentCapacity = std::exchange(other.componentCapacity,
EntityID::Index{0});
1608 array = std::exchange(other.array,
nullptr);
1609 destroy = other.destroy;
1610 destructiveMove = other.destructiveMove;
1611 copyOrClone = other.copyOrClone;
1612#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1613 currentIteratorGeneration->fetch_add(1);
1614 std::swap(currentIteratorGeneration, other.currentIteratorGeneration);
1619 void clear() noexcept {
1623#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1624 currentIteratorGeneration->fetch_add(1);
1628 template <component
T,
typename... Args>
1629 T& add(EntityID
id, Args&&... args) {
1630 T*
const result = addIfMissing<T>(
id, std::forward<Args>(args)...);
1632 throw std::logic_error{
"Component was already added to entity."};
1637 template <component
T,
typename... Args>
1638 T* addIfMissing(EntityID
id, Args&&... args) {
1643 const size_t oldCount = entityIDs.size();
1644 if constexpr (detail::empty_component<T>) {
1645 entityIDs.push_back(
id);
1647#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1648 currentIteratorGeneration->fetch_add(1);
1650 return &detail::DUMMY_COMPONENT<T>;
1652 const size_t oldCapacity =
static_cast<size_t>(componentCapacity);
1653 if (oldCount >= oldCapacity) {
1655 const size_t newCapacity =
clamp(oldCapacity + oldCapacity / 2,
size_t{8},
static_cast<size_t>(
static_cast<size_t>(
EntityID::MAX_INDEX) + 1));
1657 const T*
const oldArray =
static_cast<const T*
>(array);
1658 T*
const newArray = std::allocator<T>{}.allocate(newCapacity);
1660 std::uninitialized_move_n(oldArray, oldCount, newArray);
1662 std::allocator<T>{}.deallocate(newArray, newCapacity);
1671 T*
const newComponent = std::construct_at(
static_cast<T*
>(array) + oldCount, std::forward<Args>(args)...);
1673 entityIDs.push_back(
id);
1675 std::destroy_at(newComponent);
1679#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1680 currentIteratorGeneration->fetch_add(1);
1682 return newComponent;
1690 const EntityID lastComponentEntityID = entityIDs[lastComponentIndex];
1691 indices[lastComponentEntityID.getIndex()] = componentIndex;
1692 entityIDs[componentIndex] = lastComponentEntityID;
1693 destructiveMove(array, componentIndex, lastComponentIndex);
1694 entityIDs.pop_back();
1696#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
1697 currentIteratorGeneration->fetch_add(1);
1712template <
typename Component>
1715 using iterator = detail::SingleComponentIterator<Component>;
1739#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
1740 return (storage) ?
iterator{storage->entityIDs.data(),
static_cast<Component*
>(storage->array), storage->indices.data(), storage->array, storage->indices.size()}
1741 :
iterator{
nullptr,
nullptr,
nullptr,
nullptr, 0};
1744 ?
iterator{storage->entityIDs.data(),
static_cast<Component*
>(storage->array), storage->indices.data(), storage->array, storage->indices.size(), validGeneration}
1745 :
iterator{
nullptr,
nullptr,
nullptr,
nullptr, 0, validGeneration};
1750 return (storage) ?
sentinel{storage->entityIDs.data() + storage->entityIDs.size()} :
sentinel{
nullptr};
1756 if constexpr (detail::empty_component<Component>) {
1757 return {id, detail::DUMMY_COMPONENT<Component>};
1759 return {id,
static_cast<Component*
>(storage->array)[storage->indices[
id.getIndex()]]};
1776 GREM_ASSERT(storage->indices.size() == slots.size());
1792 Component*
const result =
find(
id);
1814 template <
typename U>
1816 Component*
const result =
find(
id);
1818 return std::forward<U>(defaultValue);
1836 GREM_ASSERT(storage->indices.size() == slots.size());
1837 if (isValidEntity(
id)) {
1838 const EntityID::Index componentIndex = storage->indices[
id.getIndex()];
1840 if constexpr (detail::empty_component<Component>) {
1841 return &detail::DUMMY_COMPONENT<Component>;
1843 return static_cast<Component*
>(storage->array) + componentIndex;
1868 template <
typename... Args>
1873 if (!isValidEntity(
id)) {
1874 throw std::out_of_range{
"Invalid entity handle."};
1876 return storage->template
add<Component>(
id, std::forward<Args>(args)...);
1898 template <
typename... Args>
1903 if (!isValidEntity(
id)) {
1904 throw std::out_of_range{
"Invalid entity handle."};
1930 if (Component*
const result =
find(
id)) {
1931 return *result = std::move(value);
1933 return add(
id, std::move(value));
1957 if (Component*
const result =
find(
id)) {
1958 return *result = value;
1960 return add(
id, value);
1976 storage->remove(
id.getIndex());
1988 const size_type result = storage->entityIDs.size();
1996 template <
typename... KnownComponents>
1999 template <
typename U>
2002 GREM_ALWAYS_INLINE constexpr ComponentPool(std::conditional_t<std::is_const_v<Component>,
const detail::ComponentStorage, detail::ComponentStorage>* storage,
2006#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2008 validGeneration = storage->currentIteratorGeneration;
2014 return id.getIndex() < slots.size() && (
id.getGeneration() & 1) != 0 && slots[
id.getIndex()].generation ==
id.getGeneration();
2017 std::conditional_t<std::is_const_v<Component>,
const detail::ComponentStorage, detail::ComponentStorage>* storage =
nullptr;
2018 Span<const detail::EntitySlot> slots{};
2019#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2020 SharedPointer<Atomic<size_t>> validGeneration{};
2032template <
typename EntReg>
2042 : registry(registry)
2043 , entityID(entityID) {}
2053 : registry(other.registry)
2054 , entityID(std::exchange(other.entityID, {})) {}
2118 template <
component T,
typename... Args>
2138 template <
component T,
typename... Args>
2160 template <component T>
2182 template <component T>
2194 template <component T>
2294 return std::exchange(entityID,
EntityID{});
2303template <
typename Registry>
2322template <
typename... KnownComponents>
2325 using KnownComponentTypeList =
meta::TypeList<KnownComponents...>;
2343#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2346 currentIteratorGeneration->fetch_add(1);
2360 *
this = std::move(other);
2365 if (
this == &other) {
2369 slots = other.slots;
2370 entityIDs = other.entityIDs;
2371 firstAvailableSlotIndex = other.firstAvailableSlotIndex;
2372 for (
auto&& [typeIndex, storage] : componentPools) {
2373 if (!other.componentPools.contains(typeIndex)) {
2378 for (
const auto& [typeIndex, otherStorage] : other.componentPools) {
2379 componentPools[typeIndex] = otherStorage;
2381 for (
size_t i = 0; i <
sizeof...(KnownComponents); ++i) {
2382 knownComponentPools[i] = other.knownComponentPools[i];
2388 componentPools.clear();
2394#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2395 currentIteratorGeneration->fetch_add(1);
2399#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2400 currentIteratorGeneration->fetch_add(1);
2407 if (
this == &other) {
2410 slots = std::exchange(other.slots, {});
2411 entityIDs = std::exchange(other.entityIDs, {});
2412 firstAvailableSlotIndex = std::exchange(other.firstAvailableSlotIndex,
EntityID::Index{0});
2413 componentPools = std::exchange(other.componentPools, {});
2414 knownComponentPools = std::exchange(other.knownComponentPools, {});
2415#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2416 currentIteratorGeneration->fetch_add(1);
2417 std::swap(currentIteratorGeneration, other.currentIteratorGeneration);
2430 for (
EntityID::Index slotIndex = 0; slotIndex < slots.size(); ++slotIndex) {
2435 slots[slotIndex] = detail::EntitySlot{
2436 .generation = destroyedGeneration,
2437 .inactive{.previousAvailableSlotIndex = previousAvailableSlotIndex, .nextAvailableSlotIndex = nextAvailableSlotIndex},
2442 for (
auto&& [typeIndex, storage] : componentPools) {
2451#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2452 currentIteratorGeneration->fetch_add(1);
2472 throw std::length_error{
"Maximum entity count exceeded."};
2476 EntityID&
id = entityIDs.push_back_unspecified_value();
2482 const size_t oldSlotCount = slots.size();
2483 slots.push_back(detail::EntitySlot{.generation = 1, .active{.idIndex = idIndex, .flags = flags}});
2485 for (
auto&& [typeIndex, storage] : componentPools) {
2487#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2488 storage.currentIteratorGeneration->fetch_add(1);
2491 for (Optional<detail::ComponentStorage>& storage : knownComponentPools) {
2494#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2495 storage->currentIteratorGeneration->fetch_add(1);
2500 for (
auto&& [typeIndex, storage] : componentPools) {
2501 storage.indices.resize(oldSlotCount);
2503 for (Optional<detail::ComponentStorage>& storage : knownComponentPools) {
2505 storage->indices.resize(oldSlotCount);
2512 entityIDs.pop_back();
2515 id =
EntityID{slotIndex, 1, flags};
2518 const EntityID::Index nextAvailableSlotIndex = slots[slotIndex].inactive.nextAvailableSlotIndex;
2520 slots[nextAvailableSlotIndex].inactive.previousAvailableSlotIndex = slotIndex;
2522 firstAvailableSlotIndex = nextAvailableSlotIndex;
2524 slots[slotIndex] = detail::EntitySlot{.generation = generation, .active{.idIndex = idIndex, .flags = flags}};
2525 id =
EntityID{slotIndex, generation, flags};
2527#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2528 currentIteratorGeneration->fetch_add(1);
2553 throw std::invalid_argument{
"Invalid entity handle."};
2557 throw std::logic_error{
"An entity already exists at the index of the given ID."};
2561 throw std::length_error{
"Maximum entity count exceeded."};
2565 entityIDs.push_back(
id);
2570 if (slotIndex >= slots.size()) {
2572 const size_t oldSlotCount = slots.size();
2573 const size_t newSlotCount =
static_cast<size_t>(slotIndex + 1);
2574 slots.reserve(newSlotCount);
2575 for (
size_t newSlotIndex = oldSlotCount; newSlotIndex < newSlotCount - 1; ++newSlotIndex) {
2578 slots.push_back(detail::EntitySlot{
2580 .inactive{.previousAvailableSlotIndex = previousAvailableSlotIndex, .nextAvailableSlotIndex = nextAvailableSlotIndex},
2583 if (newSlotCount - oldSlotCount >= 2) {
2585 slots.back().inactive.nextAvailableSlotIndex = firstAvailableSlotIndex;
2589 slots.push_back(detail::EntitySlot{.generation = generation, .active{.idIndex = idIndex, .flags = flags}});
2591 for (
auto&& [typeIndex, storage] : componentPools) {
2593#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2594 storage.currentIteratorGeneration->fetch_add(1);
2600#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2601 storage->currentIteratorGeneration->fetch_add(1);
2606 for (
auto&& [typeIndex, storage] : componentPools) {
2607 storage.indices.resize(oldSlotCount);
2611 storage->indices.resize(oldSlotCount);
2614 slots.resize(oldSlotCount);
2618 entityIDs.pop_back();
2622 const EntityID::Index previousAvailableSlotIndex = slots[slotIndex].inactive.previousAvailableSlotIndex;
2623 const EntityID::Index nextAvailableSlotIndex = slots[slotIndex].inactive.nextAvailableSlotIndex;
2625 firstAvailableSlotIndex = nextAvailableSlotIndex;
2627 slots[previousAvailableSlotIndex].inactive.nextAvailableSlotIndex = nextAvailableSlotIndex;
2630 slots[nextAvailableSlotIndex].inactive.previousAvailableSlotIndex = previousAvailableSlotIndex;
2632 slots[slotIndex] = detail::EntitySlot{.generation = generation, .active{.idIndex = idIndex, .flags = flags}};
2634#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2635 currentIteratorGeneration->fetch_add(1);
2652 if (slotIndex < slots.size() && (
id.getGeneration() & 1) != 0) {
2653 detail::EntitySlot& slot = slots[slotIndex];
2654 if (slot.generation ==
id.getGeneration()) {
2656 slot.active.flags = newFlags;
2657 const EntityID newEntityID{slotIndex,
id.getGeneration(), newFlags};
2658 GREM_ASSERT(entityIDs[idIndex].getGeneration() ==
id.getGeneration());
2659 entityIDs[idIndex] = newEntityID;
2660 for (
auto&& [typeIndex, storage] : componentPools) {
2663 GREM_ASSERT(storage.entityIDs[componentIndex].getGeneration() ==
id.getGeneration());
2664 storage.entityIDs[componentIndex] = newEntityID;
2665#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2666 storage.currentIteratorGeneration->fetch_add(1);
2674 GREM_ASSERT(storage->entityIDs[componentIndex].getGeneration() ==
id.getGeneration());
2675 storage->entityIDs[componentIndex] = newEntityID;
2676#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2677 storage->currentIteratorGeneration->fetch_add(1);
2682#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2683 currentIteratorGeneration->fetch_add(1);
2728 const EntityID lastEntityID = entityIDs.back();
2730 if (lastSlotIndex != slotIndex) {
2731 slots[lastSlotIndex] = detail::EntitySlot{.generation = lastEntityID.
getGeneration(),
2732 .active{.idIndex = idIndex, .flags = lastEntityID.
getFlags()}};
2733 entityIDs[idIndex] = lastEntityID;
2735 for (
auto&& [typeIndex, storage] : componentPools) {
2736 storage.remove(slotIndex);
2740 storage->remove(slotIndex);
2743 entityIDs.pop_back();
2746 slots[slotIndex] = detail::EntitySlot{.generation =
static_cast<EntityID::Generation>(generation + 1),
2747 .inactive{.previousAvailableSlotIndex =
EntityID::INVALID_INDEX, .nextAvailableSlotIndex = firstAvailableSlotIndex}};
2748 firstAvailableSlotIndex = slotIndex;
2750#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
2751 currentIteratorGeneration->fetch_add(1);
2757 return entityIDs.begin();
2761 return entityIDs.begin();
2769 return entityIDs.end();
2773 return entityIDs.end();
2790 return slotIndex < slots.size() && (
id.getGeneration() & 1) != 0 && slots[slotIndex].generation ==
id.getGeneration();
2802 return slotIndex < slots.size() && (slots[slotIndex].generation & 1) != 0;
2811 return entityIDs.size();
2821 return entityIDs.empty();
2832 static_cast<size_type>(slots.max_size()),
2833 static_cast<size_type>(entityIDs.max_size()),
2860 template <
component T,
typename... Args>
2886 template <
component T,
typename... Args>
2913 template <component T>
2940 template <component T>
2959 template <component T>
2979 for (
auto&& [typeIndex, storage] : componentPools) {
2980 storage.remove(
id.getIndex());
2984 storage->remove(
id.getIndex());
3001 template <component T>
3016 template <component T>
3033 template <component T>
3054 template <component T>
3077 template <component T,
typename U>
3081 return std::forward<U>(defaultValue);
3097 template <component T>
3102 GREM_ASSERT(storage->indices.size() == slots.size());
3103 const EntityID::Index componentIndex = storage->indices[
id.getIndex()];
3105 if constexpr (detail::empty_component<T>) {
3106 return &detail::DUMMY_COMPONENT<T>;
3108 return static_cast<T*
>(storage->array) + componentIndex;
3113 if (
const auto it = componentPools.find(
typeid(T)); it != componentPools.end()) {
3114 detail::ComponentStorage& storage = it->second;
3115 GREM_ASSERT(storage.indices.size() == slots.size());
3116 const EntityID::Index componentIndex = storage.indices[
id.getIndex()];
3118 if constexpr (detail::empty_component<T>) {
3119 return &detail::DUMMY_COMPONENT<T>;
3121 return static_cast<T*
>(storage.array) + componentIndex;
3141 template <component T>
3146 GREM_ASSERT(storage->indices.size() == slots.size());
3147 const EntityID::Index componentIndex = storage->indices[
id.getIndex()];
3149 if constexpr (detail::empty_component<T>) {
3150 return &detail::DUMMY_COMPONENT<const T>;
3152 return static_cast<const T*
>(storage->array) + componentIndex;
3157 if (
const auto it = componentPools.find(
typeid(T)); it != componentPools.end()) {
3158 const detail::ComponentStorage& storage = it->second;
3159 GREM_ASSERT(storage.indices.size() == slots.size());
3160 const EntityID::Index componentIndex = storage.indices[
id.getIndex()];
3162 if constexpr (detail::empty_component<T>) {
3163 return &detail::DUMMY_COMPONENT<const T>;
3165 return static_cast<const T*
>(storage.array) + componentIndex;
3186 GREM_ASSERT((slots[slotIndex].generation & 1) != 0);
3187 return EntityID{slotIndex, slots[slotIndex].generation, slots[slotIndex].active.flags};
3205 template <component T>
3207 if constexpr (detail::empty_component<T>) {
3208 return detail::DUMMY_COMPONENT<T>;
3214 return static_cast<T*
>(storage.array)[componentIndex];
3216 const auto it = componentPools.find(
typeid(T));
3218 detail::ComponentStorage& storage = it->second;
3222 return static_cast<T*
>(storage.array)[componentIndex];
3241 template <component T>
3243 if constexpr (detail::empty_component<T>) {
3244 return detail::DUMMY_COMPONENT<const T>;
3250 return static_cast<const T*
>(storage.array)[componentIndex];
3252 const auto it = componentPools.find(
typeid(T));
3254 const detail::ComponentStorage& storage = it->second;
3258 return static_cast<const T*
>(storage.array)[componentIndex];
3283 template <
typename... ComponentsAndExclusions>
3285 requires(!
meta::type_list_empty_v<typename
Entities<ComponentsAndExclusions...>::MutableComponents>) {
3286 using EntityRange =
Entities<ComponentsAndExclusions...>;
3287 return getEntitiesImplementation<EntityRange>(
typename EntityRange::IncludedComponents{},
typename EntityRange::ExcludedComponents{});
3311 template <
typename... ComponentsAndExclusions>
3313 requires(
meta::type_list_empty_v<typename
Entities<ComponentsAndExclusions...>::MutableComponents>) {
3314 using EntityRange =
Entities<ComponentsAndExclusions...>;
3315 return const_cast<EntityRegistry*
>(
this)->getEntitiesImplementation<EntityRange>(
typename EntityRange::IncludedComponents{},
typename EntityRange::ExcludedComponents{});
3325 template <
typename... ComponentsAndExclusions>
3338 template <
typename... ComponentsAndExclusions>
3372 template <
typename... ComponentsAndExclusions>
3375 using EntityRange =
Entities<ComponentsAndExclusions...>;
3376 return getEntitiesChunkImplementation<EntityRange>(chunkIndex, chunkCount,
typename EntityRange::IncludedComponents{},
typename EntityRange::ExcludedComponents{});
3407 template <
typename... ComponentsAndExclusions>
3410 using EntityRange =
Entities<ComponentsAndExclusions...>;
3411 return const_cast<EntityRegistry*
>(
this)->getEntitiesChunkImplementation<EntityRange>(chunkIndex, chunkCount,
typename EntityRange::IncludedComponents{},
3412 typename EntityRange::ExcludedComponents{});
3430 template <component T>
3435 storage.
emplace(slots.size(), std::in_place_type<T>);
3439 detail::ComponentStorage& storage = componentPools.try_emplace(
typeid(T), slots.size(), std::in_place_type<T>).first->second;
3459 template <component T>
3467 if (
const auto it = componentPools.find(
typeid(T)); it != componentPools.end()) {
3489 template <component T>
3497 if (
const auto it = componentPools.find(
typeid(T)); it != componentPools.end()) {
3512 template <
typename... ComponentsAndExclusions>
3516 const auto entities =
getEntities<ComponentsAndExclusions...>();
3517 const auto first = entities.begin();
3518 const auto last = entities.end();
3519 if (first == last) {
3541 template <
typename Predicate>
3545 while (i < c.entityIDs.size()) {
3546 const EntityID id = c.entityIDs[i];
3547 if (predicate(
id)) {
3558 template <
typename EntityRange,
typename... IncludedComponents,
typename... ExcludedComponents>
3563 Array<
void*, detail::NON_EMPTY_COMPONENT_COUNT<IncludedComponents...>> componentArrays{};
3564#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
3565 Array<detail::IteratorValidity,
sizeof...(ExcludedComponents)> excludedIteratorValidity{};
3566 Array<detail::IteratorValidity,
max(
sizeof...(IncludedComponents),
size_t{1})> componentIteratorValidity{};
3569 [[maybe_unused]]
size_t excludedComponentIndex = 0;
3570 (([&]<
typename Component>(meta::Type<Component>) ->
void {
3571 static_assert(!std::is_const_v<Component>);
3572 using T = Component;
3575 excludedIndexArrays[excludedComponentIndex] = storage->indices.data();
3576#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
3577 excludedIteratorValidity[excludedComponentIndex] = storage->currentIteratorGeneration;
3581 if (
const auto it = componentPools.find(
typeid(T)); it != componentPools.end()) {
3582 excludedIndexArrays[excludedComponentIndex] = it->second.indices.data();
3583#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
3584 excludedIteratorValidity[excludedComponentIndex] = it->second.currentIteratorGeneration;
3588 ++excludedComponentIndex;
3592 [[maybe_unused]]
size_t includedComponentIndex = 0;
3593 [[maybe_unused]]
size_t nonEmptyIncludedComponentIndex = 0;
3594 if constexpr (
sizeof...(IncludedComponents) == 0) {
3595 candidateEntities = entityIDs;
3596#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
3597 componentIteratorValidity.front() = currentIteratorGeneration;
3600 (([&]<
typename Component>(meta::Type<Component>) ->
void {
3601 using T = std::remove_const_t<Component>;
3604 componentIndexArrays[includedComponentIndex] = storage->indices.data();
3605#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
3606 componentIteratorValidity[includedComponentIndex] = storage->currentIteratorGeneration;
3608 if constexpr (!detail::empty_component<Component>) {
3609 componentArrays[nonEmptyIncludedComponentIndex] = storage->array;
3612 if (!candidateEntities || componentEntityIDs.size() < candidateEntities->size()) {
3613 candidateEntities = componentEntityIDs;
3619 if (
const auto it = componentPools.find(
typeid(T)); it != componentPools.end()) {
3620 componentIndexArrays[includedComponentIndex] = it->second.indices.data();
3621#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
3622 componentIteratorValidity[includedComponentIndex] = it->second.currentIteratorGeneration;
3624 if constexpr (!detail::empty_component<Component>) {
3625 componentArrays[nonEmptyIncludedComponentIndex] = it->second.array;
3628 if (!candidateEntities || componentEntityIDs.size() < candidateEntities->size()) {
3629 candidateEntities = componentEntityIDs;
3635 ++includedComponentIndex;
3636 if constexpr (!detail::empty_component<Component>) {
3637 ++nonEmptyIncludedComponentIndex;
3643#if defined(NDEBUG) && !defined(GREM_USE_RELEASE_ASSERTIONS)
3644 return EntityRange{candidateEntities.
value_or(
Span<const EntityID>{}), excludedIndexArrays, componentIndexArrays, componentArrays, slots};
3646 return EntityRange{candidateEntities.
value_or(
Span<const EntityID>{}), excludedIndexArrays, componentIndexArrays, componentArrays, slots, excludedIteratorValidity,
3647 componentIteratorValidity};
3651 template <
typename EntityRange,
typename... IncludedComponents,
typename... ExcludedComponents>
3652 [[nodiscard]] EntityRange getEntitiesChunkImplementation(
size_t chunkIndex,
size_t chunkCount, meta::TypeList<IncludedComponents...>,
3653 meta::TypeList<ExcludedComponents...>)
noexcept {
3655 const size_t candidateCount = result.getCandidateCount();
3657 const size_t chunkSize = (candidateCount + chunkCount - 1) / chunkCount;
3658 const size_t chunkBegin =
min(chunkIndex * chunkSize, candidateCount);
3659 const size_t chunkEnd =
min(chunkBegin + chunkSize, candidateCount);
3660 result.chunkCandidates(chunkBegin, chunkEnd);
3664 Buffer<detail::EntitySlot> slots{};
3665 Buffer<EntityID> entityIDs{};
3667 HashMap<std::type_index, detail::ComponentStorage> componentPools{};
3669#if !defined(NDEBUG) || defined(GREM_USE_RELEASE_ASSERTIONS)
3670 SharedPointer<Atomic<size_t>> currentIteratorGeneration = SharedPointer<Atomic<size_t>>::create();
3674template <
typename EntReg>
3677 registry.destroyEntity(entityID);
3681template <
typename EntReg>
3683 entityID = registry.setEntityFlags(entityID, newFlags);
3687template <
typename EntReg>
3690 return registry.template
addComponent<T>(entityID, std::forward<Args>(args)...);
3693template <
typename EntReg>
3699template <
typename EntReg>
3700template <component T>
3705template <
typename EntReg>
3706template <component T>
3711template <
typename EntReg>
3712template <component T>
3717template <
typename EntReg>
3719 return registry.removeAllComponents(entityID);
3722template <
typename EntReg>
3723template <component T>
3728template <
typename EntReg>
3729template <component T>
3734template <
typename EntReg>
3735template <component T>
3740template <
typename EntReg>
3741template <component T,
typename U>
3743 return registry.template
getComponentOr<T>(entityID, std::forward<U>(defaultValue));
3746template <
typename EntReg>
3747template <component T>
3752template <
typename EntReg>
3753template <component T>
3761struct std::hash<
grem::execution::EntityID> {
3767 [[no_unique_address]] std::hash<typename grem::execution::EntityID::Identifier> hasher;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
const_pointer const_iterator
Definition Buffer.hpp:43
pointer iterator
Definition Buffer.hpp:42
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
constexpr T value_or(U &&defaultValue) const &
Get the underlying value, or a fallback value in case the optional is empty.
Definition Optional.hpp:453
constexpr T & emplace(Args &&... args)
Construct a new underlying value, destroying the old value if present.
Definition Optional.hpp:706
Tightly packed set of elements with stable IDs, supporting fast iteration as well as amortized O(1) i...
Definition Registry.hpp:175
constexpr detail::subspan_type_t< T, EXTENT, Offset, N > subspan() const
Definition Span.hpp:284
Reference to an EntityRegistry's pool of instances of a specific component type.
Definition EntityRegistry.hpp:1713
typename iterator::difference_type difference_type
Definition EntityRegistry.hpp:1720
GREM_ALWAYS_INLINE bool remove(EntityID id) noexcept
Erase the component instance associated with a specific entity.
Definition EntityRegistry.hpp:1972
detail::SingleComponentIterator< Component > iterator
Definition EntityRegistry.hpp:1715
typename iterator::reference reference
Definition EntityRegistry.hpp:1718
GREM_ALWAYS_INLINE size_type removeFromAllEntities() noexcept
Erase all associated component instances.
Definition EntityRegistry.hpp:1986
friend class ComponentPool
Definition EntityRegistry.hpp:2000
GREM_ALWAYS_INLINE constexpr reference operator[](EntityID id) const
Definition EntityRegistry.hpp:1753
typename iterator::value_type value_type
Definition EntityRegistry.hpp:1717
GREM_ALWAYS_INLINE constexpr ComponentPool() noexcept=default
Construct an invalid component pool reference.
GREM_ALWAYS_INLINE Component & get(EntityID id) const
Get the component instance associated with a specific entity.
Definition EntityRegistry.hpp:1791
GREM_ALWAYS_INLINE Component & addOrAssign(EntityID id, Component &&value)
Add a component instance associated with a specific entity, or assign to it if it already has one.
Definition EntityRegistry.hpp:1929
size_t size_type
Definition EntityRegistry.hpp:1719
GREM_ALWAYS_INLINE constexpr sentinel end() const noexcept
Definition EntityRegistry.hpp:1749
GREM_ALWAYS_INLINE Component * addIfMissing(EntityID id, Args &&... args)
Add a component instance associated with a specific entity if it doesn't already have one.
Definition EntityRegistry.hpp:1899
Component component_type
Definition EntityRegistry.hpp:1721
GREM_ALWAYS_INLINE Component & addOrAssign(EntityID id, const Component &value)
Add a component instance associated with a specific entity, or assign to it if it already has one.
Definition EntityRegistry.hpp:1956
GREM_ALWAYS_INLINE constexpr bool has(EntityID id) const
Check if the referenced pool has a component instance associated with a specific entity.
Definition EntityRegistry.hpp:1772
detail::SingleComponentSentinel sentinel
Definition EntityRegistry.hpp:1716
GREM_ALWAYS_INLINE Component & add(EntityID id, Args &&... args)
Add a component instance associated with a specific entity.
Definition EntityRegistry.hpp:1869
GREM_ALWAYS_INLINE Component * find(EntityID id) const
Try to get the component instance associated with a specific entity.
Definition EntityRegistry.hpp:1832
GREM_ALWAYS_INLINE Component getOr(EntityID id, U &&defaultValue) const
Get the component instance associated with a specific entity, or a default value in case the entity d...
Definition EntityRegistry.hpp:1815
GREM_ALWAYS_INLINE constexpr iterator begin() const noexcept
Definition EntityRegistry.hpp:1738
friend class EntityRegistry
Entity registry used by a Simulation.
Definition EntityRegistry.hpp:1997
ptrdiff_t difference_type
Definition EntityRegistry.hpp:1139
GREM_ALWAYS_INLINE constexpr size_t getCandidateCount() const noexcept
Get an upper bound estimate on the number of entities in the range, which is equal to the exact numbe...
Definition EntityRegistry.hpp:1220
typename iterator::reference reference
Definition EntityRegistry.hpp:1137
GREM_ALWAYS_INLINE constexpr reference operator[](EntityID id) const
Definition EntityRegistry.hpp:1243
GREM_ALWAYS_INLINE auto & getComponent(EntityID id) const
Get a specific component of a specific entity in the range.
Definition EntityRegistry.hpp:1278
detail::SingleComponentIterator< Component > iterator
Definition EntityRegistry.hpp:1134
meta::type_list_concat_t< typename detail::entities_extract_components< ComponentsAndExclusions >::ExcludedComponents... > ExcludedComponents
List of components excluded by the range, provided as a meta::TypeList.
Definition EntityRegistry.hpp:1126
GREM_ALWAYS_INLINE constexpr Entities() noexcept=default
Construct an empty entity range.
GREM_ALWAYS_INLINE bool hasComponent(EntityID id) const noexcept
Check if the range contains a specific entity with a specific component.
Definition EntityRegistry.hpp:1260
GREM_ALWAYS_INLINE constexpr bool empty() const noexcept
Definition EntityRegistry.hpp:1210
typename iterator::value_type value_type
Definition EntityRegistry.hpp:1136
GREM_ALWAYS_INLINE constexpr size_type size() const noexcept
Definition EntityRegistry.hpp:1206
GREM_ALWAYS_INLINE constexpr Component * data() const noexcept
Definition EntityRegistry.hpp:1202
GREM_ALWAYS_INLINE auto & getComponentAtEntityIndexUnsafe(EntityID::Index slotIndex) const
Get a specific component of the entity in a specific slot of the underlying EntityRegistry.
Definition EntityRegistry.hpp:1366
GREM_ALWAYS_INLINE constexpr Entities(const iterator &first, const iterator &last) noexcept
Construct an entity subrange from an iterator pair.
Definition EntityRegistry.hpp:1172
GREM_ALWAYS_INLINE EntityID getEntityIDAtEntityIndexUnsafe(EntityID::Index slotIndex) const
Get the entity handle of the entity in a specific slot of the underlying EntityRegistry.
Definition EntityRegistry.hpp:1343
size_t size_type
Definition EntityRegistry.hpp:1138
GREM_ALWAYS_INLINE auto * findComponent(EntityID id) const noexcept
Try to get a specific component of a specific entity in the range.
Definition EntityRegistry.hpp:1324
detail::SingleComponentSentinel sentinel
Definition EntityRegistry.hpp:1135
GREM_ALWAYS_INLINE constexpr sentinel end() const noexcept
Definition EntityRegistry.hpp:1198
meta::type_list_concat_t< typename detail::entities_extract_components< ComponentsAndExclusions >::IncludedComponents... > IncludedComponents
List of components required by the range, provided as a meta::TypeList.
Definition EntityRegistry.hpp:1123
GREM_ALWAYS_INLINE constexpr bool containsEntity(EntityID id) const
Check if the range contains a specific entity.
Definition EntityRegistry.hpp:1232
GREM_ALWAYS_INLINE constexpr iterator begin() const noexcept
Definition EntityRegistry.hpp:1184
meta::type_list_concat_t< typename detail::entities_extract_components< ComponentsAndExclusions >::MutableComponents... > MutableComponents
List of non-const components required by the range, provided as a meta::TypeList.
Definition EntityRegistry.hpp:1117
meta::type_list_concat_t< typename detail::entities_extract_components< ComponentsAndExclusions >::ImmutableComponents... > ImmutableComponents
List of const components required by the range, provided as a meta::TypeList.
Definition EntityRegistry.hpp:1120
GREM_ALWAYS_INLINE auto getComponentOr(EntityID id, U &&defaultValue) const
Get a specific component of a specific entity in the range, with a default value in case the entity d...
Definition EntityRegistry.hpp:1303
friend class EntityRegistry
Entity registry used by a Simulation.
Definition EntityRegistry.hpp:1380
View of entities in an EntityRegistry that have all of the required components and none of the exclud...
Definition EntityRegistry.hpp:815
detail::MultiComponentIterator< IncludedComponents, ExcludedComponents > iterator
Definition EntityRegistry.hpp:835
typename iterator::reference reference
Definition EntityRegistry.hpp:838
GREM_ALWAYS_INLINE constexpr bool containsEntity(EntityID id) const
Check if the range contains a specific entity.
Definition EntityRegistry.hpp:900
GREM_ALWAYS_INLINE constexpr size_type getCandidateCount() const noexcept
Get an upper bound estimate on the number of entities in the range.
Definition EntityRegistry.hpp:888
ptrdiff_t difference_type
Definition EntityRegistry.hpp:840
GREM_ALWAYS_INLINE constexpr Entities() noexcept=default
Construct an empty entity range.
typename iterator::value_type value_type
Definition EntityRegistry.hpp:837
meta::type_list_concat_t< typename detail::entities_extract_components< ComponentsAndExclusions >::ExcludedComponents... > ExcludedComponents
List of components excluded by the range, provided as a meta::TypeList.
Definition EntityRegistry.hpp:827
GREM_ALWAYS_INLINE constexpr iterator begin() const noexcept
Definition EntityRegistry.hpp:869
GREM_ALWAYS_INLINE auto getComponentOr(EntityID id, U &&defaultValue) const
Get a specific component of a specific entity in the range, or a default value in case the entity doe...
Definition EntityRegistry.hpp:975
GREM_ALWAYS_INLINE auto * findComponent(EntityID id) const noexcept
Try to get a specific component of a specific entity in the range.
Definition EntityRegistry.hpp:996
meta::type_list_concat_t< typename detail::entities_extract_components< ComponentsAndExclusions >::MutableComponents... > MutableComponents
List of non-const components required by the range, provided as a meta::TypeList.
Definition EntityRegistry.hpp:818
GREM_ALWAYS_INLINE constexpr reference operator[](EntityID id) const
Definition EntityRegistry.hpp:912
GREM_ALWAYS_INLINE bool hasComponent(EntityID id) const noexcept
Check if the range contains a specific entity with a specific component.
Definition EntityRegistry.hpp:932
GREM_ALWAYS_INLINE EntityID getEntityIDAtEntityIndexUnsafe(EntityID::Index slotIndex) const
Get the entity handle of the entity in a specific slot of the underlying EntityRegistry.
Definition EntityRegistry.hpp:1015
meta::type_list_concat_t< typename detail::entities_extract_components< ComponentsAndExclusions >::IncludedComponents... > IncludedComponents
List of components required by the range, provided as a meta::TypeList.
Definition EntityRegistry.hpp:824
meta::type_list_concat_t< typename detail::entities_extract_components< ComponentsAndExclusions >::ImmutableComponents... > ImmutableComponents
List of const components required by the range, provided as a meta::TypeList.
Definition EntityRegistry.hpp:821
GREM_ALWAYS_INLINE auto & getComponent(EntityID id) const
Get a specific component of a specific entity in the range.
Definition EntityRegistry.hpp:950
detail::MultiComponentSentinel sentinel
Definition EntityRegistry.hpp:836
GREM_ALWAYS_INLINE auto & getComponentAtEntityIndexUnsafe(EntityID::Index slotIndex) const
Get a specific component of the entity in a specific slot of the underlying EntityRegistry.
Definition EntityRegistry.hpp:1038
GREM_ALWAYS_INLINE constexpr sentinel end() const noexcept
Definition EntityRegistry.hpp:878
size_t size_type
Definition EntityRegistry.hpp:839
friend class EntityRegistry
Entity registry used by a Simulation.
Definition EntityRegistry.hpp:1054
Temporary reference to an incomplete entity that is currently being built inside of an EntityRegistry...
Definition EntityRegistry.hpp:2033
GREM_ALWAYS_INLINE constexpr EntityBuilder(EntReg ®istry, EntityID entityID) noexcept
Construct an entity builder.
Definition EntityRegistry.hpp:2041
GREM_ALWAYS_INLINE EntityID build()
Definition EntityRegistry.hpp:2293
T getComponentOr(U &&defaultValue) const
T & addComponent(Args &&... args)
T * findComponent() noexcept
GREM_ALWAYS_INLINE EntityID getEntityID() const noexcept
Definition EntityRegistry.hpp:2096
T & addOrAssignComponent(const T &value)
GREM_ALWAYS_INLINE const EntityRegistry< N > & getRegistry() const noexcept
Definition EntityRegistry.hpp:2087
EntityBuilder & operator=(EntityBuilder &&)=delete
T & addOrAssignComponent(T &&value)
EntityID setEntityFlags(EntityID::Flags newFlags)
T * addComponentIfMissing(Args &&... args)
GREM_ALWAYS_INLINE EntityRegistry< N > & getRegistry() noexcept
Definition EntityRegistry.hpp:2078
bool hasComponent() const noexcept
EntityBuilder(EntityBuilder &&other) noexcept
Definition EntityRegistry.hpp:2052
bool removeComponent() noexcept
EntityBuilder(const EntityBuilder &)=delete
EntityBuilder & operator=(const EntityBuilder &)=delete
bool removeAllComponents() noexcept
Handle to a specific entity in a registry.
Definition EntityRegistry.hpp:71
static constexpr Index INVALID_INDEX
Reserved index value for an invalid entity handle.
Definition EntityRegistry.hpp:78
GREM_ALWAYS_INLINE constexpr Generation getGeneration() const noexcept
Get the generation counter value of the entity.
Definition EntityRegistry.hpp:134
GREM_ALWAYS_INLINE constexpr Index getIndex() const noexcept
Get the index of the entity's generation slot.
Definition EntityRegistry.hpp:125
uint64_t Identifier
Underlying value type.
Definition EntityRegistry.hpp:73
GREM_ALWAYS_INLINE constexpr Identifier getIdentifier() const noexcept
Get a unique identifier for the underlying representation of the entity handle.
Definition EntityRegistry.hpp:116
static constexpr Flags ALL_FLAGS
Maximum valid set of user-defined flags.
Definition EntityRegistry.hpp:81
GREM_ALWAYS_INLINE constexpr Flags getFlags() const noexcept
Get the user-defined flags of the entity.
Definition EntityRegistry.hpp:143
static constexpr Index MAX_INDEX
Maximum valid index value.
Definition EntityRegistry.hpp:79
uint16_t Flags
User-defined flags type.
Definition EntityRegistry.hpp:76
GREM_ALWAYS_INLINE constexpr std::strong_ordering operator<=>(const EntityID &other) const noexcept=default
Compare this handle to another.
uint16_t Generation
Generation counter type.
Definition EntityRegistry.hpp:75
GREM_ALWAYS_INLINE constexpr bool operator==(const EntityID &other) const noexcept=default
Compare this handle to another for equality.
GREM_ALWAYS_INLINE constexpr EntityID() noexcept=default
Construct an invalid entity handle.
static constexpr Generation MAX_GENERATION
Maximum valid generation counter value.
Definition EntityRegistry.hpp:80
uint32_t Index
Index type.
Definition EntityRegistry.hpp:74
Dynamic container of entities with stable EntityID handles and associated component instances,...
Definition EntityRegistry.hpp:2323
GREM_ALWAYS_INLINE const_iterator cend() const noexcept
Definition EntityRegistry.hpp:2776
GREM_ALWAYS_INLINE const T * findComponent(EntityID id) const noexcept
Try to get a specific component of an entity.
Definition EntityRegistry.hpp:3142
EntityRegistry & operator=(const EntityRegistry &other)
Copy assignment.
Definition EntityRegistry.hpp:2364
EntityRegistry(EntityRegistry &&other) noexcept
Move constructor.
Definition EntityRegistry.hpp:2359
GREM_ALWAYS_INLINE const_iterator cbegin() const noexcept
Definition EntityRegistry.hpp:2764
EntityID & reference
Definition EntityRegistry.hpp:2331
GREM_ALWAYS_INLINE bool containsEntityAtIndex(EntityID::Index slotIndex) const
Check if the registry contains an entity in a specific slot.
Definition EntityRegistry.hpp:2801
GREM_ALWAYS_INLINE T & addOrAssignComponent(EntityID id, T &&value)
Add a component to an entity, or assign to it if it already has one.
Definition EntityRegistry.hpp:2914
GREM_ALWAYS_INLINE bool removeComponent(EntityID id) noexcept
Erase a component from an entity.
Definition EntityRegistry.hpp:2960
GREM_ALWAYS_INLINE T * findComponent(EntityID id) noexcept
Try to get a specific component of an entity.
Definition EntityRegistry.hpp:3098
GREM_ALWAYS_INLINE const_iterator end() const noexcept
Definition EntityRegistry.hpp:2772
EntityBuilder< EntityRegistry > createEntity(EntityID::Flags flags={})
Create a new entity.
Definition EntityRegistry.hpp:2470
EntityRegistry() noexcept=default
Construct an empty entity registry.
GREM_ALWAYS_INLINE T * addComponentIfMissing(EntityID id, Args &&... args)
Add a component to an entity if it doesn't already have it.
Definition EntityRegistry.hpp:2887
size_type GREM_ALWAYS_INLINE removeComponentFromAllEntities() noexcept
Erase a specific component from all entities.
Definition EntityRegistry.hpp:3002
GREM_ALWAYS_INLINE iterator end() noexcept
Definition EntityRegistry.hpp:2768
friend size_type erase_if(EntityRegistry &c, Predicate predicate)
Destroy all entities in an entity registry whose entity handles match a predicate,...
Definition EntityRegistry.hpp:3542
GREM_ALWAYS_INLINE const T & getComponent(EntityID id) const
Get a specific component of an entity.
Definition EntityRegistry.hpp:3055
typename Buffer< EntityID >::const_iterator const_iterator
Definition EntityRegistry.hpp:2338
size_type destroyEntities() noexcept
Destroy all entities that have all of the required components and none of the excluded components,...
Definition EntityRegistry.hpp:3513
GREM_ALWAYS_INLINE T & addComponent(EntityID id, Args &&... args)
Add a component to an entity.
Definition EntityRegistry.hpp:2861
size_t size_type
Definition EntityRegistry.hpp:2335
GREM_ALWAYS_INLINE Entities< ComponentsAndExclusions... > getEntitiesChunk(size_t chunkIndex, size_t chunkCount) const noexcept
Get a specific chunk of the entites in the registry that have all of the required components and none...
Definition EntityRegistry.hpp:3408
GREM_ALWAYS_INLINE bool empty() const noexcept
Check if the registry is empty.
Definition EntityRegistry.hpp:2820
ptrdiff_t difference_type
Definition EntityRegistry.hpp:2336
EntityID setEntityFlags(EntityID id, EntityID::Flags newFlags)
Update the flags embedded in an entity's handle.
Definition EntityRegistry.hpp:2650
void clear() noexcept
Destroy all entities and erase all of their associated components from the registry.
Definition EntityRegistry.hpp:2426
GREM_ALWAYS_INLINE const T & getComponentAtEntityIndexUnsafe(EntityID::Index slotIndex) const
Get a specific component of the entity in a specific slot.
Definition EntityRegistry.hpp:3242
GREM_ALWAYS_INLINE EntityID getEntityIDAtEntityIndexUnsafe(EntityID::Index slotIndex) const
Get the entity handle of the entity in a specific slot.
Definition EntityRegistry.hpp:3185
const EntityID & const_reference
Definition EntityRegistry.hpp:2332
GREM_ALWAYS_INLINE iterator begin() noexcept
Definition EntityRegistry.hpp:2756
GREM_ALWAYS_INLINE T & getComponentAtEntityIndexUnsafe(EntityID::Index slotIndex)
Get a specific component of the entity in a specific slot.
Definition EntityRegistry.hpp:3206
GREM_ALWAYS_INLINE size_type size() const noexcept
Get the number of valid entity handles currently in the registry.
Definition EntityRegistry.hpp:2810
~EntityRegistry()
Destructor.
Definition EntityRegistry.hpp:2345
GREM_ALWAYS_INLINE ComponentPool< T > getComponentPool() noexcept
Get a reference to a specific component pool if it exists.
Definition EntityRegistry.hpp:3460
GREM_ALWAYS_INLINE T & addOrAssignComponent(EntityID id, const T &value)
Add a component to an entity, or assign to it if it already has one.
Definition EntityRegistry.hpp:2941
GREM_ALWAYS_INLINE const_iterator begin() const noexcept
Definition EntityRegistry.hpp:2760
const EntityID * const_pointer
Definition EntityRegistry.hpp:2334
EntityRegistry & operator=(EntityRegistry &&other) noexcept
Move assignment.
Definition EntityRegistry.hpp:2406
GREM_ALWAYS_INLINE bool hasComponent(EntityID id) const noexcept
Check if an entity has a specific component.
Definition EntityRegistry.hpp:3017
EntityID value_type
Definition EntityRegistry.hpp:2330
EntityRegistry(const EntityRegistry &other)
Copy constructor.
Definition EntityRegistry.hpp:2354
GREM_ALWAYS_INLINE Entities< ComponentsAndExclusions... > getEntities() const noexcept
Get a view of all entites in the registry that have all of the required components and none of the ex...
Definition EntityRegistry.hpp:3312
GREM_ALWAYS_INLINE bool destroyEntity(EntityID id) noexcept
Destroy an entity and all of its associated components.
Definition EntityRegistry.hpp:2699
GREM_ALWAYS_INLINE T getComponentOr(EntityID id, U &&defaultValue) const
Get a specific component of an entity, or a default value in case the entity doesn't exist or doesn't...
Definition EntityRegistry.hpp:3078
GREM_ALWAYS_INLINE bool containsEntity(EntityID id) const noexcept
Check if the registry contains a specific entity.
Definition EntityRegistry.hpp:2788
GREM_ALWAYS_INLINE Entities< ComponentsAndExclusions... > getEntities() noexcept
Get a view of all entites in the registry that have all of the required components and none of the ex...
Definition EntityRegistry.hpp:3284
GREM_ALWAYS_INLINE Entities< ComponentsAndExclusions... > getEntitiesChunk(size_t chunkIndex, size_t chunkCount) noexcept
Get a specific chunk of the entites in the registry that have all of the required components and none...
Definition EntityRegistry.hpp:3373
EntityBuilder< EntityRegistry > createEntityAtID(EntityID id)
Create a new entity with a specific entity handle.
Definition EntityRegistry.hpp:2551
GREM_ALWAYS_INLINE size_type max_size() const noexcept
Get the maximum number of simultaneous valid entity handles that the registry can contain.
Definition EntityRegistry.hpp:2830
typename Buffer< EntityID >::iterator iterator
Definition EntityRegistry.hpp:2337
GREM_ALWAYS_INLINE T & getComponent(EntityID id)
Get a specific component of an entity.
Definition EntityRegistry.hpp:3034
GREM_ALWAYS_INLINE ComponentPool< const T > getComponentPool() const noexcept
Get a reference to a specific component pool if it exists.
Definition EntityRegistry.hpp:3490
GREM_ALWAYS_INLINE bool removeAllComponents(EntityID id) noexcept
Erase all components from an entity.
Definition EntityRegistry.hpp:2975
GREM_ALWAYS_INLINE ComponentPool< T > createComponentPool()
Ensure that the component pool for a specific component type has been created, and get a reference to...
Definition EntityRegistry.hpp:3431
bool destroyEntityAtIndex(EntityID::Index slotIndex) noexcept
Destroy the entity in a specific slot and all of its associated components.
Definition EntityRegistry.hpp:2718
Concept that checks if a given type is a valid component type that can be stored and associated with ...
Definition component.hpp:21
Definition concepts.hpp:106
Definition concepts.hpp:35
@ T
't'
Definition keyboard.hpp:90
@ I
'i'
Definition keyboard.hpp:79
EntityBuilder(Registry &, EntityID) -> EntityBuilder< Registry >
constexpr decltype(auto) get(EntityTable< Row, Allocator > &t, std::size_t rowIndex)
Definition EntityTable.hpp:518
execution::EntityID EntityID
Opaque handle to a specific entity in a Simulation's EntityRegistry.
Definition EntityID.hpp:16
Array(T, Us...) -> Array< T, 1+sizeof...(Us)>
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234
GREM_ALWAYS_INLINE constexpr decltype(auto) clamp(T &&value, Low &&minValue, High &&maxValue)
Clamp a value to a specific interval.
Definition fundamentals.hpp:630
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
Tuple(Ts...) -> Tuple< Ts... >
constexpr Offset2D operator*(Offset2D a, int32_t b) noexcept
Definition extents.hpp:692
Span(R &&range) -> Span< std::remove_reference_t< decltype(*std::data(range))> >
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
static constexpr T MAX
Definition fundamentals.hpp:29
std::size_t operator()(const grem::execution::EntityID &entityID) const
Definition EntityRegistry.hpp:3762