feat: Add Example module implementation and registration

This commit is contained in:
xiaozhou233 committed 2026-04-21 18:47:42 +08:00
1 parent e1577d4fae
commit 601c999023
2 files changed
+70 -21

No files matched your search

+20 -21
View File
@@ -42,28 +42,27 @@ private:
}
};
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;
#define DEFINE_MODULE(ModuleName) \
class ModuleName##Module : public IModule { \
public: \
ModuleName##Module() = default; \
~ModuleName##Module() override = default; \
\
std::string name() const override { \
return #ModuleName; \
} \
\
bool init() override; \
bool start() override; \
void stop() override; \
\
private: \
bool _initialized = false; \
bool _running = false; \
};
#define REGISTER_MODULE(ModuleClass) \
static bool _registered_##ModuleClass = []() { \
ModuleRegistry::registerModule(#ModuleClass, []() { return std::make_unique<ModuleClass>(); }); \
return true; \
}()
DEFINE_MODULE(JarLoader)
DEFINE_MODULE(Example)
} // namespace JuiceAgent::Core::Modules
+50
View File
@@ -0,0 +1,50 @@
#include <modules/Modules.hpp>
#include <JuiceAgent/Logger.hpp>
#include <libagent.hpp>
namespace JuiceAgent::Core::Modules {
struct _ExampleConfig {
bool Enabled = true;
std::string Message = "Hello from Example Module!";
} ExampleConfig;
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
bool ExampleModule::init() {
if (_initialized) return true;
// Coding here
_initialized = true;
return true;
}
bool ExampleModule::start() {
if (!_initialized) return false;
if (_running) return true;
// Coding here
_running = true;
return true;
}
void ExampleModule::stop() {
if (!_running) return;
// Coding here
_running = false;
}
} // namespace JuiceAgent::Core::Modules
namespace {
bool registerExample = []() {
JuiceAgent::Core::Modules::ModuleRegistry::registerModule("Example", []() {
return std::make_unique<JuiceAgent::Core::Modules::ExampleModule>();
});
return true;
}();
}