VirtualBox

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

Last change on this file since 24130 was 22173, checked in by vboxsync, 15 years ago

Main: the big XML settings rework. Move XML reading/writing out of interface implementation code into separate layer so it can handle individual settings versions in the future.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 22.4 KB
Line 
1/* $Id: SupportErrorInfo.h 22173 2009-08-11 15:38:59Z 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 * Sets the error information for the current thread.
251 *
252 * When the error information is set, it can be retrieved by a caller of an
253 * interface method using the respective methods that return an IErrorInfo
254 * object in MS COM (nsIException object in XPCOM) set for the current
255 * thread. This object can also be or queried for the platform-independent
256 * IVirtualBoxErrorInfo interface that provides extended error information
257 * (only for components from the VirtualBox COM library). Alternatively, the
258 * platform-independent ErrorInfo class can be used to retrieve error info
259 * in a convenient way.
260 *
261 * It is assumed that the interface method that uses this function returns
262 * an non S_OK result code to the caller (otherwise, there is no reason
263 * for the caller to check for error info after method invocation).
264 *
265 * Here is a table of correspondence between this method's arguments and
266 * IErrorInfo/nsIException/IVirtualBoxErrorInfo attributes/methods:
267 *
268 * <pre>
269 * argument IErrorInfo nsIException IVirtualBoxErrorInfo
270 * ----------------------------------------------------------------
271 * resultCode -- result resultCode
272 * iid GetGUID -- interfaceID
273 * component GetSource -- component
274 * text GetDescription message text
275 * </pre>
276 *
277 * Note that this is a generic method. There are more convenient overloaded
278 * versions that automatically substitute some arguments taking their
279 * values from the template parameters. See #setError (HRESULT, const char
280 * *, ...) for an example.
281 *
282 * It is also possible to turn on the multi-error mode so that setting a new
283 * error information does not destroy the previous error (if any) but makes
284 * it accessible using the IVirtualBoxErrorInfo::next attribute. See
285 * MultiResult for more information.
286 *
287 * @param aResultCode Result (error) code, must not be S_OK.
288 * @param aIID IID of the interface that defines the error.
289 * @param aComponent Name of the component that sets the error (UTF8).
290 * @param aText Error message in UTF8 (must not be NULL).
291 *
292 * @return @a aResultCode argument, for convenience. If an error occurs
293 * while setting error info itself, that error is returned instead
294 * of the @a aResultCode argument.
295 */
296 static HRESULT setError(HRESULT aResultCode,
297 const GUID &aIID,
298 const char *aComponent,
299 const char *aText)
300 {
301 return setErrorInternal(aResultCode,
302 &aIID,
303 aComponent,
304 aText,
305 false /* aWarning */);
306 }
307
308 static HRESULT setError(HRESULT aResultCode,
309 const GUID &aIID,
310 const char *aComponent,
311 const Utf8Str &strText)
312 {
313 return setErrorInternal(aResultCode,
314 &aIID,
315 aComponent,
316 strText,
317 false /* aWarning */);
318 }
319
320 /**
321 * Same as #setError() except that it makes sure that aResultCode doesn't
322 * have the error severity bit (31) set when passed down to the created
323 * IVirtualBoxErrorInfo object.
324 *
325 * The error severity bit is always cleared by this call, thereof you can
326 * use ordinary E_XXX result code constants, for convenience. However, this
327 * behavior may be non-standard on some COM platforms.
328 */
329 static HRESULT setWarning(HRESULT aResultCode,
330 const GUID &aIID,
331 const char *aComponent,
332 const char *aText)
333 {
334 return setErrorInternal(aResultCode,
335 &aIID,
336 aComponent,
337 aText,
338 true /* aWarning */);
339 }
340
341 /**
342 * Same as #setError (HRESULT, const GUID &, const char *, const char *) but
343 * interprets the @a aText argument as a RTPrintf-like format string and the
344 * @a aArgs argument as an argument list for this format string.
345 */
346 static HRESULT setErrorV(HRESULT aResultCode, const GUID &aIID,
347 const char *aComponent, const char *aText,
348 va_list aArgs)
349 {
350 return setErrorInternal(aResultCode, &aIID, aComponent,
351 Utf8StrFmtVA(aText, aArgs),
352 false /* aWarning */);
353 }
354
355 /**
356 * Same as #setWarning (HRESULT, const GUID &, const char *, const char *)
357 * but interprets the @a aText argument as a RTPrintf-like format string and
358 * the @a aArgs argument as an argument list for this format string.
359 */
360 static HRESULT setWarningV(HRESULT aResultCode, const GUID &aIID,
361 const char *aComponent, const char *aText,
362 va_list aArgs)
363 {
364 return setErrorInternal(aResultCode, &aIID,
365 aComponent,
366 Utf8StrFmtVA(aText, aArgs),
367 true /* aWarning */);
368 }
369
370 /**
371 * Same as #setError (HRESULT, const GUID &, const char *, const char *) but
372 * interprets the @a aText argument as a RTPrintf-like format string and
373 * takes a variable list of arguments for this format string.
374 */
375 static HRESULT setError(HRESULT aResultCode,
376 const GUID &aIID,
377 const char *aComponent,
378 const char *aText,
379 ...);
380
381 /**
382 * Same as #setWarning (HRESULT, const GUID &, const char *, const char *)
383 * but interprets the @a aText argument as a RTPrintf-like format string and
384 * takes a variable list of arguments for this format string.
385 */
386 static HRESULT setWarning(HRESULT aResultCode, const GUID &aIID,
387 const char *aComponent, const char *aText,
388 ...);
389
390 /**
391 * Sets the given error info object on the current thread.
392 *
393 * Note that In multi-error mode (see MultiResult), the existing error info
394 * object (if any) will be preserved by attaching it to the tail of the
395 * error chain of the given aInfo object.
396 *
397 * @param aInfo Error info object to set (must not be NULL).
398 */
399 static HRESULT setErrorInfo(IVirtualBoxErrorInfo *aInfo)
400 {
401 AssertReturn (aInfo != NULL, E_FAIL);
402 return setErrorInternal(0, NULL, NULL, Utf8Str::Null, false, aInfo);
403 }
404
405 /**
406 * Same as #setError (HRESULT, const GUID &, const char *, const char *,
407 * ...) but uses the return value of the mainInterfaceID() method as an @a
408 * aIID argument and the return value of the componentName() method as a @a
409 * aComponent argument.
410 *
411 * This method is the most common (and convenient) way to set error
412 * information from within a component method. The typical usage pattern is:
413 * <code>
414 * return setError (E_FAIL, "Terrible Error");
415 * </code>
416 * or
417 * <code>
418 * HRESULT rc = setError (E_FAIL, "Terrible Error");
419 * ...
420 * return rc;
421 * </code>
422 */
423 HRESULT setError(HRESULT aResultCode, const char *aText, ...);
424
425 HRESULT setError(HRESULT aResultCode, const Utf8Str &strText);
426
427 /**
428 * Same as #setWarning (HRESULT, const GUID &, const char *, const char *,
429 * ...) but uses the return value of the mainInterfaceID() method as an @a
430 * aIID argument and the return value of the componentName() method as a @a
431 * aComponent argument.
432 *
433 * This method is the most common (and convenient) way to set warning
434 * information from within a component method. The typical usage pattern is:
435 * <code>
436 * return setWarning (E_FAIL, "Dangerous warning");
437 * </code>
438 * or
439 * <code>
440 * HRESULT rc = setWarning (E_FAIL, "Dangerous warning");
441 * ...
442 * return rc;
443 * </code>
444 */
445 HRESULT setWarning (HRESULT aResultCode, const char *aText, ...);
446
447 /**
448 * Same as #setError (HRESULT, const char *, ...) but takes a va_list
449 * argument instead of a variable argument list.
450 */
451 HRESULT setErrorV (HRESULT aResultCode, const char *aText, va_list aArgs)
452 {
453 return setError (aResultCode, mainInterfaceID(), componentName(),
454 aText, aArgs);
455 }
456
457 /**
458 * Same as #setWarning (HRESULT, const char *, ...) but takes a va_list
459 * argument instead of a variable argument list.
460 */
461 HRESULT setWarningV (HRESULT aResultCode, const char *aText, va_list aArgs)
462 {
463 return setWarning (aResultCode, mainInterfaceID(), componentName(),
464 aText, aArgs);
465 }
466
467 /**
468 * Same as #setError (HRESULT, const char *, ...) but allows to specify the
469 * interface ID manually.
470 */
471 HRESULT setError (HRESULT aResultCode, const GUID &aIID,
472 const char *aText, ...);
473
474 /**
475 * Same as #setWarning (HRESULT, const char *, ...) but allows to specify
476 * the interface ID manually.
477 */
478 HRESULT setWarning (HRESULT aResultCode, const GUID &aIID,
479 const char *aText, ...);
480};
481
482////////////////////////////////////////////////////////////////////////////////
483
484/**
485 * The SupportErrorInfoDerived template class implements the remaining parts
486 * of error info support in addition to SupportErrorInfoBase.
487 *
488 * These parts include the ISupportErrorInfo implementation on the MS COM
489 * platform and implementations of mandatory SupportErrorInfoBase virtual
490 * methods.
491 *
492 * On MS COM, the @a C template argument must declare a COM interface map using
493 * BEGIN_COM_MAP / END_COM_MAP macros and this map must contain a
494 * COM_INTERFACE_ENTRY(ISupportErrorInfo) definition. All interface entries that
495 * follow it will be considered to support IErrorInfo, i.e. the
496 * InterfaceSupportsErrorInfo() implementation will return S_OK for the
497 * corresponding IIDs.
498 *
499 * On all platforms, the @a C template argument must be a subclass of
500 * SupportErrorInfoBase and also define the following method: <tt>public static
501 * const char *ComponentName()</tt> that will be used as a value returned by the
502 * SupportErrorInfoBase::componentName() implementation.
503 *
504 * If SupportErrorInfoBase is used as a base for an intermediate component base
505 * class FooBase then the final component FooFinal that inherits FooBase should
506 * use this template class as follows:
507 * <code>
508 * class FooFinal : public SupportErrorInfoDerived <FooBase, FooFinal, IFoo>
509 * {
510 * ...
511 * };
512 * </code>
513 *
514 * Note that if you don not use intermediate component base classes, you should
515 * use the SupportErrorInfoImpl class as a base for your component instead.
516 *
517 * @param B Intermediate component base derived from SupportErrorInfoBase.
518 * @param C Component class that implements one or more COM interfaces.
519 * @param I Default interface for the component (for short #setError()
520 * versions).
521 */
522template <class B, class C, class I>
523class ATL_NO_VTABLE SupportErrorInfoDerived : public B
524#if !defined (VBOX_WITH_XPCOM)
525 , public ISupportErrorInfo
526#endif
527{
528public:
529
530#if !defined (VBOX_WITH_XPCOM)
531 STDMETHOD(InterfaceSupportsErrorInfo) (REFIID aIID)
532 {
533 const _ATL_INTMAP_ENTRY* pEntries = C::_GetEntries();
534 Assert (pEntries);
535 if (!pEntries)
536 return S_FALSE;
537
538 BOOL bSupports = FALSE;
539 BOOL bISupportErrorInfoFound = FALSE;
540
541 while (pEntries->pFunc != NULL && !bSupports)
542 {
543 if (!bISupportErrorInfoFound)
544 {
545 /* skip the COM map entries until ISupportErrorInfo is found */
546 bISupportErrorInfoFound =
547 InlineIsEqualGUID (*(pEntries->piid), IID_ISupportErrorInfo);
548 }
549 else
550 {
551 /* look for the requested interface in the rest of the com map */
552 bSupports = InlineIsEqualGUID (*(pEntries->piid), aIID);
553 }
554 pEntries++;
555 }
556
557 Assert (bISupportErrorInfoFound);
558
559 return bSupports ? S_OK : S_FALSE;
560 }
561#endif /* !defined (VBOX_WITH_XPCOM) */
562
563protected:
564
565 virtual const GUID &mainInterfaceID() const { return COM_IIDOF (I); }
566
567 virtual const char *componentName() const { return C::ComponentName(); }
568};
569
570////////////////////////////////////////////////////////////////////////////////
571
572/**
573 * The SupportErrorInfoImpl template class provides complete error info support
574 * for COM component classes.
575 *
576 * Complete error info support includes what both SupportErrorInfoBase and
577 * SupportErrorInfoDerived provide, e.g. a variety of setError() methods to
578 * set extended error information from within a component's method
579 * implementation and all necessary additional interface implementations (see
580 * descriptions of these classes for details).
581 *
582 * To add error info support to a Foo component that implements a IFoo
583 * interface, use the following pattern:
584 * <code>
585 * class Foo : public SupportErrorInfoImpl <Foo, IFoo>
586 * {
587 * public:
588 *
589 * ...
590 *
591 * static const char *ComponentName() const { return "Foo"; }
592 * };
593 * </code>
594 *
595 * Note that your component class (the @a C template argument) must provide the
596 * ComponentName() implementation as shown above.
597 *
598 * @param C Component class that implements one or more COM interfaces.
599 * @param I Default interface for the component (for short #setError()
600 * versions).
601 */
602template <class C, class I>
603class ATL_NO_VTABLE SupportErrorInfoImpl
604 : public SupportErrorInfoDerived <SupportErrorInfoBase, C, I>
605{
606};
607
608} /* namespace com */
609
610#endif /* ___VBox_com_SupportErrorInfo_h */
611
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