2#include "gaia/config/config.h"
7#include "gaia/core/hashing_policy.h"
8#include "gaia/core/utility.h"
9#include "gaia/ecs/id.h"
10#include "gaia/mem/data_layout_policy.h"
11#include "gaia/meta/type_info.h"
19 using ComponentVersion = uint32_t;
20 using ChunkDataVersionOffset = uint8_t;
21 using CompOffsetMappingIndex = uint8_t;
22 using ChunkDataOffset = uint16_t;
23 using ComponentLookupHash = core::direct_hash_key<uint64_t>;
24 using EntitySpan = std::span<const Entity>;
25 using EntitySpanMut = std::span<Entity>;
26 using ComponentSpan = std::span<const Component>;
27 using ChunkDataOffsetSpan = std::span<const ChunkDataOffset>;
28 using SortComponentCond = core::is_smaller<Entity>;
33 GAIA_NODISCARD
constexpr bool component_uses_table_storage(Component component)
noexcept {
34 return component.size() != 0U && (component.storage_type() != DataStorageType::Sparse || component.soa() != 0U);
40 GAIA_NODISCARD
constexpr bool component_uses_sparse_storage(Component component)
noexcept {
41 return component.size() != 0U && component.storage_type() == DataStorageType::Sparse && component.soa() == 0U;
49 GAIA_NODISCARD
constexpr Component archetype_component([[maybe_unused]] Entity term, Component component)
noexcept {
55 template <
typename,
typename =
void>
56 struct auto_storage_policy_inter {
57 static constexpr DataStorageType data_storage_type = DataStorageType::Table;
60 struct auto_storage_policy_inter<T, std::void_t<decltype(T::gaia_Data_Storage)>> {
61 static constexpr DataStorageType data_storage_type = T::gaia_Data_Storage;
69 inline constexpr DataStorageType auto_storage_policy_v = detail::auto_storage_policy_inter<T>::data_storage_type;
73 template <
typename T,
bool IsEntity = std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, Entity>>
74 struct uses_ct_sparse_storage: std::false_type {};
77 struct uses_ct_sparse_storage<T, false> {
78 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
79 using FT =
typename component_type_t<Arg>::TypeFull;
80 using U =
typename actual_type_t<Arg>::Type;
82 static constexpr bool value =
83 !mem::is_soa_layout_v<U> &&
84 (auto_storage_policy_v<U> == DataStorageType::Sparse ||
85 (auto_storage_policy_v<U> == DataStorageType::DontFragment && !std::is_empty_v<U>));
88 template <
typename T,
bool IsEntity = std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, Entity>>
89 struct uses_ct_dont_fragment: std::false_type {};
92 struct uses_ct_dont_fragment<T, false> {
93 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
94 using U =
typename actual_type_t<Arg>::Type;
96 static constexpr bool value = auto_storage_policy_v<U> == DataStorageType::DontFragment;
105 template <
typename T>
106 inline constexpr bool uses_ct_sparse_storage_v = detail::uses_ct_sparse_storage<T>::value;
112 template <
typename T>
113 inline constexpr bool uses_ct_dont_fragment_v = detail::uses_ct_dont_fragment<T>::value;
121 template <
typename T>
122 struct is_component_size_valid: std::bool_constant<sizeof(T) < Component::MaxComponentSizeInBytes> {};
124 template <typename T>
125 struct is_component_type_valid:
128 (!mem::is_soa_layout_v<T> || std::is_trivially_copyable_v<T>)> {};
137 template <typename T>
138 constexpr void verify_comp() {
139 using U = typename actual_type_t<T>::TypeOriginal;
144 "Components have to be \"raw\" types - no arrays, no const, reference, pointer or volatile");
145 static_assert(detail::is_component_type_valid<U>::value, "SoA components must be trivially copyable");
152 template <typename Container>
153 GAIA_NODISCARD constexpr ComponentLookupHash calc_lookup_hash(Container arr) noexcept {
154 constexpr auto arrSize = arr.size();
155 if constexpr (arrSize == 0) {
158 ComponentLookupHash::Type hash = arr[0];
159 core::each<arrSize - 1>([&hash, &arr](auto i) {
160 hash = core::hash_combine(hash, arr[i + 1]);
166 template <typename = void, typename...>
167 constexpr ComponentLookupHash calc_lookup_hash() noexcept;
169 template <typename T, typename... Rest>
170 GAIA_NODISCARD constexpr ComponentLookupHash calc_lookup_hash() noexcept {
171 if constexpr (sizeof...(Rest) == 0)
172 return {meta::type_info::hash<T>()};
174 return {core::hash_combine(meta::type_info::hash<T>(), meta::type_info::hash<Rest>()...)};
178 GAIA_NODISCARD constexpr ComponentLookupHash calc_lookup_hash() noexcept {
185 GAIA_NODISCARD inline ComponentLookupHash calc_lookup_hash(EntitySpan comps) noexcept {
186 const auto compsSize = comps.size();
190 auto hash = core::calculate_hash64(comps[0].value());
191 GAIA_FOR2(1, compsSize) {
192 hash = core::hash_combine(hash, core::calculate_hash64(comps[i].value()));
202 template <uint32_t MAX_COMPONENTS>
203 GAIA_NODISCARD inline uint32_t comp_idx(const Entity* pComps, Entity entity) {
206 GAIA_FOR(MAX_COMPONENTS) {
207 if (pComps[i] == entity)
219 GAIA_NODISCARD inline uint32_t comp_idx(std::span<const Entity> comps, Entity entity) {
222 const auto cnt = (uint32_t)comps.size();
224 if (comps[i] == entity)