Support for easy type casting from borrowed_ptr.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-05-26 17:18:12 +02:00
parent f1ebfd78f6
commit 2ae108517d
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 42 additions and 21 deletions

View File

@ -3,32 +3,53 @@
#include <memory> #include <memory>
/// A borrowed pointer is used to indicate a pointer that is not owned by an object, but instead borrowed from another namespace ArbUt::Memory {
/// owning object that is assumed to always be kept alive during the entire lifetime of the borrowing object. /// A borrowed pointer is used to indicate a pointer that is not owned by an object, but instead borrowed from
template <class T> class borrowed_ptr { /// another owning object that is assumed to always be kept alive during the entire lifetime of the borrowing
private: /// object.
T* _raw; template <class T> class borrowed_ptr {
private:
T* _raw;
public: public:
inline borrowed_ptr<T>() : _raw(nullptr){}; inline borrowed_ptr<T>() : _raw(nullptr){};
inline borrowed_ptr<T>(T* ptr) : _raw(ptr){}; inline borrowed_ptr<T>(T* ptr) : _raw(ptr){};
inline borrowed_ptr(const borrowed_ptr<T>& other) : _raw(other._raw){}; inline borrowed_ptr(const borrowed_ptr<T>& other) : _raw(other._raw){};
inline borrowed_ptr(const std::unique_ptr<T>& other) : _raw(other.get()){}; inline borrowed_ptr(const std::unique_ptr<T>& other) : _raw(other.get()){};
~borrowed_ptr() = default; ~borrowed_ptr() = default;
inline borrowed_ptr<T>& operator=(const borrowed_ptr<T>& rhs) { inline borrowed_ptr<T>& operator=(const borrowed_ptr<T>& rhs) {
_raw = rhs._raw; _raw = rhs._raw;
return *this; return *this;
} }
inline T* operator->() const noexcept { return _raw; } inline T* operator->() const noexcept { return _raw; }
inline T* GetRaw() const noexcept { return _raw; } inline T* GetRaw() const noexcept { return _raw; }
inline bool operator==(const borrowed_ptr& rhs) const { return _raw == rhs._raw; } inline bool operator==(const borrowed_ptr& rhs) const { return _raw == rhs._raw; }
inline bool operator!=(const borrowed_ptr& rhs) const { return _raw != rhs._raw; } inline bool operator!=(const borrowed_ptr& rhs) const { return _raw != rhs._raw; }
[[nodiscard]] inline constexpr bool IsNull() const noexcept { return _raw == nullptr; } [[nodiscard]] inline constexpr bool IsNull() const noexcept { return _raw == nullptr; }
};
template <class TCast> inline borrowed_ptr<TCast> As() const {
auto cast = dynamic_cast<TCast>(_raw);
return borrowed_ptr<TCast>(cast);
}
template <class TCast> inline bool TryAs(borrowed_ptr<TCast>& out) const {
auto cast = dynamic_cast<TCast>(_raw);
if (cast == nullptr)
return false;
out = borrowed_ptr<TCast>(cast);
return true;
}
template <class TCast> inline borrowed_ptr<TCast> ForceAs() const {
auto cast = reinterpret_cast<TCast>(_raw);
return borrowed_ptr<TCast>(cast);
}
};
}
#endif // ARBUTILS_BORROWED_PTR_HPP #endif // ARBUTILS_BORROWED_PTR_HPP