PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
Assert.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "PixelBullet/Application/Log.hpp"
4
5#include <fmt/format.h>
6
7#include <cstdlib>
8#include <filesystem>
9
16#if defined(_WIN32)
17#define DEBUG_BREAK() __debugbreak()
18#elif defined(__linux__)
19#include <signal.h>
20#define DEBUG_BREAK() raise(SIGTRAP)
21#else
22#define DEBUG_BREAK() std::abort()
23#endif
24
25namespace PixelBullet
26{
37 inline void Fail(const char* file, int line, const char* conditionStr)
38 {
39 Log::Error("Assertion '{}' failed at {}:{}", conditionStr, file, line);
41 std::abort();
42 }
43
57 template <typename... Args>
58 void Fail(const char* file, int line, const char* conditionStr, fmt::format_string<Args...> msg, Args&&... args)
59 {
60 Log::Error(msg, std::forward<Args>(args)...);
62 std::abort();
63 }
64
74 inline void Panic(const char* file, int line)
75 {
76 Log::Error("Panic triggered at {}:{}", file, line);
78 std::abort();
79 }
80
92 template <typename... Args>
93 void Panic(const char* file, int line, fmt::format_string<Args...> msg, Args&&... args)
94 {
95 Log::Error(msg, std::forward<Args>(args)...);
97 std::abort();
98 }
99
109 inline void Unreachable(const char* file, int line)
110 {
111 Log::Error("Reached unreachable code at {}:{}", file, line);
112 DEBUG_BREAK();
113 std::abort();
114 }
115
128 template <typename... Args>
129 void Unreachable(const char* file, int line, fmt::format_string<Args...> msg, Args&&... args)
130 {
131 Log::Error(msg, std::forward<Args>(args)...);
132 DEBUG_BREAK();
133 std::abort();
134 }
135
136} // namespace PixelBullet
137
138// -----------------------------------------------------------------------------
139// === Conditional Assertions ================================================
140// -----------------------------------------------------------------------------
141
151#define ASSERT(condition, ...) \
152 do \
153 { \
154 if (!(condition)) \
155 { \
156 PixelBullet::Fail(__FILE__, __LINE__, #condition, ##__VA_ARGS__); \
157 } \
158 } while (0)
159
168#ifdef PB_DEBUG
169#define DEBUG_ASSERT(condition, ...) ASSERT(condition, ##__VA_ARGS__)
170#else
171#define DEBUG_ASSERT(condition, ...) ((void)0)
172#endif
173
183#ifdef PB_DEBUG
184#define ASSUME(condition, ...) ASSERT(condition, ##__VA_ARGS__)
185#else
186#if defined(_MSC_VER)
187#define ASSUME(condition, ...) __assume(condition)
188#elif defined(__clang__) || defined(__GNUC__)
189#define ASSUME(condition, ...) \
190 do \
191 { \
192 if (!(condition)) \
193 __builtin_unreachable(); \
194 } while (0)
195#else
196#define ASSUME(condition, ...) ((void)0)
197#endif
198#endif
199
200// -----------------------------------------------------------------------------
201// === Unconditional Assertions ================================================
202// -----------------------------------------------------------------------------
203
211#define PANIC(...) \
212 do \
213 { \
214 PixelBullet::Panic(__FILE__, __LINE__, ##__VA_ARGS__); \
215 } while (0)
216
225#ifdef PB_DEBUG
226#define UNREACHABLE(...) \
227 do \
228 { \
229 PixelBullet::Unreachable(__FILE__, __LINE__, ##__VA_ARGS__); \
230 } while (0)
231#else
232#if defined(_MSC_VER)
233#define UNREACHABLE(...) __assume(0)
234#elif defined(__clang__) || defined(__GNUC__)
235#define UNREACHABLE(...) __builtin_unreachable()
236#else
237#define UNREACHABLE(...) ((void)0)
238#endif
239#endif
void Panic(const char *file, int line)
Triggers a panic without a custom message.
Definition Assert.hpp:74
#define DEBUG_BREAK()
Platform-specific debug break.
Definition Assert.hpp:22
void Fail(const char *file, int line, const char *conditionStr)
Fails an assertion without a custom message.
Definition Assert.hpp:37
void Unreachable(const char *file, int line)
Handles unreachable code without a custom message.
Definition Assert.hpp:109