From c5cfc31436a7147fc6ff97a2d64eae0c96d5b07e Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 1 Nov 2023 21:48:10 -0500 Subject: [PATCH] Rewrote assertions --- src/dawn/CMakeLists.txt | 1 + src/dawn/assert/assert.cpp | 81 ++++------- src/dawn/assert/assert.hpp | 137 ++++++++++-------- src/dawn/asset/IAssetLoader.hpp | 4 +- src/dawn/audio/CMakeLists.txt | 10 ++ src/dawn/audio/IAudioManager.cpp | 13 ++ .../{_AudioManager.hpp => IAudioManager.hpp} | 23 ++- src/dawn/display/Color.cpp | 2 +- .../components/ui/text/UIRichTextLabel.cpp | 2 +- src/dawn/util/Xml.cpp | 2 +- src/dawnarchiveasset/asset/AssetLoader.cpp | 2 +- src/dawnopengl/assert/assertgl.cpp | 2 +- 12 files changed, 146 insertions(+), 133 deletions(-) create mode 100644 src/dawn/audio/CMakeLists.txt create mode 100644 src/dawn/audio/IAudioManager.cpp rename src/dawn/audio/{_AudioManager.hpp => IAudioManager.hpp} (53%) diff --git a/src/dawn/CMakeLists.txt b/src/dawn/CMakeLists.txt index 9c38871f..4cd6c9c7 100644 --- a/src/dawn/CMakeLists.txt +++ b/src/dawn/CMakeLists.txt @@ -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) diff --git a/src/dawn/assert/assert.cpp b/src/dawn/assert/assert.cpp index b4fbc027..0ca945ae 100644 --- a/src/dawn/assert/assert.cpp +++ b/src/dawn/assert/assert.cpp @@ -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()); -} \ No newline at end of file diff --git a/src/dawn/assert/assert.hpp b/src/dawn/assert/assert.hpp index 18a2fb55..a5fe4a42 100644 --- a/src/dawn/assert/assert.hpp +++ b/src/dawn/assert/assert.hpp @@ -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 -void assertMapHasKey(std::map 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 -void assertMapHasKey(std::map map, K key, std::string message) { - assertMapHasKey(map, key, message.c_str()); -} \ No newline at end of file +#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__) \ No newline at end of file diff --git a/src/dawn/asset/IAssetLoader.hpp b/src/dawn/asset/IAssetLoader.hpp index 19cab0de..cef60ca5 100644 --- a/src/dawn/asset/IAssetLoader.hpp +++ b/src/dawn/asset/IAssetLoader.hpp @@ -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(); diff --git a/src/dawn/audio/CMakeLists.txt b/src/dawn/audio/CMakeLists.txt new file mode 100644 index 00000000..c594ce51 --- /dev/null +++ b/src/dawn/audio/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/src/dawn/audio/IAudioManager.cpp b/src/dawn/audio/IAudioManager.cpp new file mode 100644 index 00000000..0a5de025 --- /dev/null +++ b/src/dawn/audio/IAudioManager.cpp @@ -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; +} \ No newline at end of file diff --git a/src/dawn/audio/_AudioManager.hpp b/src/dawn/audio/IAudioManager.hpp similarity index 53% rename from src/dawn/audio/_AudioManager.hpp rename to src/dawn/audio/IAudioManager.hpp index 06af4582..97bbf99c 100644 --- a/src/dawn/audio/_AudioManager.hpp +++ b/src/dawn/audio/IAudioManager.hpp @@ -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; }; } \ No newline at end of file diff --git a/src/dawn/display/Color.cpp b/src/dawn/display/Color.cpp index 36b58d5f..796aca53 100644 --- a/src/dawn/display/Color.cpp +++ b/src/dawn/display/Color.cpp @@ -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 {}; } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/text/UIRichTextLabel.cpp b/src/dawn/scene/components/ui/text/UIRichTextLabel.cpp index 02de8155..99a3b342 100644 --- a/src/dawn/scene/components/ui/text/UIRichTextLabel.cpp +++ b/src/dawn/scene/components/ui/text/UIRichTextLabel.cpp @@ -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")) { diff --git a/src/dawn/util/Xml.cpp b/src/dawn/util/Xml.cpp index 08dd8b55..7e6caa1e 100644 --- a/src/dawn/util/Xml.cpp +++ b/src/dawn/util/Xml.cpp @@ -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 { diff --git a/src/dawnarchiveasset/asset/AssetLoader.cpp b/src/dawnarchiveasset/asset/AssetLoader.cpp index 380e97c0..773e3595 100644 --- a/src/dawnarchiveasset/asset/AssetLoader.cpp +++ b/src/dawnarchiveasset/asset/AssetLoader.cpp @@ -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(); diff --git a/src/dawnopengl/assert/assertgl.cpp b/src/dawnopengl/assert/assertgl.cpp index 7a195600..b3869dcb 100644 --- a/src/dawnopengl/assert/assertgl.cpp +++ b/src/dawnopengl/assert/assertgl.cpp @@ -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()); } } \ No newline at end of file