#pragma once #include #include #include #include #include #include #include #include #include #include #include namespace Void::Service { #ifdef VOID_HAS_META_REFLECTION /// The reflected (unqualified) name of an interface type — a sensible /// default service name when an IService impl doesn't override /// service_name(). E.g. interface_name() == "IClock". template [[nodiscard]] std::string interface_name() { return Void::Meta::type_name(); } #endif /// Type-keyed service container: providers register an implementation /// against an interface type, consumers resolve by that interface and /// never name the concrete type — the core of the dependency-inversion /// the orchestrator and FSMs rely on. /// /// registry.provide(std::make_shared()); /// auto& clock = registry.require(); // no SystemClock dep /// /// Thread-safe. Reads (get/require/has) take a shared lock; they are /// the hot path for running FSMs resolving services per step. class ServiceRegistry { public: ServiceRegistry() = default; ServiceRegistry(const ServiceRegistry&) = delete; ServiceRegistry& operator=(const ServiceRegistry&) = delete; /// Create a scoped child registry that overlays `parent`: lookups /// fall through to the parent when a key is not provided locally, /// and a local provide() shadows the parent's entry. Lifecycle /// (start_all/stop_all) covers only locally-provided services. The /// parent must outlive this registry. explicit ServiceRegistry(const ServiceRegistry* parent) : m_parent{parent} { } /// Register (or replace) the implementation for interface I. /// If I derives IService, the instance joins the lifecycle. template void provide(std::shared_ptr impl) { Entry entry; entry.impl = std::shared_ptr(impl, impl.get()); if constexpr (std::is_base_of_v) { entry.service = static_cast(impl.get()); } else if constexpr (std::is_polymorphic_v) { // The interface I doesn't derive IService, but the // concrete implementation might — recover the lifecycle // aspect by cross-cast. (dynamic_cast requires a // polymorphic I.) entry.service = dynamic_cast(impl.get()); } else { // A plain, non-polymorphic service type: storable and // resolvable, but has no IService lifecycle aspect. entry.service = nullptr; } const std::type_index key{typeid(I)}; std::unique_lock lock{m_mutex}; if (!m_services.contains(key)) { m_order.push_back(key); } m_services.insert_or_assign(key, std::move(entry)); } /// Withdraw the implementation for interface I from this registry /// (a parent's entry is unaffected: later lookups fall through to /// it again). Consumers still holding the shared_ptr keep the /// instance alive; only new lookups miss. Returns true when an /// entry was removed. template bool remove() { const std::type_index key{typeid(I)}; std::unique_lock lock{m_mutex}; if (m_services.erase(key) == 0) { return false; } std::erase(m_order, key); return true; } /// Resolve I, or nullptr if not provided. Falls through to the /// parent registry (if any) on a local miss. template [[nodiscard]] std::shared_ptr get() const { { std::shared_lock lock{m_mutex}; const auto it = m_services.find(std::type_index{typeid(I)}); if (it != m_services.end()) { return std::static_pointer_cast(it->second.impl); } } return m_parent ? m_parent->get() : nullptr; } /// Resolve I; throws std::runtime_error if not provided. template [[nodiscard]] I& require() const { auto p = get(); if (!p) { throw std::runtime_error( std::string{"ServiceRegistry: no service for '"} + typeid(I).name() + "'"); } return *p; } template [[nodiscard]] bool has() const { { std::shared_lock lock{m_mutex}; if (m_services.contains(std::type_index{typeid(I)})) { return true; } } return m_parent ? m_parent->has() : false; } /// Call on_start() on every lifecycle service, registration /// order. void start_all() { std::shared_lock lock{m_mutex}; for (const auto& key : m_order) { if (auto* s = m_services.at(key).service) { s->on_start(); } } } /// Call on_stop() on every lifecycle service, reverse order. void stop_all() { std::shared_lock lock{m_mutex}; for (auto it = m_order.rbegin(); it != m_order.rend(); ++it) { if (auto* s = m_services.at(*it).service) { s->on_stop(); } } } [[nodiscard]] std::size_t size() const { std::shared_lock lock{m_mutex}; return m_services.size(); } private: struct Entry { std::shared_ptr impl; IService* service = nullptr; // non-null iff I : IService }; const ServiceRegistry* m_parent = nullptr; mutable std::shared_mutex m_mutex; std::unordered_map m_services; std::vector m_order; }; } // namespace Void::Service