2020-12-11 14:29:27 +00:00
|
|
|
#ifndef ARBUTILS___SCOPEDPTR_HPP
|
|
|
|
#define ARBUTILS___SCOPEDPTR_HPP
|
2020-12-11 13:58:16 +00:00
|
|
|
|
|
|
|
#include "../Assert.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* _raw;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// @brief Initialise a ScopedPtr with a specific raw pointer.
|
2020-12-11 14:12:17 +00:00
|
|
|
inline ScopedPtr<T>(T* ptr) : _raw(ptr){};
|
2020-12-11 13:58:16 +00:00
|
|
|
/// @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()){};
|
|
|
|
|
|
|
|
~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;
|
|
|
|
}
|
|
|
|
|
2020-12-11 14:22:21 +00:00
|
|
|
/// @brief Assign operator with raw pointer.
|
2020-12-11 14:12:17 +00:00
|
|
|
inline ScopedPtr<T>& operator=(T* rhs) {
|
2020-12-11 13:58:16 +00:00
|
|
|
if (_raw == &rhs)
|
|
|
|
return *this;
|
|
|
|
_raw = rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Operator for access into underlying pointer.
|
|
|
|
/// @warning Note that this asserts that the underlying pointer is not null first, to prevent segfaults.
|
2020-12-11 14:22:21 +00:00
|
|
|
inline T* operator->() const noexcept {
|
|
|
|
AssertNotNull(_raw);
|
|
|
|
return _raw;
|
|
|
|
}
|
2020-12-11 13:58:16 +00:00
|
|
|
|
|
|
|
/// @brief Get the raw underlying pointer.
|
|
|
|
inline T* TakeOwnership() const noexcept {
|
|
|
|
auto raw = _raw;
|
|
|
|
_raw = nullptr;
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @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* 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* rhs) const noexcept { return _raw == rhs; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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::ScopedPtr<T>& k) const { return (size_t)k.GetRaw(); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-11 14:29:27 +00:00
|
|
|
#endif // ARBUTILS___SCOPEDPTR_HPP
|