Rewrote assertions
This commit is contained in:
@ -7,60 +7,29 @@
|
||||
|
||||
#include "assert.hpp"
|
||||
|
||||
void assertTrue(bool_t x, const char message[]) {
|
||||
if(x != true) {
|
||||
std::cout << message << std::endl;
|
||||
throw message;
|
||||
abort();
|
||||
}
|
||||
assert(x == true);
|
||||
void assertTrueImplement(
|
||||
const char *file,
|
||||
const int32_t line,
|
||||
const char *func,
|
||||
const bool_t result,
|
||||
const char *message,
|
||||
...
|
||||
) {
|
||||
if(result) return;
|
||||
|
||||
// Print file info.
|
||||
fprintf(
|
||||
stderr,
|
||||
"Assert failed in %s:%i :: %s\n",
|
||||
file,
|
||||
line,
|
||||
func
|
||||
);
|
||||
|
||||
va_list argptr;
|
||||
va_start(argptr, message);
|
||||
vfprintf(stderr, message, argptr);
|
||||
va_end(argptr);
|
||||
fprintf(stderr, "\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
void assertTrue(bool_t x, std::string message) {
|
||||
assertTrue(x, message.c_str());
|
||||
}
|
||||
|
||||
|
||||
void assertFalse(bool_t x, const char message[]) {
|
||||
assertTrue(!x, message);
|
||||
}
|
||||
|
||||
void assertFalse(bool_t x, std::string message) {
|
||||
assertFalse(x, message.c_str());
|
||||
}
|
||||
|
||||
|
||||
void assertUnreachable(const char message[]) {
|
||||
assertTrue(false, message);
|
||||
}
|
||||
|
||||
void assertUnreachable(std::string message) {
|
||||
assertUnreachable(message.c_str());
|
||||
}
|
||||
|
||||
|
||||
void assertNotNull(void *pointer, const char message[]) {
|
||||
assertTrue(pointer != nullptr && pointer != NULL, message);
|
||||
}
|
||||
|
||||
void assertNotNull(void *pointer, std::string message) {
|
||||
assertNotNull(pointer, message.c_str());
|
||||
}
|
||||
|
||||
|
||||
void assertNull(void *pointer, const char message[]) {
|
||||
assertTrue(pointer == NULL || pointer == nullptr, message);
|
||||
}
|
||||
|
||||
void assertNull(void *pointer, std::string message) {
|
||||
assertNull(pointer, message.c_str());
|
||||
}
|
||||
|
||||
|
||||
void assertDeprecated(const char message[]) {
|
||||
assertUnreachable(message);
|
||||
}
|
||||
|
||||
void assertDeprecated(std::string message) {
|
||||
assertDeprecated(message.c_str());
|
||||
}
|
@ -9,72 +9,83 @@
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
/**
|
||||
* Assert a given value to be true.
|
||||
* @param x Value to assert as true.
|
||||
* @param message Message to throw against assertion failure.
|
||||
*/
|
||||
void assertTrue(bool_t x, const char message[]);
|
||||
void assertTrue(bool_t x, std::string message);
|
||||
|
||||
/**
|
||||
* Asserts a given statement to be false.
|
||||
* @param x Value to assert as false.
|
||||
* @param message Message to throw against assertion failure.
|
||||
*/
|
||||
void assertFalse(bool_t x, const char message[]);
|
||||
void assertFalse(bool_t x, std::string message);
|
||||
|
||||
/**
|
||||
* Asserts that a given line of code is unreachable. Essentially a forced
|
||||
* assertion failure, good for "edge cases"
|
||||
* @param message Message to throw against assertion failure.
|
||||
*/
|
||||
void assertUnreachable(const char message[]);
|
||||
void assertUnreachable(std::string message);
|
||||
|
||||
/**
|
||||
* Assert a given pointer to not point to a null pointer.
|
||||
* @param pointer Pointer to assert is not a null pointer.
|
||||
* @param message Message to throw against assertion failure.
|
||||
*/
|
||||
void assertNotNull(void *pointer, const char message[]);
|
||||
void assertNotNull(void *pointer, std::string message);
|
||||
|
||||
/**
|
||||
* Asserts a given pointer to be a nullptr.
|
||||
* @param pointer Pointer to assert is nullptr.
|
||||
* @param message Message to throw against assertion failure.
|
||||
*/
|
||||
void assertNull(void *pointer, const char message[]);
|
||||
void assertNull(void *pointer, std::string message);
|
||||
|
||||
/**
|
||||
* Asserts a function as being deprecated.
|
||||
* @param message Message to throw against assertion failure.
|
||||
*/
|
||||
void assertDeprecated(const char message[]);
|
||||
void assertDeprecated(std::string message);
|
||||
|
||||
/**
|
||||
* Asserts that a given map has a key.
|
||||
* Asserts that a given statement must evaluate to true or the assertion fails
|
||||
* and the game will close semi-gracefully.
|
||||
*
|
||||
* @param map Map to check.
|
||||
* @param key Key to try and assert exists.
|
||||
* @param message Message to throw against assertion failure.
|
||||
* @param file String filename of the file that has the assertion.
|
||||
* @param line Integer line number within the file that the assertion is on.
|
||||
* @param func Called function that has the assertion.
|
||||
* @param result The statement that must equate to true.
|
||||
* @param message Message (sprintf format) to send to the stdout.
|
||||
* @param ... Varargs of the sprintf arguments.
|
||||
*/
|
||||
template<typename K, typename V>
|
||||
void assertMapHasKey(std::map<K,V> map, K key, const char message[]) {
|
||||
assertTrue(map.find(key) != map.end(), message);
|
||||
}
|
||||
void assertTrueImplement(
|
||||
const char *file,
|
||||
const int32_t line,
|
||||
const char *func,
|
||||
const bool_t result,
|
||||
const char *message,
|
||||
...
|
||||
);
|
||||
|
||||
/**
|
||||
* Asserts that a given map has a key.
|
||||
* Asserts that a statement must be true in order for the assertion to not cause
|
||||
* an error. Basically this is a throw statement in disguise.
|
||||
*
|
||||
* @param map Map to check.
|
||||
* @param key Key to try and assert exists.
|
||||
* @param message Message to throw against assertion failure.
|
||||
* @param statement Statement of the assertion.
|
||||
* @param message Message (sprintf format) to send to the logger.
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
template<typename K, typename V>
|
||||
void assertMapHasKey(std::map<K,V> map, K key, std::string message) {
|
||||
assertMapHasKey(map, key, message.c_str());
|
||||
}
|
||||
#define assertTrue(...) assertTrueImplement( \
|
||||
__FILE__, __LINE__, __func__, __VA_ARGS__ \
|
||||
)
|
||||
|
||||
/**
|
||||
* Asserts that a statement must be false in order for the assertion to not
|
||||
* cause an error.
|
||||
*
|
||||
* @param statement Statement of the assertion.
|
||||
* @param message Message (sprintf format) to send to the logger.
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
#define assertFalse(x, ...) assertTrue(!(x), __VA_ARGS__)
|
||||
|
||||
/**
|
||||
* Asserts that a specified piece of code should be entirely unreachable.
|
||||
* @param message Message (sprintf format) to send to the logger.
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
#define assertUnreachable(...) assertTrue(false, __VA_ARGS__)
|
||||
|
||||
/**
|
||||
* Asserts that a given pointer is not null.
|
||||
* @param x Pointer to check.
|
||||
* @param message Message (sprintf format) to send to the logger.
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
#define assertNotNull(x, ...) assertTrue(x != nullptr, __VA_ARGS__)
|
||||
|
||||
/**
|
||||
* Asserts that a given pointer is null.
|
||||
* @param x Pointer to check.
|
||||
* @param message Message (sprintf format) to send to the logger.
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
#define assertNull(x, ...) assertTrue(x == nullptr, __VA_ARGS__)
|
||||
|
||||
/**
|
||||
* Asserts that a given map has a specific key.
|
||||
* @param map Map to check.
|
||||
* @param key Key to check for.
|
||||
* @param message Message (sprintf format) to send to the logger.
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
#define assertMapHasKey(map, key, ...) assertTrue( \
|
||||
map.find(key) != map.end(), __VA_ARGS__ \
|
||||
)
|
||||
|
||||
/**
|
||||
* Asserts that the current code is deprecated and should not be used anymore.
|
||||
* @deprecated
|
||||
*/
|
||||
#define assertDeprecated(...) assertUnreachable(__VA_ARGS__)
|
Reference in New Issue
Block a user