Arbutils/src/Memory/NonNullOwnPtr.hpp

39 lines
1.5 KiB
C++
Raw Normal View History

2020-03-28 20:08:55 +00:00
#ifndef ARBUTILS_NONNULLOWNPTR_HPP
#define ARBUTILS_NONNULLOWNPTR_HPP
#include "../Assert.hpp"
#include "NonNullBorrowedPtr.hpp"
#include "BorrowedPtr.hpp"
2020-03-28 20:08:55 +00:00
namespace Arbutils::Memory {
2020-03-29 16:09:55 +00:00
template <class T> class NonNullOwnPtr {
2020-03-28 20:08:55 +00:00
T* _ptr;
public:
inline NonNullOwnPtr(){};
inline NonNullOwnPtr(T* ptr) : _ptr(ptr) { AssertNotNull(ptr); };
2020-03-28 20:08:55 +00:00
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; }
2020-03-29 16:09:55 +00:00
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); }
2020-03-29 17:14:30 +00:00
2020-03-28 20:08:55 +00:00
T* operator->() noexcept { return _ptr; }
const T* operator->() const noexcept { return _ptr; }
2020-03-29 16:09:55 +00:00
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; }
2020-03-28 20:08:55 +00:00
};
}
#endif // ARBUTILS_NONNULLOWNPTR_HPP