mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Refactor: auto-register services and inline BytecodeService
- add ServiceRegistry and JUICEAGENT_REGISTER_SERVICE macro so each service self-registers at static init; initializeAll() no longer includes concrete service headers - move BytecodeService class entirely into its .cpp (no header needed, only accessed via IService) - split ClassFileLoadHook handling into captureBytecodes and patchBytecodes listeners, registered in order
This commit is contained in:
1 parent
6962c4a477
commit
8e1b8b38e4
4 files changed
+98
-46
No files matched your search
@@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <services/base.hpp>
|
||||
#include <event/event_type.hpp>
|
||||
|
||||
namespace JuiceAgent {
|
||||
namespace Services {
|
||||
|
||||
class BytecodeService : public IService {
|
||||
public:
|
||||
static BytecodeService& getInstance();
|
||||
|
||||
const char* name() const override { return "BytecodeService"; }
|
||||
|
||||
bool onInitialize() override;
|
||||
|
||||
void onShutdown() override;
|
||||
|
||||
private:
|
||||
void onClassFileLoad(const EventClassFileLoadHook& event);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,9 @@
|
||||
#include <services/base.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
namespace JuiceAgent {
|
||||
@@ -28,6 +30,13 @@ public:
|
||||
return raw;
|
||||
}
|
||||
|
||||
IService* createService(std::unique_ptr<IService> service) {
|
||||
IService* raw = service.get();
|
||||
spdlog::debug("[ServiceManager] Registering service: {}", raw->name());
|
||||
services_.push_back(std::move(service));
|
||||
return raw;
|
||||
}
|
||||
|
||||
template <typename ServiceT>
|
||||
ServiceT* getService() const {
|
||||
const std::type_info& target = typeid(ServiceT);
|
||||
@@ -67,5 +76,56 @@ private:
|
||||
std::vector<std::unique_ptr<IService>> services_;
|
||||
};
|
||||
|
||||
// Static self-registration: each service's .cpp uses JUICEAGENT_REGISTER_SERVICE.
|
||||
class ServiceRegistry {
|
||||
public:
|
||||
using Factory = std::function<std::unique_ptr<IService>()>;
|
||||
|
||||
static ServiceRegistry& getInstance() {
|
||||
static ServiceRegistry instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void addFactory(Factory factory) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
factories_.push_back(std::move(factory));
|
||||
}
|
||||
|
||||
void instantiateAll(ServiceManager& manager) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (auto& f : factories_) {
|
||||
manager.createService(f());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
ServiceRegistry() = default;
|
||||
std::mutex mutex_;
|
||||
std::vector<Factory> factories_;
|
||||
};
|
||||
|
||||
template <typename ServiceT>
|
||||
class ServiceRegistrar {
|
||||
public:
|
||||
ServiceRegistrar() {
|
||||
ServiceRegistry::getInstance().addFactory(
|
||||
[]() -> std::unique_ptr<IService> {
|
||||
return std::make_unique<ServiceT>();
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#define JUICEAGENT_SERVICE_CONCAT_IMPL(a, b) a##b
|
||||
#define JUICEAGENT_SERVICE_CONCAT(a, b) JUICEAGENT_SERVICE_CONCAT_IMPL(a, b)
|
||||
|
||||
// Put at file scope in the service's .cpp:
|
||||
// JUICEAGENT_REGISTER_SERVICE(juiceagent::services::MyService)
|
||||
#define JUICEAGENT_REGISTER_SERVICE(ServiceT) \
|
||||
namespace { \
|
||||
const ::JuiceAgent::Services::ServiceRegistrar<ServiceT> \
|
||||
JUICEAGENT_SERVICE_CONCAT(_juiceagent_service_registrar_, __LINE__); \
|
||||
}
|
||||
Reference in new issue
Block a user