diff options
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/Common/Assert.hpp | 31 | ||||
| -rw-r--r-- | src/GFX/Mesh.cpp | 4 |
3 files changed, 34 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d0c036f..61e57e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ add_executable(meowcraft src/Math/Functions.hpp src/Common/Casts.hpp src/World/VoxelTraversal.hpp + src/Common/Assert.hpp ) if (WIN32) diff --git a/src/Common/Assert.hpp b/src/Common/Assert.hpp new file mode 100644 index 0000000..9fc84ce --- /dev/null +++ b/src/Common/Assert.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include <csignal> + +// https://stackoverflow.com/a/26100478/11342122 +// Two levels are needed to make sure that the argument is expanded before stringification +#define _ASSERT_IS_DEFINED(x) _ASSERT_IS_DEFINED2(x) +// TODO: This doesn't compile away on -O0, but it's not a big deal +#define _ASSERT_IS_DEFINED2(x) (#x[0] == 0 || (#x[0] >= '1' && #x[0] <= '9')) + +// Stopping macro, non-fatal in debug mode +#define _ASSERT_STOP (_ASSERT_IS_DEFINED(NDEBUG) ? std::abort() : (void)raise(SIGTRAP)) + +// Assertion message macros, with optional message +#define _ASSERT_NOTIFY_NO_MESSAGE(start) std::fprintf(stderr, start ".\n", __FILE__, __LINE__) +#define _ASSERT_NOTIFY_WITH_MESSAGE(start, message) std::fprintf(stderr, start ": %s\n", __FILE__, __LINE__, message "") +#define _ASSERT_NOTIFY(start, ...) ((strcmp(__VA_ARGS__ "", "") == 0) ? _ASSERT_NOTIFY_NO_MESSAGE(start) : _ASSERT_NOTIFY_WITH_MESSAGE(start, __VA_ARGS__)) + +// Debuggable assertion macro, with optional message +#define ASSERT(condition, ...) do { \ + if (__builtin_expect(!(condition), 0)) { \ + _ASSERT_NOTIFY("ASSERT(" #condition ") failed at %s:%d", __VA_ARGS__); \ + _ASSERT_STOP; \ + } \ +} while (0) + +// Debuggable unreachable macro, with optional message +#define UNREACHABLE(...) do { \ + _ASSERT_NOTIFY("UNREACHABLE() reached at %s:%d", __VA_ARGS__); \ + _ASSERT_STOP; \ +} while (0) \ No newline at end of file diff --git a/src/GFX/Mesh.cpp b/src/GFX/Mesh.cpp index c8bda7b..5a3e8bf 100644 --- a/src/GFX/Mesh.cpp +++ b/src/GFX/Mesh.cpp @@ -1,5 +1,5 @@ #include "Mesh.hpp" -#include <cassert> +#include "../Common/Assert.hpp" namespace MC::GFX { @@ -65,7 +65,7 @@ void Mesh::store_indices(const U32* indices, USize indices_size) { } void Mesh::store_in_attribute_list(U32 attribute, Int attribute_size, Int type_size, const void* data, long data_size) { - assert(type_size == sizeof(F32)); + ASSERT(type_size == sizeof(F32), "Only F32 is supported for now"); GLuint vbo; glGenBuffers(1, &vbo); |
