Support for easy type casting from borrowed_ptr.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
f1ebfd78f6
commit
2ae108517d
|
@ -3,8 +3,10 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
/// 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.
|
||||
namespace ArbUt::Memory {
|
||||
/// 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;
|
||||
|
@ -29,6 +31,25 @@ public:
|
|||
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