Arbutils/src/Memory/NonNullOwnPtr.hpp

39 lines
1.5 KiB
C++

#ifndef ARBUTILS_NONNULLOWNPTR_HPP
#define ARBUTILS_NONNULLOWNPTR_HPP
#include "../Assert.hpp"
#include "NonNullBorrowedPtr.hpp"
#include "BorrowedPtr.hpp"
namespace Arbutils::Memory {
template <class T> class NonNullOwnPtr {
T* _ptr;
public:
inline NonNullOwnPtr(){};
inline NonNullOwnPtr(T* ptr) : _ptr(ptr) { AssertNotNull(ptr); };
NonNullOwnPtr<T>(const NonNullOwnPtr<T>&) = 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); }
inline const NonNullBorrowedPtr<const T> Borrow() const { return NonNullBorrowedPtr<const T>(_ptr); }
inline BorrowedPtr<T> BorrowOptional() { return BorrowedPtr<T>(_ptr); }
inline const BorrowedPtr<const T> BorrowOptional() const { return BorrowedPtr<const T>(_ptr); }
T* operator->() noexcept { return _ptr; }
T* operator->() const noexcept { return _ptr; }
inline bool operator==(const NonNullOwnPtr& rhs) const noexcept { return _ptr == rhs._ptr; }
inline bool operator!=(const NonNullOwnPtr& rhs) const noexcept { return _ptr != rhs._ptr; }
inline bool operator==(const T* rhs) const noexcept { return _ptr == rhs; }
inline bool operator!=(const T* rhs) const noexcept { return _ptr != rhs; }
};
}
#endif // ARBUTILS_NONNULLOWNPTR_HPP