PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
Assert.hpp File Reference

Provides assertion and panic mechanisms with optional custom formatting. More...

#include "PixelBullet/Application/Log.hpp"
#include <fmt/format.h>
#include <cstdlib>
#include <filesystem>

Go to the source code of this file.

Macros

#define DEBUG_BREAK()
 Platform-specific debug break.
 
#define ASSERT(condition, ...)
 Asserts that a condition is true.
 
#define DEBUG_ASSERT(condition, ...)
 Debug-only assertion.
 
#define ASSUME(condition, ...)
 Assumes that a condition is true.
 
#define PANIC(...)
 Unconditionally triggers a panic.
 
#define UNREACHABLE(...)
 Marks code as unreachable.
 

Functions

void PixelBullet::Fail (const char *file, int line, const char *conditionStr)
 Fails an assertion without a custom message.
 
template<typename... Args>
void PixelBullet::Fail (const char *file, int line, const char *conditionStr, fmt::format_string< Args... > msg, Args &&... args)
 Fails an assertion with a custom formatted message.
 
void PixelBullet::Panic (const char *file, int line)
 Triggers a panic without a custom message.
 
template<typename... Args>
void PixelBullet::Panic (const char *file, int line, fmt::format_string< Args... > msg, Args &&... args)
 Triggers a panic with a custom formatted message.
 
void PixelBullet::Unreachable (const char *file, int line)
 Handles unreachable code without a custom message.
 
template<typename... Args>
void PixelBullet::Unreachable (const char *file, int line, fmt::format_string< Args... > msg, Args &&... args)
 Handles unreachable code with a custom formatted message.
 

Detailed Description

Provides assertion and panic mechanisms with optional custom formatting.

Macro Definition Documentation

◆ ASSERT

#define ASSERT ( condition,
... )
Value:
do \
{ \
if (!(condition)) \
{ \
PixelBullet::Fail(__FILE__, __LINE__, #condition, ##__VA_ARGS__); \
} \
} while (0)

Asserts that a condition is true.

Always checks the condition; if it fails, logs an error and aborts the program. A custom formatted message may be provided.

Parameters
conditionThe condition to assert.
...Optional custom formatted message and its arguments.

◆ ASSUME

#define ASSUME ( condition,
... )
Value:
((void)0)

Assumes that a condition is true.

In debug builds, behaves like ASSERT (i.e., checks the condition). In release builds, provides an optimization hint to the compiler.

Parameters
conditionThe condition to assume.
...Optional custom formatted message and its arguments.

◆ DEBUG_ASSERT

#define DEBUG_ASSERT ( condition,
... )
Value:
((void)0)

Debug-only assertion.

Checks the condition only in debug builds. In release builds, this macro does nothing.

Parameters
conditionThe condition to assert.
...Optional custom formatted message and its arguments.

◆ DEBUG_BREAK

#define DEBUG_BREAK ( )
Value:
std::abort()

Platform-specific debug break.

◆ PANIC

#define PANIC ( ...)
Value:
do \
{ \
PixelBullet::Panic(__FILE__, __LINE__, ##__VA_ARGS__); \
} while (0)

Unconditionally triggers a panic.

Logs an error and aborts the program. A custom formatted message may be provided.

Parameters
...Optional custom formatted message and its arguments.

◆ UNREACHABLE

#define UNREACHABLE ( ...)
Value:
((void)0)

Marks code as unreachable.

In debug builds, logs an error (with an optional custom message) and aborts the program. In release builds, uses compiler intrinsics to mark the code as unreachable.

Parameters
...Optional custom formatted message and its arguments.

Function Documentation

◆ Fail() [1/2]

void PixelBullet::Fail ( const char * file,
int line,
const char * conditionStr )
inline

Fails an assertion without a custom message.

Logs an error message with the condition string, source file, and line number, then triggers a debug break and aborts the program.

Parameters
fileThe source file where the assertion failed.
lineThe line number where the assertion failed.
conditionStrA string representation of the failed condition.

◆ Fail() [2/2]

template<typename... Args>
void PixelBullet::Fail ( const char * file,
int line,
const char * conditionStr,
fmt::format_string< Args... > msg,
Args &&... args )

Fails an assertion with a custom formatted message.

Logs an error using a custom format string and its arguments, then triggers a debug break and aborts the program.

Template Parameters
ArgsVariadic template parameters for formatting.
Parameters
fileThe source file where the assertion failed.
lineThe line number where the assertion failed.
conditionStrA string representation of the failed condition.
msgThe custom format string.
argsThe arguments to be formatted into the message.

◆ Panic() [1/2]

void PixelBullet::Panic ( const char * file,
int line )
inline

Triggers a panic without a custom message.

Logs a panic message with the source file and line number, then triggers a debug break and aborts the program.

Parameters
fileThe source file where the panic was triggered.
lineThe line number where the panic was triggered.

◆ Panic() [2/2]

template<typename... Args>
void PixelBullet::Panic ( const char * file,
int line,
fmt::format_string< Args... > msg,
Args &&... args )

Triggers a panic with a custom formatted message.

Logs a custom panic message, then triggers a debug break and aborts the program.

Template Parameters
ArgsVariadic template parameters for formatting.
Parameters
fileThe source file where the panic was triggered.
lineThe line number where the panic was triggered.
msgThe custom format string.
argsThe arguments to be formatted into the message.

◆ Unreachable() [1/2]

void PixelBullet::Unreachable ( const char * file,
int line )
inline

Handles unreachable code without a custom message.

Logs an error indicating that unreachable code was reached, then triggers a debug break and aborts the program.

Parameters
fileThe source file where the unreachable code was executed.
lineThe line number where the unreachable code was executed.

◆ Unreachable() [2/2]

template<typename... Args>
void PixelBullet::Unreachable ( const char * file,
int line,
fmt::format_string< Args... > msg,
Args &&... args )

Handles unreachable code with a custom formatted message.

Logs a custom error message for unreachable code, then triggers a debug break and aborts the program.

Template Parameters
ArgsVariadic template parameters for formatting.
Parameters
fileThe source file where the unreachable code was executed.
lineThe line number where the unreachable code was executed.
msgThe custom format string.
argsThe arguments to be formatted into the message.