diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-02 15:29:41 +0200 |
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-02 15:29:41 +0200 |
| commit | 445caf6184cafbce361670ea6028ff76317976bc (patch) | |
| tree | 0ff8c2c33211f7a738de8f6f5d6cec79aa776f36 /compat | |
| parent | f339fa747348e742859701331b529f86d7bd8453 (diff) | |
a
Diffstat (limited to 'compat')
| -rw-r--r-- | compat/assert.hpp | 70 | ||||
| -rw-r--r-- | compat/defs.hpp | 18 | ||||
| -rw-r--r-- | compat/sysexits.hpp | 11 |
3 files changed, 99 insertions, 0 deletions
diff --git a/compat/assert.hpp b/compat/assert.hpp new file mode 100644 index 00000000..b2022231 --- /dev/null +++ b/compat/assert.hpp @@ -0,0 +1,70 @@ +#pragma once +#include "defs.hpp" +#include <limits> + +namespace Magnum::Examples { + +struct exception { + const char* file = nullptr; + const char* function = nullptr; + int line = -1; +}; + +struct assertion_failure final : exception +{ + 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(); +}; + +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 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(...) \ + ABORT_WITH(::Magnum::Examples::assertion_failure, __VA_ARGS__) + +#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 WARN(...) GAME_DEBUG_OUT("warning: ", __VA_ARGS__) +#define ERR(...) GAME_DEBUG_OUT("error: ", __VA_ARGS__) +#define DEBUG(...) GAME_DEBUG_OUT("", __VA_ARGS__) + +} // namespace Magnum::Examples diff --git a/compat/defs.hpp b/compat/defs.hpp new file mode 100644 index 00000000..cc238022 --- /dev/null +++ b/compat/defs.hpp @@ -0,0 +1,18 @@ +#pragma once +#include <cstddef> +#include <type_traits> + +namespace Magnum::Examples { + +using size_t = std::size_t; +using ssize_t = std::make_signed_t<std::size_t>; + +} // namespace Magnum::Examples + +#ifdef _MSC_VER +# define FUNCTION_NAME __FUNCSIG__ +#else +# define FUNCTION_NAME __PRETTY_FUNCTION__ +#endif + +#define progn(...) [&]{__VA_ARGS__;}() diff --git a/compat/sysexits.hpp b/compat/sysexits.hpp new file mode 100644 index 00000000..a7a9c317 --- /dev/null +++ b/compat/sysexits.hpp @@ -0,0 +1,11 @@ +#pragma once +#ifdef _WIN32 +# define EX_OK 0 /* successful termination */ +# define EX_USAGE 64 /* command line usage error */ +# define EX_DATAERR 65 /* data format error */ +# define EX_SOFTWARE 70 /* internal software error */ +# define EX_CANTCREAT 73 /* can't create (user) output file */ +# define EX_IOERR 74 /* input/output error */ +#else +# include <sysexits.h> +#endif |
