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
#define ARBUTILS___BORROWEDPTR_HPP
#include <cstddef>
#include <memory>
#include "../Defines.hpp"
#include "../Ensure.hpp"
namespace ArbUt {
@ -13,12 +13,12 @@ namespace ArbUt {
// object.
template <class T> class BorrowedPtr {
private:
T* _raw;
T* non_null _raw;
public:
inline BorrowedPtr<T>() {}
/// @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.
inline BorrowedPtr<T>(const BorrowedPtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a BorrowedPtr with a std unique_ptr.
@ -39,7 +39,7 @@ namespace ArbUt {
return *this;
}
/// @brief Assign operator with raw pointer.
inline BorrowedPtr<T>& operator=(T* rhs) {
inline BorrowedPtr<T>& operator=(T* non_null rhs) {
if (_raw == rhs) {
return *this;
}
@ -52,21 +52,21 @@ namespace ArbUt {
/// @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* non_null operator->() const noexcept { return _raw; }
/// @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
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; }
inline bool operator==(T* non_null rhs) const noexcept { return _raw == rhs; }
/// @brief Delete comparison with nullptr, BorrowedPtr can't be null
inline bool operator==(std::nullptr_t) const = delete;
/// @brief Check equality of two BorrowedPtr objects
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; }
inline bool operator!=(T* non_null rhs) const noexcept { return _raw != rhs; }
/// @brief Delete comparison with nullptr, BorrowedPtr can't be null
inline bool operator!=(std::nullptr_t) const = delete;
@ -95,7 +95,7 @@ namespace ArbUt {
}
/// @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.
template <class T> class OptionalBorrowedPtr {
private:
T* _raw;
T* nullable _raw;
public:
/// @brief Initialise a BorrowedPtr with a nullptr.
inline OptionalBorrowedPtr<T>() : _raw(nullptr) {}
/// @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.
inline OptionalBorrowedPtr<T>(const OptionalBorrowedPtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a BorrowedPtr with a std unique_ptr.
@ -34,7 +34,7 @@ namespace ArbUt {
return *this;
}
/// @brief Assign operator with raw pointer.
inline OptionalBorrowedPtr<T>& operator=(T* rhs) {
inline OptionalBorrowedPtr<T>& operator=(T* nullable rhs) {
if (_raw == rhs) {
return *this;
}
@ -46,16 +46,16 @@ namespace ArbUt {
[[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; }
/// @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
inline bool operator==(const OptionalBorrowedPtr<T>& rhs) const noexcept { return _raw == rhs._raw; }
/// @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
inline bool operator!=(const OptionalBorrowedPtr<T>& rhs) const noexcept { return _raw != rhs._raw; }
/// @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.
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.
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.
template <class T> class OptionalScopedPtr {
private:
T* _raw;
T* nullable _raw;
public:
/// @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.
inline OptionalScopedPtr<T>(const OptionalScopedPtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a ScopedPtr with a std unique_ptr.
@ -32,7 +32,7 @@ namespace ArbUt {
}
/// @brief Assign operator with raw pointer.
inline OptionalScopedPtr<T>& operator=(T* rhs) {
inline OptionalScopedPtr<T>& operator=(T* nullable rhs) {
if (_raw == rhs) {
return *this;
}
@ -44,22 +44,22 @@ namespace ArbUt {
[[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.
inline T* TakeOwnership() noexcept {
inline T* nullable TakeOwnership() noexcept {
auto raw = _raw;
_raw = nullptr;
return raw;
}
/// @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
inline bool operator==(const OptionalScopedPtr& rhs) const noexcept { return _raw == rhs._raw; }
/// @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
inline bool operator!=(const OptionalScopedPtr& rhs) const noexcept { return _raw != rhs._raw; }
/// @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.
inline operator OptionalBorrowedPtr<T>() const noexcept { return _raw; }
};

View File

@ -10,13 +10,13 @@ namespace ArbUt {
/// when its owner is deleted.
template <class T> class OptionalUniquePtr {
private:
T* _raw;
T* nullable _raw;
public:
/// @brief Initialise a BorrowedPtr with a nullptr.
inline OptionalUniquePtr<T>() : _raw(nullptr) {}
/// @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.
inline OptionalUniquePtr<T>(const OptionalUniquePtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a OptionalUniquePtr with a std unique_ptr.
@ -37,10 +37,10 @@ namespace ArbUt {
[[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; }
/// @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.
inline OptionalUniquePtr<T>& operator=(T* rhs) {
inline OptionalUniquePtr<T>& operator=(T* nullable rhs) {
if (_raw == rhs) {
return *this;
}
@ -51,14 +51,14 @@ namespace ArbUt {
/// @brief Check equality of two OptionalUniquePtr objects
inline bool operator==(const OptionalUniquePtr& rhs) const noexcept { return _raw == rhs._raw; }
/// @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
inline bool operator!=(const OptionalUniquePtr& rhs) const noexcept { return _raw != rhs._raw; }
/// @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.
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
#define ARBUTILS___OPTIONALOptionalUniquePtrList_HPP
#include <optional>
#include <vector>
#include "__OptionalBorrowedPtr.hpp"
@ -9,9 +10,9 @@ namespace ArbUt {
/// pointers will be called.
template <class ValueT> class OptionalUniquePtrList {
private:
std::vector<ValueT*> _vector;
using iterator = typename std::vector<ValueT*>::iterator;
using const_iterator = typename std::vector<ValueT*>::const_iterator;
std::vector<ValueT * nullable> _vector;
using iterator = typename std::vector<ValueT * nullable>::iterator;
using const_iterator = typename std::vector<ValueT * nullable>::const_iterator;
public:
/// @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.
/// @param begin The raw pointer to the start 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<ValueT>& operator=(const OptionalUniquePtrList<ValueT>&) = delete;
@ -62,7 +65,7 @@ namespace ArbUt {
/// to be done by the taker.
/// @param index The index to get the pointer from.
/// @return A raw pointer.
ValueT* TakeOwnership(size_t index) {
ValueT* nullable TakeOwnership(size_t index) {
auto p = _vector[index];
_vector[index] = nullptr;
return p;
@ -79,7 +82,7 @@ namespace ArbUt {
/// called.
/// @param index The index to set the pointer to.
/// @param ptr The pointer to store.
void Set(size_t index, ValueT* ptr) {
void Set(size_t index, ValueT* nullable ptr) {
delete _vector[index];
_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.
/// @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.
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);
if (it == _vector.end())
return -1;
return {};
return std::distance(_vector.begin(), it);
}
/// @brief Append a pointer 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.
/// @param index The index to retrieve.
/// @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.
/// @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.
/// @return A std::vector representation of the current list.
@ -149,10 +152,10 @@ namespace ArbUt {
/// @brief Returns 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.
/// @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.
template <class T> class ScopedPtr {
private:
T* _raw;
T* non_null _raw;
public:
/// @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.
inline ScopedPtr<T>(const ScopedPtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a ScopedPtr with a std unique_ptr.
@ -36,7 +36,7 @@ namespace ArbUt {
}
/// @brief Assign operator with raw pointer.
inline ScopedPtr<T>& operator=(T* rhs) {
inline ScopedPtr<T>& operator=(T* non_null rhs) {
if (_raw == rhs) {
return *this;
}
@ -46,30 +46,30 @@ namespace ArbUt {
}
/// @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.
inline T* TakeOwnership() noexcept {
inline T* non_null TakeOwnership() noexcept {
auto raw = _raw;
_raw = nullptr;
return raw;
}
/// @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
inline bool operator==(std::nullptr_t) const = delete;
/// @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; }
inline bool operator==(T* non_null 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; }
inline bool operator!=(T* non_null rhs) const noexcept { return _raw != rhs; }
/// @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.
inline operator BorrowedPtr<T>() const noexcept { return _raw; }
/// @brief Implicit cast to retrieve optional borrowed pointer.

View File

@ -11,12 +11,12 @@ namespace ArbUt {
/// when its owner is deleted.
template <class T> class UniquePtr {
private:
T* _raw;
T* non_null _raw;
public:
inline UniquePtr<T>() {}
/// @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.
inline UniquePtr<T>(const UniquePtr<T>& other) : _raw(other._raw){};
/// @brief Initialise a UniquePtr with a std unique_ptr.
@ -39,7 +39,7 @@ namespace ArbUt {
}
/// @brief Assign operator with raw pointer.
inline UniquePtr<T>& operator=(T* rhs) {
inline UniquePtr<T>& operator=(T* non_null rhs) {
if (_raw == rhs) {
return *this;
}
@ -54,21 +54,21 @@ namespace ArbUt {
/// @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* non_null operator->() const noexcept { return _raw; }
/// @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
inline bool operator==(const UniquePtr& rhs) const noexcept { return _raw == rhs._raw; }
/// @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
inline bool operator==(std::nullptr_t) const = delete;
/// @brief Check equality of two UniquePtr objects
inline bool operator!=(const UniquePtr& rhs) const noexcept { return _raw != rhs._raw; }
/// @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
inline bool operator!=(std::nullptr_t) const = delete;
};

View File

@ -10,31 +10,31 @@ namespace ArbUt {
/// pointers will be called.
template <class ValueT> class UniquePtrList {
private:
std::vector<ValueT*> _vector;
using iterator = typename std::vector<ValueT*>::iterator;
using const_iterator = typename std::vector<ValueT*>::const_iterator;
std::vector<ValueT* non_null> _vector;
using iterator = typename std::vector<ValueT* non_null>::iterator;
using const_iterator = typename std::vector<ValueT* non_null>::const_iterator;
public:
/// @brief The type of the value stored in the list.
typedef ValueT* type;
typedef ValueT* non_null type;
inline UniquePtrList() noexcept : _vector() {}
/// @brief Initialises a UniquePtrList from 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
/// size.
/// @param capacity The desired capacity.
explicit inline UniquePtrList(size_t capacity) : _vector() { _vector.reserve(capacity); }
/// @brief Initialises a UniquePtrList from 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);
}
/// @brief Initialises a UniquePtrList from a raw pointer range.
/// @param begin The raw pointer to the start 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);
}
@ -67,7 +67,7 @@ namespace ArbUt {
/// to be done by the taker.
/// @param index The index to get the pointer from.
/// @return A raw pointer.
ValueT* TakeOwnership(size_t index) {
ValueT* non_null TakeOwnership(size_t index) {
auto p = _vector[index];
_vector[index] = nullptr;
return p;
@ -84,7 +84,7 @@ namespace ArbUt {
/// called.
/// @param index The index to set the pointer to.
/// @param ptr The pointer to store.
void Set(size_t index, ValueT* ptr) {
void Set(size_t index, ValueT* non_null ptr) {
EnsureNotNull(ptr);
delete _vector[index];
_vector[index] = ptr;
@ -123,7 +123,7 @@ namespace ArbUt {
/// @brief Append a pointer 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);
_vector.push_back(value);
}
@ -147,7 +147,7 @@ namespace ArbUt {
/// @brief Return a raw 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.
/// @return A std::vector representation of the current list.
@ -158,10 +158,10 @@ namespace ArbUt {
/// @brief Returns 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.
/// @return A raw pointer to the current list.
inline UniquePtrList<ValueT>* GetListPointer() { return this; }
inline UniquePtrList<ValueT>* non_null GetListPointer() { return this; }
};
}