Plasma Engine  2.0
Loading...
Searching...
No Matches
Assert.h
1#pragma once
2
3#ifndef PL_INCLUDING_BASICS_H
4# error "Please don't include Assert.h directly, but instead include Foundation/Basics.h"
5#endif
6
8
31
32class plFormatString;
33
35using plAssertHandler = bool (*)(const char* szSourceFile, plUInt32 uiLine, const char* szFunction, const char* szExpression, const char* szAssertMsg);
36
37PL_FOUNDATION_DLL bool plDefaultAssertHandler(const char* szSourceFile, plUInt32 uiLine, const char* szFunction, const char* szExpression, const char* szAssertMsg);
38
40PL_FOUNDATION_DLL plAssertHandler plGetAssertHandler();
41
43PL_FOUNDATION_DLL void plSetAssertHandler(plAssertHandler handler);
44
46PL_FOUNDATION_DLL bool plFailedCheck(const char* szSourceFile, plUInt32 uiLine, const char* szFunction, const char* szExpression, const class plFormatString& msg);
47PL_FOUNDATION_DLL bool plFailedCheck(const char* szSourceFile, plUInt32 uiLine, const char* szFunction, const char* szExpression, const char* szMsg);
48
50inline const char* plFmt(const char* szFormat)
51{
52 return szFormat;
53}
54
55#if PL_ENABLED(PL_COMPILER_MSVC)
56// Hides the call to __debugbreak from MSVCs optimizer to work around a bug in VS 2019
57// that can lead to code (memcpy) after an assert to be omitted
58PL_FOUNDATION_DLL void MSVC_OutOfLine_DebugBreak(...);
59#endif
60
61#ifdef BUILDSYSTEM_CLANG_TIDY
62[[noreturn]] void ClangTidyDoNotReturn();
63# define PL_REPORT_FAILURE(szErrorMsg, ...) ClangTidyDoNotReturn()
64#else
67# define PL_REPORT_FAILURE(szErrorMsg, ...) \
68 do \
69 { \
70 if (plFailedCheck(PL_SOURCE_FILE, PL_SOURCE_LINE, PL_SOURCE_FUNCTION, "", plFmt(szErrorMsg, ##__VA_ARGS__))) \
71 PL_DEBUG_BREAK; \
72 } while (false)
73#endif
74
75#ifdef BUILDSYSTEM_CLANG_TIDY
76# define PL_ASSERT_ALWAYS(bCondition, szErrorMsg, ...) \
77 do \
78 { \
79 if (!!(bCondition) == false) \
80 ClangTidyDoNotReturn(); \
81 } while (false)
82
83# define PL_ANALYSIS_ASSUME(bCondition) PL_ASSERT_ALWAYS(bCondition, "")
84#else
87# define PL_ASSERT_ALWAYS(bCondition, szErrorMsg, ...) \
88 do \
89 { \
90 PL_MSVC_ANALYSIS_WARNING_PUSH \
91 PL_MSVC_ANALYSIS_WARNING_DISABLE(6326) /* disable static analysis for the comparison */ \
92 if (!!(bCondition) == false) \
93 { \
94 if (plFailedCheck(PL_SOURCE_FILE, PL_SOURCE_LINE, PL_SOURCE_FUNCTION, #bCondition, plFmt(szErrorMsg, ##__VA_ARGS__))) \
95 PL_DEBUG_BREAK; \
96 } \
97 PL_MSVC_ANALYSIS_WARNING_POP \
98 } while (false)
99
102# define PL_ANALYSIS_ASSUME(bCondition)
103#endif
104
107#define PL_ASSERT_NOT_IMPLEMENTED PL_REPORT_FAILURE("Not implemented");
108
109// Occurrences of PL_ASSERT_DEBUG are compiled out in non-debug builds
110#if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
116# define PL_ASSERT_DEBUG PL_ASSERT_ALWAYS
117#else
123# define PL_ASSERT_DEBUG(bCondition, szErrorMsg, ...)
124#endif
125
126
127// Occurrences of PL_ASSERT_DEV are compiled out in non-development builds
128#if PL_ENABLED(PL_COMPILE_FOR_DEVELOPMENT) || PL_ENABLED(PL_COMPILE_FOR_DEBUG)
129
135# define PL_ASSERT_DEV PL_ASSERT_ALWAYS
136
142# define PL_VERIFY PL_ASSERT_ALWAYS
143
144#else
145
151# define PL_ASSERT_DEV(bCondition, szErrorMsg, ...)
152
158# define PL_VERIFY(bCondition, szErrorMsg, ...) \
159 if (!!(bCondition) == false) \
160 { /* The condition is evaluated, even though nothing is done with it. */ \
161 }
162
163#endif
164
165#if PL_DISABLE_RELEASE_ASSERTS
166
172# define PL_ASSERT_RELEASE(bCondition, szErrorMsg, ...)
173
174#else
175
181# define PL_ASSERT_RELEASE PL_ASSERT_ALWAYS
182
183#endif
184
186#define PL_DEFAULT_CASE_NOT_IMPLEMENTED \
187 default: \
188 PL_ASSERT_NOT_IMPLEMENTED \
189 break;
Implements formating of strings with placeholders and formatting options.
Definition FormatString.h:59