8#include "ncengine/type/StableAddress.h" 
   22        template<std::derived_from<Module> T>
 
   25            NC_ASSERT(module, 
"Attempt to register a null Module.");
 
   26            if (IsRegistered<T>())
 
   32            m_typedModulePointer<T> = module.get();
 
   33            m_modules.push_back(std::move(module));
 
   37        template<std::derived_from<Module> T>
 
   40            const auto* target = m_typedModulePointer<T>;
 
   41            std::erase_if(m_modules, [target](
const auto& module)
 
   43                return module.get() == target;
 
   46            m_typedModulePointer<T> = 
nullptr;
 
   50        template<std::derived_from<Module> T>
 
   53            return Get<T>() != 
nullptr;
 
   59            return std::ranges::contains(m_modules, 
id, [](
const auto& module) { 
return module->Id(); });
 
   63        template<std::derived_from<Module> T>
 
   64        auto Get() const noexcept -> T*
 
   66            return m_typedModulePointer<T>;
 
   72            auto pos = std::ranges::find_if(m_modules, [
id](
auto& module) { 
return module->Id() == 
id; });
 
   73            return pos != std::ranges::end(m_modules) ? pos->get() : 
nullptr;
 
   77        auto GetAllModules() const noexcept -> const std::vector<std::unique_ptr<
Module>>&
 
   83        std::vector<std::unique_ptr<Module>> m_modules;
 
   84        size_t m_nextModuleId = std::numeric_limits<size_t>::max();
 
   86        template<std::derived_from<Module> T>
 
   87        inline static T* m_typedModulePointer = 
nullptr;
 
   89        void SetupId(Module& module)
 
   91            if (module.Id() == 0ull)
 
   92                module.SetId(m_nextModuleId--);
 
   94            const auto id = module.Id();
 
   96                throw NcError(fmt::format(
"Module id '{}' is already in use.", 
id));
 
Modules are extensions that provide functionality to the engine.
Definition: Module.h:19
Definition: ModuleRegistry.h:19
void Unregister()
Remove a module from the registry. Does nothing if no matching module is found.
Definition: ModuleRegistry.h:38
auto Get(size_t id) const noexcept -> Module *
Get a pointer to a module by id or nullptr if it is unregistered.
Definition: ModuleRegistry.h:70
auto IsRegistered(size_t id) const noexcept -> bool
Check if a module matching an id is registered.
Definition: ModuleRegistry.h:57
auto IsRegistered() const noexcept -> bool
Check if a module matching a type is registered.
Definition: ModuleRegistry.h:51
void Register(std::unique_ptr< T > module)
Add a new module to the registry. Overwrites any existing module of the same type.
Definition: ModuleRegistry.h:23
auto Get() const noexcept -> T *
Get a pointer to a module or nullptr if it is unregistered.
Definition: ModuleRegistry.h:64
Base class for non-copyable non-movable types.
Definition: StableAddress.h:7