ToyGine2 26.2.0
Game Engine for retro consoles
Loading...
Searching...
No Matches
toy::assertion Namespace Reference

Configurable assertion failure handling and optional stack trace. More...

Typedefs

using AssertionCallback = bool (*)(const char * assertionString)
 Called on assertion failure; receives a single formatted string (expression, message, file, function, line).
using StackWalkCallback = void (*)(const char * stackFrameString)
 Called for each stack frame when generating a trace; receives one formatted frame string.

Functions

void initialize ()
 Prepares the assertion system; call before using assertion macros or setCallbacks().
void deInitialize ()
 Shuts down the assertion system and clears callbacks.
void setCallbacks (AssertionCallback assertionCallback, StackWalkCallback stackWalkCallback)
 Registers the assertion and stack-walk callbacks.
void assertion (const char *code, const char *message, const char *fileName, const char *functionName, size_t lineNumber)
 Handles a failed assertion: formats code, message, fileName, functionName, lineNumber and invokes the registered AssertionCallback if set.
void assertCompileTimeError () noexcept
 Intentionally causes a compile-time error when instantiated or evaluated in a constexpr context.

Variables

static AssertionCallback s_assertionCallback = nullptr

Detailed Description

Configurable assertion failure handling and optional stack trace.

Call initialize() before using assertion macros; register callbacks with setCallbacks(). assertion() is invoked by assert / assert_message when a check fails (debug only). assertCompileTimeError() forces a compile-time error in unreachable branches.

Key Features

Typedef Documentation

◆ AssertionCallback

using toy::assertion::AssertionCallback = bool (*)(const char * assertionString)

Called on assertion failure; receives a single formatted string (expression, message, file, function, line).

Parameters
assertionStringFormatted failure description; valid only for the duration of the callback.
Returns
true to treat the assertion as ignored (caller may continue), false otherwise.
Note
Must not throw. Prefer setCallbacks() to register after initialize().

◆ StackWalkCallback

using toy::assertion::StackWalkCallback = void (*)(const char * stackFrameString)

Called for each stack frame when generating a trace; receives one formatted frame string.

Parameters
stackFrameStringFormatted frame; valid only for the duration of the callback.
Note
Must not throw.

Function Documentation

◆ assertCompileTimeError()

void toy::assertion::assertCompileTimeError ( )
inlinenoexcept

Intentionally causes a compile-time error when instantiated or evaluated in a constexpr context.

Use in unreachable template branches or constexpr branches that must never be taken (e.g. exhaustive switch default). Marked [[noreturn]]; does not return.

Note
For valid code this function should never be called at runtime.

◆ assertion()

void toy::assertion::assertion ( const char * code,
const char * message,
const char * fileName,
const char * functionName,
size_t lineNumber )

Handles a failed assertion: formats code, message, fileName, functionName, lineNumber and invokes the registered AssertionCallback if set.

Called by the assert / assert_message macros. Only compiled when _DEBUG is defined.

Parameters
codeAssertion expression as string (e.g. "x != nullptr").
messageOptional message or nullptr.
fileNameSource file where the assertion failed.
functionNameFunction name where the assertion failed.
lineNumberSource line number.
Precondition
initialize() must have been called.
See also
setCallbacks()

◆ deInitialize()

void toy::assertion::deInitialize ( )

Shuts down the assertion system and clears callbacks.

Postcondition
Callbacks are nullptr; no resources held.
Note
Safe to call multiple times.
See also
initialize()

◆ initialize()

void toy::assertion::initialize ( )

Prepares the assertion system; call before using assertion macros or setCallbacks().

Postcondition
Callbacks are reset to nullptr; system is ready for setCallbacks() and assertion().
Note
Safe to call multiple times.
See also
deInitialize(), setCallbacks()

◆ setCallbacks()

void toy::assertion::setCallbacks ( AssertionCallback assertionCallback,
StackWalkCallback stackWalkCallback )

Registers the assertion and stack-walk callbacks.

Parameters
assertionCallbackInvoked on assertion failure; may be nullptr to disable.
stackWalkCallbackInvoked per stack frame when walking; may be nullptr to disable.
Precondition
initialize() must have been called.
Note
Assertion callback returns true to ignore the failure, false otherwise. Callbacks must not throw.
See also
initialize(), AssertionCallback, StackWalkCallback

Variable Documentation

◆ s_assertionCallback

AssertionCallback toy::assertion::s_assertionCallback = nullptr
static