Implements Defines with common defines, initial work on nullability
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-03-23 11:13:42 +01:00
parent ac7d8e58b4
commit 0031804fe8
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
9 changed files with 105 additions and 71 deletions

31
src/Defines.hpp Normal file
View File

@ -0,0 +1,31 @@
#ifndef ARBUTILS_DEFINES_HPP
#define ARBUTILS_DEFINES_HPP
#include <cstdint>
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using f32 = float;
using f64 = double;
#if defined(__clang__)
#define non_null _Nonnull
#else
#define non_null
#endif
#if defined(__clang__)
#define nullable _Nullable
#else
#define nullable
#endif
#endif // ARBUTILS_DEFINES_HPP

View File

@ -1,8 +1,8 @@
#ifndef ARBUTILS___BORROWEDPTR_HPP #ifndef ARBUTILS___BORROWEDPTR_HPP
#define ARBUTILS___BORROWEDPTR_HPP #define ARBUTILS___BORROWEDPTR_HPP
#include <cstddef>
#include <memory> #include <memory>
#include "../Defines.hpp"
#include "../Ensure.hpp" #include "../Ensure.hpp"
namespace ArbUt { namespace ArbUt {
@ -13,12 +13,12 @@ namespace ArbUt {
// object. // object.
template <class T> class BorrowedPtr { template <class T> class BorrowedPtr {
private: private:
T* _raw; T* non_null _raw;
public: public:
inline BorrowedPtr<T>() {} inline BorrowedPtr<T>() {}
/// @brief Initialise a BorrowedPtr with a specific raw pointer. /// @brief Initialise a BorrowedPtr with a specific raw pointer.
inline BorrowedPtr<T>(T* ptr) : _raw(ptr) { EnsureNotNull(ptr); }; inline BorrowedPtr<T>(T* non_null ptr) : _raw(ptr) { EnsureNotNull(ptr); };
/// @brief Initialise a BorrowedPtr from a copy. /// @brief Initialise a BorrowedPtr from a copy.
inline BorrowedPtr<T>(const BorrowedPtr<T>& other) : _raw(other._raw){}; inline BorrowedPtr<T>(const BorrowedPtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a BorrowedPtr with a std unique_ptr. /// @brief Initialise a BorrowedPtr with a std unique_ptr.
@ -39,7 +39,7 @@ namespace ArbUt {
return *this; return *this;
} }
/// @brief Assign operator with raw pointer. /// @brief Assign operator with raw pointer.
inline BorrowedPtr<T>& operator=(T* rhs) { inline BorrowedPtr<T>& operator=(T* non_null rhs) {
if (_raw == rhs) { if (_raw == rhs) {
return *this; return *this;
} }
@ -52,21 +52,21 @@ namespace ArbUt {
/// @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. /// @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* non_null operator->() const noexcept { return _raw; }
/// @brief Get the raw underlying pointer. /// @brief Get the raw underlying pointer.
inline T* GetRaw() const noexcept { return _raw; } inline T* non_null GetRaw() const noexcept { return _raw; }
/// @brief Check equality of two BorrowedPtr objects /// @brief Check equality of two BorrowedPtr objects
inline bool operator==(const BorrowedPtr<T>& rhs) const noexcept { return _raw == rhs._raw; } inline bool operator==(const BorrowedPtr<T>& rhs) const noexcept { return _raw == rhs._raw; }
/// @brief Check equality of pointers /// @brief Check equality of pointers
inline bool operator==(T* rhs) const noexcept { return _raw == rhs; } inline bool operator==(T* non_null rhs) const noexcept { return _raw == rhs; }
/// @brief Delete comparison with nullptr, BorrowedPtr can't be null /// @brief Delete comparison with nullptr, BorrowedPtr can't be null
inline bool operator==(std::nullptr_t) const = delete; inline bool operator==(std::nullptr_t) const = delete;
/// @brief Check equality of two BorrowedPtr objects /// @brief Check equality of two BorrowedPtr objects
inline bool operator!=(const BorrowedPtr<T>& rhs) const noexcept { return _raw != rhs._raw; } inline bool operator!=(const BorrowedPtr<T>& rhs) const noexcept { return _raw != rhs._raw; }
/// @brief Check equality of pointers /// @brief Check equality of pointers
inline bool operator!=(T* rhs) const noexcept { return _raw != rhs; } inline bool operator!=(T* non_null rhs) const noexcept { return _raw != rhs; }
/// @brief Delete comparison with nullptr, BorrowedPtr can't be null /// @brief Delete comparison with nullptr, BorrowedPtr can't be null
inline bool operator!=(std::nullptr_t) const = delete; inline bool operator!=(std::nullptr_t) const = delete;
@ -95,7 +95,7 @@ namespace ArbUt {
} }
/// @brief Implicit cast to retrieve raw pointer. /// @brief Implicit cast to retrieve raw pointer.
inline operator T*() const noexcept { return _raw; } inline operator T* non_null() const noexcept { return _raw; }
}; };
} }

View File

@ -11,13 +11,13 @@ namespace ArbUt {
// object. // object.
template <class T> class OptionalBorrowedPtr { template <class T> class OptionalBorrowedPtr {
private: private:
T* _raw; T* nullable _raw;
public: public:
/// @brief Initialise a BorrowedPtr with a nullptr. /// @brief Initialise a BorrowedPtr with a nullptr.
inline OptionalBorrowedPtr<T>() : _raw(nullptr) {} inline OptionalBorrowedPtr<T>() : _raw(nullptr) {}
/// @brief Initialise a BorrowedPtr with a specific raw pointer. /// @brief Initialise a BorrowedPtr with a specific raw pointer.
inline OptionalBorrowedPtr<T>(T* ptr) : _raw(ptr){}; inline OptionalBorrowedPtr<T>(T* nullable ptr) : _raw(ptr){};
/// @brief Initialise a BorrowedPtr from a copy. /// @brief Initialise a BorrowedPtr from a copy.
inline OptionalBorrowedPtr<T>(const OptionalBorrowedPtr<T>& other) : _raw(other._raw){}; inline OptionalBorrowedPtr<T>(const OptionalBorrowedPtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a BorrowedPtr with a std unique_ptr. /// @brief Initialise a BorrowedPtr with a std unique_ptr.
@ -34,7 +34,7 @@ namespace ArbUt {
return *this; return *this;
} }
/// @brief Assign operator with raw pointer. /// @brief Assign operator with raw pointer.
inline OptionalBorrowedPtr<T>& operator=(T* rhs) { inline OptionalBorrowedPtr<T>& operator=(T* nullable rhs) {
if (_raw == rhs) { if (_raw == rhs) {
return *this; return *this;
} }
@ -46,16 +46,16 @@ namespace ArbUt {
[[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; } [[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; }
/// @brief Get the raw underlying pointer. /// @brief Get the raw underlying pointer.
inline T* GetValue() const noexcept { return _raw; } inline T* nullable GetValue() const noexcept { return _raw; }
/// @brief Check equality of two BorrowedPtr objects /// @brief Check equality of two BorrowedPtr objects
inline bool operator==(const OptionalBorrowedPtr<T>& rhs) const noexcept { return _raw == rhs._raw; } inline bool operator==(const OptionalBorrowedPtr<T>& rhs) const noexcept { return _raw == rhs._raw; }
/// @brief Check equality of pointers /// @brief Check equality of pointers
inline bool operator==(T* rhs) const noexcept { return _raw == rhs; } inline bool operator==(T* nullable rhs) const noexcept { return _raw == rhs; }
/// @brief Check equality of two BorrowedPtr objects /// @brief Check equality of two BorrowedPtr objects
inline bool operator!=(const OptionalBorrowedPtr<T>& rhs) const noexcept { return _raw != rhs._raw; } inline bool operator!=(const OptionalBorrowedPtr<T>& rhs) const noexcept { return _raw != rhs._raw; }
/// @brief Check equality of pointers /// @brief Check equality of pointers
inline bool operator!=(T* rhs) const noexcept { return _raw != rhs; } inline bool operator!=(T* nullable rhs) const noexcept { return _raw != rhs; }
/// @brief Returns a const version of the underlying pointer. /// @brief Returns a const version of the underlying pointer.
inline OptionalBorrowedPtr<const T> Const() const noexcept { return OptionalBorrowedPtr<const T>(_raw); } inline OptionalBorrowedPtr<const T> Const() const noexcept { return OptionalBorrowedPtr<const T>(_raw); }
@ -82,7 +82,7 @@ namespace ArbUt {
} }
/// @brief Implicit cast to retrieve raw pointer. /// @brief Implicit cast to retrieve raw pointer.
inline operator T*() const noexcept { return _raw; } inline operator T* nullable() const noexcept { return _raw; }
}; };
} }

View File

@ -10,11 +10,11 @@ namespace ArbUt {
/// when its owner is deleted. /// when its owner is deleted.
template <class T> class OptionalScopedPtr { template <class T> class OptionalScopedPtr {
private: private:
T* _raw; T* nullable _raw;
public: public:
/// @brief Initialise a ScopedPtr with a specific raw pointer. /// @brief Initialise a ScopedPtr with a specific raw pointer.
inline OptionalScopedPtr<T>(T* ptr) : _raw(ptr){}; inline OptionalScopedPtr<T>(T* nullable ptr) : _raw(ptr){};
/// @brief Initialise a ScopedPtr from a copy. /// @brief Initialise a ScopedPtr from a copy.
inline OptionalScopedPtr<T>(const OptionalScopedPtr<T>& other) : _raw(other._raw){}; inline OptionalScopedPtr<T>(const OptionalScopedPtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a ScopedPtr with a std unique_ptr. /// @brief Initialise a ScopedPtr with a std unique_ptr.
@ -32,7 +32,7 @@ namespace ArbUt {
} }
/// @brief Assign operator with raw pointer. /// @brief Assign operator with raw pointer.
inline OptionalScopedPtr<T>& operator=(T* rhs) { inline OptionalScopedPtr<T>& operator=(T* nullable rhs) {
if (_raw == rhs) { if (_raw == rhs) {
return *this; return *this;
} }
@ -44,22 +44,22 @@ namespace ArbUt {
[[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; } [[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; }
/// @brief Get the raw underlying pointer, and take ownership of it. This removes the existing pointer in here. /// @brief Get the raw underlying pointer, and take ownership of it. This removes the existing pointer in here.
inline T* TakeOwnership() noexcept { inline T* nullable TakeOwnership() noexcept {
auto raw = _raw; auto raw = _raw;
_raw = nullptr; _raw = nullptr;
return raw; return raw;
} }
/// @brief Get the raw underlying pointer, without taking ownership of it. /// @brief Get the raw underlying pointer, without taking ownership of it.
inline T* GetValue() noexcept { return _raw; } inline T* nullable GetValue() noexcept { return _raw; }
/// @brief Check equality of two ScopedPtr objects /// @brief Check equality of two ScopedPtr objects
inline bool operator==(const OptionalScopedPtr& rhs) const noexcept { return _raw == rhs._raw; } inline bool operator==(const OptionalScopedPtr& rhs) const noexcept { return _raw == rhs._raw; }
/// @brief Check equality of pointers /// @brief Check equality of pointers
inline bool operator==(T* rhs) const noexcept { return _raw == rhs; } inline bool operator==(T* nullable rhs) const noexcept { return _raw == rhs; }
/// @brief Check equality of two ScopedPtr objects /// @brief Check equality of two ScopedPtr objects
inline bool operator!=(const OptionalScopedPtr& rhs) const noexcept { return _raw != rhs._raw; } inline bool operator!=(const OptionalScopedPtr& rhs) const noexcept { return _raw != rhs._raw; }
/// @brief Check equality of pointers /// @brief Check equality of pointers
inline bool operator!=(T* rhs) const noexcept { return _raw != rhs; } inline bool operator!=(T* nullable rhs) const noexcept { return _raw != rhs; }
/// @brief Implicit cast to retrieve optional borrowed pointer. /// @brief Implicit cast to retrieve optional borrowed pointer.
inline operator OptionalBorrowedPtr<T>() const noexcept { return _raw; } inline operator OptionalBorrowedPtr<T>() const noexcept { return _raw; }
}; };

View File

@ -10,13 +10,13 @@ namespace ArbUt {
/// when its owner is deleted. /// when its owner is deleted.
template <class T> class OptionalUniquePtr { template <class T> class OptionalUniquePtr {
private: private:
T* _raw; T* nullable _raw;
public: public:
/// @brief Initialise a BorrowedPtr with a nullptr. /// @brief Initialise a BorrowedPtr with a nullptr.
inline OptionalUniquePtr<T>() : _raw(nullptr) {} inline OptionalUniquePtr<T>() : _raw(nullptr) {}
/// @brief Initialise a OptionalUniquePtr with a specific raw pointer. /// @brief Initialise a OptionalUniquePtr with a specific raw pointer.
inline OptionalUniquePtr<T>(T* ptr) : _raw(ptr){}; inline OptionalUniquePtr<T>(T* nullable ptr) : _raw(ptr){};
/// @brief Initialise a OptionalUniquePtr from a copy. /// @brief Initialise a OptionalUniquePtr from a copy.
inline OptionalUniquePtr<T>(const OptionalUniquePtr<T>& other) : _raw(other._raw){}; inline OptionalUniquePtr<T>(const OptionalUniquePtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a OptionalUniquePtr with a std unique_ptr. /// @brief Initialise a OptionalUniquePtr with a std unique_ptr.
@ -37,10 +37,10 @@ namespace ArbUt {
[[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; } [[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; }
/// @brief Get the raw underlying pointer. /// @brief Get the raw underlying pointer.
inline T* GetValue() const noexcept { return _raw; } inline T* nullable GetValue() const noexcept { return _raw; }
/// @brief Assign operator with raw pointer. /// @brief Assign operator with raw pointer.
inline OptionalUniquePtr<T>& operator=(T* rhs) { inline OptionalUniquePtr<T>& operator=(T* nullable rhs) {
if (_raw == rhs) { if (_raw == rhs) {
return *this; return *this;
} }
@ -51,14 +51,14 @@ namespace ArbUt {
/// @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
inline bool operator==(T* rhs) const noexcept { return _raw == rhs; } inline bool operator==(T* nullable rhs) const noexcept { return _raw == rhs; }
/// @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
inline bool operator!=(T* rhs) const noexcept { return _raw != rhs; } inline bool operator!=(T* nullable rhs) const noexcept { return _raw != rhs; }
/// @brief Implicit cast to retrieve raw pointer. /// @brief Implicit cast to retrieve raw pointer.
inline operator T*() const noexcept { return _raw; } inline operator T* nullable() const noexcept { return _raw; }
}; };
} }

View File

@ -1,6 +1,7 @@
#ifndef ARBUTILS___OPTIONALOptionalUniquePtrList_HPP #ifndef ARBUTILS___OPTIONALOptionalUniquePtrList_HPP
#define ARBUTILS___OPTIONALOptionalUniquePtrList_HPP #define ARBUTILS___OPTIONALOptionalUniquePtrList_HPP
#include <optional>
#include <vector> #include <vector>
#include "__OptionalBorrowedPtr.hpp" #include "__OptionalBorrowedPtr.hpp"
@ -9,9 +10,9 @@ namespace ArbUt {
/// pointers will be called. /// pointers will be called.
template <class ValueT> class OptionalUniquePtrList { template <class ValueT> class OptionalUniquePtrList {
private: private:
std::vector<ValueT*> _vector; std::vector<ValueT * nullable> _vector;
using iterator = typename std::vector<ValueT*>::iterator; using iterator = typename std::vector<ValueT * nullable>::iterator;
using const_iterator = typename std::vector<ValueT*>::const_iterator; using const_iterator = typename std::vector<ValueT * nullable>::const_iterator;
public: public:
/// @brief The type of the value stored in the list. /// @brief The type of the value stored in the list.
@ -31,7 +32,9 @@ namespace ArbUt {
/// @brief Initialises a OptionalUniquePtrList from a raw pointer range. /// @brief Initialises a OptionalUniquePtrList from a raw pointer range.
/// @param begin The raw pointer to the start of the list. /// @param begin The raw pointer to the start of the list.
/// @param end The raw pointer to the end of the list. /// @param end The raw pointer to the end of the list.
inline OptionalUniquePtrList(ValueT* const* begin, ValueT* const* end) noexcept : _vector(begin, end) {} inline OptionalUniquePtrList(ValueT* nullable const* non_null begin,
ValueT* nullable const* non_null end) noexcept
: _vector(begin, end) {}
OptionalUniquePtrList(const OptionalUniquePtrList<ValueT>&) = delete; OptionalUniquePtrList(const OptionalUniquePtrList<ValueT>&) = delete;
OptionalUniquePtrList<ValueT>& operator=(const OptionalUniquePtrList<ValueT>&) = delete; OptionalUniquePtrList<ValueT>& operator=(const OptionalUniquePtrList<ValueT>&) = delete;
@ -62,7 +65,7 @@ namespace ArbUt {
/// to be done by the taker. /// to be done by the taker.
/// @param index The index to get the pointer from. /// @param index The index to get the pointer from.
/// @return A raw pointer. /// @return A raw pointer.
ValueT* TakeOwnership(size_t index) { ValueT* nullable TakeOwnership(size_t index) {
auto p = _vector[index]; auto p = _vector[index];
_vector[index] = nullptr; _vector[index] = nullptr;
return p; return p;
@ -79,7 +82,7 @@ namespace ArbUt {
/// called. /// called.
/// @param index The index to set the pointer to. /// @param index The index to set the pointer to.
/// @param ptr The pointer to store. /// @param ptr The pointer to store.
void Set(size_t index, ValueT* ptr) { void Set(size_t index, ValueT* nullable ptr) {
delete _vector[index]; delete _vector[index];
_vector[index] = ptr; _vector[index] = ptr;
} }
@ -108,16 +111,16 @@ namespace ArbUt {
/// @brief Find the index of the first occurrence of a value in the list, return -1 if none is found. /// @brief Find the index of the first occurrence of a value in the list, return -1 if none is found.
/// @param value The value we want the index for. /// @param value The value we want the index for.
/// @return The index of the first occurrence of the value in the list, or -1 if none is found. /// @return The index of the first occurrence of the value in the list, or -1 if none is found.
inline size_t IndexOf(const BorrowedPtr<ValueT>& value) const noexcept { inline std::optional<size_t> IndexOf(const BorrowedPtr<ValueT>& value) const noexcept {
const auto& it = std::find(_vector.begin(), _vector.end(), value); const auto& it = std::find(_vector.begin(), _vector.end(), value);
if (it == _vector.end()) if (it == _vector.end())
return -1; return {};
return std::distance(_vector.begin(), it); return std::distance(_vector.begin(), it);
} }
/// @brief Append a pointer to the list. /// @brief Append a pointer to the list.
/// @param value The pointer to push to the list. /// @param value The pointer to push to the list.
inline void Append(ValueT* value) { _vector.push_back(value); } inline void Append(ValueT* nullable value) { _vector.push_back(value); }
/// @brief Borrow a pointer at a certain index. /// @brief Borrow a pointer at a certain index.
/// @param index The index to retrieve. /// @param index The index to retrieve.
/// @return A borrowed pointer reference to the containing raw pointer. /// @return A borrowed pointer reference to the containing raw pointer.
@ -138,7 +141,7 @@ namespace ArbUt {
/// @brief Return a raw pointer to the beginning of the list. /// @brief Return a raw pointer to the beginning of the list.
/// @return A raw array pointer to the beginning of the list. /// @return A raw array pointer to the beginning of the list.
ValueT* const* RawData() const noexcept { return _vector.data(); } ValueT* nullable const* non_null RawData() const noexcept { return _vector.data(); }
/// @brief Returns a std::vector representation of the current list. /// @brief Returns a std::vector representation of the current list.
/// @return A std::vector representation of the current list. /// @return A std::vector representation of the current list.
@ -149,10 +152,10 @@ namespace ArbUt {
/// @brief Returns a raw pointer to the current list. /// @brief Returns a raw pointer to the current list.
/// @return A raw pointer to the current list. /// @return A raw pointer to the current list.
inline const OptionalUniquePtrList<ValueT>* GetListPointer() const { return this; } inline const OptionalUniquePtrList<ValueT>* non_null GetListPointer() const { return this; }
/// @brief Returns a raw pointer to the current list. /// @brief Returns a raw pointer to the current list.
/// @return A raw pointer to the current list. /// @return A raw pointer to the current list.
inline OptionalUniquePtrList<ValueT>* GetListPointer() { return this; } inline OptionalUniquePtrList<ValueT>* non_null GetListPointer() { return this; }
}; };
} }

View File

@ -10,11 +10,11 @@ namespace ArbUt {
/// when its owner is deleted. /// when its owner is deleted.
template <class T> class ScopedPtr { template <class T> class ScopedPtr {
private: private:
T* _raw; T* non_null _raw;
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) { EnsureNotNull(_raw); }; inline ScopedPtr<T>(T* non_null 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.
@ -36,7 +36,7 @@ namespace ArbUt {
} }
/// @brief Assign operator with raw pointer. /// @brief Assign operator with raw pointer.
inline ScopedPtr<T>& operator=(T* rhs) { inline ScopedPtr<T>& operator=(T* non_null rhs) {
if (_raw == rhs) { if (_raw == rhs) {
return *this; return *this;
} }
@ -46,30 +46,30 @@ namespace ArbUt {
} }
/// @brief Operator for access into underlying pointer. /// @brief Operator for access into underlying pointer.
inline T* operator->() const noexcept { return _raw; } inline T* non_null operator->() const noexcept { return _raw; }
/// @brief Get the raw underlying pointer, and take ownership of it. This removes the existing pointer in here. /// @brief Get the raw underlying pointer, and take ownership of it. This removes the existing pointer in here.
inline T* TakeOwnership() noexcept { inline T* non_null TakeOwnership() noexcept {
auto raw = _raw; auto raw = _raw;
_raw = nullptr; _raw = nullptr;
return raw; return raw;
} }
/// @brief Get the raw underlying pointer, without taking ownership of it. /// @brief Get the raw underlying pointer, without taking ownership of it.
inline T* GetValue() noexcept { return _raw; } inline T* non_null GetValue() noexcept { return _raw; }
/// @brief Delete comparison with nullptr, ScopedPtr can't be null /// @brief Delete comparison with nullptr, ScopedPtr can't be null
inline bool operator==(std::nullptr_t) const = delete; 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
inline bool operator==(T* rhs) const noexcept { return _raw == rhs; } inline bool operator==(T* non_null rhs) const noexcept { return _raw == rhs; }
/// @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
inline bool operator!=(T* rhs) const noexcept { return _raw != rhs; } inline bool operator!=(T* non_null rhs) const noexcept { return _raw != rhs; }
/// @brief Implicit cast to retrieve raw pointer. /// @brief Implicit cast to retrieve raw pointer.
inline operator T*() const noexcept { return _raw; } inline operator T* non_null() const noexcept { return _raw; }
/// @brief Implicit cast to retrieve borrowed ptr. /// @brief Implicit cast to retrieve borrowed ptr.
inline operator BorrowedPtr<T>() const noexcept { return _raw; } inline operator BorrowedPtr<T>() const noexcept { return _raw; }
/// @brief Implicit cast to retrieve optional borrowed pointer. /// @brief Implicit cast to retrieve optional borrowed pointer.

View File

@ -11,12 +11,12 @@ namespace ArbUt {
/// when its owner is deleted. /// when its owner is deleted.
template <class T> class UniquePtr { template <class T> class UniquePtr {
private: private:
T* _raw; T* non_null _raw;
public: public:
inline UniquePtr<T>() {} inline UniquePtr<T>() {}
/// @brief Initialise a UniquePtr with a specific raw pointer. /// @brief Initialise a UniquePtr with a specific raw pointer.
inline UniquePtr<T>(T* ptr) : _raw(ptr) { EnsureNotNull(ptr); }; inline UniquePtr<T>(T* non_null ptr) : _raw(ptr) { EnsureNotNull(ptr); };
/// @brief Initialise a UniquePtr from a copy. /// @brief Initialise a UniquePtr from a copy.
inline UniquePtr<T>(const UniquePtr<T>& other) : _raw(other._raw){}; inline UniquePtr<T>(const UniquePtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a UniquePtr with a std unique_ptr. /// @brief Initialise a UniquePtr with a std unique_ptr.
@ -39,7 +39,7 @@ namespace ArbUt {
} }
/// @brief Assign operator with raw pointer. /// @brief Assign operator with raw pointer.
inline UniquePtr<T>& operator=(T* rhs) { inline UniquePtr<T>& operator=(T* non_null rhs) {
if (_raw == rhs) { if (_raw == rhs) {
return *this; return *this;
} }
@ -54,21 +54,21 @@ namespace ArbUt {
/// @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. /// @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* non_null operator->() const noexcept { return _raw; }
/// @brief Get the raw underlying pointer. /// @brief Get the raw underlying pointer.
inline T* GetRaw() const noexcept { return _raw; } inline T* non_null GetRaw() const noexcept { return _raw; }
/// @brief Check equality of two UniquePtr objects /// @brief Check equality of two UniquePtr objects
inline bool operator==(const UniquePtr& rhs) const noexcept { return _raw == rhs._raw; } inline bool operator==(const UniquePtr& rhs) const noexcept { return _raw == rhs._raw; }
/// @brief Check equality of pointers /// @brief Check equality of pointers
inline bool operator==(T* rhs) const noexcept { return _raw == rhs; } inline bool operator==(T* non_null rhs) const noexcept { return _raw == rhs; }
/// @brief Delete comparison with nullptr, UniquePtr can't be null /// @brief Delete comparison with nullptr, UniquePtr can't be null
inline bool operator==(std::nullptr_t) const = delete; inline bool operator==(std::nullptr_t) const = delete;
/// @brief Check equality of two UniquePtr objects /// @brief Check equality of two UniquePtr objects
inline bool operator!=(const UniquePtr& rhs) const noexcept { return _raw != rhs._raw; } inline bool operator!=(const UniquePtr& rhs) const noexcept { return _raw != rhs._raw; }
/// @brief Check equality of pointers /// @brief Check equality of pointers
inline bool operator!=(T* rhs) const noexcept { return _raw != rhs; } inline bool operator!=(T* non_null rhs) const noexcept { return _raw != rhs; }
/// @brief Delete comparison with nullptr, UniquePtr can't be null /// @brief Delete comparison with nullptr, UniquePtr can't be null
inline bool operator!=(std::nullptr_t) const = delete; inline bool operator!=(std::nullptr_t) const = delete;
}; };

View File

@ -10,31 +10,31 @@ namespace ArbUt {
/// pointers will be called. /// pointers will be called.
template <class ValueT> class UniquePtrList { template <class ValueT> class UniquePtrList {
private: private:
std::vector<ValueT*> _vector; std::vector<ValueT* non_null> _vector;
using iterator = typename std::vector<ValueT*>::iterator; using iterator = typename std::vector<ValueT* non_null>::iterator;
using const_iterator = typename std::vector<ValueT*>::const_iterator; using const_iterator = typename std::vector<ValueT* non_null>::const_iterator;
public: public:
/// @brief The type of the value stored in the list. /// @brief The type of the value stored in the list.
typedef ValueT* type; typedef ValueT* non_null type;
inline UniquePtrList() noexcept : _vector() {} inline UniquePtrList() noexcept : _vector() {}
/// @brief Initialises a UniquePtrList from a std::vector of raw pointers. /// @brief Initialises a UniquePtrList from a std::vector of raw pointers.
/// @param vec A std::vector of raw pointers. /// @param vec A std::vector of raw pointers.
inline UniquePtrList(const std::vector<ValueT*>& vec) : _vector(vec) { EnsureAllNotNull(vec); } inline UniquePtrList(const std::vector<ValueT* non_null>& vec) : _vector(vec) { EnsureAllNotNull(vec); }
/// @brief Initialises a UniquePtrList with a certain capacity reserved for use. This does not set immediate /// @brief Initialises a UniquePtrList with a certain capacity reserved for use. This does not set immediate
/// size. /// size.
/// @param capacity The desired capacity. /// @param capacity The desired capacity.
explicit inline UniquePtrList(size_t capacity) : _vector() { _vector.reserve(capacity); } explicit inline UniquePtrList(size_t capacity) : _vector() { _vector.reserve(capacity); }
/// @brief Initialises a UniquePtrList from a initialiser_list. /// @brief Initialises a UniquePtrList from a initialiser_list.
/// @param l A initialiser_list /// @param l A initialiser_list
inline UniquePtrList(const std::initializer_list<ValueT*>& l) noexcept : _vector(l) { inline UniquePtrList(const std::initializer_list<ValueT* non_null>& l) noexcept : _vector(l) {
EnsureAllNotNull(_vector); EnsureAllNotNull(_vector);
} }
/// @brief Initialises a UniquePtrList from a raw pointer range. /// @brief Initialises a UniquePtrList from a raw pointer range.
/// @param begin The raw pointer to the start of the list. /// @param begin The raw pointer to the start of the list.
/// @param end The raw pointer to the end of the list. /// @param end The raw pointer to the end of the list.
inline UniquePtrList(ValueT* const* begin, ValueT* const* end) noexcept : _vector(begin, end) { inline UniquePtrList(ValueT* non_null const* non_null begin, ValueT* non_null const* non_null end) noexcept : _vector(begin, end) {
EnsureAllNotNull(_vector); EnsureAllNotNull(_vector);
} }
@ -67,7 +67,7 @@ namespace ArbUt {
/// to be done by the taker. /// to be done by the taker.
/// @param index The index to get the pointer from. /// @param index The index to get the pointer from.
/// @return A raw pointer. /// @return A raw pointer.
ValueT* TakeOwnership(size_t index) { ValueT* non_null TakeOwnership(size_t index) {
auto p = _vector[index]; auto p = _vector[index];
_vector[index] = nullptr; _vector[index] = nullptr;
return p; return p;
@ -84,7 +84,7 @@ namespace ArbUt {
/// called. /// called.
/// @param index The index to set the pointer to. /// @param index The index to set the pointer to.
/// @param ptr The pointer to store. /// @param ptr The pointer to store.
void Set(size_t index, ValueT* ptr) { void Set(size_t index, ValueT* non_null ptr) {
EnsureNotNull(ptr); EnsureNotNull(ptr);
delete _vector[index]; delete _vector[index];
_vector[index] = ptr; _vector[index] = ptr;
@ -123,7 +123,7 @@ namespace ArbUt {
/// @brief Append a pointer to the list. /// @brief Append a pointer to the list.
/// @param value The pointer to push to the list. /// @param value The pointer to push to the list.
inline void Append(ValueT* value) { inline void Append(ValueT* non_null value) {
EnsureNotNull(value); EnsureNotNull(value);
_vector.push_back(value); _vector.push_back(value);
} }
@ -147,7 +147,7 @@ namespace ArbUt {
/// @brief Return a raw pointer to the beginning of the list. /// @brief Return a raw pointer to the beginning of the list.
/// @return A raw array pointer to the beginning of the list. /// @return A raw array pointer to the beginning of the list.
ValueT* const* RawData() const noexcept { return _vector.data(); } ValueT* non_null const* non_null RawData() const noexcept { return _vector.data(); }
/// @brief Returns a std::vector representation of the current list. /// @brief Returns a std::vector representation of the current list.
/// @return A std::vector representation of the current list. /// @return A std::vector representation of the current list.
@ -158,10 +158,10 @@ namespace ArbUt {
/// @brief Returns a raw pointer to the current list. /// @brief Returns a raw pointer to the current list.
/// @return A raw pointer to the current list. /// @return A raw pointer to the current list.
inline const UniquePtrList<ValueT>* GetListPointer() const { return this; } inline const UniquePtrList<ValueT>* non_null GetListPointer() const { return this; }
/// @brief Returns a raw pointer to the current list. /// @brief Returns a raw pointer to the current list.
/// @return A raw pointer to the current list. /// @return A raw pointer to the current list.
inline UniquePtrList<ValueT>* GetListPointer() { return this; } inline UniquePtrList<ValueT>* non_null GetListPointer() { return this; }
}; };
} }