ReXGlue SDK
Loading...
Searching...
No Matches
assert.h
Go to the documentation of this file.
1
12
13#pragma once
14
15#include <assert.h>
16
17#include <cstdio>
18#include <cstdlib>
19#include <exception>
20#include <string>
21#include <utility>
22
23#include <rex/platform.h>
24
25namespace rex::detail {
26
33inline std::string FormatAssertFailure(const char* file, int line, const char* expr,
34 const char* message) {
35 std::string text = std::string("Assertion failed: ") + expr + "\n";
36 if (message[0] != '\0') {
37 text += std::string(" Message: ") + message + "\n";
38 }
39 text += std::string(" Location: ") + file + ":" + std::to_string(line) + "\n";
40 return text;
41}
42
48[[noreturn]] inline void AssertFail(const char* file, int line, const char* expr,
49 const char* message) noexcept {
50 try {
51 std::fputs(FormatAssertFailure(file, line, expr, message).c_str(), stderr);
52 } catch (const std::exception&) {
53 std::fprintf(stderr, "Assertion failed: %s\n Location: %s:%d\n", expr, file, line);
54 }
55 std::fflush(stderr);
56 std::abort();
57}
58
60constexpr const char* AssertMessage() {
61 return "";
62}
63
64constexpr const char* AssertMessage(const char* message) {
65 return message;
66}
67
68} // namespace rex::detail
69
79#ifdef NDEBUG
80#define REX_ASSERT(cond, ...) \
81 ((void)sizeof(decltype(!(cond))), \
82 (void)sizeof(decltype(::rex::detail::AssertMessage(__VA_ARGS__))))
83#else
84#define REX_ASSERT(cond, ...) \
85 ((cond) ? (void)0 \
86 : ::rex::detail::AssertFail(__FILE__, __LINE__, #cond, \
87 ::rex::detail::AssertMessage(__VA_ARGS__)))
88#endif
89
99#ifdef NDEBUG
100#define REX_ASSERT_ALWAYS(...) ((void)sizeof(decltype(::rex::detail::AssertMessage(__VA_ARGS__))))
101#else
102#define REX_ASSERT_ALWAYS(...) \
103 ::rex::detail::AssertFail(__FILE__, __LINE__, "unreachable", \
104 ::rex::detail::AssertMessage(__VA_ARGS__))
105#endif
106
116#ifdef NDEBUG
117#define REX_UNHANDLED_CASE(var) ((void)0)
118#else
119#define REX_UNHANDLED_CASE(var) \
120 ::rex::detail::AssertFail(__FILE__, __LINE__, "unhandled case: " #var, "")
121#endif
122
133#define REX_STATIC_ASSERT_SIZE(type, size) \
134 static_assert(sizeof(type) == (size), "bad definition for " #type ": must be " #size " bytes")
Platform flags, packing macros and export markers.