mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
feat: Refactor module management and introduce ModuleRegistry for dynamic module loading
This commit is contained in:
1 parent
08ec414094
commit
50eb73d4e8
6 files changed
+170
-97
No files matched your search
@@ -12,7 +12,7 @@ public:
|
||||
ModuleManager() = default;
|
||||
~ModuleManager() = default;
|
||||
|
||||
void register_module(std::unique_ptr<IModule> module);
|
||||
void load_all();
|
||||
|
||||
bool init_all();
|
||||
bool start_all();
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <modules/IModule.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
class ModuleRegistry {
|
||||
public:
|
||||
using Factory = std::function<std::unique_ptr<IModule>()>;
|
||||
|
||||
struct Entry {
|
||||
std::string name;
|
||||
Factory factory;
|
||||
};
|
||||
|
||||
static ModuleRegistry& instance() {
|
||||
static ModuleRegistry inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
bool add(const std::string& name, Factory factory) {
|
||||
for (const auto& item : _entries) {
|
||||
if (item.name == name) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
_entries.push_back({name, std::move(factory)});
|
||||
return true;
|
||||
}
|
||||
|
||||
const std::vector<Entry>& entries() const {
|
||||
return _entries;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Entry> _entries;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#define JUICEAGENT_REGISTER_MODULE(TYPE, NAME) \
|
||||
namespace { \
|
||||
const bool registered_##NAME = \
|
||||
JuiceAgent::Core::Modules::ModuleRegistry::instance().add( \
|
||||
#NAME, \
|
||||
[]() -> std::unique_ptr<JuiceAgent::Core::Modules::IModule> { \
|
||||
return std::make_unique<TYPE>(); \
|
||||
} \
|
||||
); \
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <modules/IModule.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
class JarLoaderModule : public IModule {
|
||||
public:
|
||||
JarLoaderModule() = default;
|
||||
~JarLoaderModule() override = default;
|
||||
|
||||
std::string name() const override {
|
||||
return "JarLoader";
|
||||
}
|
||||
|
||||
bool init() override;
|
||||
bool start() override;
|
||||
void stop() override;
|
||||
|
||||
private:
|
||||
bool _initialized = false;
|
||||
bool _running = false;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user