SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_assert.h
1#ifndef SDL3PP_ASSERT_H_
2#define SDL3PP_ASSERT_H_
3
4#include <SDL3/SDL_assert.h>
5#include "SDL3pp_callbackWrapper.h"
6#include "SDL3pp_strings.h"
7
8namespace SDL {
9
53#ifdef SDL3PP_DOC
54
72#define SDL_ASSERT_LEVEL SomeNumberBasedOnVariousFactors
73
92#define SDL_TriggerBreakpoint() TriggerABreakpointInAPlatformSpecificManner
93
101#define SDL_FUNCTION __FUNCTION__
102
113#define SDL_FILE __FILE_NAME__
114
115#if SDL_VERSION_ATLEAST(3, 4, 0)
116
128#define SDL_ASSERT_FILE SDL_FILE
129
130#endif // SDL_VERSION_ATLEAST(3, 4, 0)
131
137#define SDL_LINE __LINE__
138
156#define SDL_NULL_WHILE_LOOP_CONDITION (0)
157
172#define SDL_disabled_assert(condition) \
173 do { \
174 (void)sizeof((condition)); \
175 } while (SDL_NULL_WHILE_LOOP_CONDITION)
176
177#endif // SDL3PP_DOC
178
192using AssertState = SDL_AssertState;
193
195 SDL_ASSERTION_RETRY;
196
198 SDL_ASSERTION_BREAK;
199
201 SDL_ASSERTION_ABORT;
202
204 SDL_ASSERTION_IGNORE;
205
207 SDL_ASSERTION_ALWAYS_IGNORE;
208
218using AssertData = SDL_AssertData;
219
236 StringParam func,
237 StringParam file,
238 int line)
239{
240 return SDL_ReportAssertion(data, func, file, line);
241}
242
243#ifdef SDL3PP_DOC
244
253#define SDL_AssertBreakpoint() SDL_TriggerBreakpoint()
254
275#define SDL_enabled_assert(condition) \
276 do { \
277 while (!(condition)) { \
278 static struct SDL_AssertData sdl_assert_data = { \
279 false, 0, #condition, NULL, 0, NULL, NULL}; \
280 const SDL_AssertState sdl_assert_state = SDL_ReportAssertion( \
281 &sdl_assert_data, SDL_FUNCTION, SDL_ASSERT_FILE, SDL_LINE); \
282 if (sdl_assert_state == SDL_ASSERTION_RETRY) { \
283 continue; /* go again. */ \
284 } else if (sdl_assert_state == SDL_ASSERTION_BREAK) { \
285 SDL_AssertBreakpoint(); \
286 } \
287 break; /* not retrying. */ \
288 } \
289 } while (SDL_NULL_WHILE_LOOP_CONDITION)
290
321#define SDL_assert(condition) \
322 if (assertion_enabled && (condition)) { trigger_assertion; }
323
354#define SDL_assert_release(condition) SDL_disabled_assert(condition)
355
383#define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
384
406#define SDL_assert_always(condition) SDL_enabled_assert(condition)
407
408#endif // SDL3PP_DOC
409
423using AssertionHandler = AssertState(SDLCALL*)(const AssertData* data,
424 void* userdata);
425
442
466inline void SetAssertionHandler(AssertionHandler handler, void* userdata)
467{
468 return SDL_SetAssertionHandler(handler, userdata);
469}
470
494{
495 SetAssertionHandler(handler.wrapper, handler.data);
496}
497
515{
516 return SDL_GetDefaultAssertionHandler();
517}
518
542inline AssertionHandler GetAssertionHandler(void** puserdata)
543{
544 return SDL_GetAssertionHandler(puserdata);
545}
579{
580 return *SDL_GetAssertionReport();
581}
582
599inline void ResetAssertionReport() { SDL_ResetAssertionReport(); }
600
602
603} // namespace SDL
604
605#endif /* SDL3PP_ASSERT_H_ */
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
AssertState(SDLCALL *)(const AssertData *data, void *userdata) AssertionHandler
A callback that fires when an SDL assertion fails.
Definition: SDL3pp_assert.h:424
const AssertData & GetAssertionReport()
Get a list of all assertion failures.
Definition: SDL3pp_assert.h:578
constexpr AssertState ASSERTION_RETRY
Retry the assert immediately.
Definition: SDL3pp_assert.h:194
void ResetAssertionReport()
Clear the list of all assertion failures.
Definition: SDL3pp_assert.h:599
constexpr AssertState ASSERTION_IGNORE
Ignore the assert.
Definition: SDL3pp_assert.h:203
constexpr AssertState ASSERTION_ABORT
Terminate the program.
Definition: SDL3pp_assert.h:200
AssertState ReportAssertion(AssertData *data, StringParam func, StringParam file, int line)
Never call this directly.
Definition: SDL3pp_assert.h:235
AssertionHandler GetAssertionHandler(void **puserdata)
Get the current assertion handler.
Definition: SDL3pp_assert.h:542
SDL_AssertState AssertState
Possible outcomes from a triggered assertion.
Definition: SDL3pp_assert.h:192
AssertionHandler GetDefaultAssertionHandler()
Get the default assertion handler.
Definition: SDL3pp_assert.h:514
constexpr AssertState ASSERTION_BREAK
Make the debugger trigger a breakpoint.
Definition: SDL3pp_assert.h:197
constexpr AssertState ASSERTION_ALWAYS_IGNORE
Ignore the assert from now on.
Definition: SDL3pp_assert.h:206
SDL_AssertData AssertData
Information about an assertion failure.
Definition: SDL3pp_assert.h:218
void SetAssertionHandler(AssertionHandler handler, void *userdata)
Set an application-defined assertion handler.
Definition: SDL3pp_assert.h:466
Main include header for the SDL3pp library.
Definition: SDL3pp_callbackWrapper.h:197