VirtualBox

root/trunk/include/VBox/com/assert.h

Revision 13856, 10.4 kB (checked in by vboxsync, 2 months ago)

Main: typos

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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 = %Rhrc (0x%08X)\n", rc, 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 = %Rhrc (0x%08X)\n", rc, 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 = %Rhrc (0x%08X)\n", rc, 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 = %Rhrc (0x%08X)\n", rc, 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 (!SUCCEEDED (rc)) { AssertComRC (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 throw
90  */
91 #define AssertComRCThrow(rc, eval)      \
92     if (!SUCCEEDED (rc)) { AssertComRC (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 (!SUCCEEDED (rc)) { AssertComRC (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 (!SUCCEEDED (rc)) { AssertComRC (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 (!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 (!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 (!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  = %Rhra\n", rc); \
150         Log (("[!] Primary RC  = %Rhra\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         RTPrintf ("[!] Full error info present: %RTbool, basic error info present: %RTbool\n", \
162                   info.isFullAvailable(), info.isBasicAvailable()); \
163         Log (("[!] Full error info present: %RTbool, basic error info present: %RTbool\n", \
164               info.isFullAvailable(), info.isBasicAvailable())); \
165         if (info.isFullAvailable() || info.isBasicAvailable()) { \
166             RTPrintf ("[!] Result Code = %Rhra\n", info.getResultCode()); \
167             RTPrintf ("[!] Text        = %ls\n", info.getText().raw()); \
168             RTPrintf ("[!] Component   = %ls, Interface: %ls, {%RTuuid}\n", \
169                       info.getComponent().raw(), info.getInterfaceName().raw(), \
170                       info.getInterfaceID().raw()); \
171             RTPrintf ("[!] Callee      = %ls, {%RTuuid}\n", \
172                       info.getCalleeName().raw(), info.getCalleeIID().raw()); \
173             Log (("[!] Result Code = %Rhra\n", info.getResultCode())); \
174             Log (("[!] Text        = %ls\n", info.getText().raw())); \
175             Log (("[!] Component   = %ls, Interface: %ls, {%RTuuid}\n", \
176                   info.getComponent().raw(), info.getInterfaceName().raw(), \
177                   info.getInterfaceID().raw())); \
178             Log (("[!] Callee      = %ls, {%RTuuid}\n", \
179                   info.getCalleeName().raw(), info.getCalleeIID().raw())); \
180         } \
181     } while (0)
182
183 /**
184  *  Calls the given interface method and then checks if the return value
185  *  (COM result code) indicates a failure. If so, prints the failed
186  *  function/line/file and the description of the result code.
187  *
188  *  Used by command line tools or for debugging and assumes the |HRESULT rc|
189  *  variable is accessible for assigning in the current scope.
190  */
191 #define CHECK_RC(method) \
192     do { \
193         rc = method; \
194         if (FAILED (rc)) { \
195             RTPrintf ("[!] FAILED calling " #method " at line %d!\n", __LINE__); \
196             Log (("[!] FAILED calling " #method " at line %d!\n", __LINE__)); \
197             PRINT_RC_MESSAGE(rc); \
198         } \
199     } while (0)
200
201 /**
202  *  Does the same as CHECK_RC(), but executes the |return rc| statement on
203  *  failure.
204  */
205 #define CHECK_RC_RET(method) \
206     do { CHECK_RC (method); if (FAILED (rc)) return rc; } while (0)
207
208 /**
209  *  Does the same as CHECK_RC(), but executes the |break| statement on
210  *  failure.
211  */
212 #define CHECK_RC_BREAK(method) \
213     if (1) { CHECK_RC (method); if (FAILED (rc)) break; } else do {} while (0)
214
215 /**
216  *  Calls the given method of the given interface and then checks if the return
217  *  value (COM result code) indicates a failure. If so, prints the failed
218  *  function/line/file, the description of the result code and attempts to
219  *  query the extended error information on the current thread (using
220  *  com::ErrorInfo) if the interface reports that it supports error information.
221  *
222  *  Used by command line tools or for debugging and assumes the |HRESULT rc|
223  *  variable is accessible for assigning in the current scope.
224  */
225 #define CHECK_ERROR(iface, method) \
226     do \
227     { \
228         CHECK_RC(iface->method); \
229         if (FAILED(rc)) { \
230             com::ErrorInfo info (iface); \
231             PRINT_ERROR_INFO (info); \
232         } \
233     } while (0)
234
235 /**
236  *  Does the same as CHECK_ERROR(), but executes the |return ret| statement on
237  *  failure.
238  */
239 #define CHECK_ERROR_RET(iface, method, ret) \
240     do { CHECK_ERROR (iface, method); if (FAILED (rc)) return (ret); } while (0)
241
242 /**
243  *  Does the same as CHECK_ERROR(), but executes the |break| statement on
244  *  failure.
245  */
246 #if defined(__GNUC__)
247  #define CHECK_ERROR_BREAK(iface, method) \
248     ({ CHECK_ERROR (iface, method); if (FAILED (rc)) break; })
249 #else
250  #define CHECK_ERROR_BREAK(iface, method) \
251     if (1) { CHECK_ERROR (iface, method); if (FAILED (rc)) break; } else do {} while (0)
252 #endif
253
254 #define CHECK_ERROR_NOCALL() \
255     do { \
256         com::ErrorInfo info; \
257         PRINT_ERROR_INFO (info); \
258     } while (0)
259
260 /**
261  *  Does the same as CHECK_ERROR(), but doesn't need the interface pointer
262  *  because doesn't do a check whether the interface supports error info or not.
263  */
264 #define CHECK_ERROR_NI(method) \
265     do { \
266         CHECK_RC (method); \
267         if (FAILED (rc)) { \
268             com::ErrorInfo info; \
269             PRINT_ERROR_INFO (info); \
270         } \
271     } while (0)
272
273 /**
274  *  Does the same as CHECK_ERROR_NI(), but executes the |return rc| statement
275  *  on failure.
276  */
277 #define CHECK_ERROR_NI_RET(method) \
278     do { CHECK_ERROR_NI (method); if (FAILED (rc)) return rc; } while (0)
279
280 /**
281  *  Does the same as CHECK_ERROR_NI(), but executes the |break| statement
282  *  on failure.
283  */
284 #define CHECK_ERROR_NI_BREAK(method) \
285     if (1) { CHECK_ERROR_NI (method); if (FAILED (rc)) break; } else do {} while (0)
286
287
288 /**
289  *  Asserts the given expression is true. When the expression is false, prints
290  *  a line containing the failed function/line/file; otherwise does nothing.
291  */
292 #define ASSERT(expr) \
293     do { \
294         if (!(expr)) \
295         { \
296             RTPrintf ("[!] ASSERTION FAILED at line %d: %s\n", __LINE__, #expr); \
297             Log (("[!] ASSERTION FAILED at line %d: %s\n", __LINE__, #expr)); \
298         } \
299     } while (0)
300
301 /**
302  *  Does the same as ASSERT(), but executes the |return ret| statement if the
303  *  expression to assert is false.
304  */
305 #define ASSERT_RET(expr, ret) \
306     do { ASSERT (expr); if (!(expr)) return (ret); } while (0)
307
308 /**
309  *  Does the same as ASSERT(), but executes the |break| statement if the
310  *  expression to assert is false.
311  */
312 #define ASSERT_BREAK(expr) \
313     if (1) { ASSERT (expr); if (!(expr)) break; } else do {} while (0)
314
315
316 #endif
317
Note: See TracBrowser for help on using the browser.

© 2008 Sun Microsystems, Inc.
ContactPrivacy policy