1 | /* $Id: SupportErrorInfo.h 30714 2010-07-07 16:20:03Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | * MS COM / XPCOM Abstraction Layer:
|
---|
5 | * SupportErrorInfo* class family declarations
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008-2009 Oracle Corporation
|
---|
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 |
|
---|
29 | #ifndef ___VBox_com_SupportErrorInfo_h
|
---|
30 | #define ___VBox_com_SupportErrorInfo_h
|
---|
31 |
|
---|
32 | #include "VBox/com/defs.h"
|
---|
33 | #include "VBox/com/string.h"
|
---|
34 |
|
---|
35 | #include <iprt/cdefs.h>
|
---|
36 |
|
---|
37 | #include <stdarg.h>
|
---|
38 |
|
---|
39 | #if !defined (VBOX_WITH_XPCOM)
|
---|
40 | interface IVirtualBoxErrorInfo;
|
---|
41 | #else
|
---|
42 | class IVirtualBoxErrorInfo;
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | namespace com
|
---|
46 | {
|
---|
47 |
|
---|
48 | /**
|
---|
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.
|
---|
52 | *
|
---|
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
|
---|
59 | * object being set.
|
---|
60 | *
|
---|
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.
|
---|
67 | *
|
---|
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.
|
---|
73 | *
|
---|
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.
|
---|
83 | *
|
---|
84 | * Here is the typical usage pattern:
|
---|
85 | * <code>
|
---|
86 |
|
---|
87 | HRESULT Bar::method()
|
---|
88 | {
|
---|
89 | // assume multi-errors are turned off here...
|
---|
90 |
|
---|
91 | if (something)
|
---|
92 | {
|
---|
93 | // Turn on multi-error mode and make sure severity is preserved
|
---|
94 | MultiResult rc = foo->method1();
|
---|
95 |
|
---|
96 | // return on fatal error, but continue on warning or on success
|
---|
97 | CheckComRCReturnRC (rc);
|
---|
98 |
|
---|
99 | rc = foo->method2();
|
---|
100 | // no matter what result, stack it and continue
|
---|
101 |
|
---|
102 | // ...
|
---|
103 |
|
---|
104 | // return the last worst result code (it will be preserved even if
|
---|
105 | // foo->method2() returns S_OK.
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 | // multi-errors are turned off here again...
|
---|
110 |
|
---|
111 | return S_OK;
|
---|
112 | }
|
---|
113 |
|
---|
114 | * </code>
|
---|
115 | *
|
---|
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);
|
---|
120 | */
|
---|
121 | class MultiResult : public FWResult
|
---|
122 | {
|
---|
123 | public:
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * @copydoc FWResult::FWResult().
|
---|
127 | */
|
---|
128 | MultiResult (HRESULT aRC = E_FAIL) : FWResult (aRC) { incCounter(); }
|
---|
129 |
|
---|
130 | MultiResult (const MultiResult &aThat) : FWResult (aThat)
|
---|
131 | {
|
---|
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. */
|
---|
136 | AssertFailed();
|
---|
137 | }
|
---|
138 |
|
---|
139 | ~MultiResult() { decCounter(); }
|
---|
140 |
|
---|
141 | MultiResult &operator= (HRESULT aRC)
|
---|
142 | {
|
---|
143 | FWResult::operator= (aRC);
|
---|
144 | return *this;
|
---|
145 | }
|
---|
146 |
|
---|
147 | MultiResult &operator= (const MultiResult & /* aThat */)
|
---|
148 | {
|
---|
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. */
|
---|
153 | AssertFailed();
|
---|
154 | return *this;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Returns true if multi-mode is enabled for the current thread (i.e. at
|
---|
159 | * least one MultiResult instance exists on the stack somewhere).
|
---|
160 | * @return
|
---|
161 | */
|
---|
162 | static bool isMultiEnabled();
|
---|
163 |
|
---|
164 | private:
|
---|
165 |
|
---|
166 | DECLARE_CLS_NEW_DELETE_NOOP (MultiResult)
|
---|
167 |
|
---|
168 | static void incCounter();
|
---|
169 | static void decCounter();
|
---|
170 |
|
---|
171 | static RTTLS sCounter;
|
---|
172 |
|
---|
173 | friend class MultiResultRef;
|
---|
174 | };
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * The MultiResultRef class is equivalent to MultiResult except that it takes
|
---|
178 | * a reference to the existing HRESULT variable instead of maintaining its own
|
---|
179 | * one.
|
---|
180 | */
|
---|
181 | class MultiResultRef
|
---|
182 | {
|
---|
183 | public:
|
---|
184 |
|
---|
185 | MultiResultRef (HRESULT &aRC) : mRC (aRC) { MultiResult::incCounter(); }
|
---|
186 |
|
---|
187 | ~MultiResultRef() { MultiResult::decCounter(); }
|
---|
188 |
|
---|
189 | MultiResultRef &operator= (HRESULT aRC)
|
---|
190 | {
|
---|
191 | /* Copied from FWResult */
|
---|
192 | if ((FAILED (aRC) && !FAILED (mRC)) ||
|
---|
193 | (mRC == S_OK && aRC != S_OK))
|
---|
194 | mRC = aRC;
|
---|
195 |
|
---|
196 | return *this;
|
---|
197 | }
|
---|
198 |
|
---|
199 | operator HRESULT() const { return mRC; }
|
---|
200 |
|
---|
201 | HRESULT *operator&() { return &mRC; }
|
---|
202 |
|
---|
203 | private:
|
---|
204 |
|
---|
205 | DECLARE_CLS_NEW_DELETE_NOOP (MultiResultRef)
|
---|
206 |
|
---|
207 | HRESULT &mRC;
|
---|
208 | };
|
---|
209 |
|
---|
210 |
|
---|
211 | } /* namespace com */
|
---|
212 |
|
---|
213 | #endif /* ___VBox_com_SupportErrorInfo_h */
|
---|
214 |
|
---|