Arbutils/src/Memory/NonNullOwnPtr.hpp

35 lines
953 B
C++
Raw Normal View History

2020-03-28 20:08:55 +00:00
#ifndef ARBUTILS_NONNULLOWNPTR_HPP
#define ARBUTILS_NONNULLOWNPTR_HPP
#include "OwnPtr.hpp"
#include "../Assert.hpp"
#include "NonNullBorrowedPtr.hpp"
namespace Arbutils::Memory {
template <class T> class NonNullOwnPtr{
T* _ptr;
public:
inline explicit NonNullOwnPtr(T* ptr) : _ptr(ptr){
AssertNotNull(ptr);
};
NonNullOwnPtr<T>(const NonNullOwnPtr<T>&) = delete;
NonNullOwnPtr<T>& operator=(OwnPtr<T> const&) = delete;
inline ~NonNullOwnPtr() noexcept { delete _ptr; }
inline constexpr T* GetUnsafe() noexcept { return _ptr; }
inline constexpr const T* GetUnsafe() const noexcept { return _ptr; }
inline NonNullBorrowedPtr<T> Borrow(){
return NonNullBorrowedPtr<T>(_ptr);
}
T* operator->() noexcept { return _ptr; }
const T* operator->() const noexcept { return _ptr; }
};
}
#endif // ARBUTILS_NONNULLOWNPTR_HPP