HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Assert.h
Go to the documentation of this file.
1 // UT_Assert.h
2 //
3 // Custom portable UT_ASSERT macro
4 //
5 
6 #ifndef UT_ASSERT_H_INCLUDED
7 #define UT_ASSERT_H_INCLUDED
8 
9 #include "UT_API.h"
10 
11 #define UT_ASSERT_LEVEL_NONE 0
12 #define UT_ASSERT_LEVEL_NORMAL 1
13 #define UT_ASSERT_LEVEL_PARANOID 2
14 #define UT_ASSERT_LEVEL_SLOW 3
15 
16 #ifndef UT_ASSERT_LEVEL
17  #define UT_ASSERT_LEVEL UT_ASSERT_LEVEL_NONE
18 #endif
19 
20 // Generates an assertion. Returns TRUE if we should ignore further asserts.
21 UT_API void UT_Assert( const char *file, int linenum, const char *function,
22  const char *condition, const char *reason, int &ignoreflag);
23 
24 // Similar to UT_Assert() but using a variadic format string for the reason.
25 UT_API void UT_AssertFormat( const char *file, int linenum,
26  const char *function, const char *condition, int &ignoreflag,
27  const char *reason_fmt...);
28 
29 
30 // Methods to temporarily disable interactive asserts in specific dangerous
31 // situations.
34 
35 
36 /// Assertions triggered within the lifetime of UT_AssertExceptionScope
37 /// variables will cause UT_AssertException to be thrown instead of the usual
38 /// handling. By default, this is only done when interactive asserts are
39 /// disabled. This class is intended for testing purposes only.
41 {
42 public:
43  UT_AssertExceptionScope(bool when_interactive = true);
45 private:
46  bool myEnabled;
47 };
48 
49 // Exception class thrown when assertions are triggered within an
50 // UT_AssertExceptionScope
52 {
53 public:
54  UT_AssertException(const char *what);
56 
57  UT_AssertException(const UT_AssertException &) = delete;
58  UT_AssertException &operator=(const UT_AssertException &) = delete;
59 
60  const char *what() const noexcept { return myWhat; }
61 
62 private:
63  char *myWhat; // using C string to avoid pulling in UT_String*.h
64 };
65 
66 
67 #if defined(__GNUC__)
68 # define UT_ASSERT_FUNC __PRETTY_FUNCTION__
69 #elif defined(_MSC_VER)
70 # define UT_ASSERT_FUNC __FUNCSIG__
71 #elif defined(__FUNCTION__)
72 # define UT_ASSERT_FUNC __FUNCTION__ "()"
73 #else
74 # define UT_ASSERT_FUNC ((char *)0)
75 #endif
76 
77 #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
78  #ifdef UT_INTERNAL_ASSERT
79  #error UT_INTERNAL_UT_ASSERT is already defined!
80  #endif
81  #define UT_INTERNAL_ASSERT(ZZ, ...) \
82  ((ZZ) ? void(0) : \
83  [&]() { \
84  static int ignore = 0; \
85  if( !ignore ) \
86  UT_AssertFormat( __FILE__, __LINE__, UT_ASSERT_FUNC, \
87  #ZZ, ignore, __VA_ARGS__); \
88  }())
89  #define UT_INTERNAL_VERIFY(ZZ, ...) \
90  do { \
91  static int ignore = 0; \
92  /* always run ZZ */ \
93  if( !(ZZ) && !ignore ) \
94  UT_AssertFormat( __FILE__, __LINE__, UT_ASSERT_FUNC, \
95  #ZZ, ignore, __VA_ARGS__); \
96  } while (false)
97 
98  #define UT_INTERNAL_VERIFY_RETURN(ZZ, RV) \
99  if (!(ZZ)) \
100  { \
101  static int ignore = 0; \
102  if (!ignore) \
103  UT_Assert( __FILE__, __LINE__, UT_ASSERT_FUNC, #ZZ, 0, \
104  ignore); \
105  return RV; \
106  }
107 #endif
108 
109 #ifdef UT_ASSERT_SLOW
110  #error UT_ASSERT_SLOW is already defined!
111 #endif
112 #ifdef UT_ASSERT_P
113  #error UT_ASSERT_P is already defined!
114 #endif
115 #ifdef UT_ASSERT
116  #error UT_ASSERT is already defined!
117 #endif
118 #ifdef UT_ASSERT_MSG_SLOW
119  #error UT_ASSERT_MSG_SLOW is already defined!
120 #endif
121 #ifdef UT_ASSERT_MSG_P
122  #error UT_ASSERT_MSG_P is already defined!
123 #endif
124 #ifdef UT_ASSERT_MSG
125  #error UT_ASSERT_MSG is already defined!
126 #endif
127 
128 // do these in descending order:
129 #if (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_SLOW)
130  #define UT_ASSERT_SLOW(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
131  #define UT_ASSERT_P(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
132  #define UT_ASSERT(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
133  #define UT_ASSERT_MSG_SLOW(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
134  #define UT_ASSERT_MSG_P(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
135  #define UT_ASSERT_MSG(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
136 #elif (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_PARANOID)
137  #define UT_ASSERT_SLOW(ZZ) ((void)0)
138  #define UT_ASSERT_P(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
139  #define UT_ASSERT(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
140  #define UT_ASSERT_MSG_SLOW(ZZ, ...) ((void)0)
141  #define UT_ASSERT_MSG_P(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
142  #define UT_ASSERT_MSG(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
143 #elif (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NORMAL)
144  #define UT_ASSERT_SLOW(ZZ) ((void)0)
145  #define UT_ASSERT_P(ZZ) ((void)0)
146  #define UT_ASSERT(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
147  #define UT_ASSERT_MSG_SLOW(ZZ, ...) ((void)0)
148  #define UT_ASSERT_MSG_P(ZZ, ...) ((void)0)
149  #define UT_ASSERT_MSG(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
150 #else // if (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NONE)
151  #define UT_ASSERT_SLOW(ZZ) ((void)0)
152  #define UT_ASSERT_P(ZZ) ((void)0)
153  #define UT_ASSERT(ZZ) ((void)0)
154  #define UT_ASSERT_MSG_SLOW(ZZ, ...) ((void)0)
155  #define UT_ASSERT_MSG_P(ZZ, ...) ((void)0)
156  #define UT_ASSERT_MSG(ZZ, ...) ((void)0)
157 #endif
158 
159 #include <SYS/SYS_StaticAssert.h>
160 #define UT_ASSERT_COMPILETIME(expr) SYS_STATIC_ASSERT(expr)
161 
162 // This macro allows you to enable a block of code only
163 // when UT_ASSERT is enabled. This will help clean up
164 // "unreferenced variables" in the case where you have variables that
165 // are only referenced in asserts.
166 
167 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NORMAL
168 #define UT_IF_ASSERT(ZZ) ZZ
169 #define UT_IFNOT_ASSERT(ZZ)
170 #else
171 #define UT_IF_ASSERT(ZZ)
172 #define UT_IFNOT_ASSERT(ZZ) ZZ
173 #endif
174 
175 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_PARANOID
176 #define UT_IF_ASSERT_P(ZZ) ZZ
177 #define UT_IFNOT_ASSERT_P(ZZ)
178 #else
179 #define UT_IF_ASSERT_P(ZZ)
180 #define UT_IFNOT_ASSERT_P(ZZ) ZZ
181 #endif
182 
183 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_SLOW
184 #define UT_IF_ASSERT_SLOW(ZZ) ZZ
185 #define UT_IFNOT_ASSERT_SLOW(ZZ)
186 #else
187 #define UT_IF_ASSERT_SLOW(ZZ)
188 #define UT_IFNOT_ASSERT_SLOW(ZZ) ZZ
189 #endif
190 
191 // When asserts are enabled, UT_VERIFY() will trigger an assert if the expr
192 // is false. When asserts are disabled, the expr is still evaluated.
193 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NORMAL
194  #define UT_VERIFY(expr) UT_INTERNAL_VERIFY(expr, 0)
195  #define UT_VERIFY_MSG(expr, ...) UT_INTERNAL_VERIFY(expr, __VA_ARGS__)
196  #define UT_VERIFY_RETURN(ZZ, RV) UT_INTERNAL_VERIFY_RETURN(ZZ, RV)
197  #define UT_VERIFY_RETURN_VOID(ZZ) UT_INTERNAL_VERIFY_RETURN(ZZ,)
198 #else
199  #define UT_VERIFY(expr) ((void)(expr))
200  #define UT_VERIFY_MSG(expr, ...) ((void)(expr))
201  #define UT_VERIFY_RETURN(ZZ, RV) if (!(ZZ)) { return RV; }
202  #define UT_VERIFY_RETURN_VOID(ZZ) if (!(ZZ)) { return; }
203 #endif
204 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_PARANOID
205  #define UT_VERIFY_P(expr) UT_INTERNAL_VERIFY(expr, 0)
206 #else
207  #define UT_VERIFY_P(expr) ((void)(expr))
208 #endif
209 
211 {
217 };
218 
219 /// UTverify_cast performs a static_cast, but when paranoid assertions are
220 /// enabled it will also perform a dynamic cast to verify the cast is valid.
221 /// UT_ASSERT_P() is used instead of UT_ASSERT() to avoid overhead in normal
222 /// development builds.
223 #include <SYS/SYS_Inline.h>
224 template <typename TO_T, typename FROM_T>
225 SYS_FORCE_INLINE TO_T
226 UTverify_cast(FROM_T from)
227 {
228  UT_ASSERT_P(dynamic_cast<TO_T>(from) == from && "Invalid static cast");
229  return static_cast<TO_T>(from);
230 }
231 
232 /// UTsubclassResponsibility raises an assertion indicating that a subclass has
233 /// not implemented an inherited method. This should only be called from a
234 /// base class method that is not expected to run.
235 UT_API void UTsubclassResponsibility(const char *classname, const char *member);
236 
237 #endif // UT_ASSERT_H_INCLUDED
UT_AssertResponse
Definition: UT_Assert.h:210
#define UT_API
Definition: UT_API.h:14
SYS_FORCE_INLINE TO_T UTverify_cast(FROM_T from)
Definition: UT_Assert.h:226
UT_API void UTsubclassResponsibility(const char *classname, const char *member)
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:152
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
UT_API void UTdisableInteractiveAssertsOff()
const char * what() const noexcept
Definition: UT_Assert.h:60
GLenum condition
Definition: glew.h:13027
#define const
Definition: zconf.h:214
UT_API void UT_Assert(const char *file, int linenum, const char *function, const char *condition, const char *reason, int &ignoreflag)
UT_API void UT_AssertFormat(const char *file, int linenum, const char *function, const char *condition, int &ignoreflag, const char *reason_fmt...)
UT_API void UTdisableInteractiveAssertsOn()