35            : m_entities{entityCapacity},
 
   36              m_freePool{entityCapacity},
 
   37              m_maxEntities{entityCapacity}
 
   39            NC_ASSERT(!s_init, 
"There may only be one ComponentRegistry instance.");
 
   45            std::ranges::for_each(m_refs, [](
auto&& p) { *p = 
nullptr; });
 
   54        template<PooledComponent T>
 
   57            NC_ASSERT(capacity <= m_maxEntities, 
"Component capacity cannot exceed entity capapcity.");
 
   58            NC_ASSERT(!s_typedPool<T>, 
"Type already registered.");
 
   60            SetupStorage<T>(std::make_unique<
ComponentPool<T>>(capacity, std::move(handler)));
 
   64        template<PooledComponent T>
 
   67            return s_typedPool<T> != 
nullptr;
 
   73            return std::ranges::contains(m_pools, 
id, [](
const auto& pool) { 
return pool->Id(); });
 
   77        template<PooledComponent T>
 
   80            NC_ASSERT(s_typedPool<T>, 
"Attempt to access an unregistered type.");
 
   81            return *s_typedPool<T>;
 
   85        template<PooledComponent T>
 
   88            NC_ASSERT(s_typedPool<T>, 
"Attempt to access an unregistered type.");
 
   89            return *s_typedPool<T>;
 
   95            auto pos = std::ranges::find_if(m_pools, [
id](
auto&& pool) { 
return pool->Id() == 
id; });
 
   96            NC_ASSERT(pos != std::ranges::end(m_pools), 
"Attempt to access an unregistered type.");
 
  103            auto pos = std::ranges::find_if(m_pools, [
id](
auto&& pool) { 
return pool->Id() == 
id; });
 
  104            NC_ASSERT(pos != std::ranges::end(m_pools), 
"Attempt to access an unregistered type.");
 
  112        template<std::same_as<Entity> T>
 
  116        template<std::same_as<Entity> T>
 
  122            return m_pools | std::views::transform(&std::unique_ptr<ComponentPoolBase>::get);
 
  132            std::ranges::for_each(m_pools, [&removed](
auto&& p) { p->CommitStagedComponents(removed); });
 
  139            std::ranges::for_each(m_pools, [](
auto&& p) { p->ClearNonPersistent(); });
 
  147            std::ranges::for_each(m_pools, [](
auto&& p) { p->Clear(); });
 
  153        std::vector<std::unique_ptr<ComponentPoolBase>> m_pools;
 
  156        std::vector<void**> m_refs;
 
  157        size_t m_maxEntities;
 
  158        size_t m_nextComponentId = std::numeric_limits<size_t>::max();
 
  160        template<PooledComponent T>
 
  163        inline static bool s_init = 
false;
 
  165        void SetupId(
size_t& 
id)
 
  168                id = m_nextComponentId--; 
 
  171                throw NcError(fmt::format(
"ComponentId '{}' is already in use.", 
id));
 
  174        template<PooledComponent T>
 
  175        void SetupStorage(std::unique_ptr<ComponentPool<T>> pool) 
noexcept 
  177            s_typedPool<T> = pool.get();
 
  178            m_refs.emplace_back(
reinterpret_cast<void**
>(&s_typedPool<T>));
 
  179            m_pools.push_back(std::move(pool));
 
Common exception type used throughout NcEngine.
Definition: NcError.h:18
Base class for non-copyable non-movable types.
Definition: StableAddress.h:7
Type-agnostic base class for component pools.
Definition: ComponentPool.h:22
Type-aware implementation for component pools.
Definition: ComponentPool.h:100
Core collection of data pools and registration information game types.
Definition: ComponentRegistry.h:26
auto GetPool(size_t id) -> ComponentPoolBase &
Get the pool base for a registered type by component id.
Definition: ComponentRegistry.h:93
auto GetPool() -> ComponentPool< T > &
Get the pool for a registered component type.
Definition: ComponentRegistry.h:78
void ClearSceneData()
Destroy all non-persistent entities and components.
Definition: ComponentRegistry.h:137
auto GetPool() -> EntityPool &
Get the entity pool.
Definition: ComponentRegistry.h:113
auto GetFreeComponentPool() -> FreeComponentPool &
Get the pool for all FreeComponents.
Definition: ComponentRegistry.h:109
auto GetPool() const -> const EntityPool &
Get the entity pool.
Definition: ComponentRegistry.h:117
auto GetComponentPools()
Get a view of all registered component pools as ComponentPoolBase*.
Definition: ComponentRegistry.h:120
void Clear()
Destroy all entities and components.
Definition: ComponentRegistry.h:145
auto GetPool(size_t id) const -> ComponentPoolBase &
Get the pool base for a registered type by component id.
Definition: ComponentRegistry.h:101
void CommitPendingChanges()
Merge staged components into their pools and finalize any entity removals.
Definition: ComponentRegistry.h:129
void RegisterType(size_t capacity, ComponentHandler< T > handler={})
Register a component type.
Definition: ComponentRegistry.h:55
auto GetPool() const -> const ComponentPool< T > &
Get the pool for a registered component type.
Definition: ComponentRegistry.h:86
ComponentRegistry(size_t entityCapacity)
Construct a ComponentRegistry.
Definition: ComponentRegistry.h:34
auto IsTypeRegistered(size_t id) const noexcept -> bool
Check if a type is registered using a component id.
Definition: ComponentRegistry.h:71
auto GetMaxEntities() const noexcept
Get the maximum number of concurrent entities supported.
Definition: ComponentRegistry.h:126
auto IsTypeRegistered() const noexcept -> bool
Check if a type is registered.
Definition: ComponentRegistry.h:65
Storage class for tracking active entities.
Definition: EntityPool.h:21
auto RecycleDeadEntities() -> std::vector< Entity >
Add removed entity indices back to the pool of possible indices.
Definition: EntityPool.h:63
void Clear()
Remove all entities.
Definition: EntityPool.h:73
void ClearNonPersistent()
Remove all entities, excluding those with the persistent flag.
Definition: EntityPool.h:84
Single pool storing all FreeComponent instances.
Definition: FreeComponentPool.h:16
void Clear() noexcept
Remove all components.
Definition: FreeComponentPool.h:91
void ClearNonPersistent() noexcept
Remove all components not attached to persistent Entities.
Definition: FreeComponentPool.h:78
void CommitStagedComponents(std::span< const Entity > removedEntities)
Finalize any staged additions and removals.
Definition: FreeComponentPool.h:64
Optional data and callbacks for generic component operations.
Definition: Component.h:101