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