ServiceCollection#
-
class ServiceCollection#
Public Types
-
using Iterator = std::vector<ServiceDescriptor>::iterator#
-
using ConstIterator = std::vector<ServiceDescriptor>::const_iterator#
-
using ReverseIterator = std::vector<ServiceDescriptor>::reverse_iterator#
-
using ConstReverseIterator = std::vector<ServiceDescriptor>::const_reverse_iterator#
-
using Ptr = std::unique_ptr<ServiceCollection>#
Public Functions
-
ServiceCollection() = default#
-
ServiceCollection(std::initializer_list<ServiceDescriptor> list)#
-
ServiceCollection(const ServiceCollection&) = default#
-
ServiceCollection(ServiceCollection&&) = default#
-
ServiceCollection &operator=(const ServiceCollection&) = default#
-
ServiceCollection &operator=(ServiceCollection&&) = default#
-
ServiceProvider buildServiceProvider(ServiceProviderOptions options = {})#
Builds service provider with specified options.
might throw exceptions
- Throws:
sb::di::ServiceAlreadyRegisteredException – if service was already registered
sb::di::ServiceLifeTimeMismatchException – if service has different lifetime than other already registered with same base type
-
ServiceProvider::Ptr buildServiceProviderAsPtr(ServiceProviderOptions options = {})#
Builds service provider as unique_ptr with specified options.
might throw exceptions
- Throws:
sb::di::ServiceAlreadyRegisteredException – if service was already registered
sb::di::ServiceLifeTimeMismatchException – if service has different lifetime than other already registered with same base type
-
inline ConstIterator cBegin() const#
-
inline ConstIterator cEnd() const#
-
inline ConstIterator begin() const#
-
inline ConstIterator end() const#
-
inline ReverseIterator rBegin()#
-
inline ReverseIterator rEnd()#
-
inline ConstReverseIterator crBegin() const#
-
inline ConstReverseIterator crEnd() const#
-
inline ConstReverseIterator rbegin() const#
-
inline ConstReverseIterator rend() const#
-
ServiceDescriptor &at(size_t index)#
Returns service descriptor at giver position.
might throw exception
- Throws:
std::out_of_range – if index >= size()
-
const ServiceDescriptor &at(size_t index) const#
Returns service descriptor at giver position.
might throw exception
- Throws:
std::out_of_range – if index >= size()
-
ServiceDescriptor &first()#
Returns first descriptor.
might throw exception
- Throws:
std::out_of_range – if empty()
-
const ServiceDescriptor &first() const#
Returns first descriptor.
might throw exception
- Throws:
std::out_of_range – if empty()
-
ServiceDescriptor &last()#
Returns last descriptor.
might throw exception
- Throws:
std::out_of_range – if empty()
-
const ServiceDescriptor &last() const#
Returns last descriptor.
might throw exception
- Throws:
std::out_of_range – if empty()
-
ServiceDescriptor &operator[](size_t index)#
Returns service descriptor at giver position.
might throw exception
- Throws:
std::out_of_range – if index >= size()
-
const ServiceDescriptor &operator[](size_t index) const#
Returns service descriptor at giver position.
might throw exception
- Throws:
std::out_of_range – if index >= size()
-
size_t size() const#
Returns number of stored descriptors.
-
size_t count() const#
Returns number of stored descriptors.
-
bool empty() const#
Returns true if there are no descriptors.
-
size_t capacity() const#
Returns capacity.
-
void reserve(size_t space)#
Reserves space for descriptors.
-
void shrinkToFit()#
Shrinks to fit current size of descriptors.
-
void clear()#
Removes all descriptors.
-
template<class TPred>
inline Iterator findIf(TPred pred)# Find first descriptor meeting TPred requirement.
- Template Parameters:
TPred – is functor with this scheme: (const ServiceDescriptor&) -> bool
-
template<class TPred>
inline ConstIterator findIf(TPred pred) const# Find first descriptor meeting TPred requirement.
- Template Parameters:
TPred – is functor with this scheme: (const ServiceDescriptor&) -> bool
-
template<class TPred>
inline bool containsIf(TPred pred) const# Checks if contains descriptor meeting TPred requirement.
- Template Parameters:
TPred – is functor with this scheme: (const ServiceDescriptor&) -> bool
-
template<class TService>
inline bool contains() const# Checks if contains descriptor matching requirement.
descriptor.getServiceTypeId() == typeid(TService)
-
bool contains(TypeId serviceTypeId) const#
Checks if contains descriptor matching requirement.
descriptor.getServiceTypeId() == serviceTypeId
-
template<class TService, class TImplementation = TService>
inline bool containsExact() const# Checks if contains descriptor matching requirement.
descriptor.getImplementationTypeId() == typeid(TImplementation) && descriptor.getServiceTypeId() == typeid(TService)
-
bool containsExact(TypeId serviceTypeId, TypeId implementationTypeId) const#
Checks if contains descriptor matching requirement.
descriptor.getImplementationTypeId() == implementationTypeId && descriptor.getServiceTypeId() == serviceTypeId
-
Iterator insert(ConstIterator pos, ServiceDescriptor descriptor)#
Inserts descriptor before giver iterator.
Returns iterator pointing to the inserted
-
ServiceCollection &add(ServiceDescriptor descriptor)#
Adds descriptor to the end of list.
-
ServiceCollection &add(const ServiceCollection &collection)#
Adds descriptors from collection to the end of list.
-
Iterator remove(Iterator pos)#
Removes descriptor with given iterator.
Returns iterator following the last removed element
-
Iterator remove(ConstIterator pos)#
Removes descriptor with given iterator.
Returns iterator following the last removed element
-
Iterator removeRange(Iterator begin, Iterator end)#
Removes descriptors between given iterators.
Returns iterator following the last removed element
-
Iterator removeRange(ConstIterator begin, ConstIterator end)#
Removes descriptors between given iterators.
Returns iterator following the last removed element
-
template<class TPred>
inline size_t removeIf(TPred pred)# Removes all descriptors meeting TPred requirement.
Returns number of removed elements
- Template Parameters:
TPred – is functor with this scheme: (const ServiceDescriptor&) -> bool
-
template<class TService>
inline size_t removeAll()# Removes all descriptors meeting requirement.
descriptor.getServiceTypeId() == typeid(TService)
Returns number of removed elements
-
size_t removeAll(TypeId serviceTypeId)#
Removes all descriptors meeting requirement.
descriptor.getServiceTypeId() == serviceTypeId
Returns number of removed elements
-
template<class TService, class TImplementation = TService>
inline size_t remove()# Removes all descriptors meeting requirement.
descriptor.getImplementationTypeId() == typeid(TImplementation) && descriptor.getServiceTypeId() == typeid(TService)
Returns number of removed elements
-
size_t remove(TypeId serviceTypeId, TypeId implementationTypeId)#
Removes all descriptors meeting requirement.
descriptor.getImplementationTypeId() == implementationTypeId && descriptor.getServiceTypeId() == serviceTypeId
Returns number of removed elements
-
void pop()#
Removes last descriptor.
-
template<class TService, class TImplementation = TService>
inline ServiceCollection &add(const ServiceLifeTime lifeTime)# Adds service descriptor.
Adds service descriptor with: lifetime - given lifetime, serviceTypeId - typeid(TService), implementationTypeId - typeid(TImplementation), factory - default factory using TImplementation constructor
Example:
ServiceCollection{}.add<TestClass>(ServiceLifeTimes::Scoped); ServiceCollection{}.add<BaseClass, ImplementationClass>(ServiceLifeTimes::Transient);
See also
Constructor requirements
- Template Parameters:
TService – base service type
TImplementation – service implementation type must inherit from TService and must have one constructor
-
template<class TService, class TImplementation = TService>
inline ServiceCollection &addSingleton()# Adds service descriptor.
Adds service descriptor with: lifetime - singleton, serviceTypeId - typeid(TService), implementationTypeId - typeid(TImplementation), factory - default factory using TImplementation constructor
Example:
ServiceCollection{}.addSingleton<TestClass>(); ServiceCollection{}.addSingleton<BaseClass, ImplementationClass>();
See also
Constructor requirements
- Template Parameters:
TService – base service type
TImplementation – service implementation type must inherit from TService and must have one constructor
-
template<class TService, class TImplementation = TService>
inline ServiceCollection &addScoped()# Adds service descriptor.
Adds service descriptor with: lifetime - scoped, serviceTypeId - typeid(TService), implementationTypeId - typeid(TImplementation), factory - default factory using TImplementation constructor
Example:
ServiceCollection{}.addScoped<TestClass>(); ServiceCollection{}.addScoped<BaseClass, ImplementationClass>();
See also
Constructor requirements
- Template Parameters:
TService – base service type
TImplementation – service implementation type must inherit from TService and must have one constructor
-
template<class TService, class TImplementation = TService>
inline ServiceCollection &addTransient()# Adds service descriptor.
Adds service descriptor with: lifetime - transient, serviceTypeId - typeid(TService), implementationTypeId - typeid(TImplementation), factory - default factory using TImplementation constructor
Example:
ServiceCollection{}.addTransient<TestClass>(); ServiceCollection{}.addTransient<BaseClass, ImplementationClass>();
See also
Constructor requirements
- Template Parameters:
TService – base service type
TImplementation – service implementation type must inherit from TService and must have one constructor
-
template<class TService>
inline ServiceCollection &addSingleton(TService *service)# Adds service descriptor.
Adds service descriptor with: lifetime - singleton, serviceTypeId - typeid(TService), implementationTypeId - typeid(TService), factory - factory with external service pointer
Example:
TestClass test; ServiceCollection{}.addSingleton(&test);
- Template Parameters:
TService – service type
-
template<class TService, class TImplementation>
inline ServiceCollection &addSingleton(TImplementation *service)# Adds service descriptor.
Adds service descriptor with: lifetime - singleton, serviceTypeId - typeid(TService), implementationTypeId - typeid(TImplementation), factory - factory with external service pointer
Example:
ImplementationClass implementation; ServiceCollection{}.addSingleton<BaseClass>(&implementation);
See also
Constructor requirements
- Template Parameters:
TService – base service type
TImplementation – service implementation type must inherit from TService
-
template<class TService, class FactoryFcn>
inline ServiceCollection &add(const ServiceLifeTime lifeTime, FactoryFcn factory)# Adds service descriptor.
Adds service descriptor with: lifetime - singleton, serviceTypeId - given service lifetime, implementationTypeId - extracted from factory return type, factory - default factory using FactoryFcn factory functor
Example:
ServiceCollection{}.add<BaseClass>(ServiceLifeTimes::Scoped, []() { return std::make_unique<ImplementationClass>(); });
- Template Parameters:
TService – base service type
FactoryFcn – is factory functor with this scheme: (Services…) -> std::unique_ptr<TImplementation> | TImplementation, where services are pointers, unique pointers, references, vectors with pointers or unique pointers
-
template<class TService, class FactoryFcn>
inline ServiceCollection &addSingleton(FactoryFcn factory)# Adds service descriptor.
Adds service descriptor with: lifetime - singleton, serviceTypeId - typeid(TService), implementationTypeId - extracted from factory return type, factory - default factory using FactoryFcn factory functor
Example:
ServiceCollection{}.addSingleton<BaseClass>([]() { return std::make_unique<ImplementationClass>(); });
- Template Parameters:
TService – base service type
FactoryFcn – is factory functor with this scheme: (Services…) -> std::unique_ptr<TImplementation> | TImplementation, where services are pointers, unique pointers, references, vectors with pointers or unique pointers
-
template<class TService, class FactoryFcn>
inline ServiceCollection &addScoped(FactoryFcn factory)# Adds service descriptor.
Adds service descriptor with: lifetime - scoped, serviceTypeId - typeid(TService), implementationTypeId - extracted from factory return type, factory - default factory using FactoryFcn factory functor
Example:
ServiceCollection{}.addScoped<BaseClass>([]() { return std::make_unique<ImplementationClass>(); });
- Template Parameters:
TService – base service type
FactoryFcn – is factory functor with this scheme: (Services…) -> std::unique_ptr<TImplementation> | TImplementation, where services are pointers, unique pointers, references, vectors with pointers or unique pointers
-
template<class TService, class FactoryFcn>
inline ServiceCollection &addTransient(FactoryFcn factory)# Adds service descriptor.
Adds service descriptor with: lifetime - transient, serviceTypeId - typeid(TService), implementationTypeId - extracted from factory return type, factory - default factory using FactoryFcn factory functor
Example:
ServiceCollection{}.addTransient<BaseClass>([]() { return std::make_unique<ImplementationClass>(); });
- Template Parameters:
TService – base service type
FactoryFcn – is factory functor with this scheme: (Services…) -> std::unique_ptr<TImplementation> | TImplementation, where services are pointers, unique pointers, references, vectors with pointers or unique pointers
-
template<class FactoryFcn>
inline ServiceCollection &add(const ServiceLifeTime lifeTime, FactoryFcn factory)# Adds service descriptor.
Adds service descriptor with: lifetime - given service lifetime, serviceTypeId - extracted from factory return type, implementationTypeId - extracted from factory return type, factory - default factory using FactoryFcn factory functor
Example:
ServiceCollection{}.add(ServiceLifeTimes::Transient, []() { return std::make_unique<TestClass>(); });
- Template Parameters:
FactoryFcn – is factory functor with this scheme: (Services…) -> std::unique_ptr<TImplementation> | TImplementation, where services are pointers, unique pointers, references, vectors with pointers or unique pointers
-
template<class FactoryFcn>
inline ServiceCollection &addSingleton(FactoryFcn factory)# Adds service descriptor.
Adds service descriptor with: lifetime - singleton, serviceTypeId - extracted from factory return type, implementationTypeId - extracted from factory return type, factory - default factory using FactoryFcn factory functor
Example:
ServiceCollection{}.addSingleton([]() { return std::make_unique<TestClass>(); });
- Template Parameters:
FactoryFcn – is factory functor with this scheme: (Services…) -> std::unique_ptr<TImplementation> | TImplementation, where services are pointers, unique pointers, references, vectors with pointers or unique pointers
-
template<class FactoryFcn>
inline ServiceCollection &addScoped(FactoryFcn factory)# Adds service descriptor.
Adds service descriptor with: lifetime - scoped, serviceTypeId - extracted from factory return type, implementationTypeId - extracted from factory return type, factory - default factory using FactoryFcn factory functor
Example:
ServiceCollection{}.addScoped([]() { return std::make_unique<TestClass>(); });
- Template Parameters:
FactoryFcn – is factory functor with this scheme: (Services…) -> std::unique_ptr<TImplementation> | TImplementation, where services are pointers, unique pointers, references, vectors with pointers or unique pointers
-
template<class FactoryFcn>
inline ServiceCollection &addTransient(FactoryFcn factory)# Adds service descriptor.
Adds service descriptor with: lifetime - transient, serviceTypeId - extracted from factory return type, implementationTypeId - extracted from factory return type, factory - default factory using FactoryFcn factory functor
Example:
ServiceCollection{}.addTransient([]() { return std::make_unique<TestClass>(); });
- Template Parameters:
FactoryFcn – is factory functor with this scheme: (Services…) -> std::unique_ptr<TImplementation> | TImplementation, where services are pointers, unique pointers, references, vectors with pointers or unique pointers
-
template<class TAlias, class TService>
inline ServiceCollection &addAlias()# Adds service descriptor.
Adds service descriptor with: lifetime - scoped, serviceTypeId - typeid(TAlias), implementationTypeId - typeid(TService), factory - nullptr
Example:
ServiceCollection{}.addAlias<AliasClass, ServiceClass>();
- Template Parameters:
TAlias – base service type - alias type
TService – service type must inherit from TService
-
using Iterator = std::vector<ServiceDescriptor>::iterator#