Arbutils/src/Memory/__UniquePtr.hpp

88 lines
3.5 KiB
C++
Raw Normal View History

#ifndef ARBUTILS___UNIQUEPTR_HPP
#define ARBUTILS___UNIQUEPTR_HPP
2020-12-11 13:58:16 +00:00
#include "../Ensure.hpp"
#include "../Misc.hpp"
2020-12-11 13:58:16 +00:00
namespace ArbUt {
/// @brief A unique pointer is used to indicate a pointer that is owned by its holder, and will be deleted when its
2020-12-11 14:12:17 +00:00
/// owner is deleted. As with all Arbutils pointers, this cannot be assigned null. Use an OptionalUniquePtr for
/// pointers that can be null.
2020-12-11 13:58:16 +00:00
/// @details A unique pointer is used to indicate a pointer that is owned by an object, and that needs to be deleted
/// when its owner is deleted.
template <class T> class UniquePtr {
private:
T* non_null _raw;
2020-12-11 13:58:16 +00:00
public:
2020-12-11 15:14:12 +00:00
inline UniquePtr<T>() {}
2020-12-11 13:58:16 +00:00
/// @brief Initialise a UniquePtr with a specific raw pointer.
inline UniquePtr<T>(T* non_null ptr) : _raw(ptr) { EnsureNotNull(ptr) };
2022-05-14 14:14:30 +00:00
NO_COPY_OR_MOVE(UniquePtr)
2020-12-11 13:58:16 +00:00
2021-06-20 10:05:30 +00:00
#if !WINDOWS // This doesn't work on mingw-w64 for some reason
/// @brief Do not allow nullreference assignment
2020-12-11 14:12:17 +00:00
UniquePtr<T>(std::nullptr_t) = delete;
2021-06-20 10:05:30 +00:00
#endif
2020-12-11 14:12:17 +00:00
~UniquePtr() noexcept { delete _raw; }
2020-12-11 13:58:16 +00:00
/// @brief Assign operator with raw pointer.
inline UniquePtr<T>& operator=(T* non_null rhs) {
if (_raw == rhs) {
2020-12-11 13:58:16 +00:00
return *this;
2020-12-11 14:12:17 +00:00
}
delete _raw;
EnsureNotNull(rhs);
2020-12-11 13:58:16 +00:00
_raw = rhs;
return *this;
}
/// @brief Don't allow nullptr assignments
2020-12-11 14:12:17 +00:00
inline UniquePtr<T>& operator=(std::nullptr_t) = delete;
2020-12-11 13:58:16 +00:00
/// @brief Operator for access into underlying pointer.
/// @warning Note that this asserts that the underlying pointer is not null first, to prevent segfaults.
inline T* non_null operator->() const noexcept { return _raw; }
2020-12-11 13:58:16 +00:00
/// @brief Get the raw underlying pointer, and take ownership of it. This removes the existing pointer in here.
#if defined(__clang__)
[[clang::no_sanitize("nullability-assign")]]
#endif
inline T* non_null
TakeOwnership() noexcept {
auto raw = _raw;
_raw = nullptr;
return raw;
}
2020-12-11 13:58:16 +00:00
/// @brief Get the raw underlying pointer.
inline T* non_null GetRaw() const noexcept { return _raw; }
2020-12-11 13:58:16 +00:00
/// @brief Check equality of two UniquePtr objects
inline bool operator==(const UniquePtr& rhs) const noexcept { return _raw == rhs._raw; }
/// @brief Check equality of pointers
inline bool operator==(T* non_null rhs) const noexcept { return _raw == rhs; }
/// @brief Delete comparison with nullptr, UniquePtr can't be null
inline bool operator==(std::nullptr_t) const = delete;
2020-12-11 13:58:16 +00:00
/// @brief Check equality of two UniquePtr objects
inline bool operator!=(const UniquePtr& rhs) const noexcept { return _raw != rhs._raw; }
/// @brief Check equality of pointers
inline bool operator!=(T* non_null rhs) const noexcept { return _raw != rhs; }
/// @brief Delete comparison with nullptr, UniquePtr can't be null
inline bool operator!=(std::nullptr_t) const = delete;
2020-12-11 13:58:16 +00:00
};
}
namespace std {
/// @brief Helper class for allowing hashing of UniquePtr.
template <class T> struct hash<ArbUt::UniquePtr<T>> {
/// @brief Returns a hash of for a borrowed Pointer. Effectively just the raw memory address.
/// @param k A borrowed pointer.
/// @return The hash of the borrowed pointer.
std::size_t operator()(const ArbUt::UniquePtr<T>& k) const { return (size_t)k.GetRaw(); }
};
}
#endif // ARBUTILS___UNIQUEPTR_HPP