source: trunk/include/k/kHlpAssert.h@ 37

Last change on this file since 37 was 37, checked in by bird, 16 years ago

kHlpAssert: stmt variations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.6 KB
Line 
1/* $Id: kHlpAssert.h 37 2009-11-10 00:01:22Z bird $ */
2/** @file
3 * kHlpAssert - Assertion Macros.
4 */
5
6/*
7 * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31#ifndef ___kHlpAssert_h___
32#define ___kHlpAssert_h___
33
34#include <k/kHlpDefs.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/** @defgroup grp_kHlpAssert - Assertion Macros
41 * @addtogroup grp_kHlp
42 * @{ */
43
44/** @def K_STRICT
45 * Assertions are enabled when K_STRICT is \#defined. */
46
47/** @def kHlpAssertBreakpoint
48 * Emits a breakpoint instruction or somehow triggers a debugger breakpoint.
49 */
50#ifdef _MSC_VER
51# define kHlpAssertBreakpoint() do { __debugbreak(); } while (0)
52#elif defined(__GNUC__)
53# define kHlpAssertBreakpoint() do { __asm__ __volatile__ ("int3"); } while (0)
54#else
55# error "Port Me"
56#endif
57
58#ifdef K_STRICT
59
60# define kHlpAssert(expr) \
61 do { \
62 if (!(expr)) \
63 { \
64 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
65 kHlpAssertBreakpoint(); \
66 } \
67 } while (0)
68
69# define kHlpAssert(expr, stmt) \
70 do { \
71 if (!(expr)) \
72 { \
73 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
74 kHlpAssertBreakpoint(); \
75 stmt; \
76 } \
77 } while (0)
78
79# define kHlpAssertReturn(expr, rcRet) \
80 do { \
81 if (!(expr)) \
82 { \
83 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
84 kHlpAssertBreakpoint(); \
85 return (rcRet); \
86 } \
87 } while (0)
88
89# define kHlpAssertStmtReturn(expr, stmt, rcRet) \
90 do { \
91 if (!(expr)) \
92 { \
93 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
94 kHlpAssertBreakpoint(); \
95 stmt; \
96 return (rcRet); \
97 } \
98 } while (0)
99
100# define kHlpAssertReturnVoid(expr) \
101 do { \
102 if (!(expr)) \
103 { \
104 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
105 kHlpAssertBreakpoint(); \
106 return; \
107 } \
108 } while (0)
109
110# define kHlpAssertStmtReturnVoid(expr, stmt) \
111 do { \
112 if (!(expr)) \
113 { \
114 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
115 kHlpAssertBreakpoint(); \
116 stmt; \
117 return; \
118 } \
119 } while (0)
120
121# define kHlpAssertMsg(expr, msg) \
122 do { \
123 if (!(expr)) \
124 { \
125 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
126 kHlpAssertMsg2 msg; \
127 kHlpAssertBreakpoint(); \
128 } \
129 } while (0)
130
131# define kHlpAssertMsgStmt(expr, msg, stmt) \
132 do { \
133 if (!(expr)) \
134 { \
135 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
136 kHlpAssertMsg2 msg; \
137 kHlpAssertBreakpoint(); \
138 stmt; \
139 } \
140 } while (0)
141
142# define kHlpAssertMsgReturn(expr, msg, rcRet) \
143 do { \
144 if (!(expr)) \
145 { \
146 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
147 kHlpAssertMsg2 msg; \
148 kHlpAssertBreakpoint(); \
149 return (rcRet); \
150 } \
151 } while (0)
152
153# define kHlpAssertMsgReturn(expr, msg, stmt, rcRet) \
154 do { \
155 if (!(expr)) \
156 { \
157 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
158 kHlpAssertMsg2 msg; \
159 kHlpAssertBreakpoint(); \
160 stmt; \
161 return (rcRet); \
162 } \
163 } while (0)
164
165# define kHlpAssertMsgReturnVoid(expr, msg) \
166 do { \
167 if (!(expr)) \
168 { \
169 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
170 kHlpAssertMsg2 msg; \
171 kHlpAssertBreakpoint(); \
172 return; \
173 } \
174 } while (0)
175
176# define kHlpAssertMsgReturnVoid(expr, msg, stmt) \
177 do { \
178 if (!(expr)) \
179 { \
180 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
181 kHlpAssertMsg2 msg; \
182 kHlpAssertBreakpoint(); \
183 stmt; \
184 return; \
185 } \
186 } while (0)
187
188#else /* !K_STRICT */
189# define kHlpAssert(expr) do { } while (0)
190# define kHlpAssertStmt(expr, stmt) do { if (!(expr)) { stmt; } } while (0)
191# define kHlpAssertReturn(expr, rcRet) do { if (!(expr)) return (rcRet); } while (0)
192# define kHlpAssertStmtReturn(expr, stmt, rcRet) do { if (!(expr)) { stmt; return (rcRet); } } while (0)
193# define kHlpAssertReturnVoid(expr) do { if (!(expr)) return; } while (0)
194# define kHlpAssertStmtReturnVoid(expr, stmt) do { if (!(expr)) { stmt; return; } } while (0)
195# define kHlpAssertMsg(expr, msg) do { } while (0)
196# define kHlpAssertMsgStmt(expr, msg, stmt) do { if (!(expr)) { stmt; } } while (0)
197# define kHlpAssertMsgReturn(expr, msg, rcRet) do { if (!(expr)) return (rcRet); } while (0)
198# define kHlpAssertMsgStmtReturn(expr, msg, stmt, rcRet) do { if (!(expr)) { stmt; return (rcRet); } } while (0)
199# define kHlpAssertMsgReturnVoid(expr, msg) do { if (!(expr)) return; } while (0)
200# define kHlpAssertMsgStmtReturnVoid(expr, msg, stmt) do { if (!(expr)) { stmt; return; } } while (0)
201#endif /* !K_STRICT */
202
203#define kHlpAssertPtr(ptr) kHlpAssertMsg(K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
204#define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
205#define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
206#define kHlpAssertPtrReturnVoid(ptr) kHlpAssertMsgReturnVoid(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
207#define kHlpAssertPtrNull(ptr) kHlpAssertMsg(!(ptr) || K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
208#define kHlpAssertPtrNullReturn(ptr, rcRet) kHlpAssertMsgReturn(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
209#define kHlpAssertPtrNullReturnVoid(ptr) kHlpAssertMsgReturnVoid(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
210#define kHlpAssertRC(rc) kHlpAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
211#define kHlpAssertRCReturn(rc, rcRet) kHlpAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
212#define kHlpAssertRCReturnVoid(rc) kHlpAssertMsgReturnVoid((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)))
213#define kHlpAssertFailed() kHlpAssert(0)
214#define kHlpAssertFailedReturn(rcRet) kHlpAssertReturn(0, (rcRet))
215#define kHlpAssertFailedReturnVoid() kHlpAssertReturnVoid(0)
216#define kHlpAssertMsgFailed(msg) kHlpAssertMsg(0, msg)
217#define kHlpAssertMsgFailedReturn(msg, rcRet) kHlpAssertMsgReturn(0, msg, (rcRet))
218#define kHlpAssertMsgFailedReturnVoid(msg) kHlpAssertMsgReturnVoid(0, msg))
219
220/**
221 * Helper function that displays the first part of the assertion message.
222 *
223 * @param pszExpr The expression.
224 * @param pszFile The file name.
225 * @param iLine The line number is the file.
226 * @param pszFunction The function name.
227 * @internal
228 */
229KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
230
231/**
232 * Helper function that displays custom assert message.
233 *
234 * @param pszFormat Format string that get passed to vprintf.
235 * @param ... Format arguments.
236 * @internal
237 */
238KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...);
239
240
241/** @} */
242
243#ifdef __cplusplus
244}
245#endif
246
247#endif
Note: See TracBrowser for help on using the repository browser.