ServiceInstance#

class ServiceInstance#

Public Types

using Ptr = std::unique_ptr<ServiceInstance>#

Public Functions

ServiceInstance() = default#

Creates default invalid service instance.

explicit ServiceInstance(IServiceInstance::Ptr implementation, ptrdiff_t castOffset = 0)#

Created service instance with specified implementation and cast offset.

ServiceInstance(const ServiceInstance &other) = delete#
ServiceInstance(ServiceInstance &&other) = default#
ServiceInstance &operator=(const ServiceInstance &other) = delete#
ServiceInstance &operator=(ServiceInstance &&other) = default#
IServiceInstance *tryGetImplementation()#

Try get service instance implementation.

const IServiceInstance *tryGetImplementation() const#

Try get service instance implementation.

IServiceInstance &getImplementation()#

Get service instance implementation.

If service instance implementation is nullptr, method throws exception

Throws:

sb::di::NullPointerException

const IServiceInstance &getImplementation() const#

Get service instance implementation.

If service instance implementation is nullptr, method throws exception

Throws:

sb::di::NullPointerException

void addCastOffset(ptrdiff_t castOffset)#

cast offset is added to service pointer when get/release methods are used

void setCastOffset(ptrdiff_t castOffset)#

cast offset is added to service pointer when get/release methods are used

ptrdiff_t getCastOffset() const#

cast offset is added to service pointer when get/release methods are used

bool isValid() const#

Checks if service instance is valid (implementation is not nullptr and implementation is valid)

If service instance is invalid, get and move methods might lead to undefined behaviour

explicit operator bool() const#

Wrapper around isValid method.

template<class T>
inline T *getAs() const#

Returns service pointer as T *.

The client is responsible for ensuring that the T type is correct

Example:

T* service = instance.getAs<T>();

template<class T>
inline T *releaseAs()#

Releases service ownership as pointer T *.

The client is responsible for ensuring that the T type is correct

Example:

T* service = instance.releaseAs<T>();

Warning

Using this method might cause memory leaks, client is responsible for managing this pointner lifetime, the best approach is to immediately wrap this pointer with proper std::unique_ptr<T>

template<class T>
inline std::unique_ptr<T> moveOutAsUniquePtr()#

Moves out service as unique_ptr<T>

The client is responsible for ensuring that the T type is correct

Example:

std::unique_ptr<T> service = instance.moveOutAsUniquePtr<T>();

template<class T>
inline T &&moveOutAs()#

Moves out service as T.

The client is responsible for ensuring that the T type is correct

Example:

T service = instance.moveOutAs<T>();

template<class T>
inline T copyAs()#

Copies service as T.

The client is responsible for ensuring that the T type is correct

Example:

T service = instance.copyAs<T>();

void clear()#

Clears service instance (implemnentation is reset)

after clearing instance becomes invalid