VirtualBox

source: vbox/trunk/include/VBox/com/SupportErrorInfo.h@ 26553

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

Main: remove some dead errorinfo code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 18.2 KB
Line 
1/* $Id: SupportErrorInfo.h 26236 2010-02-04 14:38:16Z vboxsync $ */
2
3/** @file
4 * MS COM / XPCOM Abstraction Layer:
5 * SupportErrorInfo* class family declarations
6 */
7
8/*
9 * Copyright (C) 2008-2009 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * The contents of this file may alternatively be used under the terms
20 * of the Common Development and Distribution License Version 1.0
21 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
22 * VirtualBox OSE distribution, in which case the provisions of the
23 * CDDL are applicable instead of those of the GPL.
24 *
25 * You may elect to license modified versions of this file under the
26 * terms and conditions of either the GPL or the CDDL or both.
27 *
28 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
29 * Clara, CA 95054 USA or visit http://www.sun.com if you need
30 * additional information or have any questions.
31 */
32
33#ifndef ___VBox_com_SupportErrorInfo_h
34#define ___VBox_com_SupportErrorInfo_h
35
36#include "VBox/com/defs.h"
37#include "VBox/com/string.h"
38
39#include <iprt/cdefs.h>
40
41#include <stdarg.h>
42
43#if !defined (VBOX_WITH_XPCOM)
44interface IVirtualBoxErrorInfo;
45#else
46class IVirtualBoxErrorInfo;
47#endif
48
49namespace com
50{
51
52/**
53 * The MultiResult class is a com::FWResult enhancement that also acts as a
54 * switch to turn on multi-error mode for SupportErrorInfo::setError() and
55 * SupportErrorInfo::setWarning() calls.
56 *
57 * When an instance of this class is created, multi-error mode is turned on
58 * for the current thread and the turn-on counter is increased by one. In
59 * multi-error mode, a call to setError() or setWarning() does not
60 * overwrite the current error or warning info object possibly set on the
61 * current thread by other method calls, but instead it stores this old
62 * object in the IVirtualBoxErrorInfo::next attribute of the new error
63 * object being set.
64 *
65 * This way, error/warning objects are stacked together and form a chain of
66 * errors where the most recent error is the first one retrieved by the
67 * calling party, the preceding error is what the
68 * IVirtualBoxErrorInfo::next attribute of the first error points to, and so
69 * on, up to the first error or warning occurred which is the last in the
70 * chain. See IVirtualBoxErrorInfo documentation for more info.
71 *
72 * When the instance of the MultiResult class goes out of scope and gets
73 * destroyed, it automatically decreases the turn-on counter by one. If
74 * the counter drops to zero, multi-error mode for the current thread is
75 * turned off and the thread switches back to single-error mode where every
76 * next error or warning object overwrites the previous one.
77 *
78 * Note that the caller of a COM method uses a non-S_OK result code to
79 * decide if the method has returned an error (negative codes) or a warning
80 * (positive non-zero codes) and will query extended error info only in
81 * these two cases. However, since multi-error mode implies that the method
82 * doesn't return control return to the caller immediately after the first
83 * error or warning but continues its execution, the functionality provided
84 * by the base com::FWResult class becomes very useful because it allows to
85 * preserve the error or the warning result code even if it is later assigned
86 * a S_OK value multiple times. See com::FWResult for details.
87 *
88 * Here is the typical usage pattern:
89 * <code>
90
91 HRESULT Bar::method()
92 {
93 // assume multi-errors are turned off here...
94
95 if (something)
96 {
97 // Turn on multi-error mode and make sure severity is preserved
98 MultiResult rc = foo->method1();
99
100 // return on fatal error, but continue on warning or on success
101 CheckComRCReturnRC (rc);
102
103 rc = foo->method2();
104 // no matter what result, stack it and continue
105
106 // ...
107
108 // return the last worst result code (it will be preserved even if
109 // foo->method2() returns S_OK.
110 return rc;
111 }
112
113 // multi-errors are turned off here again...
114
115 return S_OK;
116 }
117
118 * </code>
119 *
120 * @note This class is intended to be instantiated on the stack, therefore
121 * You cannot create them using new(). Although it is possible to copy
122 * instances of MultiResult or return them by value, please never do
123 * that as it is breaks the class semantics (and will assert);
124 */
125class MultiResult : public FWResult
126{
127public:
128
129 /**
130 * @copydoc FWResult::FWResult().
131 */
132 MultiResult (HRESULT aRC = E_FAIL) : FWResult (aRC) { incCounter(); }
133
134 MultiResult (const MultiResult &aThat) : FWResult (aThat)
135 {
136 /* We need this copy constructor only for GCC that wants to have
137 * it in case of expressions like |MultiResult rc = E_FAIL;|. But
138 * we assert since the optimizer should actually avoid the
139 * temporary and call the other constructor directly instead. */
140 AssertFailed();
141 }
142
143 ~MultiResult() { decCounter(); }
144
145 MultiResult &operator= (HRESULT aRC)
146 {
147 FWResult::operator= (aRC);
148 return *this;
149 }
150
151 MultiResult &operator= (const MultiResult & /* aThat */)
152 {
153 /* We need this copy constructor only for GCC that wants to have
154 * it in case of expressions like |MultiResult rc = E_FAIL;|. But
155 * we assert since the optimizer should actually avoid the
156 * temporary and call the other constructor directly instead. */
157 AssertFailed();
158 return *this;
159 }
160
161private:
162
163 DECLARE_CLS_NEW_DELETE_NOOP (MultiResult)
164
165 static void incCounter();
166 static void decCounter();
167
168 static RTTLS sCounter;
169
170 friend class SupportErrorInfoBase;
171 friend class MultiResultRef;
172};
173
174/**
175 * The MultiResultRef class is equivalent to MultiResult except that it takes
176 * a reference to the existing HRESULT variable instead of maintaining its own
177 * one.
178 */
179class MultiResultRef
180{
181public:
182
183 MultiResultRef (HRESULT &aRC) : mRC (aRC) { MultiResult::incCounter(); }
184
185 ~MultiResultRef() { MultiResult::decCounter(); }
186
187 MultiResultRef &operator= (HRESULT aRC)
188 {
189 /* Copied from FWResult */
190 if ((FAILED (aRC) && !FAILED (mRC)) ||
191 (mRC == S_OK && aRC != S_OK))
192 mRC = aRC;
193
194 return *this;
195 }
196
197 operator HRESULT() const { return mRC; }
198
199 HRESULT *operator&() { return &mRC; }
200
201private:
202
203 DECLARE_CLS_NEW_DELETE_NOOP (MultiResultRef)
204
205 HRESULT &mRC;
206};
207
208/**
209 * The SupportErrorInfoBase template class provides basic error info support.
210 *
211 * Basic error info support includes a group of setError() methods to set
212 * extended error information on the current thread. This support does not
213 * include all necessary implementation details (for example, implementation of
214 * the ISupportErrorInfo interface on MS COM) to make the error info support
215 * fully functional in a target component. These details are provided by the
216 * SupportErrorInfoDerived class.
217 *
218 * This way, this class is intended to be directly inherited only by
219 * intermediate component base classes that will be then inherited by final
220 * component classes through the SupportErrorInfoDerived template class. In
221 * all other cases, the SupportErrorInfoImpl class should be used as a base for
222 * final component classes instead.
223 */
224class SupportErrorInfoBase
225{
226 static HRESULT setErrorInternal(HRESULT aResultCode,
227 const GUID *aIID,
228 const char *aComponent,
229 const Utf8Str &strText,
230 bool aWarning,
231 IVirtualBoxErrorInfo *aInfo = NULL);
232
233protected:
234
235 /**
236 * Returns an interface ID that is to be used in short setError() variants
237 * to specify the interface that has defined the error. Must be implemented
238 * in subclasses.
239 */
240 virtual const GUID &mainInterfaceID() const = 0;
241
242 /**
243 * Returns an component name (in UTF8) that is to be used in short
244 * setError() variants to specify the interface that has defined the error.
245 * Must be implemented in subclasses.
246 */
247 virtual const char *componentName() const = 0;
248
249 /**
250 * Same as #setError (HRESULT, const GUID &, const char *, const char *) but
251 * interprets the @a aText argument as a RTPrintf-like format string and the
252 * @a aArgs argument as an argument list for this format string.
253 */
254 static HRESULT setErrorV(HRESULT aResultCode, const GUID &aIID,
255 const char *aComponent, const char *aText,
256 va_list aArgs)
257 {
258 return setErrorInternal(aResultCode, &aIID, aComponent,
259 Utf8StrFmtVA(aText, aArgs),
260 false /* aWarning */);
261 }
262
263 /**
264 * Same as #setWarning (HRESULT, const GUID &, const char *, const char *)
265 * but interprets the @a aText argument as a RTPrintf-like format string and
266 * the @a aArgs argument as an argument list for this format string.
267 */
268 static HRESULT setWarningV(HRESULT aResultCode, const GUID &aIID,
269 const char *aComponent, const char *aText,
270 va_list aArgs)
271 {
272 return setErrorInternal(aResultCode, &aIID,
273 aComponent,
274 Utf8StrFmtVA(aText, aArgs),
275 true /* aWarning */);
276 }
277
278 /**
279 * Same as #setError (HRESULT, const GUID &, const char *, const char *) but
280 * interprets the @a aText argument as a RTPrintf-like format string and
281 * takes a variable list of arguments for this format string.
282 */
283 static HRESULT setError(HRESULT aResultCode,
284 const GUID &aIID,
285 const char *aComponent,
286 const char *aText,
287 ...);
288
289 /**
290 * Same as #setWarning (HRESULT, const GUID &, const char *, const char *)
291 * but interprets the @a aText argument as a RTPrintf-like format string and
292 * takes a variable list of arguments for this format string.
293 */
294 static HRESULT setWarning(HRESULT aResultCode, const GUID &aIID,
295 const char *aComponent, const char *aText,
296 ...);
297
298 /**
299 * Sets the given error info object on the current thread.
300 *
301 * Note that In multi-error mode (see MultiResult), the existing error info
302 * object (if any) will be preserved by attaching it to the tail of the
303 * error chain of the given aInfo object.
304 *
305 * @param aInfo Error info object to set (must not be NULL).
306 */
307 static HRESULT setErrorInfo(IVirtualBoxErrorInfo *aInfo)
308 {
309 AssertReturn (aInfo != NULL, E_FAIL);
310 return setErrorInternal(0, NULL, NULL, Utf8Str::Null, false, aInfo);
311 }
312
313 /**
314 * Same as #setError (HRESULT, const GUID &, const char *, const char *,
315 * ...) but uses the return value of the mainInterfaceID() method as an @a
316 * aIID argument and the return value of the componentName() method as a @a
317 * aComponent argument.
318 *
319 * This method is the most common (and convenient) way to set error
320 * information from within a component method. The typical usage pattern is:
321 * <code>
322 * return setError (E_FAIL, "Terrible Error");
323 * </code>
324 * or
325 * <code>
326 * HRESULT rc = setError (E_FAIL, "Terrible Error");
327 * ...
328 * return rc;
329 * </code>
330 */
331 HRESULT setError(HRESULT aResultCode, const char *aText, ...);
332
333 HRESULT setError(HRESULT aResultCode, const Utf8Str &strText);
334
335 /**
336 * Same as #setWarning (HRESULT, const GUID &, const char *, const char *,
337 * ...) but uses the return value of the mainInterfaceID() method as an @a
338 * aIID argument and the return value of the componentName() method as a @a
339 * aComponent argument.
340 *
341 * This method is the most common (and convenient) way to set warning
342 * information from within a component method. The typical usage pattern is:
343 * <code>
344 * return setWarning (E_FAIL, "Dangerous warning");
345 * </code>
346 * or
347 * <code>
348 * HRESULT rc = setWarning (E_FAIL, "Dangerous warning");
349 * ...
350 * return rc;
351 * </code>
352 */
353 HRESULT setWarning (HRESULT aResultCode, const char *aText, ...);
354
355 /**
356 * Same as #setError (HRESULT, const char *, ...) but takes a va_list
357 * argument instead of a variable argument list.
358 */
359 HRESULT setErrorV (HRESULT aResultCode, const char *aText, va_list aArgs)
360 {
361 return setError (aResultCode, mainInterfaceID(), componentName(),
362 aText, aArgs);
363 }
364
365 /**
366 * Same as #setWarning (HRESULT, const char *, ...) but takes a va_list
367 * argument instead of a variable argument list.
368 */
369 HRESULT setWarningV (HRESULT aResultCode, const char *aText, va_list aArgs)
370 {
371 return setWarning (aResultCode, mainInterfaceID(), componentName(),
372 aText, aArgs);
373 }
374
375 /**
376 * Same as #setError (HRESULT, const char *, ...) but allows to specify the
377 * interface ID manually.
378 */
379 HRESULT setError (HRESULT aResultCode, const GUID &aIID,
380 const char *aText, ...);
381
382 /**
383 * Same as #setWarning (HRESULT, const char *, ...) but allows to specify
384 * the interface ID manually.
385 */
386 HRESULT setWarning (HRESULT aResultCode, const GUID &aIID,
387 const char *aText, ...);
388};
389
390////////////////////////////////////////////////////////////////////////////////
391
392/**
393 * The SupportErrorInfoDerived template class implements the remaining parts
394 * of error info support in addition to SupportErrorInfoBase.
395 *
396 * These parts include the ISupportErrorInfo implementation on the MS COM
397 * platform and implementations of mandatory SupportErrorInfoBase virtual
398 * methods.
399 *
400 * On MS COM, the @a C template argument must declare a COM interface map using
401 * BEGIN_COM_MAP / END_COM_MAP macros and this map must contain a
402 * COM_INTERFACE_ENTRY(ISupportErrorInfo) definition. All interface entries that
403 * follow it will be considered to support IErrorInfo, i.e. the
404 * InterfaceSupportsErrorInfo() implementation will return S_OK for the
405 * corresponding IIDs.
406 *
407 * On all platforms, the @a C template argument must be a subclass of
408 * SupportErrorInfoBase and also define the following method: <tt>public static
409 * const char *ComponentName()</tt> that will be used as a value returned by the
410 * SupportErrorInfoBase::componentName() implementation.
411 *
412 * If SupportErrorInfoBase is used as a base for an intermediate component base
413 * class FooBase then the final component FooFinal that inherits FooBase should
414 * use this template class as follows:
415 * <code>
416 * class FooFinal : public SupportErrorInfoDerived <FooBase, FooFinal, IFoo>
417 * {
418 * ...
419 * };
420 * </code>
421 *
422 * Note that if you don not use intermediate component base classes, you should
423 * use the SupportErrorInfoImpl class as a base for your component instead.
424 *
425 * @param B Intermediate component base derived from SupportErrorInfoBase.
426 * @param C Component class that implements one or more COM interfaces.
427 * @param I Default interface for the component (for short #setError()
428 * versions).
429 */
430template <class B, class C, class I>
431class ATL_NO_VTABLE SupportErrorInfoDerived : public B
432#if !defined (VBOX_WITH_XPCOM)
433 , public ISupportErrorInfo
434#endif
435{
436public:
437
438#if !defined (VBOX_WITH_XPCOM)
439 STDMETHOD(InterfaceSupportsErrorInfo) (REFIID aIID)
440 {
441 const _ATL_INTMAP_ENTRY* pEntries = C::_GetEntries();
442 Assert (pEntries);
443 if (!pEntries)
444 return S_FALSE;
445
446 BOOL bSupports = FALSE;
447 BOOL bISupportErrorInfoFound = FALSE;
448
449 while (pEntries->pFunc != NULL && !bSupports)
450 {
451 if (!bISupportErrorInfoFound)
452 {
453 /* skip the COM map entries until ISupportErrorInfo is found */
454 bISupportErrorInfoFound =
455 InlineIsEqualGUID (*(pEntries->piid), IID_ISupportErrorInfo);
456 }
457 else
458 {
459 /* look for the requested interface in the rest of the com map */
460 bSupports = InlineIsEqualGUID (*(pEntries->piid), aIID);
461 }
462 pEntries++;
463 }
464
465 Assert (bISupportErrorInfoFound);
466
467 return bSupports ? S_OK : S_FALSE;
468 }
469#endif /* !defined (VBOX_WITH_XPCOM) */
470
471protected:
472
473 virtual const GUID &mainInterfaceID() const { return COM_IIDOF (I); }
474
475 virtual const char *componentName() const { return C::ComponentName(); }
476};
477
478////////////////////////////////////////////////////////////////////////////////
479
480/**
481 * The SupportErrorInfoImpl template class provides complete error info support
482 * for COM component classes.
483 *
484 * Complete error info support includes what both SupportErrorInfoBase and
485 * SupportErrorInfoDerived provide, e.g. a variety of setError() methods to
486 * set extended error information from within a component's method
487 * implementation and all necessary additional interface implementations (see
488 * descriptions of these classes for details).
489 *
490 * To add error info support to a Foo component that implements a IFoo
491 * interface, use the following pattern:
492 * <code>
493 * class Foo : public SupportErrorInfoImpl <Foo, IFoo>
494 * {
495 * public:
496 *
497 * ...
498 *
499 * static const char *ComponentName() const { return "Foo"; }
500 * };
501 * </code>
502 *
503 * Note that your component class (the @a C template argument) must provide the
504 * ComponentName() implementation as shown above.
505 *
506 * @param C Component class that implements one or more COM interfaces.
507 * @param I Default interface for the component (for short #setError()
508 * versions).
509 */
510template <class C, class I>
511class ATL_NO_VTABLE SupportErrorInfoImpl
512 : public SupportErrorInfoDerived <SupportErrorInfoBase, C, I>
513{
514};
515
516} /* namespace com */
517
518#endif /* ___VBox_com_SupportErrorInfo_h */
519
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