VirtualBox

source: vbox/trunk/include/VBox/com/ErrorInfo.h@ 30682

Last change on this file since 30682 was 30682, checked in by vboxsync, 15 years ago

Main: build fix (add missing ErrorInfo copy constructor)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.9 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * ErrorInfo class declaration
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
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
27#ifndef ___VBox_com_ErrorInfo_h
28#define ___VBox_com_ErrorInfo_h
29
30#include "VBox/com/ptr.h"
31#include "VBox/com/string.h"
32#include "VBox/com/Guid.h"
33#include "VBox/com/assert.h"
34
35struct IProgress;
36struct IVirtualBoxErrorInfo;
37
38namespace com
39{
40
41/**
42 * The ErrorInfo class provides a convenient way to retrieve error
43 * information set by the most recent interface method, that was invoked on
44 * the current thread and returned an unsuccessful result code.
45 *
46 * Once the instance of this class is created, the error information for
47 * the current thread is cleared.
48 *
49 * There is no sense to use instances of this class after the last
50 * invoked interface method returns a success.
51 *
52 * The class usage pattern is as follows:
53 * <code>
54 * IFoo *foo;
55 * ...
56 * HRESULT rc = foo->SomeMethod();
57 * if (FAILED (rc)) {
58 * ErrorInfo info (foo);
59 * if (info.isFullAvailable()) {
60 * printf ("error message = %ls\n", info.getText().raw());
61 * }
62 * }
63 * </code>
64 *
65 * This class fetches error information using the IErrorInfo interface on
66 * Win32 (MS COM) or the nsIException interface on other platforms (XPCOM),
67 * or the extended IVirtualBoxErrorInfo interface when when it is available
68 * (i.e. a given IErrorInfo or nsIException instance implements it).
69 * Currently, IVirtualBoxErrorInfo is only available for VirtualBox components.
70 *
71 * ErrorInfo::isFullAvailable() and ErrorInfo::isBasicAvailable() determine
72 * what level of error information is available. If #isBasicAvailable()
73 * returns true, it means that only IErrorInfo or nsIException is available as
74 * the source of information (depending on the platform), but not
75 * IVirtualBoxErrorInfo. If #isFullAvailable() returns true, it means that all
76 * three interfaces are available. If both methods return false, no error info
77 * is available at all.
78 *
79 * Here is a table of correspondence between this class methods and
80 * and IErrorInfo/nsIException/IVirtualBoxErrorInfo attributes/methods:
81 *
82 * ErrorInfo IErrorInfo nsIException IVirtualBoxErrorInfo
83 * --------------------------------------------------------------------
84 * getResultCode -- result resultCode
85 * getIID GetGUID -- interfaceID
86 * getComponent GetSource -- component
87 * getText GetDescription message text
88 *
89 * '--' means that this interface does not provide the corresponding portion
90 * of information, therefore it is useless to query it if only
91 * #isBasicAvailable() returns true. As it can be seen, the amount of
92 * information provided at the basic level, depends on the platform
93 * (MS COM or XPCOM).
94 */
95class ErrorInfo
96{
97public:
98
99 /**
100 * Constructs a new, "interfaceless" ErrorInfo instance that takes
101 * the error information possibly set on the current thread by an
102 * interface method of some COM component or by the COM subsystem.
103 *
104 * This constructor is useful, for example, after an unsuccessful attempt
105 * to instantiate (create) a component, so there is no any valid interface
106 * pointer available.
107 */
108 explicit ErrorInfo()
109 : mIsBasicAvailable(false),
110 mIsFullAvailable(false),
111 mResultCode(S_OK),
112 m_pNext(NULL)
113 {
114 init();
115 }
116
117 ErrorInfo(IUnknown *pObj, const GUID &aIID)
118 : mIsBasicAvailable(false),
119 mIsFullAvailable(false),
120 mResultCode(S_OK),
121 m_pNext(NULL)
122 {
123 init(pObj, aIID);
124 }
125
126 ErrorInfo(const ErrorInfo &x);
127
128 virtual ~ErrorInfo();
129
130 /**
131 * Returns whether basic error info is actually available for the current
132 * thread. If the instance was created from an interface pointer that
133 * supports basic error info and successfully provided it, or if it is an
134 * "interfaceless" instance and there is some error info for the current
135 * thread, the returned value will be true.
136 *
137 * See the class description for details about the basic error info level.
138 *
139 * The appropriate methods of this class provide meaningful info only when
140 * this method returns true (otherwise they simply return NULL-like values).
141 */
142 bool isBasicAvailable() const
143 {
144 return mIsBasicAvailable;
145 }
146
147 /**
148 * Returns whether full error info is actually available for the current
149 * thread. If the instance was created from an interface pointer that
150 * supports full error info and successfully provided it, or if it is an
151 * "interfaceless" instance and there is some error info for the current
152 * thread, the returned value will be true.
153 *
154 * See the class description for details about the full error info level.
155 *
156 * The appropriate methods of this class provide meaningful info only when
157 * this method returns true (otherwise they simply return NULL-like values).
158 */
159 bool isFullAvailable() const
160 {
161 return mIsFullAvailable;
162 }
163
164 /**
165 * Returns the COM result code of the failed operation.
166 */
167 HRESULT getResultCode() const
168 {
169 return mResultCode;
170 }
171
172 /**
173 * Returns the IID of the interface that defined the error.
174 */
175 const Guid& getInterfaceID() const
176 {
177 return mInterfaceID;
178 }
179
180 /**
181 * Returns the name of the component that generated the error.
182 */
183 const Bstr& getComponent() const
184 {
185 return mComponent;
186 }
187
188 /**
189 * Returns the textual description of the error.
190 */
191 const Bstr& getText() const
192 {
193 return mText;
194 }
195
196 /**
197 * Returns the next error information object or @c NULL if there is none.
198 */
199 const ErrorInfo* getNext() const
200 {
201 return m_pNext;
202 }
203
204 /**
205 * Returns the name of the interface that defined the error
206 */
207 const Bstr& getInterfaceName() const
208 {
209 return mInterfaceName;
210 }
211
212 /**
213 * Returns the IID of the interface that returned the error.
214 *
215 * This method returns a non-null IID only if the instance was created
216 * using #template <class I> ErrorInfo (I *i) or
217 * template <class I> ErrorInfo (const ComPtr <I> &i) constructor.
218 */
219 const Guid& getCalleeIID() const
220 {
221 return mCalleeIID;
222 }
223
224 /**
225 * Returns the name of the interface that returned the error
226 *
227 * This method returns a non-null name only if the instance was created
228 * using #template <class I> ErrorInfo (I *i) or
229 * template <class I> ErrorInfo (const ComPtr <I> &i) constructor.
230 */
231 const Bstr& getCalleeName() const
232 {
233 return mCalleeName;
234 }
235
236 /**
237 * Resets all collected error information. #isNull() will
238 * return @c true after this method is called.
239 */
240 void setNull()
241 {
242 mIsBasicAvailable = false;
243 mIsFullAvailable = false;
244
245 if (m_pNext)
246 {
247 delete m_pNext;
248 m_pNext = NULL;
249 }
250
251 mResultCode = S_OK;
252 mInterfaceID.clear();
253 mComponent.setNull();
254 mText.setNull();
255 mInterfaceName.setNull();
256 mCalleeIID.clear();
257 mCalleeName.setNull();
258 mErrorInfo.setNull();
259 }
260
261protected:
262
263 ErrorInfo(bool /* aDummy */)
264 : mIsBasicAvailable(false),
265 mIsFullAvailable(false),
266 mResultCode(S_OK),
267 m_pNext(NULL)
268 { }
269
270 void init(bool aKeepObj = false);
271 void init(IUnknown *aUnk, const GUID &aIID, bool aKeepObj = false);
272 void init(IVirtualBoxErrorInfo *aInfo);
273
274 bool mIsBasicAvailable : 1;
275 bool mIsFullAvailable : 1;
276
277 HRESULT mResultCode;
278 Guid mInterfaceID;
279 Bstr mComponent;
280 Bstr mText;
281
282 ErrorInfo *m_pNext;
283
284 Bstr mInterfaceName;
285 Guid mCalleeIID;
286 Bstr mCalleeName;
287
288 ComPtr<IUnknown> mErrorInfo;
289};
290
291/**
292 * A convenience subclass of ErrorInfo that, given an IProgress interface
293 * pointer, reads its errorInfo attribute and uses the returned
294 * IVirtualBoxErrorInfo instance to construct itself.
295 */
296class ProgressErrorInfo : public ErrorInfo
297{
298public:
299
300 /**
301 * Constructs a new instance by fetching error information from the
302 * IProgress interface pointer. If the progress object is not NULL,
303 * its completed attribute is true, resultCode represents a failure,
304 * and the errorInfo attribute returns a valid IVirtualBoxErrorInfo pointer,
305 * both #isFullAvailable() and #isBasicAvailable() will return true.
306 *
307 * @param progress the progress object representing a failed operation
308 */
309 ProgressErrorInfo(IProgress *progress);
310};
311
312/**
313 * A convenience subclass of ErrorInfo that allows to preserve the current
314 * error info. Instances of this class fetch an error info object set on the
315 * current thread and keep a reference to it, which allows to restore it
316 * later using the #restore() method. This is useful to preserve error
317 * information returned by some method for the duration of making another COM
318 * call that may set its own error info and overwrite the existing
319 * one. Preserving and restoring error information makes sense when some
320 * method wants to return error information set by other call as its own
321 * error information while it still needs to make another call before return.
322 *
323 * Instead of calling #restore() explicitly you may let the object destructor
324 * do it for you, if you correctly limit the object's lifetime.
325 *
326 * The usage pattern is:
327 * <code>
328 * rc = foo->method();
329 * if (FAILED (rc))
330 * {
331 * ErrorInfoKeeper eik;
332 * ...
333 * // bar may return error info as well
334 * bar->method();
335 * ...
336 * // no need to call #restore() explicitly here because the eik's
337 * // destructor will restore error info fetched after the failed
338 * // call to foo before returning to the caller
339 * return rc;
340 * }
341 * </code>
342 */
343class ErrorInfoKeeper : public ErrorInfo
344{
345public:
346
347 /**
348 * Constructs a new instance that will fetch the current error info if
349 * @a aIsNull is @c false (by default) or remain uninitialized (null)
350 * otherwise.
351 *
352 * @param aIsNull @c true to prevent fetching error info and leave
353 * the instance uninitialized.
354 */
355 ErrorInfoKeeper (bool aIsNull = false)
356 : ErrorInfo (false), mForgot (aIsNull)
357 {
358 if (!aIsNull)
359 init (true /* aKeepObj */);
360 }
361
362 /**
363 * Destroys this instance and automatically calls #restore() which will
364 * either restore error info fetched by the constructor or do nothing
365 * if #forget() was called before destruction.
366 */
367 ~ErrorInfoKeeper() { if (!mForgot) restore(); }
368
369 /**
370 * Tries to (re-)fetch error info set on the current thread. On success,
371 * the previous error information, if any, will be overwritten with the
372 * new error information. On failure, or if there is no error information
373 * available, this instance will be reset to null.
374 */
375 void fetch()
376 {
377 setNull();
378 mForgot = false;
379 init(true /* aKeepObj */);
380 }
381
382 /**
383 * Restores error info fetched by the constructor and forgets it
384 * afterwards. Does nothing if the error info was forgotten by #forget().
385 *
386 * @return COM result of the restore operation.
387 */
388 HRESULT restore();
389
390 /**
391 * Forgets error info fetched by the constructor to prevent it from
392 * being restored by #restore() or by the destructor.
393 */
394 void forget() { mForgot = true; }
395
396 /**
397 * Forgets error info fetched by the constructor to prevent it from
398 * being restored by #restore() or by the destructor, and returns the
399 * stored error info object to the caller.
400 */
401 ComPtr <IUnknown> takeError() { mForgot = true; return mErrorInfo; }
402
403private:
404
405 bool mForgot : 1;
406};
407
408} /* namespace com */
409
410#endif
411
Note: See TracBrowser for help on using the repository browser.

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