Arbutils/src/Memory/__ScopedPtr.hpp

94 lines
3.8 KiB
C++

#ifndef ARBUTILS___SCOPEDPTR_HPP
#define ARBUTILS___SCOPEDPTR_HPP
#include "../Ensure.hpp"
namespace ArbUt {
/// @brief An optional unique pointer is used to indicate a pointer that is owned by its holder, and will be deleted
/// when its owner is deleted.
/// @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 ScopedPtr {
private:
T* non_null _raw;
public:
/// @brief Initialise a ScopedPtr with a specific raw pointer.
inline ScopedPtr<T>(T* non_null ptr) : _raw(ptr) { EnsureNotNull(_raw); };
/// @brief Initialise a ScopedPtr from a copy.
inline ScopedPtr<T>(const ScopedPtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a ScopedPtr with a std unique_ptr.
inline ScopedPtr<T>(const std::unique_ptr<T>& other) : _raw(other.get()){};
#if !WINDOWS // This doesn't work on mingw-w64 for some reason
ScopedPtr<T>(std::nullptr_t) = delete;
#endif
~ScopedPtr() noexcept { delete _raw; }
/// @brief Copy operator.
inline ScopedPtr<T>& operator=(const ScopedPtr<T>& rhs) {
if (this == &rhs) {
return *this;
}
_raw = rhs._raw;
return *this;
}
/// @brief Assign operator with raw pointer.
inline ScopedPtr<T>& operator=(T* non_null rhs) {
if (_raw == rhs) {
return *this;
}
EnsureNotNull(rhs);
_raw = rhs;
return *this;
}
/// @brief Operator for access into underlying pointer.
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, without taking ownership of it.
inline T* non_null GetValue() noexcept { return _raw; }
/// @brief Delete comparison with nullptr, ScopedPtr can't be null
inline bool operator==(std::nullptr_t) const = delete;
/// @brief Check equality of two ScopedPtr objects
inline bool operator==(const ScopedPtr& 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 Check equality of two ScopedPtr objects
inline bool operator!=(const ScopedPtr& 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 Implicit cast to retrieve raw pointer.
inline operator T* non_null() const noexcept { return _raw; }
/// @brief Implicit cast to retrieve borrowed ptr.
inline operator BorrowedPtr<T>() const noexcept { return _raw; }
/// @brief Implicit cast to retrieve optional borrowed pointer.
inline operator OptionalBorrowedPtr<T>() const noexcept { return _raw; }
};
}
namespace std {
/// @brief Helper class for allowing hashing of ScopedPtr.
template <class T> struct hash<ArbUt::ScopedPtr<T>> {
/// @brief Returns a hash of for a scoped Pointer. Effectively just the raw memory address.
/// @param k A scoped pointer.
/// @return The hash of the scoped pointer.
std::size_t operator()(const ArbUt::ScopedPtr<T>& k) const { return (size_t)k.GetRaw(); }
};
}
#endif // ARBUTILS___SCOPEDPTR_HPP