Adds OptionalScopedPtr, made ScopedPtr non nullable, fixes some APIs on the optional pointer variants.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
47ee3a0295
commit
ce1268b1de
|
@ -3,8 +3,10 @@
|
||||||
|
|
||||||
#include "__BorrowedPtr.hpp"
|
#include "__BorrowedPtr.hpp"
|
||||||
#include "__OptionalBorrowedPtr.hpp"
|
#include "__OptionalBorrowedPtr.hpp"
|
||||||
|
#include "__OptionalScopedPtr.hpp"
|
||||||
#include "__OptionalUniquePtr.hpp"
|
#include "__OptionalUniquePtr.hpp"
|
||||||
#include "__OptionalUniquePtrList.hpp"
|
#include "__OptionalUniquePtrList.hpp"
|
||||||
|
#include "__ScopedPtr.hpp"
|
||||||
#include "__UniquePtr.hpp"
|
#include "__UniquePtr.hpp"
|
||||||
#include "__UniquePtrList.hpp"
|
#include "__UniquePtrList.hpp"
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
#ifndef ARBUTILS___OPTIONALSCOPEDPTR_HPP
|
||||||
|
#define ARBUTILS___OPTIONALSCOPEDPTR_HPP
|
||||||
|
|
||||||
|
#include "../Ensure.hpp"
|
||||||
|
|
||||||
|
namespace ArbUt {
|
||||||
|
/// @brief An optional unique pointer is used to indicate a pointer that is owned by its holder, and will be deleted
|
||||||
|
/// when its owner is deleted.
|
||||||
|
/// @details A unique pointer is used to indicate a pointer that is owned by an object, and that needs to be deleted
|
||||||
|
/// when its owner is deleted.
|
||||||
|
template <class T> class OptionalScopedPtr {
|
||||||
|
private:
|
||||||
|
T* _raw;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// @brief Initialise a ScopedPtr with a specific raw pointer.
|
||||||
|
inline ScopedPtr<T>(T* ptr) : _raw(ptr){};
|
||||||
|
/// @brief Initialise a ScopedPtr from a copy.
|
||||||
|
inline ScopedPtr<T>(const ScopedPtr<T>& other) : _raw(other._raw){};
|
||||||
|
/// @brief Initialise a ScopedPtr with a std unique_ptr.
|
||||||
|
inline ScopedPtr<T>(const std::unique_ptr<T>& other) : _raw(other.get()){};
|
||||||
|
|
||||||
|
~ScopedPtr() noexcept { delete _raw; }
|
||||||
|
|
||||||
|
/// @brief Copy operator.
|
||||||
|
inline ScopedPtr<T>& operator=(const ScopedPtr<T>& rhs) {
|
||||||
|
if (this == &rhs) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
_raw = rhs._raw;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Assign operator with raw pointer.
|
||||||
|
inline ScopedPtr<T>& operator=(T* rhs) {
|
||||||
|
if (_raw == rhs) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
_raw = rhs;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Return whether the pointer is null or not.
|
||||||
|
[[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; }
|
||||||
|
|
||||||
|
/// @brief Get the raw underlying pointer.
|
||||||
|
inline T* GetValue() const noexcept { return _raw; }
|
||||||
|
|
||||||
|
/// @brief Get the raw underlying pointer.
|
||||||
|
inline T* TakeOwnership() const noexcept {
|
||||||
|
auto raw = _raw;
|
||||||
|
_raw = nullptr;
|
||||||
|
return raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Check equality of two ScopedPtr objects
|
||||||
|
inline bool operator==(const ScopedPtr& rhs) const noexcept { return _raw == rhs._raw; }
|
||||||
|
/// @brief Check equality of pointers
|
||||||
|
inline bool operator==(T* rhs) const noexcept { return _raw == rhs; }
|
||||||
|
/// @brief Check equality of two ScopedPtr objects
|
||||||
|
inline bool operator!=(const ScopedPtr& rhs) const noexcept { return _raw != rhs._raw; }
|
||||||
|
/// @brief Check equality of pointers
|
||||||
|
inline bool operator!=(T* rhs) const noexcept { return _raw != rhs; }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace std {
|
||||||
|
/// @brief Helper class for allowing hashing of OptionalScopedPtr.
|
||||||
|
template <class T> struct hash<ArbUt::OptionalScopedPtr<T>> {
|
||||||
|
/// @brief Returns a hash of for an optional scoped Pointer. Effectively just the raw memory address.
|
||||||
|
/// @param k An optional scoped pointer.
|
||||||
|
/// @return The hash of the optional scoped pointer.
|
||||||
|
std::size_t operator()(const ArbUt::OptionalScopedPtr<T>& k) const { return (size_t)k.GetRaw(); }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // ARBUTILS___OPTIONALSCOPEDPTR_HPP
|
|
@ -48,13 +48,6 @@ namespace ArbUt {
|
||||||
return *this;
|
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 noexcept { return _raw; }
|
|
||||||
|
|
||||||
/// @brief Get the raw underlying pointer.
|
|
||||||
inline T* GetRaw() const noexcept { return _raw; }
|
|
||||||
|
|
||||||
/// @brief Check equality of two OptionalUniquePtr objects
|
/// @brief Check equality of two OptionalUniquePtr objects
|
||||||
inline bool operator==(const OptionalUniquePtr& rhs) const noexcept { return _raw == rhs._raw; }
|
inline bool operator==(const OptionalUniquePtr& rhs) const noexcept { return _raw == rhs._raw; }
|
||||||
/// @brief Check equality of pointers
|
/// @brief Check equality of pointers
|
||||||
|
|
|
@ -14,12 +14,14 @@ namespace ArbUt {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// @brief Initialise a ScopedPtr with a specific raw pointer.
|
/// @brief Initialise a ScopedPtr with a specific raw pointer.
|
||||||
inline ScopedPtr<T>(T* ptr) : _raw(ptr){};
|
inline ScopedPtr<T>(T* ptr) : _raw(ptr) { EnsureNotNull(_raw); };
|
||||||
/// @brief Initialise a ScopedPtr from a copy.
|
/// @brief Initialise a ScopedPtr from a copy.
|
||||||
inline ScopedPtr<T>(const ScopedPtr<T>& other) : _raw(other._raw){};
|
inline ScopedPtr<T>(const ScopedPtr<T>& other) : _raw(other._raw){};
|
||||||
/// @brief Initialise a ScopedPtr with a std unique_ptr.
|
/// @brief Initialise a ScopedPtr with a std unique_ptr.
|
||||||
inline ScopedPtr<T>(const std::unique_ptr<T>& other) : _raw(other.get()){};
|
inline ScopedPtr<T>(const std::unique_ptr<T>& other) : _raw(other.get()){};
|
||||||
|
|
||||||
|
ScopedPtr<T>(std::nullptr_t) = delete;
|
||||||
|
|
||||||
~ScopedPtr() noexcept { delete _raw; }
|
~ScopedPtr() noexcept { delete _raw; }
|
||||||
|
|
||||||
/// @brief Copy operator.
|
/// @brief Copy operator.
|
||||||
|
@ -36,16 +38,13 @@ namespace ArbUt {
|
||||||
if (_raw == rhs) {
|
if (_raw == rhs) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
EnsureNotNull(rhs);
|
||||||
_raw = rhs;
|
_raw = rhs;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Operator for access into underlying pointer.
|
/// @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 noexcept { return _raw; }
|
||||||
inline T* operator->() const noexcept {
|
|
||||||
EnsureNotNull(_raw);
|
|
||||||
return _raw;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// @brief Get the raw underlying pointer.
|
/// @brief Get the raw underlying pointer.
|
||||||
inline T* TakeOwnership() const noexcept {
|
inline T* TakeOwnership() const noexcept {
|
||||||
|
@ -54,6 +53,8 @@ namespace ArbUt {
|
||||||
return raw;
|
return raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Delete comparison with nullptr, ScopedPtr can't be null
|
||||||
|
inline bool operator==(std::nullptr_t) const = delete;
|
||||||
/// @brief Check equality of two ScopedPtr objects
|
/// @brief Check equality of two ScopedPtr objects
|
||||||
inline bool operator==(const ScopedPtr& rhs) const noexcept { return _raw == rhs._raw; }
|
inline bool operator==(const ScopedPtr& rhs) const noexcept { return _raw == rhs._raw; }
|
||||||
/// @brief Check equality of pointers
|
/// @brief Check equality of pointers
|
||||||
|
@ -68,9 +69,9 @@ namespace ArbUt {
|
||||||
namespace std {
|
namespace std {
|
||||||
/// @brief Helper class for allowing hashing of ScopedPtr.
|
/// @brief Helper class for allowing hashing of ScopedPtr.
|
||||||
template <class T> struct hash<ArbUt::ScopedPtr<T>> {
|
template <class T> struct hash<ArbUt::ScopedPtr<T>> {
|
||||||
/// @brief Returns a hash of for a borrowed Pointer. Effectively just the raw memory address.
|
/// @brief Returns a hash of for a scoped Pointer. Effectively just the raw memory address.
|
||||||
/// @param k A borrowed pointer.
|
/// @param k A scoped pointer.
|
||||||
/// @return The hash of the borrowed pointer.
|
/// @return The hash of the scoped pointer.
|
||||||
std::size_t operator()(const ArbUt::ScopedPtr<T>& k) const { return (size_t)k.GetRaw(); }
|
std::size_t operator()(const ArbUt::ScopedPtr<T>& k) const { return (size_t)k.GetRaw(); }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue