Rewrote assertions

This commit is contained in:
2023-11-01 21:48:10 -05:00
parent 0752d7ba4b
commit 0126661ce2
12 changed files with 146 additions and 133 deletions

View File

@ -26,6 +26,7 @@ target_sources(${DAWN_TARGET_NAME}
# Subdirs
add_subdirectory(assert)
add_subdirectory(asset)
add_subdirectory(audio)
add_subdirectory(display)
add_subdirectory(game)
add_subdirectory(games)

View File

@ -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());
}

View File

@ -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__)

View File

@ -97,8 +97,8 @@ namespace Dawn {
int32_t i;
bool_t result;
assertNotNull(instance);
assertNotNull(callback);
assertNotNull(instance, "Instance cannot be null.");
assertNotNull(callback, "Callback cannot be null.");
// Open the buffer.
this->open();

View File

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

View File

@ -0,0 +1,13 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "IAudioManager.hpp"
using namespace Dawn;
IAudioManager::IAudioManager(DawnGame *game) {
assertNotNull(game, "Game cannot be null.");
this->game = game;
}

View File

@ -4,22 +4,31 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "assert/assert.hpp"
namespace Dawn {
class DawnGame;
class IAudioManager {
public:
protected:
DawnGame *game;
IAudioManager(DawnGame *game) {
assertNotNull(game);
this->game = game;
}
public:
/**
* Construct a new IAudioManager.
*
* @param game The game instance.
*/
IAudioManager(DawnGame *game);
/**
* Initializes the audio manager system.
*/
virtual void init() = 0;
/**
* Ticks/Update the audio manager system.
*/
virtual void update() = 0;
};
}

View File

@ -78,6 +78,6 @@ struct Color Color::fromString(std::string str) {
}
// TODO: Parse other kinds of colors
assertUnreachable("Failed to find a color match for " + str);
assertUnreachable("Failed to find a color match for %s", str);
return {};
}

View File

@ -39,7 +39,7 @@ void UIRichTextLabel::onStart() {
bufferTexts.push_back(text);
} else if(child.nodeType == XML_NODE_TYPE_ELEMENT) {
auto node = child.child;
assertTrue(node->node == "font", "UIRichTextLabel::onStart: Unknown node type '" + node->node + "'");
assertTrue(node->node == "font", "UIRichTextLabel::onStart: Unknown node type '%s'", node->node.c_str());
struct UILabelStyle style;
if(node->attributes.contains("font")) {

View File

@ -221,7 +221,7 @@ void Xml::load(Xml *xml, std::string data, size_t *j) {
int code = std::stoi(sc.substr(1));
buffer += (char)code;
} else {
assertUnreachable("Unknown Special character: " + sc);
assertUnreachable("Unknown Special character: %s", sc.c_str());
}
}
} else {

View File

@ -65,7 +65,7 @@ void AssetLoader::open() {
assertNull(this->assetArchiveEntry, "AssetLoader::open: Entry is already open");
this->assetArchiveFile = fopen(DAWN_ASSET_LOCATION, "rb");
assertNotNull(this->assetArchiveFile, "AssetLoader::open: Failed to open file " + std::string(DAWN_ASSET_LOCATION));
assertNotNull(this->assetArchiveFile, "AssetLoader::open: Failed to open file %s", DAWN_ASSET_LOCATION);
// Open archive reader
assetArchive = archive_read_new();

View File

@ -53,6 +53,6 @@ void assertNotGLErrorCheck(const char *file, int32_t line) {
if(errorCount != 0) {
error += "\n" + std::string(file) + " (" + std::to_string(line) + ")";
assertUnreachable(error);
assertUnreachable(error.c_str());
}
}