Arbutils/src/Memory/__UniquePtr.hpp

88 lines
3.5 KiB
C++

#ifndef ARBUTILS___UNIQUEPTR_HPP
#define ARBUTILS___UNIQUEPTR_HPP
#include "../Ensure.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* _raw;
public:
inline UniquePtr<T>() {}
/// @brief Initialise a UniquePtr with a specific raw pointer.
inline UniquePtr<T>(T* ptr) : _raw(ptr) { EnsureNotNull(ptr); };
/// @brief Initialise a UniquePtr from a copy.
inline UniquePtr<T>(const UniquePtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a UniquePtr with a std unique_ptr.
inline UniquePtr<T>(const std::unique_ptr<T>& other) : _raw(other.get()){};
#if !WINDOWS // This doesn't work on mingw-w64 for some reason
UniquePtr<T>(std::nullptr_t) = delete;
#endif
~UniquePtr() noexcept { delete _raw; }
/// @brief Copy operator.
inline UniquePtr<T>& operator=(const UniquePtr<T>& rhs) {
if (this == &rhs) {
return *this;
}
delete _raw;
_raw = rhs._raw;
return *this;
}
/// @brief Assign operator with raw pointer.
inline UniquePtr<T>& operator=(T* 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* operator->() const noexcept { return _raw; }
/// @brief Get the raw underlying pointer.
inline T* 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* 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* 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