Renamed borrowed ptr to be in line with other types.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
c8ca951dd8
commit
90bda57d79
|
@ -0,0 +1,55 @@
|
||||||
|
#ifndef ARBUTILS_BORROWEDPTR_HPP
|
||||||
|
#define ARBUTILS_BORROWEDPTR_HPP
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
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:
|
||||||
|
inline BorrowedPtr<T>() : _raw(nullptr){};
|
||||||
|
inline BorrowedPtr<T>(T* ptr) : _raw(ptr){};
|
||||||
|
inline BorrowedPtr(const BorrowedPtr<T>& other) : _raw(other._raw){};
|
||||||
|
inline BorrowedPtr(const std::unique_ptr<T>& other) : _raw(other.get()){};
|
||||||
|
|
||||||
|
~BorrowedPtr() = default;
|
||||||
|
|
||||||
|
inline BorrowedPtr<T>& operator=(const BorrowedPtr<T>& rhs) {
|
||||||
|
_raw = rhs._raw;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline T* operator->() const noexcept { return _raw; }
|
||||||
|
inline T* GetRaw() const noexcept { return _raw; }
|
||||||
|
|
||||||
|
inline bool operator==(const BorrowedPtr& rhs) const { return _raw == rhs._raw; }
|
||||||
|
inline bool operator!=(const BorrowedPtr& rhs) const { return _raw != rhs._raw; }
|
||||||
|
|
||||||
|
[[nodiscard]] inline constexpr bool IsNull() const noexcept { return _raw == nullptr; }
|
||||||
|
|
||||||
|
template <class TCast> inline BorrowedPtr<TCast> As() const {
|
||||||
|
auto cast = dynamic_cast<TCast>(_raw);
|
||||||
|
return BorrowedPtr<TCast>(cast);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class TCast> inline bool TryAs(BorrowedPtr<TCast>& out) const {
|
||||||
|
auto cast = dynamic_cast<TCast>(_raw);
|
||||||
|
if (cast == nullptr)
|
||||||
|
return false;
|
||||||
|
out = BorrowedPtr<TCast>(cast);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class TCast> inline BorrowedPtr<TCast> ForceAs() const {
|
||||||
|
auto cast = reinterpret_cast<TCast>(_raw);
|
||||||
|
return BorrowedPtr<TCast>(cast);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // ARBUTILS_BORROWEDPTR_HPP
|
|
@ -1,55 +0,0 @@
|
||||||
#ifndef ARBUTILS_BORROWED_PTR_HPP
|
|
||||||
#define ARBUTILS_BORROWED_PTR_HPP
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
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 borrowed_ptr {
|
|
||||||
private:
|
|
||||||
T* _raw;
|
|
||||||
|
|
||||||
public:
|
|
||||||
inline borrowed_ptr<T>() : _raw(nullptr){};
|
|
||||||
inline borrowed_ptr<T>(T* ptr) : _raw(ptr){};
|
|
||||||
inline borrowed_ptr(const borrowed_ptr<T>& other) : _raw(other._raw){};
|
|
||||||
inline borrowed_ptr(const std::unique_ptr<T>& other) : _raw(other.get()){};
|
|
||||||
|
|
||||||
~borrowed_ptr() = default;
|
|
||||||
|
|
||||||
inline borrowed_ptr<T>& operator=(const borrowed_ptr<T>& rhs) {
|
|
||||||
_raw = rhs._raw;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline T* operator->() 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; }
|
|
||||||
|
|
||||||
[[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
|
|
Loading…
Reference in New Issue