UniquePtrService

template<class T>
class UniquePtrService : public sb::di::IServiceInstance

Public Types

using Ptr = std::unique_ptr<UniquePtrService>

Public Functions

inline explicit UniquePtrService(std::unique_ptr<T> service)
template<class ...Args>
inline explicit UniquePtrService(Args&&... args)
UniquePtrService(const UniquePtrService&) = delete
UniquePtrService(UniquePtrService&&) = default
UniquePtrService &operator=(const UniquePtrService&) = delete
UniquePtrService &operator=(UniquePtrService&&) = default
inline virtual void *get() const override

Returns service pointer as void *.

inline virtual void *getForMoveOut() override

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

inline virtual void *release() override

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

inline virtual TypeId getTypeId() const override

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

inline virtual bool isValid() const override

Checks if service instance is valid.

If service instance is invalid, get and release methods might lead to undefined behavior