PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
assert.h
Go to the documentation of this file.
1#pragma once
2
3#include "pixelbullet/application/log.h"
4
5#include <fmt/format.h>
6
7#include <csignal>
8#include <cstdlib>
9#if defined(_MSC_VER)
10#include <intrin.h>
11#endif
12
18namespace pixelbullet
19{
20namespace assert_detail
21{
22inline void TriggerDebugBreak()
23{
24#if defined(_MSC_VER)
25 __debugbreak();
26#elif defined(SIGTRAP)
27 raise(SIGTRAP);
28#endif
29}
30} // namespace assert_detail
31
42[[noreturn]] inline void Fail(const char* file, int line, const char* conditionStr)
43{
44 log::Error("Assertion '{}' failed at {}:{}", conditionStr, file, line);
45 assert_detail::TriggerDebugBreak();
46 std::abort();
47}
48
62template <typename... Args>
63[[noreturn]] void Fail(const char* file, int line, const char* conditionStr, fmt::format_string<Args...> msg, Args&&... args)
64{
65 (void)file;
66 (void)line;
67 (void)conditionStr;
68 log::Error(msg, std::forward<Args>(args)...);
69 assert_detail::TriggerDebugBreak();
70 std::abort();
71}
72
82[[noreturn]] inline void Panic(const char* file, int line)
83{
84 log::Error("Panic triggered at {}:{}", file, line);
85 assert_detail::TriggerDebugBreak();
86 std::abort();
87}
88
100template <typename... Args>
101[[noreturn]] void Panic(const char* file, int line, fmt::format_string<Args...> msg, Args&&... args)
102{
103 (void)file;
104 (void)line;
105 log::Error(msg, std::forward<Args>(args)...);
106 assert_detail::TriggerDebugBreak();
107 std::abort();
108}
109
119[[noreturn]] inline void Unreachable(const char* file, int line)
120{
121 log::Error("Reached unreachable code at {}:{}", file, line);
122 assert_detail::TriggerDebugBreak();
123 std::abort();
124}
125
138template <typename... Args>
139[[noreturn]] void Unreachable(const char* file, int line, fmt::format_string<Args...> msg, Args&&... args)
140{
141 (void)file;
142 (void)line;
143 log::Error(msg, std::forward<Args>(args)...);
144 assert_detail::TriggerDebugBreak();
145 std::abort();
146}
147
148} // namespace pixelbullet
149
150// -----------------------------------------------------------------------------
151// === Conditional Assertions ================================================
152// -----------------------------------------------------------------------------
153
163#define ASSERT(condition, ...) \
164 do \
165 { \
166 if (!(condition)) \
167 { \
168 pixelbullet::Fail(__FILE__, __LINE__, #condition __VA_OPT__(, ) __VA_ARGS__); \
169 } \
170 } while (0)
171
180#ifdef PB_DEBUG
181#define DEBUG_ASSERT(condition, ...) ASSERT(condition __VA_OPT__(, ) __VA_ARGS__)
182#else
183#define DEBUG_ASSERT(condition, ...) ((void)0)
184#endif
185
195#ifdef PB_DEBUG
196#define ASSUME(condition, ...) ASSERT(condition __VA_OPT__(, ) __VA_ARGS__)
197#else
198#if defined(_MSC_VER)
199#define ASSUME(condition, ...) __assume(condition)
200#elif defined(__clang__) || defined(__GNUC__)
201#define ASSUME(condition, ...) \
202 do \
203 { \
204 if (!(condition)) \
205 __builtin_unreachable(); \
206 } while (0)
207#else
208#define ASSUME(condition, ...) ((void)0)
209#endif
210#endif
211
212// -----------------------------------------------------------------------------
213// === Unconditional Assertions ================================================
214// -----------------------------------------------------------------------------
215
223#define PANIC(...) \
224 do \
225 { \
226 pixelbullet::Panic(__FILE__, __LINE__ __VA_OPT__(, ) __VA_ARGS__); \
227 } while (0)
228
237#ifdef PB_DEBUG
238#define UNREACHABLE(...) \
239 do \
240 { \
241 pixelbullet::Unreachable(__FILE__, __LINE__ __VA_OPT__(, ) __VA_ARGS__); \
242 } while (0)
243#else
244#if defined(_MSC_VER)
245#define UNREACHABLE(...) __assume(0)
246#elif defined(__clang__) || defined(__GNUC__)
247#define UNREACHABLE(...) __builtin_unreachable()
248#else
249#define UNREACHABLE(...) ((void)0)
250#endif
251#endif
void Fail(const char *file, int line, const char *conditionStr)
Fails an assertion without a custom message.
Definition assert.h:42
void Unreachable(const char *file, int line)
Handles unreachable code without a custom message.
Definition assert.h:119
void Panic(const char *file, int line)
Triggers a panic without a custom message.
Definition assert.h:82