Arbutils/src/Memory/__UniquePtr.hpp

90 lines
3.6 KiB
C++

#ifndef ARBUTILS___UNIQUEPTR_HPP
#define ARBUTILS___UNIQUEPTR_HPP
#include "../Ensure.hpp"
#include "../Misc.hpp"
namespace ArbUt {
/// @brief A unique pointer is used to indicate a pointer that is owned by its holder, and will be deleted when its
/// owner is deleted. As with all Arbutils pointers, this cannot be assigned null. Use an OptionalUniquePtr for
/// pointers that can be null.
/// @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;
public:
inline UniquePtr<T>() {}
/// @brief Initialise a UniquePtr with a specific raw pointer.
inline UniquePtr<T>(T* non_null ptr) : _raw(ptr){EnsureNotNull(ptr)};
/// @brief We do not allow copying or moving of unique pointers, as that would cause memory leaks and use after
/// frees.
NO_COPY_OR_MOVE(UniquePtr);
#if !WINDOWS // This doesn't work on mingw-w64 for some reason
/// @brief Do not allow nullreference assignment
UniquePtr<T>(std::nullptr_t) = delete;
#endif
~UniquePtr() noexcept { delete _raw; }
/// @brief Assign operator with raw pointer.
inline UniquePtr<T>& operator=(T* non_null rhs) {
if (_raw == rhs) {
return *this;
}
delete _raw;
EnsureNotNull(rhs);
_raw = rhs;
return *this;
}
/// @brief Don't allow nullptr assignments
inline UniquePtr<T>& operator=(std::nullptr_t) = delete;
/// @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; }
/// @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;
}
/// @brief Get the raw underlying pointer.
inline T* non_null GetRaw() const noexcept { return _raw; }
/// @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;
/// @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;
};
}
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