2026-02-28 20:56:11 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
2026-02-28 23:35:34 +08:00
|
|
|
|
#include <functional>
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
#include <module/module.h>
|
2026-02-28 20:56:11 +08:00
|
|
|
|
#include <string>
|
2026-02-28 23:35:34 +08:00
|
|
|
|
#include <typeindex>
|
2026-02-28 20:56:11 +08:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-28 23:35:34 +08:00
|
|
|
|
* @brief 模块工厂函数类型
|
|
|
|
|
|
*/
|
|
|
|
|
|
using ModuleFactory = std::function<std::unique_ptr<Module>()>;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 模块注册信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
struct ModuleInfo {
|
|
|
|
|
|
std::string name;
|
|
|
|
|
|
ModuleFactory factory;
|
|
|
|
|
|
int32 priority;
|
|
|
|
|
|
std::type_index type;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 模块注册表
|
2026-02-28 20:56:11 +08:00
|
|
|
|
*
|
2026-02-28 23:35:34 +08:00
|
|
|
|
* 支持自动模块注册和获取
|
|
|
|
|
|
* 使用服务定位器模式
|
2026-02-28 20:56:11 +08:00
|
|
|
|
*/
|
|
|
|
|
|
class ModuleRegistry {
|
|
|
|
|
|
public:
|
|
|
|
|
|
/**
|
2026-02-28 23:35:34 +08:00
|
|
|
|
* @brief 获取全局注册表实例
|
2026-02-28 20:56:11 +08:00
|
|
|
|
*/
|
2026-02-28 23:35:34 +08:00
|
|
|
|
static ModuleRegistry &instance();
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-28 23:35:34 +08:00
|
|
|
|
* @brief 注册模块类型
|
|
|
|
|
|
* @tparam T 模块类型
|
2026-02-28 20:56:11 +08:00
|
|
|
|
* @param name 模块名称
|
2026-02-28 23:35:34 +08:00
|
|
|
|
* @param priority 初始化优先级
|
2026-02-28 20:56:11 +08:00
|
|
|
|
*/
|
2026-02-28 23:35:34 +08:00
|
|
|
|
template <typename T>
|
|
|
|
|
|
void registerModule(const char *name, int32 priority = 100) {
|
|
|
|
|
|
static_assert(std::is_base_of_v<Module, T>, "T must inherit from Module");
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
2026-02-28 23:35:34 +08:00
|
|
|
|
ModuleInfo info{
|
|
|
|
|
|
name, []() -> std::unique_ptr<Module> { return std::make_unique<T>(); },
|
|
|
|
|
|
priority, std::type_index(typeid(T))};
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
2026-02-28 23:35:34 +08:00
|
|
|
|
registrations_.push_back(std::move(info));
|
|
|
|
|
|
}
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-28 23:35:34 +08:00
|
|
|
|
* @brief 创建所有已注册的模块
|
|
|
|
|
|
* @return 创建的模块列表(已按优先级排序)
|
2026-02-28 20:56:11 +08:00
|
|
|
|
*/
|
2026-02-28 23:35:34 +08:00
|
|
|
|
std::vector<std::unique_ptr<Module>> createModules();
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-28 23:35:34 +08:00
|
|
|
|
* @brief 获取已注册的模块信息列表
|
2026-02-28 20:56:11 +08:00
|
|
|
|
*/
|
2026-02-28 23:35:34 +08:00
|
|
|
|
const std::vector<ModuleInfo> &getRegistrations() const {
|
|
|
|
|
|
return registrations_;
|
|
|
|
|
|
}
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
|
|
|
|
|
private:
|
2026-02-28 23:35:34 +08:00
|
|
|
|
ModuleRegistry() = default;
|
|
|
|
|
|
std::vector<ModuleInfo> registrations_;
|
|
|
|
|
|
};
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
2026-02-28 23:35:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 自动模块注册辅助类
|
|
|
|
|
|
*/
|
|
|
|
|
|
template <typename T> class ModuleRegistrar {
|
|
|
|
|
|
public:
|
|
|
|
|
|
ModuleRegistrar(const char *name, int32 priority = 100) {
|
|
|
|
|
|
ModuleRegistry::instance().registerModule<T>(name, priority);
|
|
|
|
|
|
}
|
2026-02-28 20:56:11 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-28 23:35:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 模块注册宏
|
|
|
|
|
|
*
|
|
|
|
|
|
* 在模块类定义中使用,实现自动注册
|
|
|
|
|
|
* 示例:
|
|
|
|
|
|
* class MyModule : public Module {
|
|
|
|
|
|
* E2D_REGISTER_MODULE(MyModule, "MyModule", 10)
|
|
|
|
|
|
* public:
|
|
|
|
|
|
* // ...
|
|
|
|
|
|
* };
|
|
|
|
|
|
*/
|
|
|
|
|
|
#define E2D_REGISTER_MODULE(ClassName, Name, Priority) \
|
|
|
|
|
|
private: \
|
|
|
|
|
|
static inline const extra2d::ModuleRegistrar<ClassName> _registrar{ \
|
|
|
|
|
|
Name, Priority}; \
|
|
|
|
|
|
\
|
|
|
|
|
|
public: \
|
|
|
|
|
|
const char *getName() const override { return Name; } \
|
|
|
|
|
|
int32 getPriority() const override { return Priority; }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 简化版模块注册宏(使用类名作为模块名)
|
|
|
|
|
|
*/
|
|
|
|
|
|
#define E2D_REGISTER_MODULE_SIMPLE(ClassName) \
|
|
|
|
|
|
E2D_REGISTER_MODULE(ClassName, #ClassName, 100)
|
|
|
|
|
|
|
2026-02-28 20:56:11 +08:00
|
|
|
|
} // namespace extra2d
|