From adc0de310ed8f41412821334a453b30e4fdaf94d Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 22 Jun 2026 16:59:02 +0300 Subject: [PATCH] Service: allow registering non-polymorphic service types ServiceRegistry::provide cross-cast to IService via dynamic_cast, which fails to compile when I is a plain (non-polymorphic) value type. Guard the cross-cast with std::is_polymorphic_v; non-polymorphic services are stored and resolvable but carry no lifecycle aspect. Test added. Co-Authored-By: Claude Opus 4.8 --- include/Void/Service/Registry.hpp | 9 +++++++-- tests/standalone/Service/Registry.cpp | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/Void/Service/Registry.hpp b/include/Void/Service/Registry.hpp index 6d91112..5902755 100644 --- a/include/Void/Service/Registry.hpp +++ b/include/Void/Service/Registry.hpp @@ -61,11 +61,16 @@ namespace Void::Service entry.impl = std::shared_ptr(impl, impl.get()); if constexpr (std::is_base_of_v) { entry.service = static_cast(impl.get()); - } else { + } 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. + // 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}; diff --git a/tests/standalone/Service/Registry.cpp b/tests/standalone/Service/Registry.cpp index 4fbff1d..f8359e4 100644 --- a/tests/standalone/Service/Registry.cpp +++ b/tests/standalone/Service/Registry.cpp @@ -53,8 +53,27 @@ namespace [[nodiscard]] std::string greet() const override { return "hi"; } std::vector* m_log; }; + + // A plain, non-polymorphic value type registered directly as a service. + struct Config { + int port = 8080; + std::string host = "localhost"; + }; } // namespace +Void::Testing::Test registry_provides_non_polymorphic_service() +{ + ServiceRegistry reg; + reg.provide(std::make_shared(Config{.port = 9000})); + auto cfg = reg.get(); + Void::Testing::expect(cfg != nullptr); + Void::Testing::expect_eq(cfg->port, 9000); + Void::Testing::expect(reg.has()); + // No IService lifecycle aspect, but start_all/stop_all must not choke. + reg.start_all(); + reg.stop_all(); +} + Void::Testing::Test registry_provide_and_get() { ServiceRegistry reg;