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/core/debug_break.h"
4#include "pixelbullet/logging/log.h"
5
6#include <fmt/format.h>
7
8#include <cstdlib>
9#include <utility>
10
15
16namespace pixelbullet::assert_internal
17{
28[[noreturn]] inline void fail_assertion(const char* file, int line, const char* condition_str)
29{
30 log::error("Assertion '{}' failed at {}:{}", condition_str, file, line);
31 platform::debug_break();
32 std::abort();
33}
34
48template <typename... Args>
49[[noreturn]] void fail_assertion(const char* file, int line, const char* condition_str, fmt::format_string<Args...> msg, Args&&... args)
50{
51 log::error("Assertion '{}' failed at {}:{}: {}", condition_str, file, line, fmt::format(msg, std::forward<Args>(args)...));
52 platform::debug_break();
53 std::abort();
54}
55
65[[noreturn]] inline void panic(const char* file, int line)
66{
67 log::error("Panic triggered at {}:{}", file, line);
68 platform::debug_break();
69 std::abort();
70}
71
83template <typename... Args>
84[[noreturn]] void panic(const char* file, int line, fmt::format_string<Args...> msg, Args&&... args)
85{
86 log::error("Panic triggered at {}:{}: {}", file, line, fmt::format(msg, std::forward<Args>(args)...));
87 platform::debug_break();
88 std::abort();
89}
90
100[[noreturn]] inline void unreachable(const char* file, int line)
101{
102 log::error("Reached unreachable code at {}:{}", file, line);
103 platform::debug_break();
104 std::abort();
105}
106
119template <typename... Args>
120[[noreturn]] void unreachable(const char* file, int line, fmt::format_string<Args...> msg, Args&&... args)
121{
122 log::error("Reached unreachable code at {}:{}: {}", file, line, fmt::format(msg, std::forward<Args>(args)...));
123 platform::debug_break();
124 std::abort();
125}
126
127} // namespace pixelbullet::assert_internal
128
129// -----------------------------------------------------------------------------
130// === Conditional Assertions ================================================
131// -----------------------------------------------------------------------------
132
142#define ASSERT(condition, ...) \
143 do \
144 { \
145 if (!(condition)) \
146 { \
147 pixelbullet::assert_internal::fail_assertion(__FILE__, __LINE__, #condition __VA_OPT__(, ) __VA_ARGS__); \
148 } \
149 } while (0)
150
159#ifdef PB_DEBUG
160#define DEBUG_ASSERT(condition, ...) ASSERT(condition __VA_OPT__(, ) __VA_ARGS__)
161#else
162#define DEBUG_ASSERT(condition, ...) ((void)0)
163#endif
164
174#ifdef PB_DEBUG
175#define ASSUME(condition, ...) ASSERT(condition __VA_OPT__(, ) __VA_ARGS__)
176#else
177#if defined(_MSC_VER)
178#define ASSUME(condition, ...) __assume(condition)
179#elif defined(__clang__) || defined(__GNUC__)
180#define ASSUME(condition, ...) \
181 do \
182 { \
183 if (!(condition)) \
184 __builtin_unreachable(); \
185 } while (0)
186#else
187#define ASSUME(condition, ...) ((void)0)
188#endif
189#endif
190
191// -----------------------------------------------------------------------------
192// === Unconditional Assertions ================================================
193// -----------------------------------------------------------------------------
194
202#define PANIC(...) \
203 do \
204 { \
205 pixelbullet::assert_internal::panic(__FILE__, __LINE__ __VA_OPT__(, ) __VA_ARGS__); \
206 } while (0)
207
216#ifdef PB_DEBUG
217#define UNREACHABLE(...) \
218 do \
219 { \
220 pixelbullet::assert_internal::unreachable(__FILE__, __LINE__ __VA_OPT__(, ) __VA_ARGS__); \
221 } while (0)
222#else
223#if defined(_MSC_VER)
224#define UNREACHABLE(...) __assume(0)
225#elif defined(__clang__) || defined(__GNUC__)
226#define UNREACHABLE(...) __builtin_unreachable()
227#else
228#define UNREACHABLE(...) ((void)0)
229#endif
230#endif
void fail_assertion(const char *file, int line, const char *condition_str)
Fails an assertion without a custom message.
Definition assert.h:28
void panic(const char *file, int line)
Triggers a panic without a custom message.
Definition assert.h:65
void unreachable(const char *file, int line)
Handles unreachable code without a custom message.
Definition assert.h:100