Configurable assertion failure handling and optional stack trace.
More...
|
| 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.
|
|
| 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.
|
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
◆ AssertionCallback
Called on assertion failure; receives a single formatted string (expression, message, file, function, line).
- Parameters
-
| assertionString | Formatted 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
Called for each stack frame when generating a trace; receives one formatted frame string.
- Parameters
-
| stackFrameString | Formatted frame; valid only for the duration of the callback. |
- Note
- Must not throw.
◆ 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
-
| code | Assertion expression as string (e.g. "x != nullptr"). |
| message | Optional message or nullptr. |
| fileName | Source file where the assertion failed. |
| functionName | Function name where the assertion failed. |
| lineNumber | Source 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 |
( |
| ) |
|
◆ setCallbacks()
Registers the assertion and stack-walk callbacks.
- Parameters
-
| assertionCallback | Invoked on assertion failure; may be nullptr to disable. |
| stackWalkCallback | Invoked 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
◆ s_assertionCallback