2020-05-26 15:39:27 +00:00
|
|
|
#ifndef ARBUTILS_BORROWEDPTR_HPP
|
|
|
|
#define ARBUTILS_BORROWEDPTR_HPP
|
|
|
|
|
|
|
|
#include <memory>
|
2020-06-26 11:12:58 +00:00
|
|
|
#include "../Assert.hpp"
|
2020-05-26 15:39:27 +00:00
|
|
|
|
|
|
|
namespace ArbUt {
|
|
|
|
/// A borrowed pointer is used to indicate a pointer that is not owned by an object, but instead borrowed from
|
|
|
|
/// another owning object that is assumed to always be kept alive during the entire lifetime of the borrowing
|
|
|
|
/// object.
|
|
|
|
template <class T> class BorrowedPtr {
|
|
|
|
private:
|
|
|
|
T* _raw;
|
|
|
|
|
|
|
|
public:
|
2020-06-20 15:45:41 +00:00
|
|
|
inline BorrowedPtr<T>() noexcept : _raw(nullptr){};
|
|
|
|
inline BorrowedPtr<T>(T* ptr) noexcept : _raw(ptr){};
|
|
|
|
inline BorrowedPtr<T>(const BorrowedPtr<T>& other) noexcept : _raw(other._raw){};
|
|
|
|
inline BorrowedPtr<T>(const std::unique_ptr<T>& other) noexcept : _raw(other.get()){};
|
2020-05-26 15:39:27 +00:00
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
~BorrowedPtr() noexcept = default;
|
2020-05-26 15:39:27 +00:00
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
inline BorrowedPtr<T>& operator=(const BorrowedPtr<T>& rhs) noexcept {
|
2020-05-26 15:39:27 +00:00
|
|
|
_raw = rhs._raw;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-06-26 14:31:33 +00:00
|
|
|
inline T* operator->() const {
|
2020-06-26 11:12:58 +00:00
|
|
|
AssertNotNull(_raw);
|
|
|
|
return _raw;
|
|
|
|
}
|
2020-06-26 14:31:33 +00:00
|
|
|
inline T operator*() const {
|
2020-06-26 15:17:06 +00:00
|
|
|
if constexpr (std::is_abstract<T>()){
|
|
|
|
static_assert(!std::is_abstract<T>(), "You're not allowed to dereference an abstract class.");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
AssertNotNull(_raw);
|
|
|
|
return *_raw;
|
|
|
|
}
|
2020-06-26 11:13:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 15:39:27 +00:00
|
|
|
inline T* GetRaw() const noexcept { return _raw; }
|
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
inline bool operator==(const BorrowedPtr& rhs) const noexcept { return _raw == rhs._raw; }
|
|
|
|
inline bool operator!=(const BorrowedPtr& rhs) const noexcept { return _raw != rhs._raw; }
|
2020-05-26 15:39:27 +00:00
|
|
|
|
|
|
|
[[nodiscard]] inline constexpr bool IsNull() const noexcept { return _raw == nullptr; }
|
|
|
|
|
2020-06-26 11:12:58 +00:00
|
|
|
inline BorrowedPtr<const T> Const() const noexcept { return BorrowedPtr<const T>(_raw); }
|
2020-06-20 15:45:41 +00:00
|
|
|
|
2020-05-26 15:39:27 +00:00
|
|
|
template <class TCast> inline BorrowedPtr<TCast> As() const {
|
2020-05-26 17:40:08 +00:00
|
|
|
auto cast = dynamic_cast<TCast*>(_raw);
|
2020-05-26 15:39:27 +00:00
|
|
|
return BorrowedPtr<TCast>(cast);
|
|
|
|
}
|
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
template <class TCast> inline bool TryAs(BorrowedPtr<TCast>& out) const noexcept {
|
2020-05-26 17:40:08 +00:00
|
|
|
auto cast = dynamic_cast<TCast*>(_raw);
|
2020-05-26 15:39:27 +00:00
|
|
|
if (cast == nullptr)
|
|
|
|
return false;
|
|
|
|
out = BorrowedPtr<TCast>(cast);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
template <class TCast> inline BorrowedPtr<TCast> ForceAs() const noexcept {
|
2020-05-26 17:40:08 +00:00
|
|
|
auto cast = reinterpret_cast<TCast*>(_raw);
|
2020-05-26 15:39:27 +00:00
|
|
|
return BorrowedPtr<TCast>(cast);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:03:47 +00:00
|
|
|
namespace std {
|
|
|
|
template <class T> struct hash<ArbUt::BorrowedPtr<T>> {
|
|
|
|
std::size_t operator()(const ArbUt::BorrowedPtr<T>& k) const { return (size_t)k.GetRaw(); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:39:27 +00:00
|
|
|
#endif // ARBUTILS_BORROWEDPTR_HPP
|