diff --git a/CMakeLists.txt b/CMakeLists.txt index f1dc645..a4e033e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,7 @@ target_link_libraries(EchoionCore Void::Math Void::Net Void::Sqlite + Void::Service PRIVATE Threads::Threads qjs diff --git a/include/echoion/core/Core.hpp b/include/echoion/core/Core.hpp index 049f03e..808d06f 100644 --- a/include/echoion/core/Core.hpp +++ b/include/echoion/core/Core.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -176,6 +177,22 @@ namespace Echoion::Core // RPC server for Desktop/API clients std::shared_ptr rpcServer_; + + // Composition root: a type-keyed registry of all Core services + // (logger, config, event bus, network, database, music, rpc). + // initialize() provides each instance; resolveServices() and other + // code can require() them by interface instead of reaching for + // global singletons. The former singletons stay as thin macro + // shims over the same instances. + Void::Service::ServiceRegistry registry_; + + public: + + // Access the service registry (composition root). + [[nodiscard]] Void::Service::ServiceRegistry& services() noexcept + { + return registry_; + } }; } // namespace Echoion::Core diff --git a/src/Core.cpp b/src/Core.cpp index 73801ca..52a84eb 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -286,6 +286,40 @@ namespace Echoion::Core setupEventBusSubscriptions(); } + // ── Compose the service registry ───────────────────────────────── + // Register every Core service by interface so code can resolve them + // via registry_.require() instead of reaching for global singletons. + // The three former singletons (logger, config, event bus) are wrapped + // in non-owning aliasing shared_ptrs — they remain valid statics behind + // the ECHOION_LOG_*/PUBLISH/SUBSCRIBE macros, the registry just makes + // them discoverable as services too. + { + const auto alias = [](auto* p) { + using T = std::remove_reference_t; + return std::shared_ptr(p, [](T*) {}); + }; + if (auto logger = Api::LoggerRegistry::instance().getLogger()) { + registry_.provide(logger); + } + registry_.provide( + alias(&Api::ConfigManager::instance())); + registry_.provide( + alias(&Api::GlobalEventBus::instance())); + if (networkClient_) { + registry_.provide(networkClient_); + } + if (database_) { + registry_.provide(database_); + } + if (musicManager_) { + registry_.provide(musicManager_); + } + if (rpcServer_) { + registry_.provide(rpcServer_); + } + ECHOION_LOG_INFO("Service registry composed"); + } + startTime_ = std::chrono::system_clock::now(); initialized_ = true; ECHOION_LOG_INFO(std::format("Core initialized (this={})",