Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
service.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
26#pragma once
27
28#include <concepts>
29#include <type_traits>
30#include <memory>
31#include <functional>
32
33namespace kcenon::common {
34
35// Forward declarations
36namespace di {
38}
39
40namespace concepts {
41
57template<typename T>
58concept ServiceInterface = std::is_polymorphic_v<T> &&
59 std::has_virtual_destructor_v<T>;
60
76template<typename TImpl, typename TInterface>
79 std::is_base_of_v<TInterface, TImpl> &&
80 std::is_default_constructible_v<TImpl>;
81
97template<typename F, typename T>
98concept ServiceFactory = std::invocable<F, di::IServiceContainer&> &&
99 std::convertible_to<std::invoke_result_t<F, di::IServiceContainer&>,
100 std::shared_ptr<T>>;
101
117template<typename F, typename T>
118concept SimpleServiceFactory = std::invocable<F> &&
119 std::convertible_to<std::invoke_result_t<F>, std::shared_ptr<T>>;
120
136template<typename T>
137concept ServiceContainerLike = requires(T t) {
138 { t.create_scope() };
139 { t.registered_services() };
140 { t.clear() };
141};
142
159template<typename T>
160concept ServiceScopeLike = ServiceContainerLike<T> && requires(T t) {
161 { t.parent() };
162};
163
179template<typename T>
180concept InjectableService = std::is_class_v<T> &&
181 std::is_default_constructible_v<T>;
182
198template<typename T>
199concept SharedService = std::is_class_v<T> &&
200 requires { typename std::shared_ptr<T>; };
201
219template<typename T>
221 std::is_default_constructible_v<T> &&
222 std::is_copy_constructible_v<T>;
223
240template<typename T>
241concept Validatable = requires(const T t) {
242 { t.validate() };
243};
244
260template<typename T>
261concept ServiceWithDependencies = requires {
262 typename T::dependencies;
263};
264
285template<typename T>
286concept InitializableService = requires(T t) {
287 { t.initialize() };
288};
289
305template<typename T>
306concept DisposableService = requires(T t) {
307 { t.dispose() } -> std::same_as<void>;
308};
309
341template<typename T>
342concept ModuleRegistrar = requires(T registrar, di::IServiceContainer& container) {
343 { T::module_name() } -> std::convertible_to<std::string_view>;
344 { registrar.register_services(container) };
345};
346
347} // namespace concepts
348} // namespace kcenon::common
Abstract interface for dependency injection containers.
Definition di.cppm:39
A serializable configuration section type.
Definition service.h:220
A service that requires explicit cleanup.
Definition service.h:306
A service that requires explicit initialization.
Definition service.h:286
A service that can be automatically injected.
Definition service.h:180
A class-based module registrar for ecosystem DI integration.
Definition service.h:342
A type that satisfies service container interface requirements.
Definition service.h:137
A callable that creates service instances.
Definition service.h:98
A type that implements a service interface.
Definition service.h:77
A type that can be used as a service interface.
Definition service.h:58
A type that represents a service scope.
Definition service.h:160
A service that declares its dependencies.
Definition service.h:261
A type that can be shared via shared_ptr.
Definition service.h:199
A callable that creates service instances without container access.
Definition service.h:118
A type that can validate its own state.
Definition service.h:241
Core interfaces.
Definition adapter.h:21