1 | /** @file
|
---|
2 | * MS COM / XPCOM Abstraction Layer:
|
---|
3 | * Assertion macros for COM/XPCOM
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef ___VBox_com_assert_h
|
---|
28 | #define ___VBox_com_assert_h
|
---|
29 |
|
---|
30 | #include <iprt/assert.h>
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Asserts that the COM result code is succeeded in strict builds.
|
---|
34 | * In non-strict builds the result code will be NOREF'ed to kill compiler warnings.
|
---|
35 | *
|
---|
36 | * @param rc COM result code
|
---|
37 | */
|
---|
38 | #define AssertComRC(rc) \
|
---|
39 | do { AssertMsg (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc)); NOREF (rc); } while (0)
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * A special version of AssertComRC that returns the given expression
|
---|
43 | * if the result code is failed.
|
---|
44 | *
|
---|
45 | * @param rc COM result code
|
---|
46 | * @param ret the expression to return
|
---|
47 | */
|
---|
48 | #define AssertComRCReturn(rc, ret) \
|
---|
49 | AssertMsgReturn (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc), ret)
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * A special version of AssertComRC that returns the given result code
|
---|
53 | * if it is failed.
|
---|
54 | *
|
---|
55 | * @param rc COM result code
|
---|
56 | * @param ret the expression to return
|
---|
57 | */
|
---|
58 | #define AssertComRCReturnRC(rc) \
|
---|
59 | AssertMsgReturn (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc), rc)
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * A special version of AssertComRC that returns if the result code is failed.
|
---|
63 | *
|
---|
64 | * @param rc COM result code
|
---|
65 | * @param ret the expression to return
|
---|
66 | */
|
---|
67 | #define AssertComRCReturnVoid(rc) \
|
---|
68 | AssertMsgReturnVoid (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc))
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * A special version of AssertComRC that evaluates the given expression and
|
---|
72 | * breaks if the result code is failed.
|
---|
73 | *
|
---|
74 | * @param rc COM result code
|
---|
75 | * @param eval the expression to evaluate
|
---|
76 | */
|
---|
77 | #define AssertComRCBreak(rc, eval) \
|
---|
78 | if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { eval; break; } } else do {} while (0)
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * A special version of AssertComRC that evaluates the given expression and
|
---|
82 | * throws it if the result code is failed.
|
---|
83 | *
|
---|
84 | * @param rc COM result code
|
---|
85 | * @param eval the expression to evaluate
|
---|
86 | */
|
---|
87 | #define AssertComRCThrow(rc, eval) \
|
---|
88 | if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { throw (eval); } } else do {} while (0)
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * A special version of AssertComRC that just breaks if the result code is
|
---|
92 | * failed.
|
---|
93 | *
|
---|
94 | * @param rc COM result code
|
---|
95 | */
|
---|
96 | #define AssertComRCBreakRC(rc) \
|
---|
97 | if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { break; } } else do {} while (0)
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * A special version of AssertComRC that just throws @a rc if the result code is
|
---|
101 | * failed.
|
---|
102 | *
|
---|
103 | * @param rc COM result code
|
---|
104 | */
|
---|
105 | #define AssertComRCThrowRC(rc) \
|
---|
106 | if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { throw rc; } } else do {} while (0)
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Checks whether the given COM result code is successful.
|
---|
110 | * If not, executes the return statement with this result code.
|
---|
111 | *
|
---|
112 | * @param rc COM result code
|
---|
113 | */
|
---|
114 | #define CheckComRCReturnRC(rc) \
|
---|
115 | if (1) { if (!SUCCEEDED (rc)) return (rc); } else do {} while (0)
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Checks whether the given COM result code is successful.
|
---|
119 | * If not, executes the break statement.
|
---|
120 | *
|
---|
121 | * @param rc COM result code
|
---|
122 | */
|
---|
123 | #define CheckComRCBreakRC(rc) \
|
---|
124 | if (1) { if (!SUCCEEDED (rc)) { break; } } else do {} while (0)
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Checks whether the given COM result code is successful.
|
---|
128 | * If not, throws the given COM result.
|
---|
129 | *
|
---|
130 | * @param rc COM result code
|
---|
131 | */
|
---|
132 | #define CheckComRCThrowRC(rc) \
|
---|
133 | if (1) { if (!SUCCEEDED (rc)) { throw rc; } } else do {} while (0)
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * A section of helpful macros for error output
|
---|
137 | */
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Prints a line describing the given COM result code.
|
---|
141 | * Used by command line tools or for debugging.
|
---|
142 | */
|
---|
143 | #define PRINT_RC_MESSAGE(rc) \
|
---|
144 | do { \
|
---|
145 | RTPrintf ("[!] Primary RC = %Rwa\n", rc); \
|
---|
146 | Log (("[!] Primary RC = %Rwa\n", rc)); \
|
---|
147 | } while (0)
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Prints the extended error information.
|
---|
151 | * Used by command line tools or for debugging.
|
---|
152 | *
|
---|
153 | * @param info com::ErrorInfo instance
|
---|
154 | */
|
---|
155 | #define PRINT_ERROR_INFO(info) \
|
---|
156 | do { \
|
---|
157 | info.print ("[!] "); \
|
---|
158 | } while (0)
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Calls the given interface method and then checks if the return value
|
---|
162 | * (COM result code) indicates a failure. If so, prints the failed
|
---|
163 | * function/line/file and the description of the result code.
|
---|
164 | *
|
---|
165 | * Used by command line tools or for debugging and assumes the |HRESULT rc|
|
---|
166 | * variable is accessible for assigning in the current scope.
|
---|
167 | */
|
---|
168 | #define CHECK_RC(method) \
|
---|
169 | do { \
|
---|
170 | rc = method; \
|
---|
171 | if (FAILED (rc)) { \
|
---|
172 | RTPrintf ("[!] FAILED calling " #method " at line %d!\n", __LINE__); \
|
---|
173 | Log (("[!] FAILED calling " #method " at line %d!\n", __LINE__)); \
|
---|
174 | PRINT_RC_MESSAGE(rc); \
|
---|
175 | } \
|
---|
176 | } while (0)
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Does the same as CHECK_RC(), but executes the |return rc| statement on
|
---|
180 | * failure.
|
---|
181 | */
|
---|
182 | #define CHECK_RC_RET(method) \
|
---|
183 | do { CHECK_RC (method); if (FAILED (rc)) return rc; } while (0)
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Does the same as CHECK_RC(), but executes the |break| statement on
|
---|
187 | * failure.
|
---|
188 | */
|
---|
189 | #define CHECK_RC_BREAK(method) \
|
---|
190 | if (1) { CHECK_RC (method); if (FAILED (rc)) break; } else do {} while (0)
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Calls the given method of the given interface and then checks if the return
|
---|
194 | * value (COM result code) indicates a failure. If so, prints the failed
|
---|
195 | * function/line/file, the description of the result code and attempts to
|
---|
196 | * query the extended error information on the current thread (using
|
---|
197 | * com::ErrorInfo) if the interface reports that it supports error information.
|
---|
198 | *
|
---|
199 | * Used by command line tools or for debugging and assumes the |HRESULT rc|
|
---|
200 | * variable is accessible for assigning in the current scope.
|
---|
201 | */
|
---|
202 | #define CHECK_ERROR(iface, method) \
|
---|
203 | do \
|
---|
204 | { \
|
---|
205 | CHECK_RC(iface->method); \
|
---|
206 | if (FAILED(rc)) { \
|
---|
207 | com::ErrorInfo info (iface); \
|
---|
208 | info.print ("[!] "); \
|
---|
209 | } \
|
---|
210 | } while (0)
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Does the same as CHECK_ERROR(), but executes the |return ret| statement on
|
---|
214 | * failure.
|
---|
215 | */
|
---|
216 | #define CHECK_ERROR_RET(iface, method, ret) \
|
---|
217 | do { CHECK_ERROR (iface, method); if (FAILED (rc)) return (ret); } while (0)
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Does the same as CHECK_ERROR(), but executes the |break| statement on
|
---|
221 | * failure.
|
---|
222 | */
|
---|
223 | #define CHECK_ERROR_BREAK(iface, method) \
|
---|
224 | if (1) { CHECK_ERROR (iface, method); if (FAILED (rc)) break; } else do {} while (0)
|
---|
225 |
|
---|
226 | #define CHECK_ERROR_NOCALL() \
|
---|
227 | do { \
|
---|
228 | com::ErrorInfo info; \
|
---|
229 | PRINT_ERROR_INFO (info); \
|
---|
230 | } while (0)
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Does the same as CHECK_ERROR(), but doesn't need the interface pointer
|
---|
234 | * because doesn't do a check whether the interface supports error info or not.
|
---|
235 | */
|
---|
236 | #define CHECK_ERROR_NI(method) \
|
---|
237 | do { \
|
---|
238 | CHECK_RC (method); \
|
---|
239 | if (FAILED (rc)) { \
|
---|
240 | com::ErrorInfo info; \
|
---|
241 | PRINT_ERROR_INFO (info); \
|
---|
242 | } \
|
---|
243 | } while (0)
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Does the same as CHECK_ERROR_NI(), but executes the |return rc| statement
|
---|
247 | * on failure.
|
---|
248 | */
|
---|
249 | #define CHECK_ERROR_NI_RET(method) \
|
---|
250 | do { CHECK_ERROR_NI (method); if (FAILED (rc)) return rc; } while (0)
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Does the same as CHECK_ERROR_NI(), but executes the |break| statement
|
---|
254 | * on failure.
|
---|
255 | */
|
---|
256 | #define CHECK_ERROR_NI_BREAK(method) \
|
---|
257 | if (1) { CHECK_ERROR_NI (method); if (FAILED (rc)) break; } else do {} while (0)
|
---|
258 |
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Asserts the given expression is true. When the expression is false, prints
|
---|
262 | * a line containing the failied function/line/file; otherwise does nothing.
|
---|
263 | */
|
---|
264 | #define ASSERT(expr) \
|
---|
265 | do { \
|
---|
266 | if (!(expr)) \
|
---|
267 | { \
|
---|
268 | RTPrintf ("[!] ASSERTION FAILED at line %d: %s\n", __LINE__, #expr); \
|
---|
269 | Log (("[!] ASSERTION FAILED at line %d: %s\n", __LINE__, #expr)); \
|
---|
270 | } \
|
---|
271 | } while (0)
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Does the same as ASSERT(), but executes the |return ret| statement if the
|
---|
275 | * expression to assert is false.
|
---|
276 | */
|
---|
277 | #define ASSERT_RET(expr, ret) \
|
---|
278 | do { ASSERT (expr); if (!(expr)) return (ret); } while (0)
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Does the same as ASSERT(), but executes the |break| statement if the
|
---|
282 | * expression to assert is false.
|
---|
283 | */
|
---|
284 | #define ASSERT_BREAK(expr) \
|
---|
285 | if (1) { ASSERT (expr); if (!(expr)) break; } else do {} while (0)
|
---|
286 |
|
---|
287 |
|
---|
288 | #endif
|
---|
289 |
|
---|