VirtualBox

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

Last change on this file since 3318 was 3318, checked in by vboxsync, 17 years ago

Main/Glue: Useful enhancements to com::ErrorInfo[Keeper].

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