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