106 std::map<std::type_index, std::vector<ServiceDescriptor>>
_services;
110 using std::ranges::reverse_view;
112 for (
const auto& descriptor : reverse_view(descriptors))
114 const auto& typeInfo = descriptor.GetTypeInfo();
115 bool serviceExists = _services.contains(typeInfo);
118 _services[typeInfo].push_back(descriptor);
122 _services.insert({ typeInfo, { descriptor }});
130 [[nodiscard]] std::any
GetService(
const std::type_info& type)
final
132 const auto position = _services.find(type);
133 const auto notFound = (position == _services.end());
139 const auto descriptorList = position->second;
140 const auto& descriptor = descriptorList[0];
141 const auto factory = descriptor.GetFactory();
142 auto service = factory(*
this);
151 auto service = GetService(
typeid(T));
152 return std::any_cast<T>(service);
172 _descriptors.push_back(descriptor);
176 template<
class TService,
class TImplementation = TService>
181 static const auto& service = TImplementation();
184 return AddSingleton<TService, TImplementation>(factory);
187 template<
class TService,
class TImplementation = TService>
193 ServiceLifetime::Singleton
195 return Add(descriptor);
198 template<
class TService,
class TImplementation = TService>
203 TImplementation service;
206 return AddTransient<TService, TImplementation>(factory);
209 template<
class TService,
class TImplementation = TService>
215 ServiceLifetime::Transient
217 return Add(descriptor);
Definition: DependencyInjection.hpp:159
virtual IServiceCollection & Add(const ServiceDescriptor &descriptor)=0
Definition: DependencyInjection.hpp:98
virtual std::any GetService(const std::type_info &type)=0
Definition: DependencyInjection.hpp:165
ServiceCollection & AddSingleton(const ServiceFactory &factory)
Definition: DependencyInjection.hpp:188
ServiceProvider BuildServiceProvider() const
Definition: DependencyInjection.hpp:220
std::vector< ServiceDescriptor > _descriptors
Definition: DependencyInjection.hpp:167
ServiceCollection & AddTransient()
Definition: DependencyInjection.hpp:199
ServiceCollection & Add(const ServiceDescriptor &descriptor) final
Definition: DependencyInjection.hpp:170
ServiceCollection & AddTransient(const ServiceFactory &factory)
Definition: DependencyInjection.hpp:210
ServiceCollection & AddSingleton()
Definition: DependencyInjection.hpp:177
Definition: DependencyInjection.hpp:65
const ServiceLifetime _lifetime
Definition: DependencyInjection.hpp:69
auto & GetTypeInfo() const
Definition: DependencyInjection.hpp:80
auto GetFactory() const
Definition: DependencyInjection.hpp:84
ServiceFactory & operator=(const ServiceFactory &)=delete
const ServiceFactory _factory
Definition: DependencyInjection.hpp:68
ServiceFactory & operator=(ServiceFactory &&) noexcept=delete
ServiceDescriptor(const ServiceDescriptor &obj)=default
auto GetLifetime() const
Definition: DependencyInjection.hpp:88
const std::type_info & _typeInfo
Definition: DependencyInjection.hpp:67
ServiceDescriptor(ServiceDescriptor &&deadObj) noexcept=default
ServiceDescriptor(const std::type_info &typeInfo, ServiceFactory factory, const ServiceLifetime lifetime)
Definition: DependencyInjection.hpp:73
ServiceDescriptor()=delete
Definition: DependencyInjection.hpp:104
std::map< std::type_index, std::vector< ServiceDescriptor > > _services
Definition: DependencyInjection.hpp:106
auto GetService()
Definition: DependencyInjection.hpp:149
std::any GetService(const std::type_info &type) final
Definition: DependencyInjection.hpp:130
ServiceProvider(const std::vector< ServiceDescriptor > &descriptors)
Definition: DependencyInjection.hpp:108
Definition: DependencyInjection.hpp:40
std::function< std::any(IServiceProvider &)> ServiceFactory
Definition: DependencyInjection.hpp:45
ServiceLifetime
The lifetime of a service.
Definition: DependencyInjection.hpp:58