summaryrefslogtreecommitdiffhomepage
path: root/compat/assert.hpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-04 20:47:26 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-04 20:47:26 +0200
commit32f504c6afb811363b5af0a25fe213d37233c49d (patch)
tree104c5ee7222e83e4361bf9590ed8fe63ec6ee7ef /compat/assert.hpp
parent90a08348862c17488a751f8d8f6a15af218edf95 (diff)
a
Diffstat (limited to 'compat/assert.hpp')
-rw-r--r--compat/assert.hpp68
1 files changed, 36 insertions, 32 deletions
diff --git a/compat/assert.hpp b/compat/assert.hpp
index b2022231..23dfdf42 100644
--- a/compat/assert.hpp
+++ b/compat/assert.hpp
@@ -1,6 +1,8 @@
#pragma once
#include "defs.hpp"
+#include <cstdio>
#include <limits>
+#include <type_traits>
namespace Magnum::Examples {
@@ -21,46 +23,48 @@ struct out_of_range final : exception {
ssize_t max = std::numeric_limits<ssize_t>::max();
};
-struct key_error final : exception {
- ssize_t value = 0;
-};
-
-#define KEY_ERROR(value) \
- ::Magnum::Examples::key_error{{__FILE__, FUNCTION_NAME, __LINE__}, (value)}
-
-#define OUT_OF_RANGE(value, min, max) \
- ::Magnum::Examples::out_of_range{ \
- {__FILE__, FUNCTION_NAME, __LINE__}, \
- ::Magnum::Examples::ssize_t((value)), \
- ::Magnum::Examples::ssize_t((min)), \
- ::Magnum::Examples::ssize_t((max)) \
+#define OUT_OF_RANGE(value, min, max) \
+ ::Magnum::Examples::out_of_range{ \
+ {__FILE__, FUNCTION_NAME, __LINE__}, \
+ ::Magnum::Examples::ssize_t((value)), \
+ ::Magnum::Examples::ssize_t((min)), \
+ ::Magnum::Examples::ssize_t((max)) \
}
-#define ABORT_WITH(exc_type, ...) ([&]() { \
- exc_type _e; \
- _e.line = __LINE__; \
- _e.file = __FILE__; \
- _e.function = FUNCTION_NAME; \
- std::snprintf(_e.msg, sizeof(_e.msg), __VA_ARGS__); \
- throw _e;/*NOLINT(misc-throw-by-value-catch-by-reference)*/ \
+#define ABORT_WITH(exc_type, ...) ([&]() { \
+ if (std::is_constant_evaluated()) { \
+ exc_type _e; \
+ _e.line = __LINE__; \
+ _e.file = __FILE__; \
+ _e.function = FUNCTION_NAME; \
+ std::snprintf(_e.msg, sizeof(_e.msg), __VA_ARGS__); \
+ throw _e;/*NOLINT(misc-throw-by-value-catch-by-reference)*/ \
+ } else \
+ throw "aborting"; \
}())
#define ABORT(...) \
- ABORT_WITH(::Magnum::Examples::assertion_failure, __VA_ARGS__)
+ do { \
+ if (std::is_constant_evaluated()) \
+ throw "aborting"; \
+ else \
+ ABORT_WITH(::Magnum::Examples::assertion_failure, __VA_ARGS__); \
+ } while (false)
-#define ASSERT(expr) \
- do { \
- if (!(expr)) \
- ABORT("assertion failed: '%s' in %s:%d", \
- #expr, __FILE__, __LINE__); \
+#define ASSERT(expr) \
+ do { \
+ if (!(expr)) { \
+ ABORT("assertion failed: '%s' in %s:%d", \
+ #expr, __FILE__, __LINE__); \
+ } \
} while(false)
-#define GAME_DEBUG_OUT(pfx, ...) ([&]() { \
- if constexpr (sizeof((pfx)) > 1) \
- std::fputs((pfx), stderr); \
- std::fprintf(stderr, __VA_ARGS__); \
- std::fputs("\n", stderr); \
- std::fflush(stderr); \
+#define GAME_DEBUG_OUT(pfx, ...) ([&]() { \
+ if constexpr (sizeof((pfx)) > 1) \
+ std::fputs((pfx), stderr); \
+ std::fprintf(stderr, __VA_ARGS__); \
+ std::fputs("\n", stderr); \
+ std::fflush(stderr); \
}())
#define WARN(...) GAME_DEBUG_OUT("warning: ", __VA_ARGS__)