VirtualBox

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

Last change on this file since 2734 was 1, checked in by vboxsync, 55 years ago

import

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette