IServiceInstance#

struct IServiceInstance#

Subclassed by sb::di::details::services::ExternalService< T >, sb::di::details::services::InPlaceService< T >, sb::di::details::services::UniquePtrService< T >

Public Types

using Ptr = std::unique_ptr<IServiceInstance>#

Public Functions

virtual void *get() const = 0#

Returns service pointer as void *.

virtual void *getForMoveOut() = 0#

Returns service pointer as void *,.

Method is used to ensure that service can be moved out

Throws:

sb::di::CannotMoveOutServiceException – Example:

void* service = instance->getForMoveOut();

virtual void *release() = 0#

Releases service ownership as void *.

If instance is owner of service it will release this ownership just like std::unique_ptr<T>::release(), otherwise it will throw exception

Example:

std::unique_ptr<T> service{static_cast<T *>(instance->release())};

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>

Throws:

sb::di::CannotReleaseServiceException

virtual TypeId getTypeId() const = 0#

Get the TypeId of service.

This method can be used to check if casting is safe

Example:

if(instance->getTypeId() == typeid(T)) {
     T* service = instance->getAs<T>();
}

virtual bool isValid() const = 0#

Checks if service instance is valid.

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

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

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

virtual ~IServiceInstance() = default#