DependencyInjection
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
DependencyInjection::ServiceProvider Class Reference

#include <DependencyInjection.hpp>

Inheritance diagram for DependencyInjection::ServiceProvider:
DependencyInjection::IServiceProvider

Public Member Functions

 ServiceProvider ()=delete
 
std::any GetService (const std::type_info &type) final
 
template<class T >
auto GetService ()
 
virtual std::any GetService (const std::type_info &type)=0
 

Private Member Functions

 ServiceProvider (const std::vector< ServiceDescriptor > &descriptors)
 

Private Attributes

std::map< std::type_index, std::vector< ServiceDescriptor > > _services
 

Friends

class ServiceCollection
 

Constructor & Destructor Documentation

◆ ServiceProvider() [1/2]

DependencyInjection::ServiceProvider::ServiceProvider ( const std::vector< ServiceDescriptor > &  descriptors)
inlineexplicitprivate
109 {
110 using std::ranges::reverse_view;
111
112 for (const auto& descriptor : reverse_view(descriptors))
113 {
114 const auto& typeInfo = descriptor.GetTypeInfo();
115 bool serviceExists = _services.contains(typeInfo);
116 if (serviceExists)
117 {
118 _services[typeInfo].push_back(descriptor);
119 }
120 else
121 {
122 _services.insert({ typeInfo, { descriptor }});
123 }
124 }
125 }
std::map< std::type_index, std::vector< ServiceDescriptor > > _services
Definition: DependencyInjection.hpp:106

◆ ServiceProvider() [2/2]

DependencyInjection::ServiceProvider::ServiceProvider ( )
delete

Member Function Documentation

◆ GetService() [1/2]

template<class T >
auto DependencyInjection::ServiceProvider::GetService ( )
inline
150 {
151 auto service = GetService(typeid(T));
152 return std::any_cast<T>(service);
153 }
auto GetService()
Definition: DependencyInjection.hpp:149

◆ GetService() [2/2]

std::any DependencyInjection::ServiceProvider::GetService ( const std::type_info &  type)
inlinefinalvirtual

Implements DependencyInjection::IServiceProvider.

131 {
132 const auto position = _services.find(type);
133 const auto notFound = (position == _services.end());
134 if (notFound)
135 {
136 return {};
137 }
138
139 const auto descriptorList = position->second;
140 const auto& descriptor = descriptorList[0];
141 const auto factory = descriptor.GetFactory();
142 auto service = factory(*this);
143
144 return service;
145 }

Friends And Related Function Documentation

◆ ServiceCollection

friend class ServiceCollection
friend

Member Data Documentation

◆ _services

std::map<std::type_index, std::vector<ServiceDescriptor> > DependencyInjection::ServiceProvider::_services
private

The documentation for this class was generated from the following file: