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(const ServiceCollection&) = default#
ServiceCollection(ServiceCollection&&) = default#
ServiceCollection &operator=(const ServiceCollection&) = default#
ServiceCollection &operator=(ServiceCollection&&) = default#
IServiceProvider::Ptr buildServiceProvider(ServiceProviderOptions options = {})#

Builds service provider with specified options.

might throw exceptions

Throws:
inline Iterator begin()#
inline Iterator end()#
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(const TPred &pred)#

Find first descriptor meeting TPred requirement.

Template Parameters:

TPred – is functor with this sheme: (const ServiceDescriptor&) -> bool

template<class TPred>
inline ConstIterator findIf(const TPred &pred) const#

Find first descriptor meeting TPred requirement.

Template Parameters:

TPred – is functor with this sheme: (const ServiceDescriptor&) -> bool

template<class TPred>
inline bool containsIf(const TPred &pred) const#

Cheks if contains descriptor meeting TPred requirement.

Template Parameters:

TPred – is functor with this sheme: (const ServiceDescriptor&) -> bool

template<class TService>
inline bool contains() const#

Cheks if contains descriptor matching requirement.

descriptor.getServiceTypeId() == typeid(TService)
bool contains(TypeId serviceTypeId) const#

Cheks if contains descriptor matching requirement.

descriptor.getServiceTypeId() == serviceTypeId
template<class TService, class TImplementation = TService>
inline bool containsExact() const#

Cheks if contains descriptor matching requirement.

descriptor.getImplementationTypeId() == typeid(TImplementation) && descriptor.getServiceTypeId() ==
typeid(TService)
bool containsExact(TypeId serviceTypeId, TypeId implementationTypeId) const#

Cheks 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(const TPred &pred)#

Removes all descriptors meeting TPred requirement.

Returns number of removed elements

Template Parameters:

TPred – is functor with this sheme: (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(ServiceLifeTime lifeTime)#

Adds ServiceDescriptor.

Adds service descriptor with: lifetime - given lifetime, serviceTypeId - typeid(TService), implementationTypeId - typeid(TImplementation), factory - default factory using TImplementation constructor

Example:

ServiceCollection{}.add<TestClass>(ServiceLifeTime::scoped());
ServiceCollection{}.add<BaseClass, ImplementationClass>(ServiceLifeTime::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 ServiceDescriptor.

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 ServiceDescriptor.

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 ServiceDescriptor.

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 ServiceDescriptor.

Adds service descriptor with: lifetime - singleton, serviceTypeId - typeid(TService), implementationTypeId - typeid(TService), factory - factory with external service pointner

Example:

TestClass test;
ServiceCollection{}.addSingleton(&test);

Template Parameters:

TService – service type must have one constructor

template<class TService, class TImplementation>
inline ServiceCollection &addSingleton(TImplementation *service)#

Adds ServiceDescriptor.

Adds service descriptor with: lifetime - singleton, serviceTypeId - typeid(TService), implementationTypeId - typeid(TImplementation), factory - factory with external service pointner

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 and must have one constructor

template<class TService, class FactoryFcn>
inline ServiceCollection &add(ServiceLifeTime lifeTime, FactoryFcn factory)#

Adds ServiceDescriptor.

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>(ServiceLifeTime::scoped(),
      []() { return std::make_unique<ImplementationClass>(); });

See also

Constructor requirements

Template Parameters:
  • TService – base service type

  • FactoryFcn – is factory functor with this sheme: (const ServiceDescriptor&) -> std::unique_ptr<TImplementation> (argument is optional), functor must be copyable and movable, implementation type must inherit from TService and must have one constructor

template<class TService, class FactoryFcn>
inline ServiceCollection &addSingleton(FactoryFcn factory)#

Adds ServiceDescriptor.

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>(); });

See also

Constructor requirements

Template Parameters:
  • TService – base service type

  • FactoryFcn – is factory functor with this sheme: (const ServiceDescriptor&) -> std::unique_ptr<TImplementation> (argument is optional), functor must be copyable and movable, implementation type must inherit from TService and must have one constructor

template<class TService, class FactoryFcn>
inline ServiceCollection &addScoped(FactoryFcn factory)#

Adds ServiceDescriptor.

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>(); });

See also

Constructor requirements

Template Parameters:
  • TService – base service type

  • FactoryFcn – is factory functor with this sheme: (const ServiceDescriptor&) -> std::unique_ptr<TImplementation> (argument is optional), functor must be copyable and movable, implementation type must inherit from TService and must have one constructor

template<class TService, class FactoryFcn>
inline ServiceCollection &addTransient(FactoryFcn factory)#

Adds ServiceDescriptor.

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>(); });

See also

Constructor requirements

Template Parameters:
  • TService – base service type

  • FactoryFcn – is factory functor with this sheme: (const ServiceDescriptor&) -> std::unique_ptr<TImplementation> (argument is optional), functor must be copyable and movable, implementation type must inherit from TService and must have one constructor

template<class FactoryFcn>
inline ServiceCollection &add(ServiceLifeTime lifeTime, FactoryFcn factory)#

Adds ServiceDescriptor.

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(ServiceLifeTime::transient(), []() { return std::make_unique<TestClass>(); });

See also

Constructor requirements

Template Parameters:
  • TService – base service type

  • FactoryFcn – is factory functor with this sheme: (const ServiceDescriptor&) -> std::unique_ptr<TImplementation> (argument is optional), functor must be copyable and movable, implementation type must have one constructor

template<class FactoryFcn>
inline ServiceCollection &addSingleton(FactoryFcn factory)#

Adds ServiceDescriptor.

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>(); });

See also

Constructor requirements

Template Parameters:
  • TService – base service type

  • FactoryFcn – is factory functor with this sheme: (const ServiceDescriptor&) -> std::unique_ptr<TImplementation> (argument is optional), functor must be copyable and movable, implementation type must have one constructor

template<class FactoryFcn>
inline ServiceCollection &addScoped(FactoryFcn factory)#

Adds ServiceDescriptor.

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>(); });

See also

Constructor requirements

Template Parameters:
  • TService – base service type

  • FactoryFcn – is factory functor with this sheme: (const ServiceDescriptor&) -> std::unique_ptr<TImplementation> (argument is optional), functor must be copyable and movable, implementation type must have one constructor

template<class FactoryFcn>
inline ServiceCollection &addTransient(FactoryFcn factory)#

Adds ServiceDescriptor.

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>(); });

See also

Constructor requirements

Template Parameters:
  • TService – base service type

  • FactoryFcn – is factory functor with this sheme: (const ServiceDescriptor&) -> std::unique_ptr<TImplementation> (argument is optional), functor must be copyable and movable, implementation type must have one constructor