ServiceInstance

class ServiceInstance

Public Types

using Ptr = std::unique_ptr<ServiceInstance>

Public Functions

ServiceInstance() = default

Creates default invalid service instance.

inline explicit ServiceInstance(IServiceInstance::Ptr implementation, const 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
inline IServiceInstance *tryGetImplementation()

Try to get service instance implementation.

inline const IServiceInstance *tryGetImplementation() const

Try to get service instance implementation.

inline IServiceInstance &getImplementation()

Get service instance implementation.

If service instance implementation is nullptr, method throws exception

Throws:

sb::di::NullPointerException

inline const IServiceInstance &getImplementation() const

Get service instance implementation.

If service instance implementation is nullptr, method throws exception

Throws:

sb::di::NullPointerException

inline void addCastOffset(const ptrdiff_t castOffset)

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

inline void setCastOffset(const ptrdiff_t castOffset)

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

inline ptrdiff_t getCastOffset() const

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

inline 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 behavior

inline 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 the 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 pointer 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 the 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 the 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 the correct

Example:

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

inline void clear()

Clears service instance (implementation is reset)

after clearing instance becomes invalid