From 2c26d57dc97eb7105a6dca2089fd42847a8c2b37 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 5 Oct 2022 13:25:39 +0200 Subject: a --- compat/assert.hpp | 53 ++++++++++++++++------------------------------------- compat/defs.hpp | 2 +- 2 files changed, 17 insertions(+), 38 deletions(-) (limited to 'compat') 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 #include -#include #include -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 +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::min(); - ssize_t max = std::numeric_limits::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) \ diff --git a/compat/defs.hpp b/compat/defs.hpp index cc238022..bde7d7e3 100644 --- a/compat/defs.hpp +++ b/compat/defs.hpp @@ -5,7 +5,7 @@ namespace Magnum::Examples { using size_t = std::size_t; -using ssize_t = std::make_signed_t; +using ssize_t = std::common_type_t>; } // namespace Magnum::Examples -- cgit v1.2.3