VirtualBox

source: vbox/trunk/include/VBox/com/MultiResult.h@ 32781

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

Main: remove VirtualBoxSupportTranslation template, add translation support to generic base class, clean up COM headers more, remove SupportErrorInfo.cpp|h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 8.3 KB
Line 
1/* $Id: MultiResult.h 30739 2010-07-08 12:27:42Z 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 <stdarg.h>
36
37namespace com
38{
39
40/**
41 * "First worst" result type.
42 *
43 * Variables of this class are used instead of HRESULT variables when it is
44 * desirable to memorize the "first worst" result code instead of the last
45 * assigned one. In other words, an assignment operation to a variable of this
46 * class will succeed only if the result code to assign has worse severity. The
47 * following table demonstrate this (the first column lists the previous result
48 * code stored in the variable, the first row lists the new result code being
49 * assigned, 'A' means the assignment will take place, '> S_OK' means a warning
50 * result code):
51 *
52 * {{{
53 * FAILED > S_OK S_OK
54 * FAILED - - -
55 * > S_OK A - -
56 * S_OK A A -
57 *
58 * }}}
59 *
60 * In practice, you will need to use a FWResult variable when you call some COM
61 * method B after another COM method A fails and want to return the result code
62 * of A even if B also fails, but want to return the failed result code of B if
63 * A issues a warning or succeeds.
64 */
65class FWResult
66{
67
68public:
69
70 /**
71 * Constructs a new variable. Note that by default this constructor sets the
72 * result code to E_FAIL to make sure a failure is returned to the caller if
73 * the variable is never assigned another value (which is considered as the
74 * improper use of this class).
75 */
76 FWResult (HRESULT aRC = E_FAIL) : mRC (aRC) {}
77
78 FWResult &operator= (HRESULT aRC)
79 {
80 if ((FAILED (aRC) && !FAILED (mRC)) ||
81 (mRC == S_OK && aRC != S_OK))
82 mRC = aRC;
83
84 return *this;
85 }
86
87 operator HRESULT() const { return mRC; }
88
89 HRESULT *operator&() { return &mRC; }
90
91private:
92
93 HRESULT mRC;
94};
95
96/**
97 * The MultiResult class is a com::FWResult enhancement that also acts as a
98 * switch to turn on multi-error mode for SupportErrorInfo::setError() and
99 * SupportErrorInfo::setWarning() calls.
100 *
101 * When an instance of this class is created, multi-error mode is turned on
102 * for the current thread and the turn-on counter is increased by one. In
103 * multi-error mode, a call to setError() or setWarning() does not
104 * overwrite the current error or warning info object possibly set on the
105 * current thread by other method calls, but instead it stores this old
106 * object in the IVirtualBoxErrorInfo::next attribute of the new error
107 * object being set.
108 *
109 * This way, error/warning objects are stacked together and form a chain of
110 * errors where the most recent error is the first one retrieved by the
111 * calling party, the preceding error is what the
112 * IVirtualBoxErrorInfo::next attribute of the first error points to, and so
113 * on, up to the first error or warning occurred which is the last in the
114 * chain. See IVirtualBoxErrorInfo documentation for more info.
115 *
116 * When the instance of the MultiResult class goes out of scope and gets
117 * destroyed, it automatically decreases the turn-on counter by one. If
118 * the counter drops to zero, multi-error mode for the current thread is
119 * turned off and the thread switches back to single-error mode where every
120 * next error or warning object overwrites the previous one.
121 *
122 * Note that the caller of a COM method uses a non-S_OK result code to
123 * decide if the method has returned an error (negative codes) or a warning
124 * (positive non-zero codes) and will query extended error info only in
125 * these two cases. However, since multi-error mode implies that the method
126 * doesn't return control return to the caller immediately after the first
127 * error or warning but continues its execution, the functionality provided
128 * by the base com::FWResult class becomes very useful because it allows to
129 * preserve the error or the warning result code even if it is later assigned
130 * a S_OK value multiple times. See com::FWResult for details.
131 *
132 * Here is the typical usage pattern:
133 * <code>
134
135 HRESULT Bar::method()
136 {
137 // assume multi-errors are turned off here...
138
139 if (something)
140 {
141 // Turn on multi-error mode and make sure severity is preserved
142 MultiResult rc = foo->method1();
143
144 // return on fatal error, but continue on warning or on success
145 CheckComRCReturnRC (rc);
146
147 rc = foo->method2();
148 // no matter what result, stack it and continue
149
150 // ...
151
152 // return the last worst result code (it will be preserved even if
153 // foo->method2() returns S_OK.
154 return rc;
155 }
156
157 // multi-errors are turned off here again...
158
159 return S_OK;
160 }
161
162 * </code>
163 *
164 * @note This class is intended to be instantiated on the stack, therefore
165 * You cannot create them using new(). Although it is possible to copy
166 * instances of MultiResult or return them by value, please never do
167 * that as it is breaks the class semantics (and will assert);
168 */
169class MultiResult : public FWResult
170{
171public:
172
173 /**
174 * @copydoc FWResult::FWResult().
175 */
176 MultiResult (HRESULT aRC = E_FAIL) : FWResult (aRC) { incCounter(); }
177
178 MultiResult (const MultiResult &aThat) : FWResult (aThat)
179 {
180 /* We need this copy constructor only for GCC that wants to have
181 * it in case of expressions like |MultiResult rc = E_FAIL;|. But
182 * we assert since the optimizer should actually avoid the
183 * temporary and call the other constructor directly instead. */
184 AssertFailed();
185 }
186
187 ~MultiResult() { decCounter(); }
188
189 MultiResult &operator= (HRESULT aRC)
190 {
191 FWResult::operator= (aRC);
192 return *this;
193 }
194
195 MultiResult &operator= (const MultiResult & /* aThat */)
196 {
197 /* We need this copy constructor only for GCC that wants to have
198 * it in case of expressions like |MultiResult rc = E_FAIL;|. But
199 * we assert since the optimizer should actually avoid the
200 * temporary and call the other constructor directly instead. */
201 AssertFailed();
202 return *this;
203 }
204
205 /**
206 * Returns true if multi-mode is enabled for the current thread (i.e. at
207 * least one MultiResult instance exists on the stack somewhere).
208 * @return
209 */
210 static bool isMultiEnabled();
211
212private:
213
214 DECLARE_CLS_NEW_DELETE_NOOP(MultiResult)
215
216 static void incCounter();
217 static void decCounter();
218
219 static RTTLS sCounter;
220
221 friend class MultiResultRef;
222};
223
224/**
225 * The MultiResultRef class is equivalent to MultiResult except that it takes
226 * a reference to the existing HRESULT variable instead of maintaining its own
227 * one.
228 */
229class MultiResultRef
230{
231public:
232
233 MultiResultRef (HRESULT &aRC) : mRC (aRC) { MultiResult::incCounter(); }
234
235 ~MultiResultRef() { MultiResult::decCounter(); }
236
237 MultiResultRef &operator= (HRESULT aRC)
238 {
239 /* Copied from FWResult */
240 if ((FAILED (aRC) && !FAILED (mRC)) ||
241 (mRC == S_OK && aRC != S_OK))
242 mRC = aRC;
243
244 return *this;
245 }
246
247 operator HRESULT() const { return mRC; }
248
249 HRESULT *operator&() { return &mRC; }
250
251private:
252
253 DECLARE_CLS_NEW_DELETE_NOOP (MultiResultRef)
254
255 HRESULT &mRC;
256};
257
258
259} /* namespace com */
260
261#endif /* ___VBox_com_SupportErrorInfo_h */
262
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