IServiceInstance#

struct IServiceInstance#

Interface for all service instances.

Subclassed by sb::di::details::ExternalService< T >, sb::di::details::ServiceOwner< T >

Public Types

using Ptr = std::unique_ptr<IServiceInstance>#

Public Functions

virtual void *get() const = 0#

Returns service pointner as void *.

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

Returns service pointner as T *.

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

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

virtual void *moveOut() = 0#

Moves out service pointner from instance 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

Warning

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

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

Throws:

CannotMoveOutServiceException – cannot move out service

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

Moves out service pointner from instance as T *.

This method bahaves exactly the same as moveOut method except that its casting type, the client is responsible for ensuring that the T type is correct

Warning

Using this method might couse memory leaks, clietn is responsible for managing this pointner lifetime, the best approach is to imediatly wrap this poinrner with proper std::unique_ptr<T>

std::unique_ptr<T> service{instance->moveOutAs<T>()};

Throws:

CannotMoveOutServiceException – cannot move out service

virtual TypeId getTypeId() const = 0#

Get the TypeId of service.

This method can be used to check if casting is safe

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 operator bool() const#

Wrapper around isValid method.

virtual ~IServiceInstance() = default#