99 default:
return "unknown";
183class IServiceContainer {
210 std::type_index(
typeid(TInterface)),
211 typeid(TInterface).name(),
213 return std::make_shared<TImpl>();
229 template<
typename TInterface>
234 "Cannot register null instance",
235 "di::IServiceContainer"
240 std::type_index(
typeid(TInterface)),
241 typeid(TInterface).name(),
242 std::static_pointer_cast<void>(instance)
266 std::type_index(
typeid(TInterface)),
267 typeid(TInterface).name(),
268 [f = std::forward<TFactory>(factory)](
IServiceContainer& c) -> std::shared_ptr<void> {
269 return std::static_pointer_cast<void>(f(c));
293 std::type_index(
typeid(TInterface)),
294 typeid(TInterface).name(),
295 [f = std::forward<TFactory>(factory)](
IServiceContainer&) -> std::shared_ptr<void> {
296 return std::static_pointer_cast<void>(f());
319 template<
typename TInterface>
323 if (result.is_err()) {
328 std::static_pointer_cast<TInterface>(result.value())
341 template<
typename TInterface>
344 if (result.is_ok()) {
345 return result.value();
370 template<
typename TInterface>
391 template<
typename TInterface>
413 std::type_index interface_type,
414 const std::string& type_name,
422 std::type_index interface_type,
423 const std::string& type_name,
424 std::shared_ptr<void> instance) = 0;
430 std::type_index interface_type) = 0;
493namespace di_error_codes {
Result type for error handling with member function support.
Abstract interface for dependency injection containers.
bool is_registered() const
Check if a service type is registered.
std::shared_ptr< TInterface > resolve_or_null()
Try to resolve a service, returning nullptr if not found.
VoidResult register_simple_factory(TFactory &&factory, service_lifetime lifetime=service_lifetime::singleton)
Register a simple factory function without container access.
virtual Result< std::shared_ptr< void > > resolve_internal(std::type_index interface_type)=0
Internal service resolution (type-erased).
virtual std::unique_ptr< IServiceScope > create_scope()=0
Create a new service scope.
Result< std::shared_ptr< TInterface > > resolve()
Resolve a service by its interface type.
virtual VoidResult register_instance_internal(std::type_index interface_type, const std::string &type_name, std::shared_ptr< void > instance)=0
Internal instance registration (type-erased).
VoidResult register_type(service_lifetime lifetime=service_lifetime::singleton)
Register a service type with its implementation.
virtual VoidResult unregister_internal(std::type_index interface_type)=0
Internal unregistration (type-erased).
virtual std::vector< service_descriptor > registered_services() const =0
Get list of all registered service descriptors.
virtual bool is_registered_internal(std::type_index interface_type) const =0
Internal registration check (type-erased).
VoidResult register_instance(std::shared_ptr< TInterface > instance)
Register a pre-existing instance as a singleton.
VoidResult unregister()
Unregister a service type.
virtual VoidResult clear()=0
Clear all registrations.
virtual VoidResult register_factory_internal(std::type_index interface_type, const std::string &type_name, std::function< std::shared_ptr< void >(IServiceContainer &)> factory, service_lifetime lifetime)=0
Internal factory registration (type-erased).
virtual ~IServiceContainer()=default
VoidResult register_factory(TFactory &&factory, service_lifetime lifetime=service_lifetime::singleton)
Register a factory function for creating service instances.
Scoped service container for request-level isolation.
virtual const IServiceContainer & parent() const =0
Get the parent container (const).
virtual IServiceContainer & parent()=0
Get the parent container.
~IServiceScope() override=default
A callable that creates service instances.
A type that implements a service interface.
A type that can be used as a service interface.
A callable that creates service instances without container access.
Centralized error code registry for all systems.
constexpr int scoped_from_root
Scoped service resolved from root container.
constexpr int circular_dependency
Circular dependency detected during resolution.
constexpr int invalid_lifetime
Invalid service lifetime configuration.
constexpr int factory_error
Factory threw an exception during instantiation.
constexpr int service_not_registered
Service not registered in container.
constexpr int already_registered
Service already registered (duplicate registration attempt)
const char * to_string(service_lifetime lifetime)
Convert service_lifetime to string representation.
service_lifetime
Defines the lifetime policy for registered services.
@ singleton
Single instance shared globally.
@ scoped
Single instance within a scope.
@ transient
New instance created for each request.
constexpr int di_service_not_registered
constexpr int di_invalid_lifetime
constexpr int di_factory_error
constexpr int di_scoped_from_root
constexpr int di_already_registered
constexpr int di_circular_dependency
constexpr int INVALID_ARGUMENT
Result< T > make_error(int code, const std::string &message, const std::string &module="")
Create an error result with code and message.
VoidResult ok()
Create a successful void result.
Umbrella header for Result<T> type and related utilities.
C++20 concepts for dependency injection and service container.
Metadata describing a registered service.
service_descriptor(std::type_index type, std::string name, service_lifetime lt)
std::type_index interface_type
Type index of the interface being registered.
bool is_instantiated
Whether this service has been instantiated (for singletons)
service_lifetime lifetime
Lifetime policy for this service.
std::string description
Optional description or tags for the service.
std::string interface_name
Human-readable name of the interface type.