swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdint.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #define __FILENAME__ \ |
| 7 | (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 \ |
| 8 | : strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) |
| 9 | |
| 10 | |
| 11 | #define ASSERT(message, body) \ |
| 12 | { \ |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 13 | if (!(body)) \ |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 14 | THROW("Assert failed: " message " [" #body "]"); \ |
| 15 | } |
| 16 | |
| 17 | #define MAX_CATCH_LEN 32 |
| 18 | |
| 19 | typedef void (* handle_t)(intptr_t); |
| 20 | |
| 21 | typedef struct |
| 22 | { |
| 23 | handle_t fn; |
| 24 | intptr_t arg; |
| 25 | } catch_t; |
| 26 | |
| 27 | extern catch_t g_catch[MAX_CATCH_LEN]; |
| 28 | extern unsigned g_catch_len; |
| 29 | |
| 30 | void throw_(const char *msg, const char *file, unsigned int line); |
| 31 | void catch_(handle_t hdl, intptr_t arg); |
| 32 | |
| 33 | #define THROW(msg) throw_(msg, __FILENAME__, __LINE__) |
| 34 | #define CATCH(fn, arg) catch_((handle_t) fn, (intptr_t) arg) |