VirtualBox

source: vbox/trunk/include/VBox/com/assert.h@ 8155

Last change on this file since 8155 was 8155, checked in by vboxsync, 16 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * Assertion macros for COM/XPCOM
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31#ifndef ___VBox_com_assert_h
32#define ___VBox_com_assert_h
33
34#include <iprt/assert.h>
35
36/**
37 * Asserts that the COM result code is succeeded in strict builds.
38 * In non-strict builds the result code will be NOREF'ed to kill compiler warnings.
39 *
40 * @param rc COM result code
41 */
42#define AssertComRC(rc) \
43 do { AssertMsg (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc)); NOREF (rc); } while (0)
44
45/**
46 * A special version of AssertComRC that returns the given expression
47 * if the result code is failed.
48 *
49 * @param rc COM result code
50 * @param ret the expression to return
51 */
52#define AssertComRCReturn(rc, ret) \
53 AssertMsgReturn (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc), ret)
54
55/**
56 * A special version of AssertComRC that returns the given result code
57 * if it is failed.
58 *
59 * @param rc COM result code
60 * @param ret the expression to return
61 */
62#define AssertComRCReturnRC(rc) \
63 AssertMsgReturn (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc), rc)
64
65/**
66 * A special version of AssertComRC that returns if the result code is failed.
67 *
68 * @param rc COM result code
69 * @param ret the expression to return
70 */
71#define AssertComRCReturnVoid(rc) \
72 AssertMsgReturnVoid (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc))
73
74/**
75 * A special version of AssertComRC that evaluates the given expression and
76 * breaks if the result code is failed.
77 *
78 * @param rc COM result code
79 * @param eval the expression to evaluate
80 */
81#define AssertComRCBreak(rc, eval) \
82 if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { eval; break; } } else do {} while (0)
83
84/**
85 * A special version of AssertComRC that evaluates the given expression and
86 * throws it if the result code is failed.
87 *
88 * @param rc COM result code
89 * @param eval the expression to evaluate
90 */
91#define AssertComRCThrow(rc, eval) \
92 if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { throw (eval); } } else do {} while (0)
93
94/**
95 * A special version of AssertComRC that just breaks if the result code is
96 * failed.
97 *
98 * @param rc COM result code
99 */
100#define AssertComRCBreakRC(rc) \
101 if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { break; } } else do {} while (0)
102
103/**
104 * A special version of AssertComRC that just throws @a rc if the result code is
105 * failed.
106 *
107 * @param rc COM result code
108 */
109#define AssertComRCThrowRC(rc) \
110 if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { throw rc; } } else do {} while (0)
111
112/**
113 * Checks whether the given COM result code is successful.
114 * If not, executes the return statement with this result code.
115 *
116 * @param rc COM result code
117 */
118#define CheckComRCReturnRC(rc) \
119 if (1) { if (!SUCCEEDED (rc)) return (rc); } else do {} while (0)
120
121/**
122 * Checks whether the given COM result code is successful.
123 * If not, executes the break statement.
124 *
125 * @param rc COM result code
126 */
127#define CheckComRCBreakRC(rc) \
128 if (1) { if (!SUCCEEDED (rc)) { break; } } else do {} while (0)
129
130/**
131 * Checks whether the given COM result code is successful.
132 * If not, throws the given COM result.
133 *
134 * @param rc COM result code
135 */
136#define CheckComRCThrowRC(rc) \
137 if (1) { if (!SUCCEEDED (rc)) { throw rc; } } else do {} while (0)
138
139/*
140 * A section of helpful macros for error output
141 */
142
143/**
144 * Prints a line describing the given COM result code.
145 * Used by command line tools or for debugging.
146 */
147#define PRINT_RC_MESSAGE(rc) \
148 do { \
149 RTPrintf ("[!] Primary RC = %Rwa\n", rc); \
150 Log (("[!] Primary RC = %Rwa\n", rc)); \
151 } while (0)
152
153/**
154 * Prints the extended error information.
155 * Used by command line tools or for debugging.
156 *
157 * @param info com::ErrorInfo instance
158 */
159#define PRINT_ERROR_INFO(info) \
160 do { \
161 info.print ("[!] "); \
162 } while (0)
163
164/**
165 * Calls the given interface method and then checks if the return value
166 * (COM result code) indicates a failure. If so, prints the failed
167 * function/line/file and the description of the result code.
168 *
169 * Used by command line tools or for debugging and assumes the |HRESULT rc|
170 * variable is accessible for assigning in the current scope.
171 */
172#define CHECK_RC(method) \
173 do { \
174 rc = method; \
175 if (FAILED (rc)) { \
176 RTPrintf ("[!] FAILED calling " #method " at line %d!\n", __LINE__); \
177 Log (("[!] FAILED calling " #method " at line %d!\n", __LINE__)); \
178 PRINT_RC_MESSAGE(rc); \
179 } \
180 } while (0)
181
182/**
183 * Does the same as CHECK_RC(), but executes the |return rc| statement on
184 * failure.
185 */
186#define CHECK_RC_RET(method) \
187 do { CHECK_RC (method); if (FAILED (rc)) return rc; } while (0)
188
189/**
190 * Does the same as CHECK_RC(), but executes the |break| statement on
191 * failure.
192 */
193#define CHECK_RC_BREAK(method) \
194 if (1) { CHECK_RC (method); if (FAILED (rc)) break; } else do {} while (0)
195
196/**
197 * Calls the given method of the given interface and then checks if the return
198 * value (COM result code) indicates a failure. If so, prints the failed
199 * function/line/file, the description of the result code and attempts to
200 * query the extended error information on the current thread (using
201 * com::ErrorInfo) if the interface reports that it supports error information.
202 *
203 * Used by command line tools or for debugging and assumes the |HRESULT rc|
204 * variable is accessible for assigning in the current scope.
205 */
206#define CHECK_ERROR(iface, method) \
207 do \
208 { \
209 CHECK_RC(iface->method); \
210 if (FAILED(rc)) { \
211 com::ErrorInfo info (iface); \
212 info.print ("[!] "); \
213 } \
214 } while (0)
215
216/**
217 * Does the same as CHECK_ERROR(), but executes the |return ret| statement on
218 * failure.
219 */
220#define CHECK_ERROR_RET(iface, method, ret) \
221 do { CHECK_ERROR (iface, method); if (FAILED (rc)) return (ret); } while (0)
222
223/**
224 * Does the same as CHECK_ERROR(), but executes the |break| statement on
225 * failure.
226 */
227#define CHECK_ERROR_BREAK(iface, method) \
228 if (1) { CHECK_ERROR (iface, method); if (FAILED (rc)) break; } else do {} while (0)
229
230#define CHECK_ERROR_NOCALL() \
231 do { \
232 com::ErrorInfo info; \
233 PRINT_ERROR_INFO (info); \
234 } while (0)
235
236/**
237 * Does the same as CHECK_ERROR(), but doesn't need the interface pointer
238 * because doesn't do a check whether the interface supports error info or not.
239 */
240#define CHECK_ERROR_NI(method) \
241 do { \
242 CHECK_RC (method); \
243 if (FAILED (rc)) { \
244 com::ErrorInfo info; \
245 PRINT_ERROR_INFO (info); \
246 } \
247 } while (0)
248
249/**
250 * Does the same as CHECK_ERROR_NI(), but executes the |return rc| statement
251 * on failure.
252 */
253#define CHECK_ERROR_NI_RET(method) \
254 do { CHECK_ERROR_NI (method); if (FAILED (rc)) return rc; } while (0)
255
256/**
257 * Does the same as CHECK_ERROR_NI(), but executes the |break| statement
258 * on failure.
259 */
260#define CHECK_ERROR_NI_BREAK(method) \
261 if (1) { CHECK_ERROR_NI (method); if (FAILED (rc)) break; } else do {} while (0)
262
263
264/**
265 * Asserts the given expression is true. When the expression is false, prints
266 * a line containing the failied function/line/file; otherwise does nothing.
267 */
268#define ASSERT(expr) \
269 do { \
270 if (!(expr)) \
271 { \
272 RTPrintf ("[!] ASSERTION FAILED at line %d: %s\n", __LINE__, #expr); \
273 Log (("[!] ASSERTION FAILED at line %d: %s\n", __LINE__, #expr)); \
274 } \
275 } while (0)
276
277/**
278 * Does the same as ASSERT(), but executes the |return ret| statement if the
279 * expression to assert is false.
280 */
281#define ASSERT_RET(expr, ret) \
282 do { ASSERT (expr); if (!(expr)) return (ret); } while (0)
283
284/**
285 * Does the same as ASSERT(), but executes the |break| statement if the
286 * expression to assert is false.
287 */
288#define ASSERT_BREAK(expr) \
289 if (1) { ASSERT (expr); if (!(expr)) break; } else do {} while (0)
290
291
292#endif
293
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use