2020-12-11 14:29:27 +00:00
|
|
|
#ifndef ARBUTILS___BORROWEDPTR_HPP
|
|
|
|
#define ARBUTILS___BORROWEDPTR_HPP
|
2020-05-26 15:39:27 +00:00
|
|
|
|
2021-06-20 10:05:30 +00:00
|
|
|
#include <cstddef>
|
2021-10-30 11:38:52 +00:00
|
|
|
#include <memory>
|
2021-09-25 15:59:06 +00:00
|
|
|
#include "../Ensure.hpp"
|
2020-05-26 15:39:27 +00:00
|
|
|
|
|
|
|
namespace ArbUt {
|
2020-12-11 13:58:16 +00:00
|
|
|
/// @brief A borrowed pointer is used to indicate a pointer that is not owned by its holder. As with all Arbutils
|
|
|
|
/// pointers, this cannot be assigned null. Use an OptionalBorrowedPtr for pointers that can be null.
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @details 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.
|
2020-05-26 15:39:27 +00:00
|
|
|
template <class T> class BorrowedPtr {
|
|
|
|
private:
|
|
|
|
T* _raw;
|
|
|
|
|
|
|
|
public:
|
2020-12-11 15:19:36 +00:00
|
|
|
inline BorrowedPtr<T>() {}
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Initialise a BorrowedPtr with a specific raw pointer.
|
2020-12-13 10:41:41 +00:00
|
|
|
inline BorrowedPtr<T>(T* ptr) : _raw(ptr) { EnsureNotNull(ptr); };
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Initialise a BorrowedPtr from a copy.
|
2020-12-11 13:58:16 +00:00
|
|
|
inline BorrowedPtr<T>(const BorrowedPtr<T>& other) : _raw(other._raw){};
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Initialise a BorrowedPtr with a std unique_ptr.
|
2020-12-11 13:58:16 +00:00
|
|
|
inline BorrowedPtr<T>(const std::unique_ptr<T>& other) : _raw(other.get()){};
|
2020-05-26 15:39:27 +00:00
|
|
|
|
2021-06-20 10:05:30 +00:00
|
|
|
#if !WINDOWS // This doesn't work on mingw-w64 for some reason
|
2020-12-11 14:12:17 +00:00
|
|
|
BorrowedPtr<T>(std::nullptr_t) = delete;
|
2021-06-20 10:05:30 +00:00
|
|
|
#endif
|
2020-12-11 14:12:17 +00:00
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
~BorrowedPtr() noexcept = default;
|
2020-05-26 15:39:27 +00:00
|
|
|
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Copy operator.
|
2020-12-11 13:58:16 +00:00
|
|
|
inline BorrowedPtr<T>& operator=(const BorrowedPtr<T>& rhs) {
|
2020-12-11 16:27:58 +00:00
|
|
|
if (this == &rhs) {
|
2020-09-22 17:32:40 +00:00
|
|
|
return *this;
|
2020-12-11 16:27:58 +00:00
|
|
|
}
|
2020-05-26 15:39:27 +00:00
|
|
|
_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 BorrowedPtr<T>& operator=(T* rhs) {
|
2020-12-11 16:27:58 +00:00
|
|
|
if (_raw == rhs) {
|
2020-12-11 13:58:16 +00:00
|
|
|
return *this;
|
2020-12-11 16:27:58 +00:00
|
|
|
}
|
2020-12-13 10:41:41 +00:00
|
|
|
EnsureNotNull(rhs);
|
2020-12-11 13:58:16 +00:00
|
|
|
_raw = rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-12-11 14:22:21 +00:00
|
|
|
/// @brief Don't allow nullptr assignments
|
2020-12-11 14:12:17 +00:00
|
|
|
inline BorrowedPtr<T>& operator=(std::nullptr_t) = delete;
|
2020-12-11 13:58:16 +00:00
|
|
|
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @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 13:58:16 +00:00
|
|
|
inline T* operator->() const noexcept { return _raw; }
|
2020-06-26 15:31:52 +00:00
|
|
|
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Get the raw underlying pointer.
|
2020-05-26 15:39:27 +00:00
|
|
|
inline T* GetRaw() const noexcept { return _raw; }
|
|
|
|
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Check equality of two BorrowedPtr objects
|
2020-12-11 13:58:16 +00:00
|
|
|
inline bool operator==(const BorrowedPtr<T>& rhs) const noexcept { return _raw == rhs._raw; }
|
|
|
|
/// @brief Check equality of pointers
|
|
|
|
inline bool operator==(T* rhs) const noexcept { return _raw == rhs; }
|
2020-12-13 10:51:47 +00:00
|
|
|
/// @brief Delete comparison with nullptr, BorrowedPtr can't be null
|
|
|
|
inline bool operator==(std::nullptr_t) const = delete;
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Check equality of two BorrowedPtr objects
|
2020-12-11 13:58:16 +00:00
|
|
|
inline bool operator!=(const BorrowedPtr<T>& rhs) const noexcept { return _raw != rhs._raw; }
|
|
|
|
/// @brief Check equality of pointers
|
2020-12-12 11:10:37 +00:00
|
|
|
inline bool operator!=(T* rhs) const noexcept { return _raw != rhs; }
|
2020-12-13 10:51:47 +00:00
|
|
|
/// @brief Delete comparison with nullptr, BorrowedPtr can't be null
|
|
|
|
inline bool operator!=(std::nullptr_t) const = delete;
|
2020-05-26 15:39:27 +00:00
|
|
|
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Returns a const version of the underlying pointer.
|
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-09-22 17:32:40 +00:00
|
|
|
/// @brief Casts the underlying pointer to another type using dynamic_cast.
|
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-09-22 17:32:40 +00:00
|
|
|
/// @brief Try to cast the underlying pointer to another type using dynamic_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-09-22 17:32:40 +00:00
|
|
|
/// @brief Force cast the underlying pointer to another type using reinterpret_cast.
|
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-12-11 13:58:16 +00:00
|
|
|
|
|
|
|
/// @brief Implicit cast to retrieve raw pointer.
|
|
|
|
inline operator T*() const noexcept { return _raw; }
|
2020-05-26 15:39:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:03:47 +00:00
|
|
|
namespace std {
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Helper class for allowing hashing of BorrowedPtr.
|
2020-06-02 14:03:47 +00:00
|
|
|
template <class T> struct hash<ArbUt::BorrowedPtr<T>> {
|
2020-09-24 18:40:21 +00:00
|
|
|
/// @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.
|
2020-06-02 14:03:47 +00:00
|
|
|
std::size_t operator()(const ArbUt::BorrowedPtr<T>& k) const { return (size_t)k.GetRaw(); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-11 14:29:27 +00:00
|
|
|
#endif // ARBUTILS___BORROWEDPTR_HPP
|