Service: allow registering non-polymorphic service types

ServiceRegistry::provide<I> 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<I>; non-polymorphic services are
stored and resolvable but carry no lifecycle aspect. Test added.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-22 16:59:02 +03:00
1 parent cb639a713d
commit adc0de310e
2 files changed
+26 -2

No files matched your search

+19
View File
@@ -53,8 +53,27 @@ namespace
[[nodiscard]] std::string greet() const override { return "hi"; }
std::vector<std::string>* 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<Config>(std::make_shared<Config>(Config{.port = 9000}));
auto cfg = reg.get<Config>();
Void::Testing::expect(cfg != nullptr);
Void::Testing::expect_eq(cfg->port, 9000);
Void::Testing::expect(reg.has<Config>());
// 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;