mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
feat: refactor module structure to use ModuleBase class and improve initialization logic
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
1 parent
28804c0af3
commit
2493a9243f
6 files changed
+151
-130
No files matched your search
@@ -12,6 +12,9 @@ public:
|
||||
virtual bool init() = 0;
|
||||
virtual bool start() = 0;
|
||||
virtual void stop() = 0;
|
||||
|
||||
virtual bool initialized() const = 0;
|
||||
virtual bool running() const = 0;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
#include <modules/IModule.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
class ModuleBase : public IModule {
|
||||
public:
|
||||
virtual ~ModuleBase() = default;
|
||||
|
||||
bool init() override {
|
||||
if (_initialized) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!on_init()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start() override {
|
||||
if (!_initialized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_running) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!on_start()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_running = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
if (!_running) {
|
||||
return;
|
||||
}
|
||||
|
||||
on_stop();
|
||||
_running = false;
|
||||
}
|
||||
|
||||
bool initialized() const override {
|
||||
return _initialized;
|
||||
}
|
||||
|
||||
bool running() const override {
|
||||
return _running;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual bool on_init() {
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool on_start() {
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void on_stop() {
|
||||
}
|
||||
|
||||
private:
|
||||
bool _initialized = false;
|
||||
bool _running = false;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user