diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-05 13:25:39 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-05 13:25:39 +0200 |
commit | 2c26d57dc97eb7105a6dca2089fd42847a8c2b37 (patch) | |
tree | c28d8e836312a40d22b491b58b15681699647183 /compat/assert.hpp | |
parent | cbfe7450f67eb9e0f7c9ef49d593f88527230a88 (diff) |
a
Diffstat (limited to 'compat/assert.hpp')
-rw-r--r-- | compat/assert.hpp | 53 |
1 files changed, 16 insertions, 37 deletions
diff --git a/compat/assert.hpp b/compat/assert.hpp index 23dfdf42..89c89c64 100644 --- a/compat/assert.hpp +++ b/compat/assert.hpp @@ -1,54 +1,33 @@ #pragma once -#include "defs.hpp" +#include <cstdlib> #include <cstdio> -#include <limits> #include <type_traits> -namespace Magnum::Examples { - -struct exception { - const char* file = nullptr; - const char* function = nullptr; - int line = -1; -}; +namespace Magnum::Examples::detail { -struct assertion_failure final : exception +template<typename...Xs> +constexpr inline void abort(const char* fmt, Xs... xs) { - char msg[128 - sizeof(int) - sizeof(char*)]; -}; - -struct out_of_range final : exception { - ssize_t value = 0; - ssize_t min = std::numeric_limits<ssize_t>::min(); - ssize_t max = std::numeric_limits<ssize_t>::max(); -}; + if (std::is_constant_evaluated()) { + std::fprintf(stderr, fmt, xs...); + std::putc('\n', stderr); + std::fflush(stderr); + std::abort(); + } else + throw "aborting"; +} -#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)) \ - } +} // namespace Magnum::Examples::detail -#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"; \ -}()) +namespace Magnum::Examples { #define ABORT(...) \ do { \ if (std::is_constant_evaluated()) \ throw "aborting"; \ else \ - ABORT_WITH(::Magnum::Examples::assertion_failure, __VA_ARGS__); \ + ::Magnum::Examples::detail:: \ + abort("aborting at %s:%d", __FILE__, __LINE__); \ } while (false) #define ASSERT(expr) \ |