Lots of documentation.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -5,45 +5,62 @@
|
||||
#include "../Assert.hpp"
|
||||
|
||||
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.
|
||||
/// @brief A borrowed pointer is used to indicate a pointer that is not owned by its holder.
|
||||
/// @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.
|
||||
template <class T> class BorrowedPtr {
|
||||
private:
|
||||
T* _raw;
|
||||
|
||||
public:
|
||||
/// @brief Initialise a BorrowedPtr with a null pointer.
|
||||
inline BorrowedPtr<T>() noexcept : _raw(nullptr){};
|
||||
/// @brief Initialise a BorrowedPtr with a specific raw pointer.
|
||||
inline BorrowedPtr<T>(T* ptr) noexcept : _raw(ptr){};
|
||||
/// @brief Initialise a BorrowedPtr from a copy.
|
||||
inline BorrowedPtr<T>(const BorrowedPtr<T>& other) noexcept : _raw(other._raw){};
|
||||
/// @brief Initialise a BorrowedPtr with a std unique_ptr.
|
||||
inline BorrowedPtr<T>(const std::unique_ptr<T>& other) noexcept : _raw(other.get()){};
|
||||
|
||||
~BorrowedPtr() noexcept = default;
|
||||
|
||||
/// @brief Copy operator.
|
||||
inline BorrowedPtr<T>& operator=(const BorrowedPtr<T>& rhs) noexcept {
|
||||
if(this == &rhs)
|
||||
return *this;
|
||||
_raw = rhs._raw;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// @brief Operator for access into underlying pointer.
|
||||
/// @warning Note that this asserts that the underlying pointer is not null first, to prevent segfaults.
|
||||
inline T* operator->() const {
|
||||
AssertNotNull(_raw);
|
||||
return _raw;
|
||||
}
|
||||
|
||||
/// @brief Get the raw underlying pointer.
|
||||
inline T* GetRaw() const noexcept { return _raw; }
|
||||
|
||||
/// @brief Check equality of two BorrowedPtr objects
|
||||
inline bool operator==(const BorrowedPtr& rhs) const noexcept { return _raw == rhs._raw; }
|
||||
/// @brief Check equality of two BorrowedPtr objects
|
||||
inline bool operator!=(const BorrowedPtr& rhs) const noexcept { return _raw != rhs._raw; }
|
||||
|
||||
/// @brief Returns whether the underlying pointer is a nullptr or not.
|
||||
[[nodiscard]] inline constexpr bool IsNull() const noexcept { return _raw == nullptr; }
|
||||
|
||||
/// @brief Returns a const version of the underlying pointer.
|
||||
inline BorrowedPtr<const T> Const() const noexcept { return BorrowedPtr<const T>(_raw); }
|
||||
|
||||
/// @brief Casts the underlying pointer to another type using dynamic_cast.
|
||||
template <class TCast> inline BorrowedPtr<TCast> As() const {
|
||||
auto cast = dynamic_cast<TCast*>(_raw);
|
||||
return BorrowedPtr<TCast>(cast);
|
||||
}
|
||||
|
||||
/// @brief Try to cast the underlying pointer to another type using dynamic_cast.
|
||||
template <class TCast> inline bool TryAs(BorrowedPtr<TCast>& out) const noexcept {
|
||||
auto cast = dynamic_cast<TCast*>(_raw);
|
||||
if (cast == nullptr)
|
||||
@@ -52,6 +69,7 @@ namespace ArbUt {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @brief Force cast the underlying pointer to another type using reinterpret_cast.
|
||||
template <class TCast> inline BorrowedPtr<TCast> ForceAs() const noexcept {
|
||||
auto cast = reinterpret_cast<TCast*>(_raw);
|
||||
return BorrowedPtr<TCast>(cast);
|
||||
@@ -60,6 +78,7 @@ namespace ArbUt {
|
||||
}
|
||||
|
||||
namespace std {
|
||||
/// @brief Helper class for allowing hashing of BorrowedPtr.
|
||||
template <class T> struct hash<ArbUt::BorrowedPtr<T>> {
|
||||
std::size_t operator()(const ArbUt::BorrowedPtr<T>& k) const { return (size_t)k.GetRaw(); }
|
||||
};
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "BorrowedPtr.hpp"
|
||||
|
||||
namespace ArbUt {
|
||||
/// @brief Collection of pointers that is owned by the list. When the list exits scope, the destructor on all pointers will be called.
|
||||
template <class ValueT> class UniquePtrList {
|
||||
private:
|
||||
std::vector<ValueT*> _vector;
|
||||
|
||||
Reference in New Issue
Block a user