1
/* $Id: SupportErrorInfo.h $ */
4
* MS COM / XPCOM Abstraction Layer:
5
* SupportErrorInfo* class family declarations
9
* Copyright (C) 2008-2009 Oracle Corporation
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.
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.
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.
29
#ifndef ___VBox_com_SupportErrorInfo_h
30
#define ___VBox_com_SupportErrorInfo_h
32
#include "VBox/com/defs.h"
33
#include "VBox/com/string.h"
35
#include <iprt/cdefs.h>
39
#if !defined (VBOX_WITH_XPCOM)
40
interface IVirtualBoxErrorInfo;
42
class IVirtualBoxErrorInfo;
49
* The MultiResult class is a com::FWResult enhancement that also acts as a
50
* switch to turn on multi-error mode for SupportErrorInfo::setError() and
51
* SupportErrorInfo::setWarning() calls.
53
* When an instance of this class is created, multi-error mode is turned on
54
* for the current thread and the turn-on counter is increased by one. In
55
* multi-error mode, a call to setError() or setWarning() does not
56
* overwrite the current error or warning info object possibly set on the
57
* current thread by other method calls, but instead it stores this old
58
* object in the IVirtualBoxErrorInfo::next attribute of the new error
61
* This way, error/warning objects are stacked together and form a chain of
62
* errors where the most recent error is the first one retrieved by the
63
* calling party, the preceding error is what the
64
* IVirtualBoxErrorInfo::next attribute of the first error points to, and so
65
* on, up to the first error or warning occurred which is the last in the
66
* chain. See IVirtualBoxErrorInfo documentation for more info.
68
* When the instance of the MultiResult class goes out of scope and gets
69
* destroyed, it automatically decreases the turn-on counter by one. If
70
* the counter drops to zero, multi-error mode for the current thread is
71
* turned off and the thread switches back to single-error mode where every
72
* next error or warning object overwrites the previous one.
74
* Note that the caller of a COM method uses a non-S_OK result code to
75
* decide if the method has returned an error (negative codes) or a warning
76
* (positive non-zero codes) and will query extended error info only in
77
* these two cases. However, since multi-error mode implies that the method
78
* doesn't return control return to the caller immediately after the first
79
* error or warning but continues its execution, the functionality provided
80
* by the base com::FWResult class becomes very useful because it allows to
81
* preserve the error or the warning result code even if it is later assigned
82
* a S_OK value multiple times. See com::FWResult for details.
84
* Here is the typical usage pattern:
89
// assume multi-errors are turned off here...
93
// Turn on multi-error mode and make sure severity is preserved
94
MultiResult rc = foo->method1();
96
// return on fatal error, but continue on warning or on success
97
CheckComRCReturnRC (rc);
100
// no matter what result, stack it and continue
104
// return the last worst result code (it will be preserved even if
105
// foo->method2() returns S_OK.
109
// multi-errors are turned off here again...
116
* @note This class is intended to be instantiated on the stack, therefore
117
* You cannot create them using new(). Although it is possible to copy
118
* instances of MultiResult or return them by value, please never do
119
* that as it is breaks the class semantics (and will assert);
121
class MultiResult : public FWResult
126
* @copydoc FWResult::FWResult().
128
MultiResult (HRESULT aRC = E_FAIL) : FWResult (aRC) { incCounter(); }
130
MultiResult (const MultiResult &aThat) : FWResult (aThat)
132
/* We need this copy constructor only for GCC that wants to have
133
* it in case of expressions like |MultiResult rc = E_FAIL;|. But
134
* we assert since the optimizer should actually avoid the
135
* temporary and call the other constructor directly instead. */
139
~MultiResult() { decCounter(); }
141
MultiResult &operator= (HRESULT aRC)
143
FWResult::operator= (aRC);
147
MultiResult &operator= (const MultiResult & /* aThat */)
149
/* We need this copy constructor only for GCC that wants to have
150
* it in case of expressions like |MultiResult rc = E_FAIL;|. But
151
* we assert since the optimizer should actually avoid the
152
* temporary and call the other constructor directly instead. */
159
DECLARE_CLS_NEW_DELETE_NOOP (MultiResult)
161
static void incCounter();
162
static void decCounter();
164
static RTTLS sCounter;
166
friend class SupportErrorInfoBase;
167
friend class MultiResultRef;
171
* The MultiResultRef class is equivalent to MultiResult except that it takes
172
* a reference to the existing HRESULT variable instead of maintaining its own
179
MultiResultRef (HRESULT &aRC) : mRC (aRC) { MultiResult::incCounter(); }
181
~MultiResultRef() { MultiResult::decCounter(); }
183
MultiResultRef &operator= (HRESULT aRC)
185
/* Copied from FWResult */
186
if ((FAILED (aRC) && !FAILED (mRC)) ||
187
(mRC == S_OK && aRC != S_OK))
193
operator HRESULT() const { return mRC; }
195
HRESULT *operator&() { return &mRC; }
199
DECLARE_CLS_NEW_DELETE_NOOP (MultiResultRef)
205
* The SupportErrorInfoBase template class provides basic error info support.
207
* Basic error info support includes a group of setError() methods to set
208
* extended error information on the current thread. This support does not
209
* include all necessary implementation details (for example, implementation of
210
* the ISupportErrorInfo interface on MS COM) to make the error info support
211
* fully functional in a target component. These details are provided by the
212
* SupportErrorInfoDerived class.
214
* This way, this class is intended to be directly inherited only by
215
* intermediate component base classes that will be then inherited by final
216
* component classes through the SupportErrorInfoDerived template class. In
217
* all other cases, the SupportErrorInfoImpl class should be used as a base for
218
* final component classes instead.
220
class SupportErrorInfoBase
222
static HRESULT setErrorInternal(HRESULT aResultCode,
224
const char *aComponent,
225
const Utf8Str &strText,
227
IVirtualBoxErrorInfo *aInfo = NULL);
232
* Returns an interface ID that is to be used in short setError() variants
233
* to specify the interface that has defined the error. Must be implemented
236
virtual const GUID &mainInterfaceID() const = 0;
239
* Returns an component name (in UTF8) that is to be used in short
240
* setError() variants to specify the interface that has defined the error.
241
* Must be implemented in subclasses.
243
virtual const char *componentName() const = 0;
246
* Same as #setError (HRESULT, const GUID &, const char *, const char *) but
247
* interprets the @a aText argument as a RTPrintf-like format string and the
248
* @a aArgs argument as an argument list for this format string.
250
static HRESULT setErrorV(HRESULT aResultCode, const GUID &aIID,
251
const char *aComponent, const char *aText,
254
return setErrorInternal(aResultCode, &aIID, aComponent,
255
Utf8StrFmtVA(aText, aArgs),
256
false /* aWarning */);
260
* Same as #setWarning (HRESULT, const GUID &, const char *, const char *)
261
* but interprets the @a aText argument as a RTPrintf-like format string and
262
* the @a aArgs argument as an argument list for this format string.
264
static HRESULT setWarningV(HRESULT aResultCode, const GUID &aIID,
265
const char *aComponent, const char *aText,
268
return setErrorInternal(aResultCode, &aIID,
270
Utf8StrFmtVA(aText, aArgs),
271
true /* aWarning */);
275
* Same as #setError (HRESULT, const GUID &, const char *, const char *) but
276
* interprets the @a aText argument as a RTPrintf-like format string and
277
* takes a variable list of arguments for this format string.
279
static HRESULT setError(HRESULT aResultCode,
281
const char *aComponent,
286
* Same as #setWarning (HRESULT, const GUID &, const char *, const char *)
287
* but interprets the @a aText argument as a RTPrintf-like format string and
288
* takes a variable list of arguments for this format string.
290
static HRESULT setWarning(HRESULT aResultCode, const GUID &aIID,
291
const char *aComponent, const char *aText,
295
* Sets the given error info object on the current thread.
297
* Note that In multi-error mode (see MultiResult), the existing error info
298
* object (if any) will be preserved by attaching it to the tail of the
299
* error chain of the given aInfo object.
301
* @param aInfo Error info object to set (must not be NULL).
303
static HRESULT setErrorInfo(IVirtualBoxErrorInfo *aInfo)
305
AssertReturn (aInfo != NULL, E_FAIL);
306
return setErrorInternal(0, NULL, NULL, Utf8Str::Null, false, aInfo);
310
* Same as #setError (HRESULT, const GUID &, const char *, const char *,
311
* ...) but uses the return value of the mainInterfaceID() method as an @a
312
* aIID argument and the return value of the componentName() method as a @a
313
* aComponent argument.
315
* This method is the most common (and convenient) way to set error
316
* information from within a component method. The typical usage pattern is:
318
* return setError (E_FAIL, "Terrible Error");
322
* HRESULT rc = setError (E_FAIL, "Terrible Error");
327
HRESULT setError(HRESULT aResultCode, const char *aText, ...);
329
HRESULT setError(HRESULT aResultCode, const Utf8Str &strText);
332
* Same as #setWarning (HRESULT, const GUID &, const char *, const char *,
333
* ...) but uses the return value of the mainInterfaceID() method as an @a
334
* aIID argument and the return value of the componentName() method as a @a
335
* aComponent argument.
337
* This method is the most common (and convenient) way to set warning
338
* information from within a component method. The typical usage pattern is:
340
* return setWarning (E_FAIL, "Dangerous warning");
344
* HRESULT rc = setWarning (E_FAIL, "Dangerous warning");
349
HRESULT setWarning (HRESULT aResultCode, const char *aText, ...);
352
* Same as #setError (HRESULT, const char *, ...) but takes a va_list
353
* argument instead of a variable argument list.
355
HRESULT setErrorV (HRESULT aResultCode, const char *aText, va_list aArgs)
357
return setError (aResultCode, mainInterfaceID(), componentName(),
362
* Same as #setWarning (HRESULT, const char *, ...) but takes a va_list
363
* argument instead of a variable argument list.
365
HRESULT setWarningV (HRESULT aResultCode, const char *aText, va_list aArgs)
367
return setWarning (aResultCode, mainInterfaceID(), componentName(),
372
* Same as #setError (HRESULT, const char *, ...) but allows to specify the
373
* interface ID manually.
375
HRESULT setError (HRESULT aResultCode, const GUID &aIID,
376
const char *aText, ...);
379
* Same as #setWarning (HRESULT, const char *, ...) but allows to specify
380
* the interface ID manually.
382
HRESULT setWarning (HRESULT aResultCode, const GUID &aIID,
383
const char *aText, ...);
386
////////////////////////////////////////////////////////////////////////////////
389
* The SupportErrorInfoDerived template class implements the remaining parts
390
* of error info support in addition to SupportErrorInfoBase.
392
* These parts include the ISupportErrorInfo implementation on the MS COM
393
* platform and implementations of mandatory SupportErrorInfoBase virtual
396
* On MS COM, the @a C template argument must declare a COM interface map using
397
* BEGIN_COM_MAP / END_COM_MAP macros and this map must contain a
398
* COM_INTERFACE_ENTRY(ISupportErrorInfo) definition. All interface entries that
399
* follow it will be considered to support IErrorInfo, i.e. the
400
* InterfaceSupportsErrorInfo() implementation will return S_OK for the
401
* corresponding IIDs.
403
* On all platforms, the @a C template argument must be a subclass of
404
* SupportErrorInfoBase and also define the following method: <tt>public static
405
* const char *ComponentName()</tt> that will be used as a value returned by the
406
* SupportErrorInfoBase::componentName() implementation.
408
* If SupportErrorInfoBase is used as a base for an intermediate component base
409
* class FooBase then the final component FooFinal that inherits FooBase should
410
* use this template class as follows:
412
* class FooFinal : public SupportErrorInfoDerived <FooBase, FooFinal, IFoo>
418
* Note that if you don not use intermediate component base classes, you should
419
* use the SupportErrorInfoImpl class as a base for your component instead.
421
* @param B Intermediate component base derived from SupportErrorInfoBase.
422
* @param C Component class that implements one or more COM interfaces.
423
* @param I Default interface for the component (for short #setError()
426
template <class B, class C, class I>
427
class ATL_NO_VTABLE SupportErrorInfoDerived : public B
428
#if !defined (VBOX_WITH_XPCOM)
429
, public ISupportErrorInfo
434
#if !defined (VBOX_WITH_XPCOM)
435
STDMETHOD(InterfaceSupportsErrorInfo) (REFIID aIID)
437
const _ATL_INTMAP_ENTRY* pEntries = C::_GetEntries();
442
BOOL bSupports = FALSE;
443
BOOL bISupportErrorInfoFound = FALSE;
445
while (pEntries->pFunc != NULL && !bSupports)
447
if (!bISupportErrorInfoFound)
449
/* skip the COM map entries until ISupportErrorInfo is found */
450
bISupportErrorInfoFound =
451
InlineIsEqualGUID (*(pEntries->piid), IID_ISupportErrorInfo);
455
/* look for the requested interface in the rest of the com map */
456
bSupports = InlineIsEqualGUID (*(pEntries->piid), aIID);
461
Assert (bISupportErrorInfoFound);
463
return bSupports ? S_OK : S_FALSE;
465
#endif /* !defined (VBOX_WITH_XPCOM) */
469
virtual const GUID &mainInterfaceID() const { return COM_IIDOF (I); }
471
virtual const char *componentName() const { return C::ComponentName(); }
474
////////////////////////////////////////////////////////////////////////////////
477
* The SupportErrorInfoImpl template class provides complete error info support
478
* for COM component classes.
480
* Complete error info support includes what both SupportErrorInfoBase and
481
* SupportErrorInfoDerived provide, e.g. a variety of setError() methods to
482
* set extended error information from within a component's method
483
* implementation and all necessary additional interface implementations (see
484
* descriptions of these classes for details).
486
* To add error info support to a Foo component that implements a IFoo
487
* interface, use the following pattern:
489
* class Foo : public SupportErrorInfoImpl <Foo, IFoo>
495
* static const char *ComponentName() const { return "Foo"; }
499
* Note that your component class (the @a C template argument) must provide the
500
* ComponentName() implementation as shown above.
502
* @param C Component class that implements one or more COM interfaces.
503
* @param I Default interface for the component (for short #setError()
506
template <class C, class I>
507
class ATL_NO_VTABLE SupportErrorInfoImpl
508
: public SupportErrorInfoDerived <SupportErrorInfoBase, C, I>
512
} /* namespace com */
514
#endif /* ___VBox_com_SupportErrorInfo_h */