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
54#ifdef SDL3PP_DOC
55
73#define SDL_ASSERT_LEVEL SomeNumberBasedOnVariousFactors
74
93#define SDL_TriggerBreakpoint() TriggerABreakpointInAPlatformSpecificManner
94
102#define SDL_FUNCTION __FUNCTION__
103
109#define SDL_FILE __FILE__
110
116#define SDL_LINE __LINE__
117
136#define SDL_NULL_WHILE_LOOP_CONDITION (0)
137
152#define SDL_disabled_assert(condition) \
153 do { \
154 (void)sizeof((condition)); \
155 } while (SDL_NULL_WHILE_LOOP_CONDITION)
156
157#endif // SDL3PP_DOC
158
172using AssertState = SDL_AssertState;
173
175 SDL_ASSERTION_RETRY;
176
178 SDL_ASSERTION_BREAK;
179
181 SDL_ASSERTION_ABORT;
182
184 SDL_ASSERTION_IGNORE;
185
187 SDL_ASSERTION_ALWAYS_IGNORE;
188
198using AssertData = SDL_AssertData;
199
216 StringParam func,
217 StringParam file,
218 int line)
219{
220 return SDL_ReportAssertion(data, func, file, line);
221}
222
223#ifdef SDL3PP_DOC
224
233#define SDL_AssertBreakpoint() SDL_TriggerBreakpoint()
234
256#define SDL_enabled_assert(condition) \
257 do { \
258 while (!(condition)) { \
259 static struct SDL_AssertData sdl_assert_data = { \
260 0, 0, #condition, 0, 0, 0, 0}; \
261 const SDL_AssertState sdl_assert_state = SDL_ReportAssertion( \
262 &sdl_assert_data, SDL_FUNCTION, SDL_FILE, SDL_LINE); \
263 if (sdl_assert_state == SDL_ASSERTION_RETRY) { \
264 continue; /* go again. */ \
265 } else if (sdl_assert_state == SDL_ASSERTION_BREAK) { \
266 SDL_AssertBreakpoint(); \
267 } \
268 break; /* not retrying. */ \
269 } \
270 } while (SDL_NULL_WHILE_LOOP_CONDITION)
271
302#define SDL_assert(condition) \
303 if (assertion_enabled && (condition)) { trigger_assertion; }
304
336#define SDL_assert_release(condition) SDL_disabled_assert(condition)
337
365#define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
366
388#define SDL_assert_always(condition) SDL_enabled_assert(condition)
389
390#endif // SDL3PP_DOC
391
405using AssertionHandler = SDL_AssertionHandler;
406
421 std::function<SDL_AssertState(const SDL_AssertData*)>;
422
446inline void SetAssertionHandler(AssertionHandler handler, void* userdata)
447{
449 return SDL_SetAssertionHandler(handler, userdata);
450}
451
475{
477 SetAssertionHandler(&Wrapper::CallSuffixed,
478 Wrapper::Wrap(std::move(handler)));
479}
497{
498 return SDL_GetDefaultAssertionHandler();
499}
500
524inline AssertionHandler GetAssertionHandler(void** puserdata)
525{
526 return SDL_GetAssertionHandler(puserdata);
527}
528
551{
553 void* userdata = nullptr;
554 auto cb = GetAssertionHandler(&userdata);
555 if (Wrapper::contains(userdata)) return Wrapper::Unwrap(userdata);
556 return [cb, userdata](const AssertData* data) { return cb(data, userdata); };
557}
558
591{
592 return *SDL_GetAssertionReport();
593}
594
611inline void ResetAssertionReport() { SDL_ResetAssertionReport(); }
612
614
615} // namespace SDL
616
617#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:590
constexpr AssertState ASSERTION_RETRY
Retry the assert immediately.
Definition: SDL3pp_assert.h:174
std::function< SDL_AssertState(const SDL_AssertData *)> AssertionHandlerCB
A callback that fires when an SDL assertion fails.
Definition: SDL3pp_assert.h:421
void ResetAssertionReport()
Clear the list of all assertion failures.
Definition: SDL3pp_assert.h:611
constexpr AssertState ASSERTION_IGNORE
Ignore the assert.
Definition: SDL3pp_assert.h:183
constexpr AssertState ASSERTION_ABORT
Terminate the program.
Definition: SDL3pp_assert.h:180
AssertState ReportAssertion(AssertData *data, StringParam func, StringParam file, int line)
Never call this directly.
Definition: SDL3pp_assert.h:215
SDL_AssertState AssertState
Possible outcomes from a triggered assertion.
Definition: SDL3pp_assert.h:172
AssertionHandler GetDefaultAssertionHandler()
Get the default assertion handler.
Definition: SDL3pp_assert.h:496
constexpr AssertState ASSERTION_BREAK
Make the debugger trigger a breakpoint.
Definition: SDL3pp_assert.h:177
SDL_AssertionHandler AssertionHandler
A callback that fires when an SDL assertion fails.
Definition: SDL3pp_assert.h:405
constexpr AssertState ASSERTION_ALWAYS_IGNORE
Ignore the assert from now on.
Definition: SDL3pp_assert.h:186
SDL_AssertData AssertData
Information about an assertion failure.
Definition: SDL3pp_assert.h:198
void SetAssertionHandler(AssertionHandler handler, void *userdata)
Set an application-defined assertion handler.
Definition: SDL3pp_assert.h:446
AssertionHandlerCB GetAssertionHandler()
Get the current assertion handler.
Definition: SDL3pp_assert.h:550
Main include header for the SDL3pp library.
Stored Wrapper unique by type result callbacks.
Definition: SDL3pp_callbackWrapper.h:242