mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
feat: Implement module registration and loading functionality
This commit is contained in:
1 parent
79ae4b62a3
commit
e1577d4fae
5 files changed
+78
-3
No files matched your search
@@ -4,6 +4,7 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <modules/IModule.hpp>
|
||||
#include <JuiceAgent/Config.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
@@ -20,6 +21,8 @@ public:
|
||||
|
||||
IModule* find_module(const std::string& name);
|
||||
|
||||
bool loadAllRegisteredModules();
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<IModule>> _modules;
|
||||
};
|
||||
|
||||
@@ -1,9 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <modules/IModule.hpp>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
class ModuleRegistry {
|
||||
public:
|
||||
using Creator = std::function<std::unique_ptr<IModule>()>;
|
||||
|
||||
static void registerModule(const std::string& name, Creator creator) {
|
||||
getRegistry()[name] = creator;
|
||||
}
|
||||
|
||||
static std::unique_ptr<IModule> createModule(const std::string& name) {
|
||||
auto& reg = getRegistry();
|
||||
auto it = reg.find(name);
|
||||
if (it != reg.end()) {
|
||||
return it->second();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static std::vector<std::string> getRegisteredModuleNames() {
|
||||
std::vector<std::string> names;
|
||||
auto& reg = getRegistry();
|
||||
for (const auto& pair : reg) {
|
||||
names.push_back(pair.first);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
private:
|
||||
static std::unordered_map<std::string, Creator>& getRegistry() {
|
||||
static std::unordered_map<std::string, Creator> registry;
|
||||
return registry;
|
||||
}
|
||||
};
|
||||
|
||||
class JarLoaderModule : public IModule {
|
||||
public:
|
||||
JarLoaderModule() = default;
|
||||
@@ -22,4 +60,10 @@ private:
|
||||
bool _running = false;
|
||||
};
|
||||
|
||||
}
|
||||
#define REGISTER_MODULE(ModuleClass) \
|
||||
static bool _registered_##ModuleClass = []() { \
|
||||
ModuleRegistry::registerModule(#ModuleClass, []() { return std::make_unique<ModuleClass>(); }); \
|
||||
return true; \
|
||||
}()
|
||||
|
||||
} // namespace JuiceAgent::Core::Modules
|
||||
Reference in new issue
Block a user