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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef ___VBox_com_assert_h
|
---|
23 | #define ___VBox_com_assert_h
|
---|
24 |
|
---|
25 | #include <iprt/assert.h>
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Asserts that the COM result code is succeeded in strict builds.
|
---|
29 | * In non-strict builds the result code will be NOREF'ed to kill compiler warnings.
|
---|
30 | *
|
---|
31 | * @param rc COM result code
|
---|
32 | */
|
---|
33 | #define AssertComRC(rc) \
|
---|
34 | do { AssertMsg (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc)); NOREF (rc); } while (0)
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * A special version of AssertComRC that returns the given expression
|
---|
38 | * if the result code is failed.
|
---|
39 | *
|
---|
40 | * @param rc COM result code
|
---|
41 | * @param ret the expression to return
|
---|
42 | */
|
---|
43 | #define AssertComRCReturn(rc, ret) \
|
---|
44 | AssertMsgReturn (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc), ret)
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * A special version of AssertComRC that returns the given result code
|
---|
48 | * if it is failed.
|
---|
49 | *
|
---|
50 | * @param rc COM result code
|
---|
51 | * @param ret the expression to return
|
---|
52 | */
|
---|
53 | #define AssertComRCReturnRC(rc) \
|
---|
54 | AssertMsgReturn (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc), rc)
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * A special version of AssertComRC that returns if the result code is failed.
|
---|
58 | *
|
---|
59 | * @param rc COM result code
|
---|
60 | * @param ret the expression to return
|
---|
61 | */
|
---|
62 | #define AssertComRCReturnVoid(rc) \
|
---|
63 | AssertMsgReturnVoid (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc))
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * A special version of AssertComRC that evaluates the given expression and
|
---|
67 | * breaks if the result code is failed.
|
---|
68 | *
|
---|
69 | * @param rc COM result code
|
---|
70 | * @param eval the expression to evaluate
|
---|
71 | */
|
---|
72 | #define AssertComRCBreak(rc, eval) \
|
---|
73 | if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { eval; break; } } else do {} while (0)
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * A special version of AssertComRC that just breaks if the result code is
|
---|
77 | * failed.
|
---|
78 | *
|
---|
79 | * @param rc COM result code
|
---|
80 | */
|
---|
81 | #define AssertComRCBreakRC(rc) \
|
---|
82 | if (1) { AssertComRC (rc); if (!SUCCEEDED (rc)) { break; } } else do {} while (0)
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Checks whether the given COM result code is successful.
|
---|
86 | * If not, executes the return statement with this result code.
|
---|
87 | *
|
---|
88 | * @param rc COM result code
|
---|
89 | */
|
---|
90 | #define CheckComRCReturnRC(rc) \
|
---|
91 | if (1) { if (!SUCCEEDED (rc)) return (rc); } else do {} while (0)
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Checks whether the given COM result code is successful.
|
---|
95 | * If not, executes the break statement.
|
---|
96 | *
|
---|
97 | * @param rc COM result code
|
---|
98 | */
|
---|
99 | #define CheckComRCBreakRC(rc) \
|
---|
100 | if (1) { if (!SUCCEEDED (rc)) { break; } } else do {} while (0)
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * A section of helpful macros for error output
|
---|
104 | */
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Prints a line describing the given COM result code.
|
---|
108 | * Used by command line tools or for debugging.
|
---|
109 | */
|
---|
110 | #define PRINT_RC_MESSAGE(rc) \
|
---|
111 | RTPrintf ("[!] Primary RC = %Rwa\n", rc)
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Prints the extended error information.
|
---|
115 | * Used by command line tools or for debugging.
|
---|
116 | *
|
---|
117 | * @param info com::ErrorInfo instance
|
---|
118 | */
|
---|
119 | #define PRINT_ERROR_INFO(info) \
|
---|
120 | do { \
|
---|
121 | info.print ("[!] "); \
|
---|
122 | } while (0)
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Calls the given interface method and then checks if the return value
|
---|
126 | * (COM result code) indicates a failure. If so, prints the failed
|
---|
127 | * function/line/file and the description of the result code.
|
---|
128 | *
|
---|
129 | * Used by command line tools or for debugging and assumes the |HRESULT rc|
|
---|
130 | * variable is accessible for assigning in the current scope.
|
---|
131 | */
|
---|
132 | #define CHECK_RC(method) \
|
---|
133 | do { \
|
---|
134 | rc = method; \
|
---|
135 | if (FAILED (rc)) { \
|
---|
136 | RTPrintf("[!] FAILED calling " #method " at line %d!\n", __LINE__); \
|
---|
137 | PRINT_RC_MESSAGE(rc); \
|
---|
138 | } \
|
---|
139 | } while (0)
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Does the same as CHECK_RC(), but executes the |return rc| statement on
|
---|
143 | * failure.
|
---|
144 | */
|
---|
145 | #define CHECK_RC_RET(method) \
|
---|
146 | do { CHECK_RC (method); if (FAILED (rc)) return rc; } while (0)
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Does the same as CHECK_RC(), but executes the |break| statement on
|
---|
150 | * failure.
|
---|
151 | */
|
---|
152 | #define CHECK_RC_BREAK(method) \
|
---|
153 | if (1) { CHECK_RC (method); if (FAILED (rc)) break; } else do {} while (0)
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Calls the given method of the given interface and then checks if the return
|
---|
157 | * value (COM result code) indicates a failure. If so, prints the failed
|
---|
158 | * function/line/file, the description of the result code and attempts to
|
---|
159 | * query the extended error information on the current thread (using
|
---|
160 | * com::ErrorInfo) if the interface reports that it supports error information.
|
---|
161 | *
|
---|
162 | * Used by command line tools or for debugging and assumes the |HRESULT rc|
|
---|
163 | * variable is accessible for assigning in the current scope.
|
---|
164 | */
|
---|
165 | #define CHECK_ERROR(iface, method) \
|
---|
166 | do \
|
---|
167 | { \
|
---|
168 | CHECK_RC(iface->method); \
|
---|
169 | if (FAILED(rc)) { \
|
---|
170 | com::ErrorInfo info (iface); \
|
---|
171 | info.print ("[!] "); \
|
---|
172 | } \
|
---|
173 | } while (0)
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Does the same as CHECK_ERROR(), but executes the |return ret| statement on
|
---|
177 | * failure.
|
---|
178 | */
|
---|
179 | #define CHECK_ERROR_RET(iface, method, ret) \
|
---|
180 | do { CHECK_ERROR (iface, method); if (FAILED (rc)) return (ret); } while (0)
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Does the same as CHECK_ERROR(), but executes the |break| statement on
|
---|
184 | * failure.
|
---|
185 | */
|
---|
186 | #define CHECK_ERROR_BREAK(iface, method) \
|
---|
187 | if (1) { CHECK_ERROR (iface, method); if (FAILED (rc)) break; } else do {} while (0)
|
---|
188 |
|
---|
189 | #define CHECK_ERROR_NOCALL() \
|
---|
190 | do { \
|
---|
191 | com::ErrorInfo info; \
|
---|
192 | PRINT_ERROR_INFO (info); \
|
---|
193 | } while (0)
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Does the same as CHECK_ERROR(), but doesn't need the interface pointer
|
---|
197 | * because doesn't do a check whether the interface supports error info or not.
|
---|
198 | */
|
---|
199 | #define CHECK_ERROR_NI(method) \
|
---|
200 | do { \
|
---|
201 | CHECK_RC (method); \
|
---|
202 | if (FAILED (rc)) { \
|
---|
203 | com::ErrorInfo info; \
|
---|
204 | PRINT_ERROR_INFO (info); \
|
---|
205 | } \
|
---|
206 | } while (0)
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Does the same as CHECK_ERROR_NI(), but executes the |return rc| statement
|
---|
210 | * on failure.
|
---|
211 | */
|
---|
212 | #define CHECK_ERROR_NI_RET(method) \
|
---|
213 | do { CHECK_ERROR_NI (method); if (FAILED (rc)) return rc; } while (0)
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Does the same as CHECK_ERROR_NI(), but executes the |break| statement
|
---|
217 | * on failure.
|
---|
218 | */
|
---|
219 | #define CHECK_ERROR_NI_BREAK(method) \
|
---|
220 | if (1) { CHECK_ERROR_NI (method); if (FAILED (rc)) break; } else do {} while (0)
|
---|
221 |
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Asserts the given expression is true. When the expression is false, prints
|
---|
225 | * a line containing the failied function/line/file; otherwise does nothing.
|
---|
226 | */
|
---|
227 | #define ASSERT(expr) \
|
---|
228 | do { \
|
---|
229 | if (!(expr)) \
|
---|
230 | RTPrintf("[!] ASSERTION FAILED at line %d: %s\n", __LINE__, #expr); \
|
---|
231 | } while (0)
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Does the same as ASSERT(), but executes the |return ret| statement if the
|
---|
235 | * expression to assert is false.
|
---|
236 | */
|
---|
237 | #define ASSERT_RET(expr, ret) \
|
---|
238 | do { ASSERT (expr); if (!(expr)) return (ret); } while (0)
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Does the same as ASSERT(), but executes the |break| statement if the
|
---|
242 | * expression to assert is false.
|
---|
243 | */
|
---|
244 | #define ASSERT_BREAK(expr) \
|
---|
245 | if (1) { ASSERT (expr); if (!(expr)) break; } else do {} while (0)
|
---|
246 |
|
---|
247 |
|
---|
248 | #endif
|
---|
249 |
|
---|