VirtualBox

source: vbox/trunk/include/VBox/com/errorprint.h@ 35517

Last change on this file since 35517 was 33766, checked in by vboxsync, 14 years ago

VBoxManage: Implemented 'list extpacks', 'extpack install' and 'extpack uninstall'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * Error printing macros using shared functions defined in shared glue code.
4 * Use these CHECK_* macros for efficient error checking around calling COM methods.
5 */
6
7/*
8 * Copyright (C) 2009 Oracle Corporation
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 (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * The contents of this file may alternatively be used under the terms
19 * of the Common Development and Distribution License Version 1.0
20 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
21 * VirtualBox OSE distribution, in which case the provisions of the
22 * CDDL are applicable instead of those of the GPL.
23 *
24 * You may elect to license modified versions of this file under the
25 * terms and conditions of either the GPL or the CDDL or both.
26 */
27
28#ifndef ___VBox_com_errorprint_h
29#define ___VBox_com_errorprint_h
30
31namespace com
32{
33
34// shared prototypes; these are defined in shared glue code and are
35// compiled only once for all front-ends
36void GluePrintErrorInfo(com::ErrorInfo &info);
37void GluePrintErrorContext(const char *pcszContext, const char *pcszSourceFile, uint32_t ulLine);
38void GluePrintRCMessage(HRESULT rc);
39void GlueHandleComError(ComPtr<IUnknown> iface, const char *pcszContext, HRESULT rc, const char *pcszSourceFile, uint32_t ulLine);
40
41/**
42 * Calls the given method of the given interface and then checks if the return
43 * value (COM result code) indicates a failure. If so, prints the failed
44 * function/line/file, the description of the result code and attempts to
45 * query the extended error information on the current thread (using
46 * com::ErrorInfo) if the interface reports that it supports error information.
47 *
48 * Used by command line tools or for debugging and assumes the |HRESULT rc|
49 * variable is accessible for assigning in the current scope.
50 */
51#define CHECK_ERROR(iface, method) \
52 do { \
53 rc = iface->method; \
54 if (FAILED(rc)) \
55 com::GlueHandleComError(iface, #method, rc, __FILE__, __LINE__); \
56 } while (0)
57
58/**
59 * Same as CHECK_ERROR except that it also executes the statement |stmt| on
60 * failure.
61 */
62#define CHECK_ERROR_STMT(iface, method, stmt) \
63 do { \
64 rc = iface->method; \
65 if (FAILED(rc)) \
66 { \
67 com::GlueHandleComError(iface, #method, rc, __FILE__, __LINE__); \
68 stmt; \
69 } \
70 } while (0)
71
72/**
73 * Same as CHECK_ERROR_STMT except that it uses an internal variable |hrcCheck|
74 * for holding the result.
75 */
76#define CHECK_ERROR2_STMT(iface, method, stmt) \
77 do { \
78 HRESULT hrcCheck = iface->method; \
79 if (FAILED(hrcCheck)) \
80 { \
81 com::GlueHandleComError(iface, #method, hrcCheck, __FILE__, __LINE__); \
82 stmt; \
83 } \
84 } while (0)
85
86
87/**
88 * Does the same as CHECK_ERROR(), but executes the |break| statement on
89 * failure.
90 */
91#ifdef __GNUC__
92# define CHECK_ERROR_BREAK(iface, method) \
93 __extension__ \
94 ({ \
95 rc = iface->method; \
96 if (FAILED(rc)) \
97 { \
98 com::GlueHandleComError(iface, #method, rc, __FILE__, __LINE__); \
99 break; \
100 } \
101 })
102#else
103# define CHECK_ERROR_BREAK(iface, method) \
104 if (1) \
105 { \
106 rc = iface->method; \
107 if (FAILED(rc)) \
108 { \
109 com::GlueHandleComError(iface, #method, rc, __FILE__, __LINE__); \
110 break; \
111 } \
112 } \
113 else do {} while (0)
114#endif
115
116/**
117 * Does the same as CHECK_ERROR(), but executes the |return ret| statement on
118 * failure.
119 */
120#define CHECK_ERROR_RET(iface, method, ret) \
121 do { \
122 rc = iface->method; \
123 if (FAILED(rc)) \
124 { \
125 com::GlueHandleComError(iface, #method, rc, __FILE__, __LINE__); \
126 return (ret); \
127 } \
128 } while (0)
129
130/**
131 * Does the same as CHECK_ERROR(), but returns @a ret on failure.
132 *
133 * Unlike CHECK_ERROR and CHECK_ERROR_RET, this macro does not presuppose a
134 * |rc| variable but instead employs a local variable |hrcCheck| in its own
135 * scope. This |hrcCheck| variable can be referenced by the @a rcRet
136 * parameter.
137 *
138 * @param iface The interface pointer (can be a smart pointer object).
139 * @param method The method to invoke together with the parameters.
140 * @param rcRet What to return on failure. Use |hrcCheck| to return
141 * the status code of the method call.
142 */
143#define CHECK_ERROR2_RET(iface, method, rcRet) \
144 do { \
145 HRESULT hrcCheck = iface->method; \
146 if (FAILED(hrcCheck)) \
147 { \
148 com::GlueHandleComError(iface, #method, hrcCheck, __FILE__, __LINE__); \
149 return (rcRet); \
150 } \
151 } while (0)
152
153/**
154 * Asserts the given expression is true. When the expression is false, prints
155 * a line containing the failed function/line/file; otherwise does nothing.
156 */
157#define ASSERT(expr) \
158 do { \
159 if (!(expr)) \
160 { \
161 RTPrintf ("[!] ASSERTION FAILED at line %d: %s\n", __LINE__, #expr); \
162 Log (("[!] ASSERTION FAILED at line %d: %s\n", __LINE__, #expr)); \
163 } \
164 } while (0)
165
166#endif
167
168/**
169 * Does the same as ASSERT(), but executes the |return ret| statement if the
170 * expression to assert is false;
171 */
172#define ASSERT_RET(expr, ret) \
173 do { ASSERT(expr); if (!(expr)) return (ret); } while (0)
174
175/**
176 * Does the same as ASSERT(), but executes the |break| statement if the
177 * expression to assert is false;
178 */
179#define ASSERT_BREAK(expr, ret) \
180 if (1) { ASSERT(expr); if (!(expr)) break; } else do {} while (0)
181
182} /* namespace com */
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