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
108#define SDL_FILE __FILE__
109
115#define SDL_LINE __LINE__
116
134#define SDL_NULL_WHILE_LOOP_CONDITION (0)
135
150#define SDL_disabled_assert(condition) \
151 do { \
152 (void)sizeof((condition)); \
153 } while (SDL_NULL_WHILE_LOOP_CONDITION)
154
155#endif // SDL3PP_DOC
156
170using AssertState = SDL_AssertState;
171
173 SDL_ASSERTION_RETRY;
174
176 SDL_ASSERTION_BREAK;
177
179 SDL_ASSERTION_ABORT;
180
182 SDL_ASSERTION_IGNORE;
183
185 SDL_ASSERTION_ALWAYS_IGNORE;
186
196using AssertData = SDL_AssertData;
197
214 StringParam func,
215 StringParam file,
216 int line)
217{
218 return SDL_ReportAssertion(data, func, file, line);
219}
220
221#ifdef SDL3PP_DOC
222
231#define SDL_AssertBreakpoint() SDL_TriggerBreakpoint()
232
253#define SDL_enabled_assert(condition) \
254 do { \
255 while (!(condition)) { \
256 static struct SDL_AssertData sdl_assert_data = { \
257 false, 0, #condition, NULL, 0, NULL, NULL}; \
258 const SDL_AssertState sdl_assert_state = SDL_ReportAssertion( \
259 &sdl_assert_data, SDL_FUNCTION, SDL_FILE, SDL_LINE); \
260 if (sdl_assert_state == SDL_ASSERTION_RETRY) { \
261 continue; /* go again. */ \
262 } else if (sdl_assert_state == SDL_ASSERTION_BREAK) { \
263 SDL_AssertBreakpoint(); \
264 } \
265 break; /* not retrying. */ \
266 } \
267 } while (SDL_NULL_WHILE_LOOP_CONDITION)
268
299#define SDL_assert(condition) \
300 if (assertion_enabled && (condition)) { trigger_assertion; }
301
332#define SDL_assert_release(condition) SDL_disabled_assert(condition)
333
361#define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
362
384#define SDL_assert_always(condition) SDL_enabled_assert(condition)
385
386#endif // SDL3PP_DOC
387
401using AssertionHandler = SDL_AssertionHandler;
402
418 std::function<SDL_AssertState(const SDL_AssertData*)>;
419
443inline void SetAssertionHandler(AssertionHandler handler, void* userdata)
444{
446 return SDL_SetAssertionHandler(handler, userdata);
447}
448
471{
473 SetAssertionHandler(&Wrapper::CallSuffixed,
474 Wrapper::Wrap(std::move(handler)));
475}
493{
494 return SDL_GetDefaultAssertionHandler();
495}
496
520inline AssertionHandler GetAssertionHandler(void** puserdata)
521{
522 return SDL_GetAssertionHandler(puserdata);
523}
524
547{
549 void* userdata = nullptr;
550 auto cb = GetAssertionHandler(&userdata);
551 if (Wrapper::contains(userdata)) return Wrapper::Unwrap(userdata);
552 return [cb, userdata](const AssertData* data) { return cb(data, userdata); };
553}
554
588{
589 return *SDL_GetAssertionReport();
590}
591
608inline void ResetAssertionReport() { SDL_ResetAssertionReport(); }
609
611
612} // namespace SDL
613
614#endif /* SDL3PP_ASSERT_H_ */
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
const AssertData & GetAssertionReport()
Get a list of all assertion failures.
Definition: SDL3pp_assert.h:587
constexpr AssertState ASSERTION_RETRY
Retry the assert immediately.
Definition: SDL3pp_assert.h:172
std::function< SDL_AssertState(const SDL_AssertData *)> AssertionHandlerCB
A callback that fires when an SDL assertion fails.
Definition: SDL3pp_assert.h:418
void ResetAssertionReport()
Clear the list of all assertion failures.
Definition: SDL3pp_assert.h:608
constexpr AssertState ASSERTION_IGNORE
Ignore the assert.
Definition: SDL3pp_assert.h:181
constexpr AssertState ASSERTION_ABORT
Terminate the program.
Definition: SDL3pp_assert.h:178
AssertState ReportAssertion(AssertData *data, StringParam func, StringParam file, int line)
Never call this directly.
Definition: SDL3pp_assert.h:213
SDL_AssertState AssertState
Possible outcomes from a triggered assertion.
Definition: SDL3pp_assert.h:170
AssertionHandler GetDefaultAssertionHandler()
Get the default assertion handler.
Definition: SDL3pp_assert.h:492
constexpr AssertState ASSERTION_BREAK
Make the debugger trigger a breakpoint.
Definition: SDL3pp_assert.h:175
SDL_AssertionHandler AssertionHandler
A callback that fires when an SDL assertion fails.
Definition: SDL3pp_assert.h:401
constexpr AssertState ASSERTION_ALWAYS_IGNORE
Ignore the assert from now on.
Definition: SDL3pp_assert.h:184
SDL_AssertData AssertData
Information about an assertion failure.
Definition: SDL3pp_assert.h:196
void SetAssertionHandler(AssertionHandler handler, void *userdata)
Set an application-defined assertion handler.
Definition: SDL3pp_assert.h:443
AssertionHandlerCB GetAssertionHandler()
Get the current assertion handler.
Definition: SDL3pp_assert.h:546
Main include header for the SDL3pp library.
Stored Wrapper unique by type result callbacks.
Definition: SDL3pp_callbackWrapper.h:242