Adding assert tools back

This commit is contained in:
2022-11-07 06:55:15 -08:00
parent 6f4ab49caa
commit 4c2fc4cfcf
8 changed files with 276 additions and 25 deletions

View File

@ -0,0 +1,9 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
target_sources(${PROJECT_NAME}
PRIVATE
assert.cpp
)

View File

@ -0,0 +1,44 @@
/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "assert.hpp"
#if ASSERTS_ENABLED == 0
#elif ASSERTS_ENABLED == 1
void assertTrue(bool_t x) {
if(x != true) {
throw "Assertion Failed";
free(0);
}
assert(x == true);
}
void assertFalse(bool_t x) {
assertTrue(!x);
}
void assertUnreachable() {
assertTrue(false);
}
void assertNotNull(const void *pointer) {
assertTrue(pointer != NULL);
}
void assertNotNullptr(const void *ptr) {
assertTRue(ptr != nullptr);
}
void assertNull(const void *pointer) {
assertTrue(pointer == NULL);
}
void assertDeprecated() {
assertUnreachable();
}
#endif

View File

@ -0,0 +1,74 @@
/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawnlibs.hpp"
#define ASSERTS_ENABLED 1
#if ASSERTS_ENABLED == 0
static inline void assertTrue(bool_t x) {}
static inline void assertFalse(bool_t x) {}
static inline void assertUnreachable() {}
static inline void assertNotNull(const void *pointer) {}
static inline void assertNull(const void *pointer) {}
static inline void assertDeprecated() {}
#elif ASSERTS_ENABLED == 1
/**
* Assert a given value to be true.
* @param x Value to assert as true.
*/
void assertTrue(bool_t x);
/**
* Asserts a given statement to be false.
* @param x Value to assert as false.
*/
void assertFalse(bool_t x);
/**
* Asserts that a given line of code is unreachable. Essentially a forced
* assertion failure, good for "edge cases"
*/
void assertUnreachable();
/**
* Assert a given pointer to not point to a null pointer.
* @param pointer Pointer to assert is not a null pointer.
*/
void assertNotNull(const void *pointer);
/**
* Asserts a given pointer to not be a C++ nullptr.
*
* @param ptr Pointer to assert not nullptr.
*/
void assertNotNullptr(const void *ptr);
/**
* Asserts a given pointer to be a nullptr.
* @param pointer Pointer to assert is nullptr.
*/
void assertNull(const void *pointer);
/**
* Asserts a function as being deprecated.
*/
void assertDeprecated();
#else
#define assertTrue assert
#define assertFalse(x) assertTrue(x == 0)
#define assertNotNull(x) assert(x != NULL)
#define assertUnreachable() assert(false)
#define assertDeprecated assertUnreachable
#endif