Extra2D/include/module/module_registry.h

98 lines
2.0 KiB
C++

#pragma once
#include <module/imodule.h>
#include <string>
#include <unordered_map>
#include <vector>
namespace extra2d {
/**
* @brief 模块注册表 - 非单例
*
* 管理所有模块的生命周期,通过事件总线协调模块间的通信
* 支持按优先级排序初始化和关闭
*/
class ModuleRegistry {
public:
ModuleRegistry();
~ModuleRegistry();
// 禁止拷贝
ModuleRegistry(const ModuleRegistry &) = delete;
ModuleRegistry &operator=(const ModuleRegistry &) = delete;
// 允许移动
ModuleRegistry(ModuleRegistry &&) noexcept;
ModuleRegistry &operator=(ModuleRegistry &&) noexcept;
/**
* @brief 注册模块
* @param module 模块实例指针(不由注册表管理生命周期)
*/
void registerModule(IModule *module);
/**
* @brief 注销模块
* @param name 模块名称
*/
void unregisterModule(const char *name);
/**
* @brief 获取模块
* @param name 模块名称
* @return 模块指针,不存在返回 nullptr
*/
IModule *getModule(const char *name) const;
/**
* @brief 检查模块是否存在
* @param name 模块名称
* @return 是否存在
*/
bool hasModule(const char *name) const;
/**
* @brief 获取所有模块
* @return 模块列表
*/
std::vector<IModule *> getAllModules() const;
/**
* @brief 获取指定类型的所有模块
* @param type 模块类型
* @return 模块列表
*/
std::vector<IModule *> getModulesByType(ModuleType type) const;
/**
* @brief 初始化所有模块(按优先级排序)
* @return 是否全部初始化成功
*/
bool initAll();
/**
* @brief 关闭所有模块(按优先级逆序)
*/
void shutdownAll();
/**
* @brief 获取模块数量
* @return 模块数量
*/
size_t getModuleCount() const;
private:
/**
* @brief 按优先级排序模块
*/
void sortModules();
std::vector<IModule *> modules_;
std::unordered_map<std::string, IModule *> moduleMap_;
bool sorted_ = false;
bool inited_ = false;
};
} // namespace extra2d