Rename Assert macro to Ensure, clean up exception to be in line with THROW.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-12-13 11:41:41 +01:00
parent 4466aeeee6
commit 4e854516c1
9 changed files with 67 additions and 68 deletions

View File

@@ -1,38 +1,36 @@
/** @file */
#include "Exception.hpp"
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
/// \defgroup Ensure Ensure
/// \brief A set of ensure macros.
/// \defgroup Asserts Asserts
/// \brief A set of assertion macros.
#ifndef NO_ASSERT
/// @brief Asserts an expression is true. Throws an exception if the assertion fails.
/// @ingroup Asserts
#define Assert(expr) \
#ifndef NO_ENSURE
/// @brief Ensures an expression is true. Throws an exception if the assertion fails.
/// @ingroup Ensure
#define Ensure(expr) \
if (!(expr)) { \
std::stringstream ss; \
ss << "ASSERTION FAILED: [" << __FILENAME__ << " (" << __LINE__ << ")] \""; \
ss << "[" << file_name(__FILE__) << ":" << __LINE__ << "] ENSURE FAILED: \""; \
ss << #expr << "\""; \
throw ArbUt::Exception(ss.str()); \
}
/// @brief Asserts an expression is true for a range. The value in the range can be retrieved using ``item``.
/// @ingroup Asserts
#define AssertForEach(iterator, assertion) \
/// @brief Ensures an expression is true for a range. The value in the range can be retrieved using ``item``.
/// @ingroup Ensure
#define EnsureForEach(iterator, assertion) \
{ \
for (auto item : (iterator)) \
Assert(assertion) \
Ensure(assertion) \
}
#else
// Assert is empty if NO_ASSERT is defined.
#define Assert(expr) ;
#define AssertForEach(iterator, assertion) ;
// Ensure is empty if NO_ENSURE is defined.
#define Ensure(expr) ;
#define EnsureForEach(iterator, assertion) ;
#endif
/// @brief Asserts a pointer is not null.
/// @ingroup Asserts
#define AssertNotNull(value) Assert((value) != nullptr)
/// @brief Asserts a range is not null.
/// @ingroup Asserts
#define AssertAllNotNull(iterator) AssertForEach(iterator, item != nullptr)
/// @brief Ensures a pointer is not null.
/// @ingroup Ensure
#define EnsureNotNull(value) Ensure((value) != nullptr)
/// @brief Ensures a range is not null.
/// @ingroup Ensure
#define EnsureAllNotNull(iterator) EnsureForEach(iterator, item != nullptr)

View File

@@ -140,9 +140,13 @@ namespace ArbUt {
};
}
static constexpr const char* file_name(const char* path) {
#if !__cpp_consteval
#define consteval constexpr
#endif
static consteval const char* file_name(const char* path) {
const char* file = path;
while (*path) {
while (*path != 0) {
if (*path++ == '/') {
file = path;
}

View File

@@ -16,7 +16,7 @@ namespace ArbUt {
public:
inline BorrowedPtr<T>() {}
/// @brief Initialise a BorrowedPtr with a specific raw pointer.
inline BorrowedPtr<T>(T* ptr) : _raw(ptr) { AssertNotNull(ptr); };
inline BorrowedPtr<T>(T* 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 {
if (_raw == rhs) {
return *this;
}
AssertNotNull(rhs);
EnsureNotNull(rhs);
_raw = rhs;
return *this;
}

View File

@@ -43,7 +43,7 @@ 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 {
AssertNotNull(_raw);
EnsureNotNull(_raw);
return _raw;
}

View File

@@ -16,7 +16,7 @@ namespace ArbUt {
public:
inline UniquePtr<T>() {}
/// @brief Initialise a UniquePtr with a specific raw pointer.
inline UniquePtr<T>(T* ptr) : _raw(ptr) { AssertNotNull(ptr); };
inline UniquePtr<T>(T* 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.
@@ -42,7 +42,7 @@ namespace ArbUt {
return *this;
}
delete _raw;
AssertNotNull(rhs);
EnsureNotNull(rhs);
_raw = rhs;
return *this;
}

View File

@@ -17,7 +17,7 @@ namespace ArbUt {
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) noexcept : _vector(vec) { AssertAllNotNull(vec); }
inline UniquePtrList(const std::vector<ValueT*>& vec) noexcept : _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.
@@ -25,13 +25,13 @@ namespace ArbUt {
/// @brief Initialises a UniquePtrList from a initialiser_list.
/// @param l A initialiser_list
inline UniquePtrList(const std::initializer_list<ValueT*>& l) noexcept : _vector(l) {
AssertAllNotNull(_vector);
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) {
AssertAllNotNull(_vector);
EnsureAllNotNull(_vector);
}
UniquePtrList(const UniquePtrList<ValueT>&) = delete;
@@ -80,7 +80,7 @@ namespace ArbUt {
/// @param index The index to set the pointer to.
/// @param ptr The pointer to store.
void Set(size_t index, ValueT* ptr) {
AssertNotNull(ptr);
EnsureNotNull(ptr);
delete _vector[index];
_vector[index] = ptr;
}
@@ -119,7 +119,7 @@ namespace ArbUt {
/// @brief Append a pointer to the list.
/// @param value The pointer to push to the list.
inline void Append(ValueT* value) {
AssertNotNull(value);
EnsureNotNull(value);
_vector.push_back(value);
}
/// @brief Borrow a pointer at a certain index.

View File

@@ -43,7 +43,7 @@ namespace ArbUt {
/// @brief Gets a random 32 bit integer between 0, and given max parameter.
/// @param max The exclusive max value the random value should be.
[[nodiscard]] inline int32_t Get(int32_t max) {
Assert(max > 0);
Ensure(max > 0);
std::uniform_int_distribution<int32_t> distribution(0, max - 1);
return distribution(_rng);
}
@@ -52,7 +52,7 @@ namespace ArbUt {
/// @param min The inclusive min value the random value should be.
/// @param max The exclusive max value the random value should be.
[[nodiscard]] inline int32_t Get(int32_t min, int32_t max) {
Assert(max > min);
Ensure(max > min);
std::uniform_int_distribution<int32_t> distribution(min, max - 1);
return distribution(_rng);
}
@@ -71,7 +71,7 @@ namespace ArbUt {
/// @param min The inclusive min value the random value should be.
/// @param max The exclusive max value the random value should be.
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t min, uint32_t max) {
Assert(max > min);
Ensure(max > min);
std::uniform_int_distribution<uint32_t> distribution(min, max - 1);
return distribution(_rng);
}