SDL3pp
A slim C++ wrapper for SDL3
All Classes Namespaces Functions Variables Typedefs Modules Pages Concepts
SDL3pp_assert.h
1#ifndef SDL3PP_ASSERT_H_
2#define SDL3PP_ASSERT_H_
3
4#include <SDL3/SDL_assert.h>
5#include "SDL3pp_stdinc.h"
6
7namespace SDL {
8
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
135#define SDL_NULL_WHILE_LOOP_CONDITION (0)
136
151#define SDL_disabled_assert(condition) \
152 do { \
153 (void)sizeof((condition)); \
154 } while (SDL_NULL_WHILE_LOOP_CONDITION)
155
156#endif // SDL3PP_DOC
157
171using AssertState = SDL_AssertState;
172
176constexpr AssertState ASSERTION_RETRY = SDL_ASSERTION_RETRY;
177
181constexpr AssertState ASSERTION_BREAK = SDL_ASSERTION_BREAK;
182
186constexpr AssertState ASSERTION_ABORT = SDL_ASSERTION_ABORT;
187
191constexpr AssertState ASSERTION_IGNORE = SDL_ASSERTION_IGNORE;
192
196constexpr AssertState ASSERTION_ALWAYS_IGNORE = SDL_ASSERTION_ALWAYS_IGNORE;
197
207using AssertData = SDL_AssertData;
208
225 StringParam func,
226 StringParam file,
227 int line)
228{
229 return SDL_ReportAssertion(data, func, file, line);
230}
231
232#ifdef SDL3PP_DOC
233
242#define SDL_AssertBreakpoint() SDL_TriggerBreakpoint()
243
265#define SDL_enabled_assert(condition) \
266 do { \
267 while (!(condition)) { \
268 static struct SDL_AssertData sdl_assert_data = { \
269 0, 0, #condition, 0, 0, 0, 0}; \
270 const SDL_AssertState sdl_assert_state = SDL_ReportAssertion( \
271 &sdl_assert_data, SDL_FUNCTION, SDL_FILE, SDL_LINE); \
272 if (sdl_assert_state == SDL_ASSERTION_RETRY) { \
273 continue; /* go again. */ \
274 } else if (sdl_assert_state == SDL_ASSERTION_BREAK) { \
275 SDL_AssertBreakpoint(); \
276 } \
277 break; /* not retrying. */ \
278 } \
279 } while (SDL_NULL_WHILE_LOOP_CONDITION)
280
311#define SDL_assert(condition) \
312 if (assertion_enabled && (condition)) { trigger_assertion; }
313
345#define SDL_assert_release(condition) SDL_disabled_assert(condition)
346
374#define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
375
397#define SDL_assert_always(condition) SDL_enabled_assert(condition)
398
399#endif // SDL3PP_DOC
400
414using AssertionHandler = SDL_AssertionHandler;
415
430 std::function<SDL_AssertState(const SDL_AssertData*)>;
431
455inline void SetAssertionHandler(AssertionHandler handler, void* userdata)
456{
458 return SDL_SetAssertionHandler(handler, userdata);
459}
460
484{
486 SetAssertionHandler(&Wrapper::CallSuffixed,
487 Wrapper::Wrap(std::move(handler)));
488}
506{
507 return SDL_GetDefaultAssertionHandler();
508}
509
533inline AssertionHandler GetAssertionHandler(void** puserdata)
534{
535 return SDL_GetAssertionHandler(puserdata);
536}
537
560{
562 void* userdata = nullptr;
563 auto cb = GetAssertionHandler(&userdata);
564 if (Wrapper::contains(userdata)) return Wrapper::Unwrap(userdata);
565 return [cb, userdata](const AssertData* data) { return cb(data, userdata); };
566}
567
601{
602 return SDL_GetAssertionReport();
603}
604
621inline void ResetAssertionReport() { return SDL_ResetAssertionReport(); }
622
624
625} // namespace SDL
626
627#endif /* SDL3PP_ASSERT_H_ */
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
constexpr AssertState ASSERTION_RETRY
Retry the assert immediately.
Definition SDL3pp_assert.h:176
const AssertData * GetAssertionReport()
Get a list of all assertion failures.
Definition SDL3pp_assert.h:600
std::function< SDL_AssertState(const SDL_AssertData *)> AssertionHandlerCB
A callback that fires when an SDL assertion fails.
Definition SDL3pp_assert.h:430
void ResetAssertionReport()
Clear the list of all assertion failures.
Definition SDL3pp_assert.h:621
constexpr AssertState ASSERTION_IGNORE
Ignore the assert.
Definition SDL3pp_assert.h:191
constexpr AssertState ASSERTION_ABORT
Terminate the program.
Definition SDL3pp_assert.h:186
AssertState ReportAssertion(AssertData *data, StringParam func, StringParam file, int line)
Never call this directly.
Definition SDL3pp_assert.h:224
SDL_AssertState AssertState
Possible outcomes from a triggered assertion.
Definition SDL3pp_assert.h:171
AssertionHandler GetDefaultAssertionHandler()
Get the default assertion handler.
Definition SDL3pp_assert.h:505
constexpr AssertState ASSERTION_BREAK
Make the debugger trigger a breakpoint.
Definition SDL3pp_assert.h:181
SDL_AssertionHandler AssertionHandler
A Callback that fires when an SDL assertion fails.
Definition SDL3pp_assert.h:414
constexpr AssertState ASSERTION_ALWAYS_IGNORE
Ignore the assert from now on.
Definition SDL3pp_assert.h:196
SDL_AssertData AssertData
Information about an assertion failure.
Definition SDL3pp_assert.h:207
void SetAssertionHandler(AssertionHandler handler, void *userdata)
Set an application-defined assertion handler.
Definition SDL3pp_assert.h:455
AssertionHandlerCB GetAssertionHandler()
Get the current assertion handler.
Definition SDL3pp_assert.h:559
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Stored Wrapper unique by type result callbacks.
Definition SDL3pp_callbackWrapper.h:242