VirtualBox

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

Last change on this file since 55111 was 49675, checked in by vboxsync, 11 years ago

include/VBox/com/ErrorInfo.h: avoid warnings with some compilers (when using very picky warning options), COM/XPCOM differ in the way they declare interface "classes"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.9 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer - ErrorInfo class declaration.
3 */
4
5/*
6 * Copyright (C) 2006-2013 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_com_ErrorInfo_h
27#define ___VBox_com_ErrorInfo_h
28
29#include "VBox/com/ptr.h"
30#include "VBox/com/string.h"
31#include "VBox/com/Guid.h"
32#include "VBox/com/assert.h"
33
34COM_STRUCT_OR_CLASS(IProgress);
35COM_STRUCT_OR_CLASS(IVirtualBoxErrorInfo);
36
37namespace com
38{
39
40/**
41 * General discussion:
42 *
43 * In COM all errors are stored on a per thread basis. In general this means
44 * only _one_ active error is possible per thread. A new error will overwrite
45 * the previous one. To prevent this use MultiResult or ErrorInfoKeeper (see
46 * below). The implementations in MSCOM/XPCOM differ slightly, but the details
47 * are handled by this glue code.
48 *
49 * We have different classes which are involved in the error management. I try
50 * to describe them separately to make clear what they are there for.
51 *
52 * ErrorInfo:
53 *
54 * This class is able to retrieve the per thread error and store it into its
55 * member variables. This class can also handle non-VirtualBox errors (like
56 * standard COM errors).
57 *
58 * ProgressErrorInfo:
59 *
60 * This is just a simple wrapper class to get the ErrorInfo stored within an
61 * IProgress object. That is the error which was stored when the progress
62 * object was in use and not an error produced by IProgress itself.
63 *
64 * IVirtualBoxErrorInfo:
65 *
66 * The VirtualBox interface class for accessing error information from Main
67 * clients. This class is also used for storing the error information in the
68 * thread context.
69 *
70 * ErrorInfoKeeper:
71 *
72 * A helper class which stores the current per thread info internally. After
73 * calling methods which may produce other errors it is possible to restore
74 * the previous error and therefore restore the situation before calling the
75 * other methods.
76 *
77 * MultiResult:
78 *
79 * Creating an instance of MultiResult turns error chain saving on. All errors
80 * which follow will be saved in a chain for later access.
81 *
82 * COMErrorInfo (Qt/Gui only):
83 *
84 * The Qt GUI does some additional work for saving errors. Because we create
85 * wrappers for _every_ COM call, it is possible to automatically save the
86 * error info after the execution. This allow some additional info like saving
87 * the callee. Please note that this error info is saved on the client side
88 * and therefore locally to the object instance. See COMBaseWithEI,
89 * COMErrorInfo and the generated COMWrappers.cpp in the GUI.
90 *
91 * Errors itself are set in VirtualBoxBase::setErrorInternal. First a
92 * IVirtualBoxErrorInfo object is created and the given error is saved within.
93 * If MultiResult is active the current per thread error is fetched and
94 * attached to the new created IVirtualBoxErrorInfo object. Next this object is
95 * set as the new per thread error.
96 *
97 * Some general hints:
98 *
99 * - Always use setError, especially when you are working in an asynchronous thread
100 * to indicate an error. Otherwise the error information itself will not make
101 * it into the client.
102 *
103 */
104
105/**
106 * The ErrorInfo class provides a convenient way to retrieve error
107 * information set by the most recent interface method, that was invoked on
108 * the current thread and returned an unsuccessful result code.
109 *
110 * Once the instance of this class is created, the error information for
111 * the current thread is cleared.
112 *
113 * There is no sense to use instances of this class after the last
114 * invoked interface method returns a success.
115 *
116 * The class usage pattern is as follows:
117 * <code>
118 * IFoo *foo;
119 * ...
120 * HRESULT rc = foo->SomeMethod();
121 * if (FAILED(rc)) {
122 * ErrorInfo info(foo);
123 * if (info.isFullAvailable()) {
124 * printf("error message = %ls\n", info.getText().raw());
125 * }
126 * }
127 * </code>
128 *
129 * This class fetches error information using the IErrorInfo interface on
130 * Win32 (MS COM) or the nsIException interface on other platforms (XPCOM),
131 * or the extended IVirtualBoxErrorInfo interface when when it is available
132 * (i.e. a given IErrorInfo or nsIException instance implements it).
133 * Currently, IVirtualBoxErrorInfo is only available for VirtualBox components.
134 *
135 * ErrorInfo::isFullAvailable() and ErrorInfo::isBasicAvailable() determine
136 * what level of error information is available. If #isBasicAvailable()
137 * returns true, it means that only IErrorInfo or nsIException is available as
138 * the source of information (depending on the platform), but not
139 * IVirtualBoxErrorInfo. If #isFullAvailable() returns true, it means that all
140 * three interfaces are available. If both methods return false, no error info
141 * is available at all.
142 *
143 * Here is a table of correspondence between this class methods and
144 * and IErrorInfo/nsIException/IVirtualBoxErrorInfo attributes/methods:
145 *
146 * ErrorInfo IErrorInfo nsIException IVirtualBoxErrorInfo
147 * --------------------------------------------------------------------
148 * getResultCode -- result resultCode
149 * getIID GetGUID -- interfaceID
150 * getComponent GetSource -- component
151 * getText GetDescription message text
152 *
153 * '--' means that this interface does not provide the corresponding portion
154 * of information, therefore it is useless to query it if only
155 * #isBasicAvailable() returns true. As it can be seen, the amount of
156 * information provided at the basic level, depends on the platform
157 * (MS COM or XPCOM).
158 */
159class ErrorInfo
160{
161public:
162
163 /**
164 * Constructs a new, "interfaceless" ErrorInfo instance that takes
165 * the error information possibly set on the current thread by an
166 * interface method of some COM component or by the COM subsystem.
167 *
168 * This constructor is useful, for example, after an unsuccessful attempt
169 * to instantiate (create) a component, so there is no any valid interface
170 * pointer available.
171 */
172 explicit ErrorInfo()
173 : mIsBasicAvailable(false),
174 mIsFullAvailable(false),
175 mResultCode(S_OK),
176 mResultDetail(0),
177 m_pNext(NULL)
178 {
179 init();
180 }
181
182 ErrorInfo(IUnknown *pObj, const GUID &aIID)
183 : mIsBasicAvailable(false),
184 mIsFullAvailable(false),
185 mResultCode(S_OK),
186 mResultDetail(0),
187 m_pNext(NULL)
188 {
189 init(pObj, aIID);
190 }
191
192 /** Specialization for the IVirtualBoxErrorInfo smart pointer */
193 ErrorInfo (const ComPtr <IVirtualBoxErrorInfo> &aPtr)
194 : mIsBasicAvailable (false), mIsFullAvailable (false)
195 , mResultCode (S_OK), mResultDetail(0)
196 { init (aPtr); }
197
198 /**
199 * Constructs a new ErrorInfo instance from the IVirtualBoxErrorInfo
200 * interface pointer. If this pointer is not NULL, both #isFullAvailable()
201 * and #isBasicAvailable() will return |true|.
202 *
203 * @param aInfo pointer to the IVirtualBoxErrorInfo interface that
204 * holds error info to be fetched by this instance
205 */
206 ErrorInfo (IVirtualBoxErrorInfo *aInfo)
207 : mIsBasicAvailable (false), mIsFullAvailable (false)
208 , mResultCode (S_OK), mResultDetail(0)
209 { init (aInfo); }
210
211 ErrorInfo(const ErrorInfo &x)
212 {
213 copyFrom(x);
214 }
215
216 virtual ~ErrorInfo()
217 {
218 cleanup();
219 }
220
221 ErrorInfo& operator=(const ErrorInfo& x)
222 {
223 cleanup();
224 copyFrom(x);
225 return *this;
226 }
227
228 /**
229 * Returns whether basic error info is actually available for the current
230 * thread. If the instance was created from an interface pointer that
231 * supports basic error info and successfully provided it, or if it is an
232 * "interfaceless" instance and there is some error info for the current
233 * thread, the returned value will be true.
234 *
235 * See the class description for details about the basic error info level.
236 *
237 * The appropriate methods of this class provide meaningful info only when
238 * this method returns true (otherwise they simply return NULL-like values).
239 */
240 bool isBasicAvailable() const
241 {
242 return mIsBasicAvailable;
243 }
244
245 /**
246 * Returns whether full error info is actually available for the current
247 * thread. If the instance was created from an interface pointer that
248 * supports full error info and successfully provided it, or if it is an
249 * "interfaceless" instance and there is some error info for the current
250 * thread, the returned value will be true.
251 *
252 * See the class description for details about the full error info level.
253 *
254 * The appropriate methods of this class provide meaningful info only when
255 * this method returns true (otherwise they simply return NULL-like values).
256 */
257 bool isFullAvailable() const
258 {
259 return mIsFullAvailable;
260 }
261
262 /**
263 * Returns the COM result code of the failed operation.
264 */
265 HRESULT getResultCode() const
266 {
267 return mResultCode;
268 }
269
270 /**
271 * Returns the (optional) result detail code of the failed operation.
272 */
273 LONG getResultDetail() const
274 {
275 return mResultDetail;
276 }
277
278 /**
279 * Returns the IID of the interface that defined the error.
280 */
281 const Guid& getInterfaceID() const
282 {
283 return mInterfaceID;
284 }
285
286 /**
287 * Returns the name of the component that generated the error.
288 */
289 const Bstr& getComponent() const
290 {
291 return mComponent;
292 }
293
294 /**
295 * Returns the textual description of the error.
296 */
297 const Bstr& getText() const
298 {
299 return mText;
300 }
301
302 /**
303 * Returns the next error information object or @c NULL if there is none.
304 */
305 const ErrorInfo* getNext() const
306 {
307 return m_pNext;
308 }
309
310 /**
311 * Returns the name of the interface that defined the error
312 */
313 const Bstr& getInterfaceName() const
314 {
315 return mInterfaceName;
316 }
317
318 /**
319 * Returns the IID of the interface that returned the error.
320 *
321 * This method returns a non-null IID only if the instance was created
322 * using #template <class I> ErrorInfo(I *i) or
323 * template <class I> ErrorInfo(const ComPtr<I> &i) constructor.
324 */
325 const Guid& getCalleeIID() const
326 {
327 return mCalleeIID;
328 }
329
330 /**
331 * Returns the name of the interface that returned the error
332 *
333 * This method returns a non-null name only if the instance was created
334 * using #template <class I> ErrorInfo(I *i) or
335 * template <class I> ErrorInfo(const ComPtr<I> &i) constructor.
336 */
337 const Bstr& getCalleeName() const
338 {
339 return mCalleeName;
340 }
341
342 HRESULT getVirtualBoxErrorInfo(ComPtr<IVirtualBoxErrorInfo> &pVirtualBoxErrorInfo);
343
344 /**
345 * Resets all collected error information. #isBasicAvailable() and
346 * #isFullAvailable will return @c true after this method is called.
347 */
348 void setNull()
349 {
350 cleanup();
351 }
352
353protected:
354
355 ErrorInfo(bool /* aDummy */)
356 : mIsBasicAvailable(false),
357 mIsFullAvailable(false),
358 mResultCode(S_OK),
359 m_pNext(NULL)
360 { }
361
362 void copyFrom(const ErrorInfo &x);
363 void cleanup();
364
365 void init(bool aKeepObj = false);
366 void init(IUnknown *aUnk, const GUID &aIID, bool aKeepObj = false);
367 void init(IVirtualBoxErrorInfo *aInfo);
368
369 bool mIsBasicAvailable : 1;
370 bool mIsFullAvailable : 1;
371
372 HRESULT mResultCode;
373 LONG mResultDetail;
374 Guid mInterfaceID;
375 Bstr mComponent;
376 Bstr mText;
377
378 ErrorInfo *m_pNext;
379
380 Bstr mInterfaceName;
381 Guid mCalleeIID;
382 Bstr mCalleeName;
383
384 ComPtr<IUnknown> mErrorInfo;
385};
386
387/**
388 * A convenience subclass of ErrorInfo that, given an IProgress interface
389 * pointer, reads its errorInfo attribute and uses the returned
390 * IVirtualBoxErrorInfo instance to construct itself.
391 */
392class ProgressErrorInfo : public ErrorInfo
393{
394public:
395
396 /**
397 * Constructs a new instance by fetching error information from the
398 * IProgress interface pointer. If the progress object is not NULL,
399 * its completed attribute is true, resultCode represents a failure,
400 * and the errorInfo attribute returns a valid IVirtualBoxErrorInfo pointer,
401 * both #isFullAvailable() and #isBasicAvailable() will return true.
402 *
403 * @param progress the progress object representing a failed operation
404 */
405 ProgressErrorInfo(IProgress *progress);
406};
407
408/**
409 * A convenience subclass of ErrorInfo that allows to preserve the current
410 * error info. Instances of this class fetch an error info object set on the
411 * current thread and keep a reference to it, which allows to restore it
412 * later using the #restore() method. This is useful to preserve error
413 * information returned by some method for the duration of making another COM
414 * call that may set its own error info and overwrite the existing
415 * one. Preserving and restoring error information makes sense when some
416 * method wants to return error information set by other call as its own
417 * error information while it still needs to make another call before return.
418 *
419 * Instead of calling #restore() explicitly you may let the object destructor
420 * do it for you, if you correctly limit the object's lifetime.
421 *
422 * The usage pattern is:
423 * <code>
424 * rc = foo->method();
425 * if (FAILED(rc))
426 * {
427 * ErrorInfoKeeper eik;
428 * ...
429 * // bar may return error info as well
430 * bar->method();
431 * ...
432 * // no need to call #restore() explicitly here because the eik's
433 * // destructor will restore error info fetched after the failed
434 * // call to foo before returning to the caller
435 * return rc;
436 * }
437 * </code>
438 */
439class ErrorInfoKeeper : public ErrorInfo
440{
441public:
442
443 /**
444 * Constructs a new instance that will fetch the current error info if
445 * @a aIsNull is @c false (by default) or remain uninitialized (null)
446 * otherwise.
447 *
448 * @param aIsNull @c true to prevent fetching error info and leave
449 * the instance uninitialized.
450 */
451 ErrorInfoKeeper(bool aIsNull = false)
452 : ErrorInfo(false), mForgot(aIsNull)
453 {
454 if (!aIsNull)
455 init(true /* aKeepObj */);
456 }
457
458 /**
459 * Constructs a new instance from an ErrorInfo object, to inject a full
460 * error info created elsewhere.
461 *
462 * @param aInfo @c true to prevent fetching error info and leave
463 * the instance uninitialized.
464 */
465 ErrorInfoKeeper(const ErrorInfo &aInfo)
466 : ErrorInfo(false), mForgot(false)
467 {
468 copyFrom(aInfo);
469 }
470
471 /**
472 * Destroys this instance and automatically calls #restore() which will
473 * either restore error info fetched by the constructor or do nothing
474 * if #forget() was called before destruction.
475 */
476 ~ErrorInfoKeeper() { if (!mForgot) restore(); }
477
478 /**
479 * Tries to (re-)fetch error info set on the current thread. On success,
480 * the previous error information, if any, will be overwritten with the
481 * new error information. On failure, or if there is no error information
482 * available, this instance will be reset to null.
483 */
484 void fetch()
485 {
486 setNull();
487 mForgot = false;
488 init(true /* aKeepObj */);
489 }
490
491 /**
492 * Restores error info fetched by the constructor and forgets it
493 * afterwards. Does nothing if the error info was forgotten by #forget().
494 *
495 * @return COM result of the restore operation.
496 */
497 HRESULT restore();
498
499 /**
500 * Forgets error info fetched by the constructor to prevent it from
501 * being restored by #restore() or by the destructor.
502 */
503 void forget() { mForgot = true; }
504
505 /**
506 * Forgets error info fetched by the constructor to prevent it from
507 * being restored by #restore() or by the destructor, and returns the
508 * stored error info object to the caller.
509 */
510 ComPtr<IUnknown> takeError() { mForgot = true; return mErrorInfo; }
511
512private:
513
514 bool mForgot : 1;
515};
516
517} /* namespace com */
518
519#endif
520
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