VirtualBox

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

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

Main: COM header cleanup (remove obscure and unused templates), second try

  • 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 virtual ~ErrorInfo();
127
128 /**
129 * Returns whether basic error info is actually available for the current
130 * thread. If the instance was created from an interface pointer that
131 * supports basic error info and successfully provided it, or if it is an
132 * "interfaceless" instance and there is some error info for the current
133 * thread, the returned value will be true.
134 *
135 * See the class description for details about the basic error info level.
136 *
137 * The appropriate methods of this class provide meaningful info only when
138 * this method returns true (otherwise they simply return NULL-like values).
139 */
140 bool isBasicAvailable() const
141 {
142 return mIsBasicAvailable;
143 }
144
145 /**
146 * Returns whether full error info is actually available for the current
147 * thread. If the instance was created from an interface pointer that
148 * supports full error info and successfully provided it, or if it is an
149 * "interfaceless" instance and there is some error info for the current
150 * thread, the returned value will be true.
151 *
152 * See the class description for details about the full error info level.
153 *
154 * The appropriate methods of this class provide meaningful info only when
155 * this method returns true (otherwise they simply return NULL-like values).
156 */
157 bool isFullAvailable() const
158 {
159 return mIsFullAvailable;
160 }
161
162 /**
163 * Returns the COM result code of the failed operation.
164 */
165 HRESULT getResultCode() const
166 {
167 return mResultCode;
168 }
169
170 /**
171 * Returns the IID of the interface that defined the error.
172 */
173 const Guid& getInterfaceID() const
174 {
175 return mInterfaceID;
176 }
177
178 /**
179 * Returns the name of the component that generated the error.
180 */
181 const Bstr& getComponent() const
182 {
183 return mComponent;
184 }
185
186 /**
187 * Returns the textual description of the error.
188 */
189 const Bstr& getText() const
190 {
191 return mText;
192 }
193
194 /**
195 * Returns the next error information object or @c NULL if there is none.
196 */
197 const ErrorInfo* getNext() const
198 {
199 return m_pNext;
200 }
201
202 /**
203 * Returns the name of the interface that defined the error
204 */
205 const Bstr& getInterfaceName() const
206 {
207 return mInterfaceName;
208 }
209
210 /**
211 * Returns the IID of the interface that returned the error.
212 *
213 * This method returns a non-null IID only if the instance was created
214 * using #template <class I> ErrorInfo (I *i) or
215 * template <class I> ErrorInfo (const ComPtr <I> &i) constructor.
216 */
217 const Guid& getCalleeIID() const
218 {
219 return mCalleeIID;
220 }
221
222 /**
223 * Returns the name of the interface that returned the error
224 *
225 * This method returns a non-null name only if the instance was created
226 * using #template <class I> ErrorInfo (I *i) or
227 * template <class I> ErrorInfo (const ComPtr <I> &i) constructor.
228 */
229 const Bstr& getCalleeName() const
230 {
231 return mCalleeName;
232 }
233
234 /**
235 * Resets all collected error information. #isNull() will
236 * return @c true after this method is called.
237 */
238 void setNull()
239 {
240 mIsBasicAvailable = false;
241 mIsFullAvailable = false;
242
243 if (m_pNext)
244 {
245 delete m_pNext;
246 m_pNext = NULL;
247 }
248
249 mResultCode = S_OK;
250 mInterfaceID.clear();
251 mComponent.setNull();
252 mText.setNull();
253 mInterfaceName.setNull();
254 mCalleeIID.clear();
255 mCalleeName.setNull();
256 mErrorInfo.setNull();
257 }
258
259protected:
260
261 ErrorInfo(bool /* aDummy */)
262 : mIsBasicAvailable(false),
263 mIsFullAvailable(false),
264 mResultCode(S_OK),
265 m_pNext(NULL)
266 { }
267
268 void init(bool aKeepObj = false);
269 void init(IUnknown *aUnk, const GUID &aIID, bool aKeepObj = false);
270 void init(IVirtualBoxErrorInfo *aInfo);
271
272 bool mIsBasicAvailable : 1;
273 bool mIsFullAvailable : 1;
274
275 HRESULT mResultCode;
276 Guid mInterfaceID;
277 Bstr mComponent;
278 Bstr mText;
279
280 ErrorInfo *m_pNext;
281
282 Bstr mInterfaceName;
283 Guid mCalleeIID;
284 Bstr mCalleeName;
285
286 ComPtr<IUnknown> mErrorInfo;
287
288private:
289 ErrorInfo(const ErrorInfo&);
290};
291
292/**
293 * A convenience subclass of ErrorInfo that, given an IProgress interface
294 * pointer, reads its errorInfo attribute and uses the returned
295 * IVirtualBoxErrorInfo instance to construct itself.
296 */
297class ProgressErrorInfo : public ErrorInfo
298{
299public:
300
301 /**
302 * Constructs a new instance by fetching error information from the
303 * IProgress interface pointer. If the progress object is not NULL,
304 * its completed attribute is true, resultCode represents a failure,
305 * and the errorInfo attribute returns a valid IVirtualBoxErrorInfo pointer,
306 * both #isFullAvailable() and #isBasicAvailable() will return true.
307 *
308 * @param progress the progress object representing a failed operation
309 */
310 ProgressErrorInfo(IProgress *progress);
311};
312
313/**
314 * A convenience subclass of ErrorInfo that allows to preserve the current
315 * error info. Instances of this class fetch an error info object set on the
316 * current thread and keep a reference to it, which allows to restore it
317 * later using the #restore() method. This is useful to preserve error
318 * information returned by some method for the duration of making another COM
319 * call that may set its own error info and overwrite the existing
320 * one. Preserving and restoring error information makes sense when some
321 * method wants to return error information set by other call as its own
322 * error information while it still needs to make another call before return.
323 *
324 * Instead of calling #restore() explicitly you may let the object destructor
325 * do it for you, if you correctly limit the object's lifetime.
326 *
327 * The usage pattern is:
328 * <code>
329 * rc = foo->method();
330 * if (FAILED (rc))
331 * {
332 * ErrorInfoKeeper eik;
333 * ...
334 * // bar may return error info as well
335 * bar->method();
336 * ...
337 * // no need to call #restore() explicitly here because the eik's
338 * // destructor will restore error info fetched after the failed
339 * // call to foo before returning to the caller
340 * return rc;
341 * }
342 * </code>
343 */
344class ErrorInfoKeeper : public ErrorInfo
345{
346public:
347
348 /**
349 * Constructs a new instance that will fetch the current error info if
350 * @a aIsNull is @c false (by default) or remain uninitialized (null)
351 * otherwise.
352 *
353 * @param aIsNull @c true to prevent fetching error info and leave
354 * the instance uninitialized.
355 */
356 ErrorInfoKeeper (bool aIsNull = false)
357 : ErrorInfo (false), mForgot (aIsNull)
358 {
359 if (!aIsNull)
360 init (true /* aKeepObj */);
361 }
362
363 /**
364 * Destroys this instance and automatically calls #restore() which will
365 * either restore error info fetched by the constructor or do nothing
366 * if #forget() was called before destruction.
367 */
368 ~ErrorInfoKeeper() { if (!mForgot) restore(); }
369
370 /**
371 * Tries to (re-)fetch error info set on the current thread. On success,
372 * the previous error information, if any, will be overwritten with the
373 * new error information. On failure, or if there is no error information
374 * available, this instance will be reset to null.
375 */
376 void fetch()
377 {
378 setNull();
379 mForgot = false;
380 init(true /* aKeepObj */);
381 }
382
383 /**
384 * Restores error info fetched by the constructor and forgets it
385 * afterwards. Does nothing if the error info was forgotten by #forget().
386 *
387 * @return COM result of the restore operation.
388 */
389 HRESULT restore();
390
391 /**
392 * Forgets error info fetched by the constructor to prevent it from
393 * being restored by #restore() or by the destructor.
394 */
395 void forget() { mForgot = true; }
396
397 /**
398 * Forgets error info fetched by the constructor to prevent it from
399 * being restored by #restore() or by the destructor, and returns the
400 * stored error info object to the caller.
401 */
402 ComPtr <IUnknown> takeError() { mForgot = true; return mErrorInfo; }
403
404private:
405
406 bool mForgot : 1;
407};
408
409} /* namespace com */
410
411#endif
412
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