HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 #define UT_ASSERT_LEVEL_NONE 0
11 #define UT_ASSERT_LEVEL_NORMAL 1
12 #define UT_ASSERT_LEVEL_PARANOID 2
13 #define UT_ASSERT_LEVEL_SLOW 3
14 
15 #ifndef UT_ASSERT_LEVEL
16  #define UT_ASSERT_LEVEL UT_ASSERT_LEVEL_NONE
17 #endif
18 
19 // Generates an assertion. Returns TRUE if we should ignore further asserts.
20 UT_API void UT_Assert( const char *file, int linenum, const char *function,
21  const char *condition, const char *reason, int &ignoreflag);
22 
23 // Methods to temporarily disable interactive asserts in specific dangerous
24 // situations.
27 
28 #if defined(__GNUC__)
29 # define UT_ASSERT_FUNC __PRETTY_FUNCTION__
30 #elif defined(_MSC_VER) || defined(__FUNCTION__)
31 # define UT_ASSERT_FUNC __FUNCTION__ "()"
32 #else
33 # define UT_ASSERT_FUNC ((char *)0)
34 #endif
35 
36 #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
37  #ifdef UT_INTERNAL_ASSERT
38  #error UT_INTERNAL_UT_ASSERT is already defined!
39  #endif
40  #define UT_INTERNAL_ASSERT(ZZ, MM) \
41  do { \
42  static int ignore = 0; \
43  if( !ignore && !(ZZ) ) \
44  UT_Assert( __FILE__, __LINE__, UT_ASSERT_FUNC, #ZZ, MM, \
45  ignore); \
46  } while (false)
47  #define UT_INTERNAL_VERIFY(ZZ, MM) \
48  do { \
49  static int ignore = 0; \
50  /* always run ZZ */ \
51  if( !(ZZ) && !ignore ) \
52  UT_Assert( __FILE__, __LINE__, UT_ASSERT_FUNC, #ZZ, MM, \
53  ignore); \
54  } while (false)
55 
56  #define UT_INTERNAL_VERIFY_RETURN(ZZ, RV) \
57  if (!(ZZ)) \
58  { \
59  static int ignore = 0; \
60  if (!ignore) \
61  UT_Assert( __FILE__, __LINE__, UT_ASSERT_FUNC, #ZZ, 0, \
62  ignore); \
63  return RV; \
64  }
65 #endif
66 
67 #ifdef UT_ASSERT_P
68  #error UT_ASSERT_P is already defined!
69 #endif
70 #ifdef UT_ASSERT_SLOW
71  #error UT_ASSERT_SLOW is already defined!
72 #endif
73 #ifdef UT_ASSERT
74  #error UT_ASSERT is already defined!
75 #endif
76 
77 // do these in descending order:
78 #if (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_SLOW)
79  #define UT_ASSERT_SLOW(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
80  #define UT_ASSERT_P(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
81  #define UT_ASSERT(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
82  #define UT_ASSERT_MSG_SLOW(ZZ, MM) UT_INTERNAL_ASSERT(ZZ, MM)
83  #define UT_ASSERT_MSG_P(ZZ, MM) UT_INTERNAL_ASSERT(ZZ, MM)
84  #define UT_ASSERT_MSG(ZZ, MM) UT_INTERNAL_ASSERT(ZZ, MM)
85 #elif (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_PARANOID)
86  #define UT_ASSERT_SLOW(ZZ) ((void)0)
87  #define UT_ASSERT_P(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
88  #define UT_ASSERT(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
89  #define UT_ASSERT_MSG_SLOW(ZZ, MM) ((void)0)
90  #define UT_ASSERT_MSG_P(ZZ, MM) UT_INTERNAL_ASSERT(ZZ, MM)
91  #define UT_ASSERT_MSG(ZZ, MM) UT_INTERNAL_ASSERT(ZZ, MM)
92 #elif (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NORMAL)
93  #define UT_ASSERT_SLOW(ZZ) ((void)0)
94  #define UT_ASSERT_P(ZZ) ((void)0)
95  #define UT_ASSERT(ZZ) UT_INTERNAL_ASSERT(ZZ, 0)
96  #define UT_ASSERT_MSG_SLOW(ZZ, MM) ((void)0)
97  #define UT_ASSERT_MSG_P(ZZ, MM) ((void)0)
98  #define UT_ASSERT_MSG(ZZ, MM) UT_INTERNAL_ASSERT(ZZ, MM)
99 #else // if (UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NONE)
100  #define UT_ASSERT_SLOW(ZZ) ((void)0)
101  #define UT_ASSERT_P(ZZ) ((void)0)
102  #define UT_ASSERT(ZZ) ((void)0)
103  #define UT_ASSERT_MSG_SLOW(ZZ, MM) ((void)0)
104  #define UT_ASSERT_MSG_P(ZZ, MM) ((void)0)
105  #define UT_ASSERT_MSG(ZZ, MM) ((void)0)
106 #endif
107 
108 #include <SYS/SYS_StaticAssert.h>
109 #define UT_ASSERT_COMPILETIME(expr) SYS_STATIC_ASSERT(expr)
110 
111 // This macro allows you to enable a block of code only
112 // when UT_ASSERT is enabled. This will help clean up
113 // "unreferenced variables" in the case where you have variables that
114 // are only referenced in asserts.
115 
116 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NORMAL
117 #define UT_IF_ASSERT(ZZ) ZZ
118 #define UT_IFNOT_ASSERT(ZZ)
119 #else
120 #define UT_IF_ASSERT(ZZ)
121 #define UT_IFNOT_ASSERT(ZZ) ZZ
122 #endif
123 
124 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_PARANOID
125 #define UT_IF_ASSERT_P(ZZ) ZZ
126 #define UT_IFNOT_ASSERT_P(ZZ)
127 #else
128 #define UT_IF_ASSERT_P(ZZ)
129 #define UT_IFNOT_ASSERT_P(ZZ) ZZ
130 #endif
131 
132 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_SLOW
133 #define UT_IF_ASSERT_SLOW(ZZ) ZZ
134 #define UT_IFNOT_ASSERT_SLOW(ZZ)
135 #else
136 #define UT_IF_ASSERT_SLOW(ZZ)
137 #define UT_IFNOT_ASSERT_SLOW(ZZ) ZZ
138 #endif
139 
140 // When asserts are enabled, UT_VERIFY() will trigger an assert if the expr
141 // is false. When asserts are disabled, the expr is still evaluated.
142 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_NORMAL
143  #define UT_VERIFY(expr) UT_INTERNAL_VERIFY(expr, 0)
144  #define UT_VERIFY_MSG(expr, MM) UT_INTERNAL_VERIFY(expr, MM)
145  #define UT_VERIFY_RETURN(ZZ, RV) UT_INTERNAL_VERIFY_RETURN(ZZ, RV)
146  #define UT_VERIFY_RETURN_VOID(ZZ) UT_INTERNAL_VERIFY_RETURN(ZZ,)
147 #else
148  #define UT_VERIFY(expr) ((void)(expr))
149  #define UT_VERIFY_MSG(expr, MM) ((void)(expr))
150  #define UT_VERIFY_RETURN(ZZ, RV) if (!(ZZ)) { return RV; }
151  #define UT_VERIFY_RETURN_VOID(ZZ) if (!(ZZ)) { return; }
152 #endif
153 #if UT_ASSERT_LEVEL >= UT_ASSERT_LEVEL_PARANOID
154  #define UT_VERIFY_P(expr) UT_INTERNAL_VERIFY(expr, 0)
155 #else
156  #define UT_VERIFY_P(expr) ((void)(expr))
157 #endif
158 
160 {
166 };
167 
168 /// UTverify_cast performs a static_cast, but when paranoid assertions are
169 /// enabled it will also perform a dynamic cast to verify the cast is valid.
170 /// UT_ASSERT_P() is used instead of UT_ASSERT() to avoid overhead in normal
171 /// development builds.
172 #include <SYS/SYS_Inline.h>
173 template <typename TO_T, typename FROM_T>
174 SYS_FORCE_INLINE TO_T
175 UTverify_cast(FROM_T from)
176 {
177  UT_ASSERT_P(dynamic_cast<TO_T>(from) == from && "Invalid static cast");
178  return static_cast<TO_T>(from);
179 }
180 
181 /// UTsubclassResponsibility raises an assertion indicating that a subclass has
182 /// not implemented an inherited method. This should only be called from a
183 /// base class method that is not expected to run.
184 UT_API void UTsubclassResponsibility(const char *classname, const char *member);
185 
186 #endif // UT_ASSERT_H_INCLUDED
UT_API void UTdisableInteractiveAssertsOn()
#define UT_API
Definition: UT_API.h:12
UT_AssertResponse
Definition: UT_Assert.h:159
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:101
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_FORCE_INLINE TO_T UTverify_cast(FROM_T from)
Definition: UT_Assert.h:175
UT_API void UTsubclassResponsibility(const char *classname, const char *member)
UT_API void UT_Assert(const char *file, int linenum, const char *function, const char *condition, const char *reason, int &ignoreflag)
UT_API void UTdisableInteractiveAssertsOff()