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 #if defined(__GNUC__)
36 # define UT_ASSERT_FUNC __PRETTY_FUNCTION__
37 #elif defined(_MSC_VER)
38 # define UT_ASSERT_FUNC __FUNCSIG__
39 #elif defined(__FUNCTION__)
40 # define UT_ASSERT_FUNC __FUNCTION__ "()"
41 #else
42 # define UT_ASSERT_FUNC ((char *)0)
43 #endif
44 
45 #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
46  #ifdef UT_INTERNAL_ASSERT
47  #error UT_INTERNAL_UT_ASSERT is already defined!
48  #endif
49  #define UT_INTERNAL_ASSERT(ZZ, ...) \
50  do { \
51  static int ignore = 0; \
52  if( !ignore && !(ZZ) ) \
53  UT_AssertFormat( __FILE__, __LINE__, UT_ASSERT_FUNC, \
54  #ZZ, ignore, __VA_ARGS__); \
55  } while (false)
56  #define UT_INTERNAL_VERIFY(ZZ, ...) \
57  do { \
58  static int ignore = 0; \
59  /* always run ZZ */ \
60  if( !(ZZ) && !ignore ) \
61  UT_AssertFormat( __FILE__, __LINE__, UT_ASSERT_FUNC, \
62  #ZZ, ignore, __VA_ARGS__); \
63  } while (false)
64 
65  #define UT_INTERNAL_VERIFY_RETURN(ZZ, RV) \
66  if (!(ZZ)) \
67  { \
68  static int ignore = 0; \
69  if (!ignore) \
70  UT_Assert( __FILE__, __LINE__, UT_ASSERT_FUNC, #ZZ, 0, \
71  ignore); \
72  return RV; \
73  }
74  #define UT_INTERNAL_ASSERT_EXPR(ZZ,MM) \
75  ((ZZ) ? void(0) : \
76  [&]() { \
77  static int ignore = 0; \
78  if( !ignore ) \
79  UT_Assert( __FILE__, __LINE__, UT_ASSERT_FUNC, #ZZ, MM, \
80  ignore); \
81  }())
82 #endif
83 
84 #ifdef UT_ASSERT_EXPR
85  #error UT_ASSERT_EXPR is already defined!
86 #endif
87 #ifdef UT_ASSERT_SLOW
88  #error UT_ASSERT_SLOW is already defined!
89 #endif
90 #ifdef UT_ASSERT_P
91  #error UT_ASSERT_P is already defined!
92 #endif
93 #ifdef UT_ASSERT
94  #error UT_ASSERT is already defined!
95 #endif
96 #ifdef UT_ASSERT_MSG_SLOW
97  #error UT_ASSERT_MSG_SLOW is already defined!
98 #endif
99 #ifdef UT_ASSERT_MSG_P
100  #error UT_ASSERT_MSG_P is already defined!
101 #endif
102 #ifdef UT_ASSERT_MSG
103  #error UT_ASSERT_MSG is already defined!
104 #endif
105 
106 // do these in descending order:
107 #if (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_SLOW)
108  #define UT_ASSERT_EXPR(ZZ) UT_INTERNAL_ASSERT_EXPR(ZZ, 0)
109  #define UT_ASSERT_SLOW(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
110  #define UT_ASSERT_P(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
111  #define UT_ASSERT(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
112  #define UT_ASSERT_MSG_SLOW(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
113  #define UT_ASSERT_MSG_P(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
114  #define UT_ASSERT_MSG(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
115 #elif (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_PARANOID)
116  #define UT_ASSERT_EXPR(ZZ) UT_INTERNAL_ASSERT_EXPR(ZZ, 0)
117  #define UT_ASSERT_SLOW(ZZ) ((void)0)
118  #define UT_ASSERT_P(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
119  #define UT_ASSERT(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
120  #define UT_ASSERT_MSG_SLOW(ZZ, ...) ((void)0)
121  #define UT_ASSERT_MSG_P(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
122  #define UT_ASSERT_MSG(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
123 #elif (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NORMAL)
124  #define UT_ASSERT_EXPR(ZZ) UT_INTERNAL_ASSERT_EXPR(ZZ, 0)
125  #define UT_ASSERT_SLOW(ZZ) ((void)0)
126  #define UT_ASSERT_P(ZZ) ((void)0)
127  #define UT_ASSERT(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
128  #define UT_ASSERT_MSG_SLOW(ZZ, ...) ((void)0)
129  #define UT_ASSERT_MSG_P(ZZ, ...) ((void)0)
130  #define UT_ASSERT_MSG(ZZ, ...) UT_INTERNAL_ASSERT(ZZ, __VA_ARGS__)
131 #else // if (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NONE)
132  #define UT_ASSERT_EXPR(ZZ) ((void)0)
133  #define UT_ASSERT_SLOW(ZZ) ((void)0)
134  #define UT_ASSERT_P(ZZ) ((void)0)
135  #define UT_ASSERT(ZZ) ((void)0)
136  #define UT_ASSERT_MSG_SLOW(ZZ, ...) ((void)0)
137  #define UT_ASSERT_MSG_P(ZZ, ...) ((void)0)
138  #define UT_ASSERT_MSG(ZZ, ...) ((void)0)
139 #endif
140 
141 #include <SYS/SYS_StaticAssert.h>
142 #define UT_ASSERT_COMPILETIME(expr) SYS_STATIC_ASSERT(expr)
143 
144 // This macro allows you to enable a block of code only
145 // when UT_ASSERT is enabled. This will help clean up
146 // "unreferenced variables" in the case where you have variables that
147 // are only referenced in asserts.
148 
149 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NORMAL
150 #define UT_IF_ASSERT(ZZ) ZZ
151 #define UT_IFNOT_ASSERT(ZZ)
152 #else
153 #define UT_IF_ASSERT(ZZ)
154 #define UT_IFNOT_ASSERT(ZZ) ZZ
155 #endif
156 
157 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_PARANOID
158 #define UT_IF_ASSERT_P(ZZ) ZZ
159 #define UT_IFNOT_ASSERT_P(ZZ)
160 #else
161 #define UT_IF_ASSERT_P(ZZ)
162 #define UT_IFNOT_ASSERT_P(ZZ) ZZ
163 #endif
164 
165 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_SLOW
166 #define UT_IF_ASSERT_SLOW(ZZ) ZZ
167 #define UT_IFNOT_ASSERT_SLOW(ZZ)
168 #else
169 #define UT_IF_ASSERT_SLOW(ZZ)
170 #define UT_IFNOT_ASSERT_SLOW(ZZ) ZZ
171 #endif
172 
173 // When asserts are enabled, UT_VERIFY() will trigger an assert if the expr
174 // is false. When asserts are disabled, the expr is still evaluated.
175 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NORMAL
176  #define UT_VERIFY(expr) UT_INTERNAL_VERIFY(expr, 0)
177  #define UT_VERIFY_MSG(expr, ...) UT_INTERNAL_VERIFY(expr, __VA_ARGS__)
178  #define UT_VERIFY_RETURN(ZZ, RV) UT_INTERNAL_VERIFY_RETURN(ZZ, RV)
179  #define UT_VERIFY_RETURN_VOID(ZZ) UT_INTERNAL_VERIFY_RETURN(ZZ,)
180 #else
181  #define UT_VERIFY(expr) ((void)(expr))
182  #define UT_VERIFY_MSG(expr, ...) ((void)(expr))
183  #define UT_VERIFY_RETURN(ZZ, RV) if (!(ZZ)) { return RV; }
184  #define UT_VERIFY_RETURN_VOID(ZZ) if (!(ZZ)) { return; }
185 #endif
186 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_PARANOID
187  #define UT_VERIFY_P(expr) UT_INTERNAL_VERIFY(expr, 0)
188 #else
189  #define UT_VERIFY_P(expr) ((void)(expr))
190 #endif
191 
193 {
199 };
200 
201 /// UTverify_cast performs a static_cast, but when paranoid assertions are
202 /// enabled it will also perform a dynamic cast to verify the cast is valid.
203 /// UT_ASSERT_P() is used instead of UT_ASSERT() to avoid overhead in normal
204 /// development builds.
205 #include <SYS/SYS_Inline.h>
206 template <typename TO_T, typename FROM_T>
207 SYS_FORCE_INLINE TO_T
208 UTverify_cast(FROM_T from)
209 {
210  UT_ASSERT_P(dynamic_cast<TO_T>(from) == from && "Invalid static cast");
211  return static_cast<TO_T>(from);
212 }
213 
214 /// UTsubclassResponsibility raises an assertion indicating that a subclass has
215 /// not implemented an inherited method. This should only be called from a
216 /// base class method that is not expected to run.
217 UT_API void UTsubclassResponsibility(const char *classname, const char *member);
218 
219 #endif // UT_ASSERT_H_INCLUDED
UT_AssertResponse
Definition: UT_Assert.h:192
#define UT_API
Definition: UT_API.h:13
SYS_FORCE_INLINE TO_T UTverify_cast(FROM_T from)
Definition: UT_Assert.h:208
UT_API void UTsubclassResponsibility(const char *classname, const char *member)
GLenum condition
Definition: glew.h:12785
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:134
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
UT_API void UTdisableInteractiveAssertsOff()
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()