2#include "gaia/config/config.h"
8#include "gaia/cnt/darray.h"
9#include "gaia/cnt/map.h"
10#include "gaia/core/hashing_string.h"
11#include "gaia/ecs/component.h"
12#include "gaia/ecs/component_cache_item.h"
13#include "gaia/ecs/component_desc.h"
14#include "gaia/ecs/id.h"
15#include "gaia/meta/type_info.h"
16#include "gaia/util/logging.h"
26 static constexpr uint32_t FastComponentCacheSize = 512;
45 for (
const auto* pItem: m_itemArr)
47 for (
auto [componentId, pItem]: m_itemByDescId)
51 m_itemByDescId.clear();
52 m_compByString.clear();
53 m_compByEntity.clear();
59 m_itemArr.reserve(FastComponentCacheSize);
76 GAIA_ASSERT(!entity.pair());
81 if (compDescId < FastComponentCacheSize) {
83 const auto* pItem = ComponentCacheItem::create<T>(entity);
84 GAIA_ASSERT(compDescId == pItem->comp.id());
85 m_itemArr[compDescId] = pItem;
86 m_compByString.emplace(pItem->name, pItem);
87 m_compByEntity.emplace(pItem->entity, pItem);
91 if GAIA_UNLIKELY (compDescId >= m_itemArr.size()) {
92 const auto oldSize = m_itemArr.size();
93 const auto newSize = compDescId + 1U;
96 constexpr uint32_t CapacityIncreaseSize = 128;
97 const auto newCapacity = ((newSize / CapacityIncreaseSize) * CapacityIncreaseSize) + CapacityIncreaseSize;
98 m_itemArr.reserve(newCapacity);
101 m_itemArr.resize(newSize);
104 GAIA_FOR2(oldSize, newSize - 1) m_itemArr[i] =
nullptr;
109 if GAIA_UNLIKELY (m_itemArr[compDescId] ==
nullptr) {
113 return *m_itemArr[compDescId];
119 const auto* pItem = ComponentCacheItem::create<T>(entity);
120 GAIA_ASSERT(compDescId == pItem->comp.id());
121 m_itemByDescId.emplace(compDescId, pItem);
122 m_compByString.emplace(pItem->name, pItem);
123 m_compByEntity.emplace(pItem->entity, pItem);
127 const auto it = m_itemByDescId.find(compDescId);
128 if (it == m_itemByDescId.end())
140 if (compDescId < FastComponentCacheSize) {
141 if (compDescId >= m_itemArr.size())
144 return m_itemArr[compDescId];
148 const auto it = m_itemByDescId.find(compDescId);
149 return it != m_itemByDescId.end() ? it->second :
nullptr;
158 if (compDescId < FastComponentCacheSize) {
159 GAIA_ASSERT(compDescId < m_itemArr.size());
160 return *m_itemArr[compDescId];
164 GAIA_ASSERT(m_itemByDescId.contains(compDescId));
165 return *m_itemByDescId.find(compDescId)->second;
172 GAIA_ASSERT(!entity.pair());
174 if (it != m_compByEntity.end())
185 GAIA_ASSERT(!entity.pair());
186 const auto* pItem = find(entity);
187 GAIA_ASSERT(pItem !=
nullptr);
196 GAIA_ASSERT(name !=
nullptr);
198 const auto l = len == 0 ? (uint32_t)strnlen(name, ComponentCacheItem::MaxNameLength) : len;
199 GAIA_ASSERT(l < ComponentCacheItem::MaxNameLength);
202 if (it != m_compByString.end())
214 const auto* pItem = find(name, len);
215 GAIA_ASSERT(pItem !=
nullptr);
222 template <
typename T>
226 return find(compDescId);
232 template <
typename T>
236 return get(compDescId);
244 static ComponentCacheItem::Hooks& hooks(
const ComponentCacheItem& cacheItem)
noexcept {
251 const auto registeredTypes = m_itemArr.size();
252 GAIA_LOG_N(
"Registered components: %u", registeredTypes);
254 auto logDesc = [](
const ComponentCacheItem& item) {
256 " hash:%016" PRIx64
", size:%3u B, align:%3u B, [%u:%u] %s [%s]", item.hashLookup.hash,
257 item.comp.size(), item.comp.alig(), item.entity.id(), item.entity.gen(), item.name.str(),
258 EntityKindString[item.entity.kind()]);
260 for (
const auto* pItem: m_itemArr) {
261 if (pItem ==
nullptr)
265 for (
auto [componentId, pItem]: m_itemByDescId)
Array with variable size of elements of type.
Definition darray_impl.h:25
Cache for compile-time defined components.
Definition component_cache.h:23
GAIA_NODISCARD const ComponentCacheItem & get(Entity entity) const noexcept
Returns the component cache item.
Definition component_cache.h:184
GAIA_NODISCARD const ComponentCacheItem & get(const char *name, uint32_t len=0) const noexcept
Returns the component cache item. The provided string is NOT copied internally.
Definition component_cache.h:213
GAIA_NODISCARD const ComponentCacheItem & get() const noexcept
Returns the component item for.
Definition component_cache.h:233
GAIA_NODISCARD const ComponentCacheItem * find(const char *name, uint32_t len=0) const noexcept
Searches for the component cache item. The provided string is NOT copied internally.
Definition component_cache.h:195
GAIA_NODISCARD const ComponentCacheItem * find(detail::ComponentDescId compDescId) const noexcept
Searches for the component cache item given the compDescId.
Definition component_cache.h:138
GAIA_NODISCARD const ComponentCacheItem * find(Entity entity) const noexcept
Searches for the component cache item.
Definition component_cache.h:171
GAIA_NODISCARD const ComponentCacheItem * find() const noexcept
Searches for the component item for.
Definition component_cache.h:223
GAIA_NODISCARD const ComponentCacheItem & get(detail::ComponentDescId compDescId) const noexcept
Returns the component cache item given the compDescId.
Definition component_cache.h:156
GAIA_NODISCARD GAIA_FORCEINLINE const ComponentCacheItem & add(Entity entity)
Registers the component item for.
Definition component_cache.h:74
Definition robin_hood.h:720
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition component_cache_item.h:24
Hashmap lookup structure used for Entity.
Definition id.h:336
Definition component_desc.h:24