1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox COM base classes definition
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef ____H_VIRTUALBOXBASEIMPL
|
---|
23 | #define ____H_VIRTUALBOXBASEIMPL
|
---|
24 |
|
---|
25 | #include "VBox/com/string.h"
|
---|
26 | #include "VBox/com/Guid.h"
|
---|
27 | #include "VBox/com/ptr.h"
|
---|
28 | #include "VBox/com/ErrorInfo.h"
|
---|
29 |
|
---|
30 | #include "VBox/com/VirtualBox.h"
|
---|
31 |
|
---|
32 | #include <VBox/settings.h>
|
---|
33 |
|
---|
34 | #include "AutoLock.h"
|
---|
35 |
|
---|
36 | using namespace com;
|
---|
37 | using namespace util;
|
---|
38 |
|
---|
39 | #include <iprt/cdefs.h>
|
---|
40 | #include <iprt/critsect.h>
|
---|
41 | #include <iprt/thread.h>
|
---|
42 |
|
---|
43 | #include <list>
|
---|
44 | #include <map>
|
---|
45 |
|
---|
46 | #if !defined (VBOX_WITH_XPCOM)
|
---|
47 |
|
---|
48 | #include <atlcom.h>
|
---|
49 |
|
---|
50 | /* use a special version of the singleton class factory,
|
---|
51 | * see KB811591 in msdn for more info. */
|
---|
52 |
|
---|
53 | #undef DECLARE_CLASSFACTORY_SINGLETON
|
---|
54 | #define DECLARE_CLASSFACTORY_SINGLETON(obj) DECLARE_CLASSFACTORY_EX(CMyComClassFactorySingleton<obj>)
|
---|
55 |
|
---|
56 | template <class T>
|
---|
57 | class CMyComClassFactorySingleton : public CComClassFactory
|
---|
58 | {
|
---|
59 | public:
|
---|
60 | CMyComClassFactorySingleton() : m_hrCreate(S_OK){}
|
---|
61 | virtual ~CMyComClassFactorySingleton(){}
|
---|
62 | // IClassFactory
|
---|
63 | STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObj)
|
---|
64 | {
|
---|
65 | HRESULT hRes = E_POINTER;
|
---|
66 | if (ppvObj != NULL)
|
---|
67 | {
|
---|
68 | *ppvObj = NULL;
|
---|
69 | // Aggregation is not supported in singleton objects.
|
---|
70 | ATLASSERT(pUnkOuter == NULL);
|
---|
71 | if (pUnkOuter != NULL)
|
---|
72 | hRes = CLASS_E_NOAGGREGATION;
|
---|
73 | else
|
---|
74 | {
|
---|
75 | if (m_hrCreate == S_OK && m_spObj == NULL)
|
---|
76 | {
|
---|
77 | Lock();
|
---|
78 | __try
|
---|
79 | {
|
---|
80 | // Fix: The following If statement was moved inside the __try statement.
|
---|
81 | // Did another thread arrive here first?
|
---|
82 | if (m_hrCreate == S_OK && m_spObj == NULL)
|
---|
83 | {
|
---|
84 | // lock the module to indicate activity
|
---|
85 | // (necessary for the monitor shutdown thread to correctly
|
---|
86 | // terminate the module in case when CreateInstance() fails)
|
---|
87 | _pAtlModule->Lock();
|
---|
88 | CComObjectCached<T> *p;
|
---|
89 | m_hrCreate = CComObjectCached<T>::CreateInstance(&p);
|
---|
90 | if (SUCCEEDED(m_hrCreate))
|
---|
91 | {
|
---|
92 | m_hrCreate = p->QueryInterface(IID_IUnknown, (void**)&m_spObj);
|
---|
93 | if (FAILED(m_hrCreate))
|
---|
94 | {
|
---|
95 | delete p;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | _pAtlModule->Unlock();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | __finally
|
---|
102 | {
|
---|
103 | Unlock();
|
---|
104 | }
|
---|
105 | }
|
---|
106 | if (m_hrCreate == S_OK)
|
---|
107 | {
|
---|
108 | hRes = m_spObj->QueryInterface(riid, ppvObj);
|
---|
109 | }
|
---|
110 | else
|
---|
111 | {
|
---|
112 | hRes = m_hrCreate;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return hRes;
|
---|
117 | }
|
---|
118 | HRESULT m_hrCreate;
|
---|
119 | CComPtr<IUnknown> m_spObj;
|
---|
120 | };
|
---|
121 |
|
---|
122 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
123 |
|
---|
124 | // macros
|
---|
125 | ////////////////////////////////////////////////////////////////////////////////
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Special version of the Assert macro to be used within VirtualBoxBase
|
---|
129 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
130 | *
|
---|
131 | * In the debug build, this macro is equivalent to Assert.
|
---|
132 | * In the release build, this macro uses |setError (E_FAIL, ...)| to set the
|
---|
133 | * error info from the asserted expression.
|
---|
134 | *
|
---|
135 | * @see VirtualBoxSupportErrorInfoImpl::setError
|
---|
136 | *
|
---|
137 | * @param expr Expression which should be true.
|
---|
138 | */
|
---|
139 | #if defined (DEBUG)
|
---|
140 | #define ComAssert(expr) Assert (expr)
|
---|
141 | #else
|
---|
142 | #define ComAssert(expr) \
|
---|
143 | do { \
|
---|
144 | if (!(expr)) \
|
---|
145 | setError (E_FAIL, "Assertion failed: [%s] at '%s' (%d) in %s.\n" \
|
---|
146 | "Please contact the product vendor!", \
|
---|
147 | #expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
|
---|
148 | } while (0)
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Special version of the AssertMsg macro to be used within VirtualBoxBase
|
---|
153 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
154 | *
|
---|
155 | * See ComAssert for more info.
|
---|
156 | *
|
---|
157 | * @param expr Expression which should be true.
|
---|
158 | * @param a printf argument list (in parenthesis).
|
---|
159 | */
|
---|
160 | #if defined (DEBUG)
|
---|
161 | #define ComAssertMsg(expr, a) AssertMsg (expr, a)
|
---|
162 | #else
|
---|
163 | #define ComAssertMsg(expr, a) \
|
---|
164 | do { \
|
---|
165 | if (!(expr)) \
|
---|
166 | setError (E_FAIL, "Assertion failed: [%s] at '%s' (%d) in %s.\n" \
|
---|
167 | "%s.\n" \
|
---|
168 | "Please contact the product vendor!", \
|
---|
169 | #expr, __FILE__, __LINE__, __PRETTY_FUNCTION__, Utf8StrFmt a .raw()); \
|
---|
170 | } while (0)
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Special version of the AssertRC macro to be used within VirtualBoxBase
|
---|
175 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
176 | *
|
---|
177 | * See ComAssert for more info.
|
---|
178 | *
|
---|
179 | * @param vrc VBox status code.
|
---|
180 | */
|
---|
181 | #if defined (DEBUG)
|
---|
182 | #define ComAssertRC(vrc) AssertRC (vrc)
|
---|
183 | #else
|
---|
184 | #define ComAssertRC(vrc) ComAssertMsgRC (vrc, ("%Vra", vrc))
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Special version of the AssertMsgRC macro to be used within VirtualBoxBase
|
---|
189 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
190 | *
|
---|
191 | * See ComAssert for more info.
|
---|
192 | *
|
---|
193 | * @param vrc VBox status code.
|
---|
194 | * @param msg printf argument list (in parenthesis).
|
---|
195 | */
|
---|
196 | #if defined (DEBUG)
|
---|
197 | #define ComAssertMsgRC(vrc, msg) AssertMsgRC (vrc, msg)
|
---|
198 | #else
|
---|
199 | #define ComAssertMsgRC(vrc, msg) ComAssertMsg (VBOX_SUCCESS (vrc), msg)
|
---|
200 | #endif
|
---|
201 |
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Special version of the AssertFailed macro to be used within VirtualBoxBase
|
---|
205 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
206 | *
|
---|
207 | * See ComAssert for more info.
|
---|
208 | */
|
---|
209 | #if defined (DEBUG)
|
---|
210 | #define ComAssertFailed() AssertFailed()
|
---|
211 | #else
|
---|
212 | #define ComAssertFailed() \
|
---|
213 | do { \
|
---|
214 | setError (E_FAIL, "Assertion failed at '%s' (%d) in %s.\n" \
|
---|
215 | "Please contact the product vendor!", \
|
---|
216 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \
|
---|
217 | } while (0)
|
---|
218 | #endif
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Special version of the AssertMsgFailed macro to be used within VirtualBoxBase
|
---|
222 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
223 | *
|
---|
224 | * See ComAssert for more info.
|
---|
225 | *
|
---|
226 | * @param a printf argument list (in parenthesis).
|
---|
227 | */
|
---|
228 | #if defined (DEBUG)
|
---|
229 | #define ComAssertMsgFailed(a) AssertMsgFailed(a)
|
---|
230 | #else
|
---|
231 | #define ComAssertMsgFailed(a) \
|
---|
232 | do { \
|
---|
233 | setError (E_FAIL, "Assertion failed at '%s' (%d) in %s.\n" \
|
---|
234 | "%s.\n" \
|
---|
235 | "Please contact the product vendor!", \
|
---|
236 | __FILE__, __LINE__, __PRETTY_FUNCTION__, Utf8StrFmt a .raw()); \
|
---|
237 | } while (0)
|
---|
238 | #endif
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Special version of the ComAssertMsgFailed macro that additionally takes
|
---|
242 | * line number, file and function arguments to inject an assertion position
|
---|
243 | * that differs from the position where this macro is instantiated.
|
---|
244 | *
|
---|
245 | * @param a printf argument list (in parenthesis).
|
---|
246 | * @param file, line, func Line number (int), file and function (const char *).
|
---|
247 | */
|
---|
248 | #if defined (DEBUG)
|
---|
249 | #define ComAssertMsgFailedPos(a, file, line, func) \
|
---|
250 | do { \
|
---|
251 | AssertMsg1 ((const char *) 0, line, file, func); \
|
---|
252 | AssertMsg2 a; \
|
---|
253 | AssertBreakpoint(); \
|
---|
254 | } while (0)
|
---|
255 | #else
|
---|
256 | #define ComAssertMsgFailedPos(a, file, line, func) \
|
---|
257 | do { \
|
---|
258 | setError (E_FAIL, \
|
---|
259 | "Assertion failed at '%s' (%d) in %s.\n" \
|
---|
260 | "%s.\n" \
|
---|
261 | "Please contact the product vendor!", \
|
---|
262 | file, line, func, Utf8StrFmt a .raw()); \
|
---|
263 | } while (0)
|
---|
264 | #endif
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Special version of the AssertComRC macro to be used within VirtualBoxBase
|
---|
268 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
269 | *
|
---|
270 | * See ComAssert for more info.
|
---|
271 | *
|
---|
272 | * @param rc COM result code
|
---|
273 | */
|
---|
274 | #if defined (DEBUG)
|
---|
275 | #define ComAssertComRC(rc) AssertComRC (rc)
|
---|
276 | #else
|
---|
277 | #define ComAssertComRC(rc) ComAssertMsg (SUCCEEDED (rc), ("COM RC = %Rhrc (0x%08X)\n", (rc), (rc)))
|
---|
278 | #endif
|
---|
279 |
|
---|
280 |
|
---|
281 | /** Special version of ComAssert that returns ret if expr fails */
|
---|
282 | #define ComAssertRet(expr, ret) \
|
---|
283 | do { ComAssert (expr); if (!(expr)) return (ret); } while (0)
|
---|
284 | /** Special version of ComAssertMsg that returns ret if expr fails */
|
---|
285 | #define ComAssertMsgRet(expr, a, ret) \
|
---|
286 | do { ComAssertMsg (expr, a); if (!(expr)) return (ret); } while (0)
|
---|
287 | /** Special version of ComAssertRC that returns ret if vrc does not succeed */
|
---|
288 | #define ComAssertRCRet(vrc, ret) \
|
---|
289 | do { ComAssertRC (vrc); if (!VBOX_SUCCESS (vrc)) return (ret); } while (0)
|
---|
290 | /** Special version of ComAssertMsgRC that returns ret if vrc does not succeed */
|
---|
291 | #define ComAssertMsgRCRet(vrc, msg, ret) \
|
---|
292 | do { ComAssertMsgRC (vrc, msg); if (!VBOX_SUCCESS (vrc)) return (ret); } while (0)
|
---|
293 | /** Special version of ComAssertFailed that returns ret */
|
---|
294 | #define ComAssertFailedRet(ret) \
|
---|
295 | do { ComAssertFailed(); return (ret); } while (0)
|
---|
296 | /** Special version of ComAssertMsgFailed that returns ret */
|
---|
297 | #define ComAssertMsgFailedRet(msg, ret) \
|
---|
298 | do { ComAssertMsgFailed (msg); return (ret); } while (0)
|
---|
299 | /** Special version of ComAssertComRC that returns ret if rc does not succeed */
|
---|
300 | #define ComAssertComRCRet(rc, ret) \
|
---|
301 | do { ComAssertComRC (rc); if (!SUCCEEDED (rc)) return (ret); } while (0)
|
---|
302 | /** Special version of ComAssertComRC that returns rc if rc does not succeed */
|
---|
303 | #define ComAssertComRCRetRC(rc) \
|
---|
304 | do { ComAssertComRC (rc); if (!SUCCEEDED (rc)) return (rc); } while (0)
|
---|
305 |
|
---|
306 |
|
---|
307 | /** Special version of ComAssert that evaulates eval and breaks if expr fails */
|
---|
308 | #define ComAssertBreak(expr, eval) \
|
---|
309 | if (1) { ComAssert (expr); if (!(expr)) { eval; break; } } else do {} while (0)
|
---|
310 | /** Special version of ComAssertMsg that evaulates eval and breaks if expr fails */
|
---|
311 | #define ComAssertMsgBreak(expr, a, eval) \
|
---|
312 | if (1) { ComAssertMsg (expr, a); if (!(expr)) { eval; break; } } else do {} while (0)
|
---|
313 | /** Special version of ComAssertRC that evaulates eval and breaks if vrc does not succeed */
|
---|
314 | #define ComAssertRCBreak(vrc, eval) \
|
---|
315 | if (1) { ComAssertRC (vrc); if (!VBOX_SUCCESS (vrc)) { eval; break; } } else do {} while (0)
|
---|
316 | /** Special version of ComAssertMsgRC that evaulates eval and breaks if vrc does not succeed */
|
---|
317 | #define ComAssertMsgRCBreak(vrc, msg, eval) \
|
---|
318 | if (1) { ComAssertMsgRC (vrc, msg); if (!VBOX_SUCCESS (vrc)) { eval; break; } } else do {} while (0)
|
---|
319 | /** Special version of ComAssertFailed that evaulates eval and breaks */
|
---|
320 | #define ComAssertFailedBreak(eval) \
|
---|
321 | if (1) { ComAssertFailed(); { eval; break; } } else do {} while (0)
|
---|
322 | /** Special version of ComAssertMsgFailed that evaulates eval and breaks */
|
---|
323 | #define ComAssertMsgFailedBreak(msg, eval) \
|
---|
324 | if (1) { ComAssertMsgFailed (msg); { eval; break; } } else do {} while (0)
|
---|
325 | /** Special version of ComAssertComRC that evaulates eval and breaks if rc does not succeed */
|
---|
326 | #define ComAssertComRCBreak(rc, eval) \
|
---|
327 | if (1) { ComAssertComRC (rc); if (!SUCCEEDED (rc)) { eval; break; } } else do {} while (0)
|
---|
328 | /** Special version of ComAssertComRC that just breaks if rc does not succeed */
|
---|
329 | #define ComAssertComRCBreakRC(rc) \
|
---|
330 | if (1) { ComAssertComRC (rc); if (!SUCCEEDED (rc)) { break; } } else do {} while (0)
|
---|
331 |
|
---|
332 |
|
---|
333 | /** Special version of ComAssert that evaulates eval and throws it if expr fails */
|
---|
334 | #define ComAssertThrow(expr, eval) \
|
---|
335 | if (1) { ComAssert (expr); if (!(expr)) { throw (eval); } } else do {} while (0)
|
---|
336 | /** Special version of ComAssertMsg that evaulates eval and throws it if expr fails */
|
---|
337 | #define ComAssertMsgThrow(expr, a, eval) \
|
---|
338 | if (1) { ComAssertMsg (expr, a); if (!(expr)) { throw (eval); } } else do {} while (0)
|
---|
339 | /** Special version of ComAssertRC that evaulates eval and throws it if vrc does not succeed */
|
---|
340 | #define ComAssertRCThrow(vrc, eval) \
|
---|
341 | if (1) { ComAssertRC (vrc); if (!VBOX_SUCCESS (vrc)) { throw (eval); } } else do {} while (0)
|
---|
342 | /** Special version of ComAssertMsgRC that evaulates eval and throws it if vrc does not succeed */
|
---|
343 | #define ComAssertMsgRCThrow(vrc, msg, eval) \
|
---|
344 | if (1) { ComAssertMsgRC (vrc, msg); if (!VBOX_SUCCESS (vrc)) { throw (eval); } } else do {} while (0)
|
---|
345 | /** Special version of ComAssertFailed that evaulates eval and throws it */
|
---|
346 | #define ComAssertFailedThrow(eval) \
|
---|
347 | if (1) { ComAssertFailed(); { throw (eval); } } else do {} while (0)
|
---|
348 | /** Special version of ComAssertMsgFailed that evaulates eval and throws it */
|
---|
349 | #define ComAssertMsgFailedThrow(msg, eval) \
|
---|
350 | if (1) { ComAssertMsgFailed (msg); { throw (eval); } } else do {} while (0)
|
---|
351 | /** Special version of ComAssertComRC that evaulates eval and throws it if rc does not succeed */
|
---|
352 | #define ComAssertComRCThrow(rc, eval) \
|
---|
353 | if (1) { ComAssertComRC (rc); if (!SUCCEEDED (rc)) { throw (eval); } } else do {} while (0)
|
---|
354 | /** Special version of ComAssertComRC that just throws rc if rc does not succeed */
|
---|
355 | #define ComAssertComRCThrowRC(rc) \
|
---|
356 | if (1) { ComAssertComRC (rc); if (!SUCCEEDED (rc)) { throw rc; } } else do {} while (0)
|
---|
357 |
|
---|
358 |
|
---|
359 | /// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
|
---|
360 | /**
|
---|
361 | * Checks whether this object is ready or not. Objects are typically ready
|
---|
362 | * after they are successfully created by their parent objects and become
|
---|
363 | * not ready when the respective parent itsef becomes not ready or gets
|
---|
364 | * destroyed while a reference to the child is still held by the caller
|
---|
365 | * (which prevents it from destruction).
|
---|
366 | *
|
---|
367 | * When this object is not ready, the macro sets error info and returns
|
---|
368 | * E_UNEXPECTED (the translatable error message is defined in null context).
|
---|
369 | * Otherwise, the macro does nothing.
|
---|
370 | *
|
---|
371 | * This macro <b>must</b> be used at the beginning of all interface methods
|
---|
372 | * (right after entering the class lock) in classes derived from both
|
---|
373 | * VirtualBoxBase and VirtualBoxSupportErrorInfoImpl.
|
---|
374 | */
|
---|
375 | #define CHECK_READY() \
|
---|
376 | do { \
|
---|
377 | if (!isReady()) \
|
---|
378 | return setError (E_UNEXPECTED, tr ("The object is not ready")); \
|
---|
379 | } while (0)
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Declares an empty construtor and destructor for the given class.
|
---|
383 | * This is useful to prevent the compiler from generating the default
|
---|
384 | * ctor and dtor, which in turn allows to use forward class statements
|
---|
385 | * (instead of including their header files) when declaring data members of
|
---|
386 | * non-fundamental types with constructors (which are always called implicitly
|
---|
387 | * by constructors and by the destructor of the class).
|
---|
388 | *
|
---|
389 | * This macro is to be palced within (the public section of) the class
|
---|
390 | * declaration. Its counterpart, DEFINE_EMPTY_CTOR_DTOR, must be placed
|
---|
391 | * somewhere in one of the translation units (usually .cpp source files).
|
---|
392 | *
|
---|
393 | * @param cls class to declare a ctor and dtor for
|
---|
394 | */
|
---|
395 | #define DECLARE_EMPTY_CTOR_DTOR(cls) cls(); ~cls();
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Defines an empty construtor and destructor for the given class.
|
---|
399 | * See DECLARE_EMPTY_CTOR_DTOR for more info.
|
---|
400 | */
|
---|
401 | #define DEFINE_EMPTY_CTOR_DTOR(cls) \
|
---|
402 | cls::cls () {}; cls::~cls () {};
|
---|
403 |
|
---|
404 | ////////////////////////////////////////////////////////////////////////////////
|
---|
405 |
|
---|
406 | namespace stdx
|
---|
407 | {
|
---|
408 | /**
|
---|
409 | * A wrapper around the container that owns pointers it stores.
|
---|
410 | *
|
---|
411 | * @note
|
---|
412 | * Ownership is recognized only when destructing the container!
|
---|
413 | * Pointers are not deleted when erased using erase() etc.
|
---|
414 | *
|
---|
415 | * @param container
|
---|
416 | * class that meets Container requirements (for example, an instance of
|
---|
417 | * std::list<>, std::vector<> etc.). The given class must store
|
---|
418 | * pointers (for example, std::list <MyType *>).
|
---|
419 | */
|
---|
420 | template <typename container>
|
---|
421 | class ptr_container : public container
|
---|
422 | {
|
---|
423 | public:
|
---|
424 | ~ptr_container()
|
---|
425 | {
|
---|
426 | for (typename container::iterator it = container::begin();
|
---|
427 | it != container::end();
|
---|
428 | ++ it)
|
---|
429 | delete (*it);
|
---|
430 | }
|
---|
431 | };
|
---|
432 | }
|
---|
433 |
|
---|
434 | ////////////////////////////////////////////////////////////////////////////////
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Abstract base class for all component classes implementing COM
|
---|
438 | * interfaces of the VirtualBox COM library.
|
---|
439 | *
|
---|
440 | * Declares functionality that should be available in all components.
|
---|
441 | *
|
---|
442 | * Note that this class is always subclassed using the virtual keyword so
|
---|
443 | * that only one instance of its VTBL and data is present in each derived class
|
---|
444 | * even in case if VirtualBoxBaseProto appears more than once among base classes
|
---|
445 | * of the particular component as a result of multiple inheritance.
|
---|
446 | *
|
---|
447 | * This makes it possible to have intermediate base classes used by several
|
---|
448 | * components that implement some common interface fuctionality but still let
|
---|
449 | * the final component classe choose what VirtualBoxBase variant it wants to
|
---|
450 | * use.
|
---|
451 | *
|
---|
452 | * Among the basic functionality implemented by this class is the primary object
|
---|
453 | * state that indicates if the object is ready to serve the calls, and if not,
|
---|
454 | * what stage it is currently at. Here is the pirmary state diagram:
|
---|
455 | *
|
---|
456 | * +-------------------------------------------------------+
|
---|
457 | * | |
|
---|
458 | * | (InitFailed) -----------------------+ |
|
---|
459 | * | ^ | |
|
---|
460 | * v | v |
|
---|
461 | * [*] ---> NotReady ----> (InInit) -----> Ready -----> (InUninit) ----+
|
---|
462 | * ^ | ^ | ^
|
---|
463 | * | v | v |
|
---|
464 | * | Limited | (MayUninit) --> (WillUninit)
|
---|
465 | * | | | |
|
---|
466 | * +-------+ +-------+
|
---|
467 | *
|
---|
468 | * The object is fully operational only when its state is Ready. The Limited
|
---|
469 | * state means that only some vital part of the object is operational, and it
|
---|
470 | * requires some sort of reinitialization to become fully operational. The
|
---|
471 | * NotReady state means the object is basically dead: it either was not yet
|
---|
472 | * initialized after creation at all, or was uninitialized and is waiting to be
|
---|
473 | * destroyed when the last reference to it is released. All other states are
|
---|
474 | * transitional.
|
---|
475 | *
|
---|
476 | * The NotReady->InInit->Ready, NotReady->InInit->Limited and
|
---|
477 | * NotReady->InInit->InitFailed transition is done by the AutoInitSpan smart
|
---|
478 | * class.
|
---|
479 | *
|
---|
480 | * The Limited->InInit->Ready, Limited->InInit->Limited and
|
---|
481 | * Limited->InInit->InitFailed transition is done by the AutoReinitSpan smart
|
---|
482 | * class.
|
---|
483 | *
|
---|
484 | * The Ready->InUninit->NotReady, InitFailed->InUninit->NotReady and
|
---|
485 | * WillUninit->InUninit->NotReady transitions are done by the AutoUninitSpan
|
---|
486 | * smart class.
|
---|
487 | *
|
---|
488 | * The Ready->MayUninit->Ready and Ready->MayUninit->WillUninit transitions are
|
---|
489 | * done by the AutoMayUninitSpan smart class.
|
---|
490 | *
|
---|
491 | * In order to maintain the primary state integrity and declared functionality
|
---|
492 | * all subclasses must:
|
---|
493 | *
|
---|
494 | * 1) Use the above Auto*Span classes to perform state transitions. See the
|
---|
495 | * individual class doescriptions for details.
|
---|
496 | *
|
---|
497 | * 2) All public methods of subclasses (i.e. all methods that can be called
|
---|
498 | * directly, not only from within other methods of the subclass) must have a
|
---|
499 | * standard prolog as described in the AutoCaller and AutoLimitedCaller
|
---|
500 | * documentation. Alternatively, they must use addCaller()/releaseCaller()
|
---|
501 | * directly (and therefire have both the prolog and the epilog), but this is
|
---|
502 | * not recommended.
|
---|
503 | */
|
---|
504 | class ATL_NO_VTABLE VirtualBoxBaseProto : public Lockable
|
---|
505 | {
|
---|
506 | public:
|
---|
507 |
|
---|
508 | enum State { NotReady, Ready, InInit, InUninit, InitFailed, Limited,
|
---|
509 | MayUninit, WillUninit };
|
---|
510 |
|
---|
511 | protected:
|
---|
512 |
|
---|
513 | VirtualBoxBaseProto();
|
---|
514 | virtual ~VirtualBoxBaseProto();
|
---|
515 |
|
---|
516 | public:
|
---|
517 |
|
---|
518 | // util::Lockable interface
|
---|
519 | virtual RWLockHandle *lockHandle() const;
|
---|
520 |
|
---|
521 | /**
|
---|
522 | * Unintialization method.
|
---|
523 | *
|
---|
524 | * Must be called by all final implementations (component classes) when the
|
---|
525 | * last reference to the object is released, before calling the destructor.
|
---|
526 | *
|
---|
527 | * This method is also automatically called by the uninit() method of this
|
---|
528 | * object's parent if this object is a dependent child of a class derived
|
---|
529 | * from VirtualBoxBaseWithChildren (see
|
---|
530 | * VirtualBoxBaseWithChildren::addDependentChild).
|
---|
531 | *
|
---|
532 | * @note Never call this method the AutoCaller scope or after the
|
---|
533 | * #addCaller() call not paired by #releaseCaller() because it is a
|
---|
534 | * guaranteed deadlock. See AutoUninitSpan for details.
|
---|
535 | */
|
---|
536 | virtual void uninit() {}
|
---|
537 |
|
---|
538 | virtual HRESULT addCaller (State *aState = NULL, bool aLimited = false);
|
---|
539 | virtual void releaseCaller();
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * Adds a limited caller. This method is equivalent to doing
|
---|
543 | * <tt>addCaller (aState, true)</tt>, but it is preferred because provides
|
---|
544 | * better self-descriptiveness. See #addCaller() for more info.
|
---|
545 | */
|
---|
546 | HRESULT addLimitedCaller (State *aState = NULL)
|
---|
547 | {
|
---|
548 | return addCaller (aState, true /* aLimited */);
|
---|
549 | }
|
---|
550 |
|
---|
551 | /**
|
---|
552 | * Smart class that automatically increases the number of callers of the
|
---|
553 | * given VirtualBoxBase object when an instance is constructed and decreases
|
---|
554 | * it back when the created instance goes out of scope (i.e. gets destroyed).
|
---|
555 | *
|
---|
556 | * If #rc() returns a failure after the instance creation, it means that
|
---|
557 | * the managed VirtualBoxBase object is not Ready, or in any other invalid
|
---|
558 | * state, so that the caller must not use the object and can return this
|
---|
559 | * failed result code to the upper level.
|
---|
560 | *
|
---|
561 | * See VirtualBoxBase::addCaller(), VirtualBoxBase::addLimitedCaller() and
|
---|
562 | * VirtualBoxBase::releaseCaller() for more details about object callers.
|
---|
563 | *
|
---|
564 | * @param aLimited |false| if this template should use
|
---|
565 | * VirtualiBoxBase::addCaller() calls to add callers, or
|
---|
566 | * |true| if VirtualiBoxBase::addLimitedCaller() should be
|
---|
567 | * used.
|
---|
568 | *
|
---|
569 | * @note It is preferrable to use the AutoCaller and AutoLimitedCaller
|
---|
570 | * classes than specify the @a aLimited argument, for better
|
---|
571 | * self-descriptiveness.
|
---|
572 | */
|
---|
573 | template <bool aLimited>
|
---|
574 | class AutoCallerBase
|
---|
575 | {
|
---|
576 | public:
|
---|
577 |
|
---|
578 | /**
|
---|
579 | * Increases the number of callers of the given object by calling
|
---|
580 | * VirtualBoxBase::addCaller().
|
---|
581 | *
|
---|
582 | * @param aObj Object to add a caller to. If NULL, this
|
---|
583 | * instance is effectively turned to no-op (where
|
---|
584 | * rc() will return S_OK and state() will be
|
---|
585 | * NotReady).
|
---|
586 | */
|
---|
587 | AutoCallerBase (VirtualBoxBaseProto *aObj)
|
---|
588 | : mObj (aObj)
|
---|
589 | , mRC (S_OK)
|
---|
590 | , mState (NotReady)
|
---|
591 | {
|
---|
592 | if (mObj)
|
---|
593 | mRC = mObj->addCaller (&mState, aLimited);
|
---|
594 | }
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * If the number of callers was successfully increased, decreases it
|
---|
598 | * using VirtualBoxBase::releaseCaller(), otherwise does nothing.
|
---|
599 | */
|
---|
600 | ~AutoCallerBase()
|
---|
601 | {
|
---|
602 | if (mObj && SUCCEEDED (mRC))
|
---|
603 | mObj->releaseCaller();
|
---|
604 | }
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * Stores the result code returned by VirtualBoxBase::addCaller() after
|
---|
608 | * instance creation or after the last #add() call. A successful result
|
---|
609 | * code means the number of callers was successfully increased.
|
---|
610 | */
|
---|
611 | HRESULT rc() const { return mRC; }
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Returns |true| if |SUCCEEDED (rc())| is |true|, for convenience.
|
---|
615 | * |true| means the number of callers was successfully increased.
|
---|
616 | */
|
---|
617 | bool isOk() const { return SUCCEEDED (mRC); }
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Stores the object state returned by VirtualBoxBase::addCaller() after
|
---|
621 | * instance creation or after the last #add() call.
|
---|
622 | */
|
---|
623 | State state() const { return mState; }
|
---|
624 |
|
---|
625 | /**
|
---|
626 | * Temporarily decreases the number of callers of the managed object.
|
---|
627 | * May only be called if #isOk() returns |true|. Note that #rc() will
|
---|
628 | * return E_FAIL after this method succeeds.
|
---|
629 | */
|
---|
630 | void release()
|
---|
631 | {
|
---|
632 | Assert (SUCCEEDED (mRC));
|
---|
633 | if (SUCCEEDED (mRC))
|
---|
634 | {
|
---|
635 | if (mObj)
|
---|
636 | mObj->releaseCaller();
|
---|
637 | mRC = E_FAIL;
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * Restores the number of callers decreased by #release(). May only be
|
---|
643 | * called after #release().
|
---|
644 | */
|
---|
645 | void add()
|
---|
646 | {
|
---|
647 | Assert (!SUCCEEDED (mRC));
|
---|
648 | if (mObj && !SUCCEEDED (mRC))
|
---|
649 | mRC = mObj->addCaller (&mState, aLimited);
|
---|
650 | }
|
---|
651 |
|
---|
652 | /**
|
---|
653 | * Attaches another object to this caller instance.
|
---|
654 | * The previous object's caller is released before the new one is added.
|
---|
655 | *
|
---|
656 | * @param aObj New object to attach, may be @c NULL.
|
---|
657 | */
|
---|
658 | void attach (VirtualBoxBaseProto *aObj)
|
---|
659 | {
|
---|
660 | /* detect simple self-reattachment */
|
---|
661 | if (mObj != aObj)
|
---|
662 | {
|
---|
663 | if (mObj && SUCCEEDED (mRC))
|
---|
664 | release();
|
---|
665 | mObj = aObj;
|
---|
666 | add();
|
---|
667 | }
|
---|
668 | }
|
---|
669 |
|
---|
670 | /** Verbose equivalent to <tt>attach (NULL)</tt>. */
|
---|
671 | void detach() { attach (NULL); }
|
---|
672 |
|
---|
673 | private:
|
---|
674 |
|
---|
675 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoCallerBase)
|
---|
676 | DECLARE_CLS_NEW_DELETE_NOOP (AutoCallerBase)
|
---|
677 |
|
---|
678 | VirtualBoxBaseProto *mObj;
|
---|
679 | HRESULT mRC;
|
---|
680 | State mState;
|
---|
681 | };
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * Smart class that automatically increases the number of normal
|
---|
685 | * (non-limited) callers of the given VirtualBoxBase object when an instance
|
---|
686 | * is constructed and decreases it back when the created instance goes out
|
---|
687 | * of scope (i.e. gets destroyed).
|
---|
688 | *
|
---|
689 | * A typical usage pattern to declare a normal method of some object (i.e. a
|
---|
690 | * method that is valid only when the object provides its full
|
---|
691 | * functionality) is:
|
---|
692 | * <code>
|
---|
693 | * STDMETHODIMP Component::Foo()
|
---|
694 | * {
|
---|
695 | * AutoCaller autoCaller (this);
|
---|
696 | * CheckComRCReturnRC (autoCaller.rc());
|
---|
697 | * ...
|
---|
698 | * </code>
|
---|
699 | *
|
---|
700 | * Using this class is equivalent to using the AutoCallerBase template with
|
---|
701 | * the @a aLimited argument set to |false|, but this class is preferred
|
---|
702 | * because provides better self-descriptiveness.
|
---|
703 | *
|
---|
704 | * See AutoCallerBase for more information about auto caller functionality.
|
---|
705 | */
|
---|
706 | typedef AutoCallerBase <false> AutoCaller;
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Smart class that automatically increases the number of limited callers of
|
---|
710 | * the given VirtualBoxBase object when an instance is constructed and
|
---|
711 | * decreases it back when the created instance goes out of scope (i.e. gets
|
---|
712 | * destroyed).
|
---|
713 | *
|
---|
714 | * A typical usage pattern to declare a limited method of some object (i.e.
|
---|
715 | * a method that is valid even if the object doesn't provide its full
|
---|
716 | * functionality) is:
|
---|
717 | * <code>
|
---|
718 | * STDMETHODIMP Component::Bar()
|
---|
719 | * {
|
---|
720 | * AutoLimitedCaller autoCaller (this);
|
---|
721 | * CheckComRCReturnRC (autoCaller.rc());
|
---|
722 | * ...
|
---|
723 | * </code>
|
---|
724 | *
|
---|
725 | * Using this class is equivalent to using the AutoCallerBase template with
|
---|
726 | * the @a aLimited argument set to |true|, but this class is preferred
|
---|
727 | * because provides better self-descriptiveness.
|
---|
728 | *
|
---|
729 | * See AutoCallerBase for more information about auto caller functionality.
|
---|
730 | */
|
---|
731 | typedef AutoCallerBase <true> AutoLimitedCaller;
|
---|
732 |
|
---|
733 | protected:
|
---|
734 |
|
---|
735 | /**
|
---|
736 | * Smart class to enclose the state transition NotReady->InInit->Ready.
|
---|
737 | *
|
---|
738 | * The purpose of this span is to protect object initialization.
|
---|
739 | *
|
---|
740 | * Instances must be created as a stack-based variable taking |this| pointer
|
---|
741 | * as the argument at the beginning of init() methods of VirtualBoxBase
|
---|
742 | * subclasses. When this variable is created it automatically places the
|
---|
743 | * object to the InInit state.
|
---|
744 | *
|
---|
745 | * When the created variable goes out of scope (i.e. gets destroyed) then,
|
---|
746 | * depending on the result status of this initialization span, it either
|
---|
747 | * places the object to Ready or Limited state or calls the object's
|
---|
748 | * VirtualBoxBase::uninit() method which is supposed to place the object
|
---|
749 | * back to the NotReady state using the AutoUninitSpan class.
|
---|
750 | *
|
---|
751 | * The initial result status of the initialization span is determined by the
|
---|
752 | * @a aResult argument of the AutoInitSpan constructor (Result::Failed by
|
---|
753 | * default). Inside the initialization span, the success status can be set
|
---|
754 | * to Result::Succeeded using #setSucceeded(), to to Result::Limited using
|
---|
755 | * #setLimited() or to Result::Failed using #setFailed(). Please don't
|
---|
756 | * forget to set the correct success status before getting the AutoInitSpan
|
---|
757 | * variable destryed (for example, by performing an early return from
|
---|
758 | * the init() method)!
|
---|
759 | *
|
---|
760 | * Note that if an instance of this class gets constructed when the object
|
---|
761 | * is in the state other than NotReady, #isOk() returns |false| and methods
|
---|
762 | * of this class do nothing: the state transition is not performed.
|
---|
763 | *
|
---|
764 | * A typical usage pattern is:
|
---|
765 | * <code>
|
---|
766 | * HRESULT Component::init()
|
---|
767 | * {
|
---|
768 | * AutoInitSpan autoInitSpan (this);
|
---|
769 | * AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
770 | * ...
|
---|
771 | * if (FAILED (rc))
|
---|
772 | * return rc;
|
---|
773 | * ...
|
---|
774 | * if (SUCCEEDED (rc))
|
---|
775 | * autoInitSpan.setSucceeded();
|
---|
776 | * return rc;
|
---|
777 | * }
|
---|
778 | * </code>
|
---|
779 | *
|
---|
780 | * @note Never create instances of this class outside init() methods of
|
---|
781 | * VirtualBoxBase subclasses and never pass anything other than |this|
|
---|
782 | * as the argument to the constructor!
|
---|
783 | */
|
---|
784 | class AutoInitSpan
|
---|
785 | {
|
---|
786 | public:
|
---|
787 |
|
---|
788 | enum Result { Failed = 0x0, Succeeded = 0x1, Limited = 0x2 };
|
---|
789 |
|
---|
790 | AutoInitSpan (VirtualBoxBaseProto *aObj, Result aResult = Failed);
|
---|
791 | ~AutoInitSpan();
|
---|
792 |
|
---|
793 | /**
|
---|
794 | * Returns |true| if this instance has been created at the right moment
|
---|
795 | * (when the object was in the NotReady state) and |false| otherwise.
|
---|
796 | */
|
---|
797 | bool isOk() const { return mOk; }
|
---|
798 |
|
---|
799 | /**
|
---|
800 | * Sets the initialization status to Succeeded to indicates successful
|
---|
801 | * initialization. The AutoInitSpan destructor will place the managed
|
---|
802 | * VirtualBoxBase object to the Ready state.
|
---|
803 | */
|
---|
804 | void setSucceeded() { mResult = Succeeded; }
|
---|
805 |
|
---|
806 | /**
|
---|
807 | * Sets the initialization status to Succeeded to indicate limited
|
---|
808 | * (partly successful) initialization. The AutoInitSpan destructor will
|
---|
809 | * place the managed VirtualBoxBase object to the Limited state.
|
---|
810 | */
|
---|
811 | void setLimited() { mResult = Limited; }
|
---|
812 |
|
---|
813 | /**
|
---|
814 | * Sets the initialization status to Failure to indicates failed
|
---|
815 | * initialization. The AutoInitSpan destructor will place the managed
|
---|
816 | * VirtualBoxBase object to the InitFailed state and will automatically
|
---|
817 | * call its uninit() method which is supposed to place the object back
|
---|
818 | * to the NotReady state using AutoUninitSpan.
|
---|
819 | */
|
---|
820 | void setFailed() { mResult = Failed; }
|
---|
821 |
|
---|
822 | /** Returns the current initialization result. */
|
---|
823 | Result result() { return mResult; }
|
---|
824 |
|
---|
825 | private:
|
---|
826 |
|
---|
827 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoInitSpan)
|
---|
828 | DECLARE_CLS_NEW_DELETE_NOOP (AutoInitSpan)
|
---|
829 |
|
---|
830 | VirtualBoxBaseProto *mObj;
|
---|
831 | Result mResult : 3; // must be at least total number of bits + 1 (sign)
|
---|
832 | bool mOk : 1;
|
---|
833 | };
|
---|
834 |
|
---|
835 | /**
|
---|
836 | * Smart class to enclose the state transition Limited->InInit->Ready.
|
---|
837 | *
|
---|
838 | * The purpose of this span is to protect object re-initialization.
|
---|
839 | *
|
---|
840 | * Instances must be created as a stack-based variable taking |this| pointer
|
---|
841 | * as the argument at the beginning of methods of VirtualBoxBase
|
---|
842 | * subclasses that try to re-initialize the object to bring it to the Ready
|
---|
843 | * state (full functionality) after partial initialization (limited
|
---|
844 | * functionality). When this variable is created, it automatically places
|
---|
845 | * the object to the InInit state.
|
---|
846 | *
|
---|
847 | * When the created variable goes out of scope (i.e. gets destroyed),
|
---|
848 | * depending on the success status of this initialization span, it either
|
---|
849 | * places the object to the Ready state or brings it back to the Limited
|
---|
850 | * state.
|
---|
851 | *
|
---|
852 | * The initial success status of the re-initialization span is |false|. In
|
---|
853 | * order to make it successful, #setSucceeded() must be called before the
|
---|
854 | * instance is destroyed.
|
---|
855 | *
|
---|
856 | * Note that if an instance of this class gets constructed when the object
|
---|
857 | * is in the state other than Limited, #isOk() returns |false| and methods
|
---|
858 | * of this class do nothing: the state transition is not performed.
|
---|
859 | *
|
---|
860 | * A typical usage pattern is:
|
---|
861 | * <code>
|
---|
862 | * HRESULT Component::reinit()
|
---|
863 | * {
|
---|
864 | * AutoReinitSpan autoReinitSpan (this);
|
---|
865 | * AssertReturn (autoReinitSpan.isOk(), E_UNEXPECTED);
|
---|
866 | * ...
|
---|
867 | * if (FAILED (rc))
|
---|
868 | * return rc;
|
---|
869 | * ...
|
---|
870 | * if (SUCCEEDED (rc))
|
---|
871 | * autoReinitSpan.setSucceeded();
|
---|
872 | * return rc;
|
---|
873 | * }
|
---|
874 | * </code>
|
---|
875 | *
|
---|
876 | * @note Never create instances of this class outside re-initialization
|
---|
877 | * methods of VirtualBoxBase subclasses and never pass anything other than
|
---|
878 | * |this| as the argument to the constructor!
|
---|
879 | */
|
---|
880 | class AutoReinitSpan
|
---|
881 | {
|
---|
882 | public:
|
---|
883 |
|
---|
884 | AutoReinitSpan (VirtualBoxBaseProto *aObj);
|
---|
885 | ~AutoReinitSpan();
|
---|
886 |
|
---|
887 | /**
|
---|
888 | * Returns |true| if this instance has been created at the right moment
|
---|
889 | * (when the object was in the Limited state) and |false| otherwise.
|
---|
890 | */
|
---|
891 | bool isOk() const { return mOk; }
|
---|
892 |
|
---|
893 | /**
|
---|
894 | * Sets the re-initialization status to Succeeded to indicates
|
---|
895 | * successful re-initialization. The AutoReinitSpan destructor will place
|
---|
896 | * the managed VirtualBoxBase object to the Ready state.
|
---|
897 | */
|
---|
898 | void setSucceeded() { mSucceeded = true; }
|
---|
899 |
|
---|
900 | private:
|
---|
901 |
|
---|
902 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoReinitSpan)
|
---|
903 | DECLARE_CLS_NEW_DELETE_NOOP (AutoReinitSpan)
|
---|
904 |
|
---|
905 | VirtualBoxBaseProto *mObj;
|
---|
906 | bool mSucceeded : 1;
|
---|
907 | bool mOk : 1;
|
---|
908 | };
|
---|
909 |
|
---|
910 | /**
|
---|
911 | * Smart class to enclose the state transition Ready->InUnnit->NotReady,
|
---|
912 | * InitFailed->InUnnit->NotReady or WillUninit->InUnnit->NotReady.
|
---|
913 | *
|
---|
914 | * The purpose of this span is to protect object uninitialization.
|
---|
915 | *
|
---|
916 | * Instances must be created as a stack-based variable taking |this| pointer
|
---|
917 | * as the argument at the beginning of uninit() methods of VirtualBoxBase
|
---|
918 | * subclasses. When this variable is created it automatically places the
|
---|
919 | * object to the InUninit state, unless it is already in the NotReady state
|
---|
920 | * as indicated by #uninitDone() returning |true|. In the latter case, the
|
---|
921 | * uninit() method must immediately return because there should be nothing
|
---|
922 | * to uninitialize.
|
---|
923 | *
|
---|
924 | * When this variable goes out of scope (i.e. gets destroyed), it places the
|
---|
925 | * object to NotReady state.
|
---|
926 | *
|
---|
927 | * A typical usage pattern is:
|
---|
928 | * <code>
|
---|
929 | * void Component::uninit()
|
---|
930 | * {
|
---|
931 | * AutoUninitSpan autoUninitSpan (this);
|
---|
932 | * if (autoUninitSpan.uninitDone())
|
---|
933 | * retrun;
|
---|
934 | * ...
|
---|
935 | * }
|
---|
936 | * </code>
|
---|
937 | *
|
---|
938 | * @note The constructor of this class blocks the current thread execution
|
---|
939 | * until the number of callers added to the object using #addCaller()
|
---|
940 | * or AutoCaller drops to zero. For this reason, it is forbidden to
|
---|
941 | * create instances of this class (or call uninit()) within the
|
---|
942 | * AutoCaller or #addCaller() scope because it is a guaranteed
|
---|
943 | * deadlock.
|
---|
944 | *
|
---|
945 | * @note Never create instances of this class outside uninit() methods and
|
---|
946 | * never pass anything other than |this| as the argument to the
|
---|
947 | * constructor!
|
---|
948 | */
|
---|
949 | class AutoUninitSpan
|
---|
950 | {
|
---|
951 | public:
|
---|
952 |
|
---|
953 | AutoUninitSpan (VirtualBoxBaseProto *aObj);
|
---|
954 | ~AutoUninitSpan();
|
---|
955 |
|
---|
956 | /** |true| when uninit() is called as a result of init() failure */
|
---|
957 | bool initFailed() { return mInitFailed; }
|
---|
958 |
|
---|
959 | /** |true| when uninit() has already been called (so the object is NotReady) */
|
---|
960 | bool uninitDone() { return mUninitDone; }
|
---|
961 |
|
---|
962 | private:
|
---|
963 |
|
---|
964 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoUninitSpan)
|
---|
965 | DECLARE_CLS_NEW_DELETE_NOOP (AutoUninitSpan)
|
---|
966 |
|
---|
967 | VirtualBoxBaseProto *mObj;
|
---|
968 | bool mInitFailed : 1;
|
---|
969 | bool mUninitDone : 1;
|
---|
970 | };
|
---|
971 |
|
---|
972 | /**
|
---|
973 | * Smart class to enclose the state transition Ready->MayUninit->NotReady or
|
---|
974 | * Ready->MayUninit->WillUninit.
|
---|
975 | *
|
---|
976 | * The purpose of this span is to safely check if unintialization is
|
---|
977 | * possible at the given moment and seamlessly perform it if so.
|
---|
978 | *
|
---|
979 | * Instances must be created as a stack-based variable taking |this| pointer
|
---|
980 | * as the argument at the beginning of methods of VirtualBoxBase
|
---|
981 | * subclasses that want to uninitialize the object if a necessary set of
|
---|
982 | * criteria is met and leave it Ready otherwise.
|
---|
983 | *
|
---|
984 | * When this variable is created it automatically places the object to the
|
---|
985 | * MayUninit state if it is Ready, does nothing but returns |true| in
|
---|
986 | * response to #alreadyInProgress() if it is already in MayUninit, or
|
---|
987 | * returns a failure in response to #rc() in any other case. The example
|
---|
988 | * below shows how the user must react in latter two cases.
|
---|
989 | *
|
---|
990 | * When this variable goes out of scope (i.e. gets destroyed), it places the
|
---|
991 | * object back to Ready state unless #acceptUninit() is called in which case
|
---|
992 | * the object is placed to WillUninit state and uninit() is immediately
|
---|
993 | * called after that.
|
---|
994 | *
|
---|
995 | * A typical usage pattern is:
|
---|
996 | * <code>
|
---|
997 | * void Component::uninit()
|
---|
998 | * {
|
---|
999 | * AutoMayUninitSpan mayUninitSpan (this);
|
---|
1000 | * CheckComRCReturnRC (mayUninitSpan.rc());
|
---|
1001 | * if (mayUninitSpan.alreadyInProgress())
|
---|
1002 | * return S_OK;
|
---|
1003 | * ...
|
---|
1004 | * if (FAILED (rc))
|
---|
1005 | * return rc; // will go back to Ready
|
---|
1006 | * ...
|
---|
1007 | * if (SUCCEEDED (rc))
|
---|
1008 | * mayUninitSpan.acceptUninit(); // will call uninit()
|
---|
1009 | * return rc;
|
---|
1010 | * }
|
---|
1011 | * </code>
|
---|
1012 | *
|
---|
1013 | * @note The constructor of this class blocks the current thread execution
|
---|
1014 | * until the number of callers added to the object using #addCaller()
|
---|
1015 | * or AutoCaller drops to zero. For this reason, it is forbidden to
|
---|
1016 | * create instances of this class (or call uninit()) within the
|
---|
1017 | * AutoCaller or #addCaller() scope because it is a guaranteed
|
---|
1018 | * deadlock.
|
---|
1019 | */
|
---|
1020 | class AutoMayUninitSpan
|
---|
1021 | {
|
---|
1022 | public:
|
---|
1023 |
|
---|
1024 | AutoMayUninitSpan (VirtualBoxBaseProto *aObj);
|
---|
1025 | ~AutoMayUninitSpan();
|
---|
1026 |
|
---|
1027 | /**
|
---|
1028 | * Returns a failure if the AutoMayUninitSpan variable was constructed
|
---|
1029 | * at an improper time. If there is a failure, do nothing but return
|
---|
1030 | * it to the caller.
|
---|
1031 | */
|
---|
1032 | HRESULT rc() { return mRC; }
|
---|
1033 |
|
---|
1034 | /**
|
---|
1035 | * Returns |true| if AutoMayUninitSpan is already in progress on some
|
---|
1036 | * other thread. If it's the case, do nothing but return S_OK to
|
---|
1037 | * the caller.
|
---|
1038 | */
|
---|
1039 | bool alreadyInProgress() { return mAlreadyInProgress; }
|
---|
1040 |
|
---|
1041 | /*
|
---|
1042 | * Accepts uninitialization and causes the destructor to go to
|
---|
1043 | * WillUninit state and call uninit() afterwards.
|
---|
1044 | */
|
---|
1045 | void acceptUninit() { mAcceptUninit = true; }
|
---|
1046 |
|
---|
1047 | private:
|
---|
1048 |
|
---|
1049 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoMayUninitSpan)
|
---|
1050 | DECLARE_CLS_NEW_DELETE_NOOP (AutoMayUninitSpan)
|
---|
1051 |
|
---|
1052 | VirtualBoxBaseProto *mObj;
|
---|
1053 |
|
---|
1054 | HRESULT mRC;
|
---|
1055 | bool mAlreadyInProgress : 1;
|
---|
1056 | bool mAcceptUninit : 1;
|
---|
1057 | };
|
---|
1058 |
|
---|
1059 | /**
|
---|
1060 | * Returns a lock handle used to protect the primary state fields (used by
|
---|
1061 | * #addCaller(), AutoInitSpan, AutoUninitSpan, etc.). Only intended to be
|
---|
1062 | * used for similar purposes in subclasses. WARNING: NO any other locks may
|
---|
1063 | * be requested while holding this lock!
|
---|
1064 | */
|
---|
1065 | WriteLockHandle *stateLockHandle() { return &mStateLock; }
|
---|
1066 |
|
---|
1067 | private:
|
---|
1068 |
|
---|
1069 | void setState (State aState)
|
---|
1070 | {
|
---|
1071 | Assert (mState != aState);
|
---|
1072 | mState = aState;
|
---|
1073 | mStateChangeThread = RTThreadSelf();
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | /** Primary state of this object */
|
---|
1077 | State mState;
|
---|
1078 | /** Thread that caused the last state change */
|
---|
1079 | RTTHREAD mStateChangeThread;
|
---|
1080 | /** Total number of active calls to this object */
|
---|
1081 | unsigned mCallers;
|
---|
1082 | /** Posted when the number of callers drops to zero */
|
---|
1083 | RTSEMEVENT mZeroCallersSem;
|
---|
1084 | /** Posted when the object goes from InInit/InUninit to some other state */
|
---|
1085 | RTSEMEVENTMULTI mInitUninitSem;
|
---|
1086 | /** Number of threads waiting for mInitUninitDoneSem */
|
---|
1087 | unsigned mInitUninitWaiters;
|
---|
1088 |
|
---|
1089 | /** Protects access to state related data members */
|
---|
1090 | WriteLockHandle mStateLock;
|
---|
1091 |
|
---|
1092 | /** User-level object lock for subclasses */
|
---|
1093 | mutable RWLockHandle *mObjectLock;
|
---|
1094 | };
|
---|
1095 |
|
---|
1096 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1097 |
|
---|
1098 | /**
|
---|
1099 | * This macro adds the error info support to methods of the VirtualBoxBase
|
---|
1100 | * class (by overriding them). Place it to the public section of the
|
---|
1101 | * VirtualBoxBase subclass and the following methods will set the extended
|
---|
1102 | * error info in case of failure instead of just returning the result code:
|
---|
1103 | *
|
---|
1104 | * <ul>
|
---|
1105 | * <li>VirtualBoxBase::addCaller()
|
---|
1106 | * </ul>
|
---|
1107 | *
|
---|
1108 | * @note The given VirtualBoxBase subclass must also inherit from both
|
---|
1109 | * VirtualBoxSupportErrorInfoImpl and VirtualBoxSupportTranslation templates!
|
---|
1110 | *
|
---|
1111 | * @param C VirtualBoxBase subclass to add the error info support to
|
---|
1112 | */
|
---|
1113 | #define VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(C) \
|
---|
1114 | virtual HRESULT addCaller (VirtualBoxBaseProto::State *aState = NULL, \
|
---|
1115 | bool aLimited = false) \
|
---|
1116 | { \
|
---|
1117 | VirtualBoxBaseProto::State state; \
|
---|
1118 | HRESULT rc = VirtualBoxBaseProto::addCaller (&state, aLimited); \
|
---|
1119 | if (FAILED (rc)) \
|
---|
1120 | { \
|
---|
1121 | if (state == VirtualBoxBaseProto::Limited) \
|
---|
1122 | rc = setError (rc, tr ("The object functonality is limited")); \
|
---|
1123 | else \
|
---|
1124 | rc = setError (rc, tr ("The object is not ready")); \
|
---|
1125 | } \
|
---|
1126 | if (aState) \
|
---|
1127 | *aState = state; \
|
---|
1128 | return rc; \
|
---|
1129 | } \
|
---|
1130 |
|
---|
1131 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1132 |
|
---|
1133 | /// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
|
---|
1134 | class ATL_NO_VTABLE VirtualBoxBase
|
---|
1135 | : virtual public VirtualBoxBaseProto
|
---|
1136 | #if !defined (VBOX_WITH_XPCOM)
|
---|
1137 | , public CComObjectRootEx <CComMultiThreadModel>
|
---|
1138 | #else
|
---|
1139 | , public CComObjectRootEx
|
---|
1140 | #endif
|
---|
1141 | {
|
---|
1142 |
|
---|
1143 | public:
|
---|
1144 | VirtualBoxBase()
|
---|
1145 | {
|
---|
1146 | mReady = false;
|
---|
1147 | }
|
---|
1148 | virtual ~VirtualBoxBase()
|
---|
1149 | {
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | /**
|
---|
1153 | * Virtual unintialization method. Called during parent object's
|
---|
1154 | * uninitialization, if the given subclass instance is a dependent child of
|
---|
1155 | * a class dervived from VirtualBoxBaseWithChildren (@sa
|
---|
1156 | * VirtualBoxBaseWithChildren::addDependentChild). In this case, this
|
---|
1157 | * method's impelemtation must call setReady (false),
|
---|
1158 | */
|
---|
1159 | virtual void uninit() {}
|
---|
1160 |
|
---|
1161 |
|
---|
1162 | // sets the ready state of the object
|
---|
1163 | void setReady(bool isReady)
|
---|
1164 | {
|
---|
1165 | mReady = isReady;
|
---|
1166 | }
|
---|
1167 | // get the ready state of the object
|
---|
1168 | bool isReady()
|
---|
1169 | {
|
---|
1170 | return mReady;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | static const char *translate (const char *context, const char *sourceText,
|
---|
1174 | const char *comment = 0);
|
---|
1175 |
|
---|
1176 | private:
|
---|
1177 |
|
---|
1178 | // flag determining whether an object is ready
|
---|
1179 | // for usage, i.e. methods may be called
|
---|
1180 | bool mReady;
|
---|
1181 | // mutex semaphore to lock the object
|
---|
1182 | };
|
---|
1183 |
|
---|
1184 | /**
|
---|
1185 | * Temporary class to disable deprecated methods of VirtualBoxBase.
|
---|
1186 | * Can be used as a base for components that are completely switched to
|
---|
1187 | * the new locking scheme (VirtualBoxBaseProto).
|
---|
1188 | *
|
---|
1189 | * @todo remove after we switch to VirtualBoxBaseNEXT completely.
|
---|
1190 | */
|
---|
1191 | class VirtualBoxBaseNEXT : public VirtualBoxBase
|
---|
1192 | {
|
---|
1193 | private:
|
---|
1194 |
|
---|
1195 | void lock();
|
---|
1196 | void unlock();
|
---|
1197 | void setReady (bool isReady);
|
---|
1198 | bool isReady();
|
---|
1199 | };
|
---|
1200 |
|
---|
1201 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1202 |
|
---|
1203 | /** Helper for VirtualBoxSupportTranslation. */
|
---|
1204 | class VirtualBoxSupportTranslationBase
|
---|
1205 | {
|
---|
1206 | protected:
|
---|
1207 | static bool cutClassNameFrom__PRETTY_FUNCTION__ (char *aPrettyFunctionName);
|
---|
1208 | };
|
---|
1209 |
|
---|
1210 | /**
|
---|
1211 | * The VirtualBoxSupportTranslation template implements the NLS string
|
---|
1212 | * translation support for the given class.
|
---|
1213 | *
|
---|
1214 | * Translation support is provided by the static #tr() function. This function,
|
---|
1215 | * given a string in UTF-8 encoding, looks up for a translation of the given
|
---|
1216 | * string by calling the VirtualBoxBase::translate() global function which
|
---|
1217 | * receives the name of the enclosing class ("context of translation") as the
|
---|
1218 | * additional argument and returns a translated string based on the currently
|
---|
1219 | * active language.
|
---|
1220 | *
|
---|
1221 | * @param C Class that needs to support the string translation.
|
---|
1222 | *
|
---|
1223 | * @note Every class that wants to use the #tr() function in its own methods
|
---|
1224 | * must inherit from this template, regardless of whether its base class
|
---|
1225 | * (if any) inherits from it or not. Otherwise, the translation service
|
---|
1226 | * will not work correctly. However, the declaration of the derived
|
---|
1227 | * class must contain
|
---|
1228 | * the <tt>COM_SUPPORTTRANSLATION_OVERRIDE (<ClassName>)</tt> macro if one
|
---|
1229 | * of its base classes also inherits from this template (to resolve the
|
---|
1230 | * ambiguity of the #tr() function).
|
---|
1231 | */
|
---|
1232 | template <class C>
|
---|
1233 | class VirtualBoxSupportTranslation : virtual protected VirtualBoxSupportTranslationBase
|
---|
1234 | {
|
---|
1235 | public:
|
---|
1236 |
|
---|
1237 | /**
|
---|
1238 | * Translates the given text string by calling VirtualBoxBase::translate()
|
---|
1239 | * and passing the name of the C class as the first argument ("context of
|
---|
1240 | * translation") See VirtualBoxBase::translate() for more info.
|
---|
1241 | *
|
---|
1242 | * @param aSourceText String to translate.
|
---|
1243 | * @param aComment Comment to the string to resolve possible
|
---|
1244 | * ambiguities (NULL means no comment).
|
---|
1245 | *
|
---|
1246 | * @return Translated version of the source string in UTF-8 encoding, or
|
---|
1247 | * the source string itself if the translation is not found in the
|
---|
1248 | * specifiecd context.
|
---|
1249 | */
|
---|
1250 | inline static const char *tr (const char *aSourceText,
|
---|
1251 | const char *aComment = NULL)
|
---|
1252 | {
|
---|
1253 | return VirtualBoxBase::translate (className(), aSourceText, aComment);
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | protected:
|
---|
1257 |
|
---|
1258 | static const char *className()
|
---|
1259 | {
|
---|
1260 | static char fn [sizeof (__PRETTY_FUNCTION__) + 1];
|
---|
1261 | if (!sClassName)
|
---|
1262 | {
|
---|
1263 | strcpy (fn, __PRETTY_FUNCTION__);
|
---|
1264 | cutClassNameFrom__PRETTY_FUNCTION__ (fn);
|
---|
1265 | sClassName = fn;
|
---|
1266 | }
|
---|
1267 | return sClassName;
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | private:
|
---|
1271 |
|
---|
1272 | static const char *sClassName;
|
---|
1273 | };
|
---|
1274 |
|
---|
1275 | template <class C>
|
---|
1276 | const char *VirtualBoxSupportTranslation <C>::sClassName = NULL;
|
---|
1277 |
|
---|
1278 | /**
|
---|
1279 | * This macro must be invoked inside the public section of the declaration of
|
---|
1280 | * the class inherited from the VirtualBoxSupportTranslation template in case
|
---|
1281 | * if one of its other base classes also inherits from that template. This is
|
---|
1282 | * necessary to resolve the ambiguity of the #tr() function.
|
---|
1283 | *
|
---|
1284 | * @param C Class that inherits the VirtualBoxSupportTranslation template
|
---|
1285 | * more than once (through its other base clases).
|
---|
1286 | */
|
---|
1287 | #define VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(C) \
|
---|
1288 | inline static const char *tr (const char *aSourceText, \
|
---|
1289 | const char *aComment = NULL) \
|
---|
1290 | { \
|
---|
1291 | return VirtualBoxSupportTranslation <C>::tr (aSourceText, aComment); \
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | /**
|
---|
1295 | * Dummy macro that is used to shut down Qt's lupdate tool warnings in some
|
---|
1296 | * situations. This macro needs to be present inside (better at the very
|
---|
1297 | * beginning) of the declaration of the class that inherits from
|
---|
1298 | * VirtualBoxSupportTranslation template, to make lupdate happy.
|
---|
1299 | */
|
---|
1300 | #define Q_OBJECT
|
---|
1301 |
|
---|
1302 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1303 |
|
---|
1304 | /**
|
---|
1305 | * Helper for the VirtualBoxSupportErrorInfoImpl template.
|
---|
1306 | */
|
---|
1307 | /// @todo switch to com::SupportErrorInfo* and remove
|
---|
1308 | class VirtualBoxSupportErrorInfoImplBase
|
---|
1309 | {
|
---|
1310 | static HRESULT setErrorInternal (HRESULT aResultCode, const GUID &aIID,
|
---|
1311 | const Bstr &aComponent, const Bstr &aText,
|
---|
1312 | bool aWarning, bool aLogIt);
|
---|
1313 |
|
---|
1314 | protected:
|
---|
1315 |
|
---|
1316 | /**
|
---|
1317 | * The MultiResult class is a com::FWResult enhancement that also acts as a
|
---|
1318 | * switch to turn on multi-error mode for #setError() or #setWarning()
|
---|
1319 | * calls.
|
---|
1320 | *
|
---|
1321 | * When an instance of this class is created, multi-error mode is turned on
|
---|
1322 | * for the current thread and the turn-on counter is increased by one. In
|
---|
1323 | * multi-error mode, a call to #setError() or #setWarning() does not
|
---|
1324 | * overwrite the current error or warning info object possibly set on the
|
---|
1325 | * current thread by other method calls, but instead it stores this old
|
---|
1326 | * object in the IVirtualBoxErrorInfo::next attribute of the new error
|
---|
1327 | * object being set.
|
---|
1328 | *
|
---|
1329 | * This way, error/warning objects are stacked together and form a chain of
|
---|
1330 | * errors where the most recent error is the first one retrieved by the
|
---|
1331 | * calling party, the preceeding error is what the
|
---|
1332 | * IVirtualBoxErrorInfo::next attribute of the first error points to, and so
|
---|
1333 | * on, upto the first error or warning occured which is the last in the
|
---|
1334 | * chain. See IVirtualBoxErrorInfo documentation for more info.
|
---|
1335 | *
|
---|
1336 | * When the instance of the MultiResult class goes out of scope and gets
|
---|
1337 | * destroyed, it automatically decreases the turn-on counter by one. If
|
---|
1338 | * the counter drops to zero, multi-error mode for the current thread is
|
---|
1339 | * turned off and the thread switches back to single-error mode where every
|
---|
1340 | * next error or warning object overwrites the previous one.
|
---|
1341 | *
|
---|
1342 | * Note that the caller of a COM methid uses a non-S_OK result code to
|
---|
1343 | * decide if the method has returned an error (negative codes) or a warning
|
---|
1344 | * (positive non-zero codes) and will query extended error info only in
|
---|
1345 | * these two cases. However, since multi-error mode implies that the method
|
---|
1346 | * doesn't return control return to the caller immediately after the first
|
---|
1347 | * error or warning but continues its execution, the functionality provided
|
---|
1348 | * by the base com::FWResult class becomes very useful because it allows to
|
---|
1349 | * preseve the error or the warning result code even if it is later assigned
|
---|
1350 | * a S_OK value multiple times. See com::FWResult for details.
|
---|
1351 | *
|
---|
1352 | * Here is the typical usage pattern:
|
---|
1353 | * <code>
|
---|
1354 |
|
---|
1355 | HRESULT Bar::method()
|
---|
1356 | {
|
---|
1357 | // assume multi-errors are turned off here...
|
---|
1358 |
|
---|
1359 | if (something)
|
---|
1360 | {
|
---|
1361 | // Turn on multi-error mode and make sure severity is preserved
|
---|
1362 | MultiResult rc = foo->method1();
|
---|
1363 |
|
---|
1364 | // return on fatal error, but continue on warning or on success
|
---|
1365 | CheckComRCReturnRC (rc);
|
---|
1366 |
|
---|
1367 | rc = foo->method2();
|
---|
1368 | // no matter what result, stack it and continue
|
---|
1369 |
|
---|
1370 | // ...
|
---|
1371 |
|
---|
1372 | // return the last worst result code (it will be preserved even if
|
---|
1373 | // foo->method2() returns S_OK.
|
---|
1374 | return rc;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | // multi-errors are turned off here again...
|
---|
1378 |
|
---|
1379 | return S_OK;
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | * </code>
|
---|
1383 | *
|
---|
1384 | *
|
---|
1385 | * @note This class is intended to be instantiated on the stack, therefore
|
---|
1386 | * You cannot create them using new(). Although it is possible to copy
|
---|
1387 | * instances of MultiResult or return them by value, please never do
|
---|
1388 | * that as it is breaks the class semantics (and will assert).
|
---|
1389 | */
|
---|
1390 | class MultiResult : public com::FWResult
|
---|
1391 | {
|
---|
1392 | public:
|
---|
1393 |
|
---|
1394 | /**
|
---|
1395 | * @copydoc com::FWResult::FWResult().
|
---|
1396 | */
|
---|
1397 | MultiResult (HRESULT aRC = E_FAIL) : FWResult (aRC) { init(); }
|
---|
1398 |
|
---|
1399 | MultiResult (const MultiResult &aThat) : FWResult (aThat)
|
---|
1400 | {
|
---|
1401 | /* We need this copy constructor only for GCC that wants to have
|
---|
1402 | * it in case of expressions like |MultiResult rc = E_FAIL;|. But
|
---|
1403 | * we assert since the optimizer should actually avoid the
|
---|
1404 | * temporary and call the other constructor directly istead. */
|
---|
1405 | AssertFailed();
|
---|
1406 | init();
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | ~MultiResult();
|
---|
1410 |
|
---|
1411 | MultiResult &operator= (HRESULT aRC)
|
---|
1412 | {
|
---|
1413 | com::FWResult::operator= (aRC);
|
---|
1414 | return *this;
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | MultiResult &operator= (const MultiResult &aThat)
|
---|
1418 | {
|
---|
1419 | /* We need this copy constructor only for GCC that wants to have
|
---|
1420 | * it in case of expressions like |MultiResult rc = E_FAIL;|. But
|
---|
1421 | * we assert since the optimizer should actually avoid the
|
---|
1422 | * temporary and call the other constructor directly istead. */
|
---|
1423 | AssertFailed();
|
---|
1424 | com::FWResult::operator= (aThat);
|
---|
1425 | return *this;
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | private:
|
---|
1429 |
|
---|
1430 | DECLARE_CLS_NEW_DELETE_NOOP (MultiResult)
|
---|
1431 |
|
---|
1432 | void init();
|
---|
1433 |
|
---|
1434 | static RTTLS sCounter;
|
---|
1435 |
|
---|
1436 | friend class VirtualBoxSupportErrorInfoImplBase;
|
---|
1437 | };
|
---|
1438 |
|
---|
1439 | static HRESULT setError (HRESULT aResultCode, const GUID &aIID,
|
---|
1440 | const Bstr &aComponent,
|
---|
1441 | const Bstr &aText,
|
---|
1442 | bool aLogIt = true)
|
---|
1443 | {
|
---|
1444 | return setErrorInternal (aResultCode, aIID, aComponent, aText,
|
---|
1445 | false /* aWarning */, aLogIt);
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | static HRESULT setWarning (HRESULT aResultCode, const GUID &aIID,
|
---|
1449 | const Bstr &aComponent,
|
---|
1450 | const Bstr &aText)
|
---|
1451 | {
|
---|
1452 | return setErrorInternal (aResultCode, aIID, aComponent, aText,
|
---|
1453 | true /* aWarning */, true /* aLogIt */);
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | static HRESULT setError (HRESULT aResultCode, const GUID &aIID,
|
---|
1457 | const Bstr &aComponent,
|
---|
1458 | const char *aText, va_list aArgs, bool aLogIt = true)
|
---|
1459 | {
|
---|
1460 | return setErrorInternal (aResultCode, aIID, aComponent,
|
---|
1461 | Utf8StrFmtVA (aText, aArgs),
|
---|
1462 | false /* aWarning */, aLogIt);
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | static HRESULT setWarning (HRESULT aResultCode, const GUID &aIID,
|
---|
1466 | const Bstr &aComponent,
|
---|
1467 | const char *aText, va_list aArgs)
|
---|
1468 | {
|
---|
1469 | return setErrorInternal (aResultCode, aIID, aComponent,
|
---|
1470 | Utf8StrFmtVA (aText, aArgs),
|
---|
1471 | true /* aWarning */, true /* aLogIt */);
|
---|
1472 | }
|
---|
1473 | };
|
---|
1474 |
|
---|
1475 | /**
|
---|
1476 | * This template implements ISupportErrorInfo for the given component class
|
---|
1477 | * and provides the #setError() method to conveniently set the error information
|
---|
1478 | * from within interface methods' implementations.
|
---|
1479 | *
|
---|
1480 | * On Windows, the template argument must define a COM interface map using
|
---|
1481 | * BEGIN_COM_MAP / END_COM_MAP macros and this map must contain a
|
---|
1482 | * COM_INTERFACE_ENTRY(ISupportErrorInfo) definition. All interface entries
|
---|
1483 | * that follow it will be considered to support IErrorInfo, i.e. the
|
---|
1484 | * InterfaceSupportsErrorInfo() implementation will return S_OK for the
|
---|
1485 | * corresponding IID.
|
---|
1486 | *
|
---|
1487 | * On all platforms, the template argument must also define the following
|
---|
1488 | * method: |public static const wchar_t *C::getComponentName()|. See
|
---|
1489 | * #setError (HRESULT, const char *, ...) for a description on how it is
|
---|
1490 | * used.
|
---|
1491 | *
|
---|
1492 | * @param C
|
---|
1493 | * component class that implements one or more COM interfaces
|
---|
1494 | * @param I
|
---|
1495 | * default interface for the component. This interface's IID is used
|
---|
1496 | * by the shortest form of #setError, for convenience.
|
---|
1497 | */
|
---|
1498 | /// @todo switch to com::SupportErrorInfo* and remove
|
---|
1499 | template <class C, class I>
|
---|
1500 | class ATL_NO_VTABLE VirtualBoxSupportErrorInfoImpl
|
---|
1501 | : protected VirtualBoxSupportErrorInfoImplBase
|
---|
1502 | #if !defined (VBOX_WITH_XPCOM)
|
---|
1503 | , public ISupportErrorInfo
|
---|
1504 | #else
|
---|
1505 | #endif
|
---|
1506 | {
|
---|
1507 | public:
|
---|
1508 |
|
---|
1509 | #if !defined (VBOX_WITH_XPCOM)
|
---|
1510 | STDMETHOD(InterfaceSupportsErrorInfo) (REFIID riid)
|
---|
1511 | {
|
---|
1512 | const _ATL_INTMAP_ENTRY* pEntries = C::_GetEntries();
|
---|
1513 | Assert (pEntries);
|
---|
1514 | if (!pEntries)
|
---|
1515 | return S_FALSE;
|
---|
1516 |
|
---|
1517 | BOOL bSupports = FALSE;
|
---|
1518 | BOOL bISupportErrorInfoFound = FALSE;
|
---|
1519 |
|
---|
1520 | while (pEntries->pFunc != NULL && !bSupports)
|
---|
1521 | {
|
---|
1522 | if (!bISupportErrorInfoFound)
|
---|
1523 | {
|
---|
1524 | // skip the com map entries until ISupportErrorInfo is found
|
---|
1525 | bISupportErrorInfoFound =
|
---|
1526 | InlineIsEqualGUID (*(pEntries->piid), IID_ISupportErrorInfo);
|
---|
1527 | }
|
---|
1528 | else
|
---|
1529 | {
|
---|
1530 | // look for the requested interface in the rest of the com map
|
---|
1531 | bSupports = InlineIsEqualGUID (*(pEntries->piid), riid);
|
---|
1532 | }
|
---|
1533 | pEntries++;
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 | Assert (bISupportErrorInfoFound);
|
---|
1537 |
|
---|
1538 | return bSupports ? S_OK : S_FALSE;
|
---|
1539 | }
|
---|
1540 | #endif // !defined (VBOX_WITH_XPCOM)
|
---|
1541 |
|
---|
1542 | protected:
|
---|
1543 |
|
---|
1544 | /**
|
---|
1545 | * Sets the error information for the current thread.
|
---|
1546 | * This information can be retrieved by a caller of an interface method
|
---|
1547 | * using IErrorInfo on Windows or nsIException on Linux, or the cross-platform
|
---|
1548 | * IVirtualBoxErrorInfo interface that provides extended error info (only
|
---|
1549 | * for components from the VirtualBox COM library). Alternatively, the
|
---|
1550 | * platform-independent class com::ErrorInfo (defined in VBox[XP]COM.lib)
|
---|
1551 | * can be used to retrieve error info in a convenient way.
|
---|
1552 | *
|
---|
1553 | * It is assumed that the interface method that uses this function returns
|
---|
1554 | * an unsuccessful result code to the caller (otherwise, there is no reason
|
---|
1555 | * for the caller to try to retrieve error info after method invocation).
|
---|
1556 | *
|
---|
1557 | * Here is a table of correspondence between this method's arguments
|
---|
1558 | * and IErrorInfo/nsIException/IVirtualBoxErrorInfo attributes/methods:
|
---|
1559 | *
|
---|
1560 | * argument IErrorInfo nsIException IVirtualBoxErrorInfo
|
---|
1561 | * ----------------------------------------------------------------
|
---|
1562 | * resultCode -- result resultCode
|
---|
1563 | * iid GetGUID -- interfaceID
|
---|
1564 | * component GetSource -- component
|
---|
1565 | * text GetDescription message text
|
---|
1566 | *
|
---|
1567 | * This method is rarely needs to be used though. There are more convenient
|
---|
1568 | * overloaded versions, that automatically substitute some arguments
|
---|
1569 | * taking their values from the template parameters. See
|
---|
1570 | * #setError (HRESULT, const char *, ...) for an example.
|
---|
1571 | *
|
---|
1572 | * @param aResultCode result (error) code, must not be S_OK
|
---|
1573 | * @param aIID IID of the intrface that defines the error
|
---|
1574 | * @param aComponent name of the component that generates the error
|
---|
1575 | * @param aText error message (must not be null), an RTStrPrintf-like
|
---|
1576 | * format string in UTF-8 encoding
|
---|
1577 | * @param ... list of arguments for the format string
|
---|
1578 | *
|
---|
1579 | * @return
|
---|
1580 | * the error argument, for convenience, If an error occures while
|
---|
1581 | * creating error info itself, that error is returned instead of the
|
---|
1582 | * error argument.
|
---|
1583 | */
|
---|
1584 | static HRESULT setError (HRESULT aResultCode, const GUID &aIID,
|
---|
1585 | const wchar_t *aComponent,
|
---|
1586 | const char *aText, ...)
|
---|
1587 | {
|
---|
1588 | va_list args;
|
---|
1589 | va_start (args, aText);
|
---|
1590 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
|
---|
1591 | (aResultCode, aIID, aComponent, aText, args, true /* aLogIt */);
|
---|
1592 | va_end (args);
|
---|
1593 | return rc;
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | /**
|
---|
1597 | * This method is the same as #setError() except that it makes sure @a
|
---|
1598 | * aResultCode doesn't have the error severty bit (31) set when passed
|
---|
1599 | * down to the created IVirtualBoxErrorInfo object.
|
---|
1600 | *
|
---|
1601 | * The error severity bit is always cleared by this call, thereofe you can
|
---|
1602 | * use ordinary E_XXX result code constancs, for convenience. However, this
|
---|
1603 | * behavior may be non-stanrard on some COM platforms.
|
---|
1604 | */
|
---|
1605 | static HRESULT setWarning (HRESULT aResultCode, const GUID &aIID,
|
---|
1606 | const wchar_t *aComponent,
|
---|
1607 | const char *aText, ...)
|
---|
1608 | {
|
---|
1609 | va_list args;
|
---|
1610 | va_start (args, aText);
|
---|
1611 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setWarning
|
---|
1612 | (aResultCode, aIID, aComponent, aText, args);
|
---|
1613 | va_end (args);
|
---|
1614 | return rc;
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | /**
|
---|
1618 | * Sets the error information for the current thread.
|
---|
1619 | * A convenience method that automatically sets the default interface
|
---|
1620 | * ID (taken from the I template argument) and the component name
|
---|
1621 | * (a value of C::getComponentName()).
|
---|
1622 | *
|
---|
1623 | * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
|
---|
1624 | * for details.
|
---|
1625 | *
|
---|
1626 | * This method is the most common (and convenient) way to set error
|
---|
1627 | * information from within interface methods. A typical pattern of usage
|
---|
1628 | * is looks like this:
|
---|
1629 | *
|
---|
1630 | * <code>
|
---|
1631 | * return setError (E_FAIL, "Terrible Error");
|
---|
1632 | * </code>
|
---|
1633 | * or
|
---|
1634 | * <code>
|
---|
1635 | * HRESULT rc = setError (E_FAIL, "Terrible Error");
|
---|
1636 | * ...
|
---|
1637 | * return rc;
|
---|
1638 | * </code>
|
---|
1639 | */
|
---|
1640 | static HRESULT setError (HRESULT aResultCode, const char *aText, ...)
|
---|
1641 | {
|
---|
1642 | va_list args;
|
---|
1643 | va_start (args, aText);
|
---|
1644 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
|
---|
1645 | (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, args, true /* aLogIt */);
|
---|
1646 | va_end (args);
|
---|
1647 | return rc;
|
---|
1648 | }
|
---|
1649 |
|
---|
1650 | /**
|
---|
1651 | * This method is the same as #setError() except that it makes sure @a
|
---|
1652 | * aResultCode doesn't have the error severty bit (31) set when passed
|
---|
1653 | * down to the created IVirtualBoxErrorInfo object.
|
---|
1654 | *
|
---|
1655 | * The error severity bit is always cleared by this call, thereofe you can
|
---|
1656 | * use ordinary E_XXX result code constancs, for convenience. However, this
|
---|
1657 | * behavior may be non-stanrard on some COM platforms.
|
---|
1658 | */
|
---|
1659 | static HRESULT setWarning (HRESULT aResultCode, const char *aText, ...)
|
---|
1660 | {
|
---|
1661 | va_list args;
|
---|
1662 | va_start (args, aText);
|
---|
1663 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setWarning
|
---|
1664 | (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, args);
|
---|
1665 | va_end (args);
|
---|
1666 | return rc;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | /**
|
---|
1670 | * Sets the error information for the current thread, va_list variant.
|
---|
1671 | * A convenience method that automatically sets the default interface
|
---|
1672 | * ID (taken from the I template argument) and the component name
|
---|
1673 | * (a value of C::getComponentName()).
|
---|
1674 | *
|
---|
1675 | * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
|
---|
1676 | * and #setError (HRESULT, const char *, ...) for details.
|
---|
1677 | */
|
---|
1678 | static HRESULT setErrorV (HRESULT aResultCode, const char *aText,
|
---|
1679 | va_list aArgs)
|
---|
1680 | {
|
---|
1681 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
|
---|
1682 | (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, aArgs, true /* aLogIt */);
|
---|
1683 | return rc;
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | /**
|
---|
1687 | * This method is the same as #setErrorV() except that it makes sure @a
|
---|
1688 | * aResultCode doesn't have the error severty bit (31) set when passed
|
---|
1689 | * down to the created IVirtualBoxErrorInfo object.
|
---|
1690 | *
|
---|
1691 | * The error severity bit is always cleared by this call, thereofe you can
|
---|
1692 | * use ordinary E_XXX result code constancs, for convenience. However, this
|
---|
1693 | * behavior may be non-stanrard on some COM platforms.
|
---|
1694 | */
|
---|
1695 | static HRESULT setWarningV (HRESULT aResultCode, const char *aText,
|
---|
1696 | va_list aArgs)
|
---|
1697 | {
|
---|
1698 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setWarning
|
---|
1699 | (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, aArgs);
|
---|
1700 | return rc;
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | /**
|
---|
1704 | * Sets the error information for the current thread, BStr variant.
|
---|
1705 | * A convenience method that automatically sets the default interface
|
---|
1706 | * ID (taken from the I template argument) and the component name
|
---|
1707 | * (a value of C::getComponentName()).
|
---|
1708 | *
|
---|
1709 | * This method is preferred iy you have a ready (translated and formatted)
|
---|
1710 | * Bstr string, because it omits an extra conversion Utf8Str -> Bstr.
|
---|
1711 | *
|
---|
1712 | * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
|
---|
1713 | * and #setError (HRESULT, const char *, ...) for details.
|
---|
1714 | */
|
---|
1715 | static HRESULT setErrorBstr (HRESULT aResultCode, const Bstr &aText)
|
---|
1716 | {
|
---|
1717 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
|
---|
1718 | (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, true /* aLogIt */);
|
---|
1719 | return rc;
|
---|
1720 | }
|
---|
1721 |
|
---|
1722 | /**
|
---|
1723 | * This method is the same as #setErrorBstr() except that it makes sure @a
|
---|
1724 | * aResultCode doesn't have the error severty bit (31) set when passed
|
---|
1725 | * down to the created IVirtualBoxErrorInfo object.
|
---|
1726 | *
|
---|
1727 | * The error severity bit is always cleared by this call, thereofe you can
|
---|
1728 | * use ordinary E_XXX result code constancs, for convenience. However, this
|
---|
1729 | * behavior may be non-stanrard on some COM platforms.
|
---|
1730 | */
|
---|
1731 | static HRESULT setWarningBstr (HRESULT aResultCode, const Bstr &aText)
|
---|
1732 | {
|
---|
1733 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setWarning
|
---|
1734 | (aResultCode, COM_IIDOF(I), C::getComponentName(), aText);
|
---|
1735 | return rc;
|
---|
1736 | }
|
---|
1737 |
|
---|
1738 | /**
|
---|
1739 | * Sets the error information for the current thread.
|
---|
1740 | * A convenience method that automatically sets the component name
|
---|
1741 | * (a value of C::getComponentName()), but allows to specify the interface
|
---|
1742 | * id manually.
|
---|
1743 | *
|
---|
1744 | * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
|
---|
1745 | * for details.
|
---|
1746 | */
|
---|
1747 | static HRESULT setError (HRESULT aResultCode, const GUID &aIID,
|
---|
1748 | const char *aText, ...)
|
---|
1749 | {
|
---|
1750 | va_list args;
|
---|
1751 | va_start (args, aText);
|
---|
1752 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
|
---|
1753 | (aResultCode, aIID, C::getComponentName(), aText, args, true /* aLogIt */);
|
---|
1754 | va_end (args);
|
---|
1755 | return rc;
|
---|
1756 | }
|
---|
1757 |
|
---|
1758 | /**
|
---|
1759 | * This method is the same as #setError() except that it makes sure @a
|
---|
1760 | * aResultCode doesn't have the error severty bit (31) set when passed
|
---|
1761 | * down to the created IVirtualBoxErrorInfo object.
|
---|
1762 | *
|
---|
1763 | * The error severity bit is always cleared by this call, thereofe you can
|
---|
1764 | * use ordinary E_XXX result code constancs, for convenience. However, this
|
---|
1765 | * behavior may be non-stanrard on some COM platforms.
|
---|
1766 | */
|
---|
1767 | static HRESULT setWarning (HRESULT aResultCode, const GUID &aIID,
|
---|
1768 | const char *aText, ...)
|
---|
1769 | {
|
---|
1770 | va_list args;
|
---|
1771 | va_start (args, aText);
|
---|
1772 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setWarning
|
---|
1773 | (aResultCode, aIID, C::getComponentName(), aText, args);
|
---|
1774 | va_end (args);
|
---|
1775 | return rc;
|
---|
1776 | }
|
---|
1777 |
|
---|
1778 | /**
|
---|
1779 | * Sets the error information for the current thread but doesn't put
|
---|
1780 | * anything in the release log. This is very useful for avoiding
|
---|
1781 | * harmless error from causing confusion.
|
---|
1782 | *
|
---|
1783 | * It is otherwise identical to #setError (HRESULT, const char *text, ...).
|
---|
1784 | */
|
---|
1785 | static HRESULT setErrorNoLog (HRESULT aResultCode, const char *aText, ...)
|
---|
1786 | {
|
---|
1787 | va_list args;
|
---|
1788 | va_start (args, aText);
|
---|
1789 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
|
---|
1790 | (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, args, false /* aLogIt */);
|
---|
1791 | va_end (args);
|
---|
1792 | return rc;
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | private:
|
---|
1796 |
|
---|
1797 | };
|
---|
1798 |
|
---|
1799 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1800 |
|
---|
1801 | /**
|
---|
1802 | * Base class to track VirtualBoxBase chlidren of the component.
|
---|
1803 | *
|
---|
1804 | * This class is a preferrable VirtualBoxBase replacement for components
|
---|
1805 | * that operate with collections of child components. It gives two useful
|
---|
1806 | * possibilities:
|
---|
1807 | *
|
---|
1808 | * <ol><li>
|
---|
1809 | * Given an IUnknown instance, it's possible to quickly determine
|
---|
1810 | * whether this instance represents a child object created by the given
|
---|
1811 | * component, and if so, get a valid VirtualBoxBase pointer to the child
|
---|
1812 | * object. The returned pointer can be then safely casted to the
|
---|
1813 | * actual class of the child object (to get access to its "internal"
|
---|
1814 | * non-interface methods) provided that no other child components implement
|
---|
1815 | * the same initial interface IUnknown is queried from.
|
---|
1816 | * </li><li>
|
---|
1817 | * When the parent object uninitializes itself, it can easily unintialize
|
---|
1818 | * all its VirtualBoxBase derived children (using their
|
---|
1819 | * VirtualBoxBase::uninit() implementations). This is done simply by
|
---|
1820 | * calling the #uninitDependentChildren() method.
|
---|
1821 | * </li></ol>
|
---|
1822 | *
|
---|
1823 | * In order to let the above work, the following must be done:
|
---|
1824 | * <ol><li>
|
---|
1825 | * When a child object is initialized, it calls #addDependentChild() of
|
---|
1826 | * its parent to register itself within the list of dependent children.
|
---|
1827 | * </li><li>
|
---|
1828 | * When a child object it is uninitialized, it calls #removeDependentChild()
|
---|
1829 | * to unregister itself. This must be done <b>after</b> the child has called
|
---|
1830 | * setReady(false) to indicate it is no more valid, and <b>not</b> from under
|
---|
1831 | * the child object's lock. Note also, that the first action the child's
|
---|
1832 | * uninit() implementation must do is to check for readiness after acquiring
|
---|
1833 | * the object's lock and return immediately if not ready.
|
---|
1834 | * </li></ol>
|
---|
1835 | *
|
---|
1836 | * Children added by #addDependentChild() are <b>weakly</b> referenced
|
---|
1837 | * (i.e. AddRef() is not called), so when a child is externally destructed
|
---|
1838 | * (i.e. its reference count goes to zero), it will automatically remove
|
---|
1839 | * itself from a map of dependent children, provided that it follows the
|
---|
1840 | * rules described here.
|
---|
1841 | *
|
---|
1842 | * @note
|
---|
1843 | * Because of weak referencing, deadlocks and assertions are very likely
|
---|
1844 | * if #addDependentChild() or #removeDependentChild() are used incorrectly
|
---|
1845 | * (called at inappropriate times). Check the above rules once more.
|
---|
1846 | *
|
---|
1847 | * @deprecated Use VirtualBoxBaseWithChildrenNEXT for new classes.
|
---|
1848 | */
|
---|
1849 | class VirtualBoxBaseWithChildren : public VirtualBoxBase
|
---|
1850 | {
|
---|
1851 | public:
|
---|
1852 |
|
---|
1853 | VirtualBoxBaseWithChildren()
|
---|
1854 | : mUninitDoneSem (NIL_RTSEMEVENT), mChildrenLeft (0)
|
---|
1855 | {}
|
---|
1856 |
|
---|
1857 | virtual ~VirtualBoxBaseWithChildren()
|
---|
1858 | {}
|
---|
1859 |
|
---|
1860 | /**
|
---|
1861 | * Adds the given child to the map of dependent children.
|
---|
1862 | * Intended to be called from the child's init() method,
|
---|
1863 | * from under the child's lock.
|
---|
1864 | *
|
---|
1865 | * @param C the child object to add (must inherit VirtualBoxBase AND
|
---|
1866 | * implement some interface)
|
---|
1867 | */
|
---|
1868 | template <class C>
|
---|
1869 | void addDependentChild (C *child)
|
---|
1870 | {
|
---|
1871 | AssertReturn (child, (void) 0);
|
---|
1872 | addDependentChild (child, child);
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 | /**
|
---|
1876 | * Removes the given child from the map of dependent children.
|
---|
1877 | * Must be called <b>after<b> the child has called setReady(false), and
|
---|
1878 | * <b>not</b> from under the child object's lock.
|
---|
1879 | *
|
---|
1880 | * @param C the child object to remove (must inherit VirtualBoxBase AND
|
---|
1881 | * implement some interface)
|
---|
1882 | */
|
---|
1883 | template <class C>
|
---|
1884 | void removeDependentChild (C *child)
|
---|
1885 | {
|
---|
1886 | AssertReturn (child, (void) 0);
|
---|
1887 | /// @todo (r=dmik) the below check (and the relevant comment above)
|
---|
1888 | // seems to be not necessary any more once we completely switch to
|
---|
1889 | // the NEXT locking scheme. This requires altering removeDependentChild()
|
---|
1890 | // and uninitDependentChildren() as well (due to the new state scheme,
|
---|
1891 | // there is a separate mutex for state transition, so calling the
|
---|
1892 | // child's uninit() from under the children map lock should not produce
|
---|
1893 | // dead-locks any more).
|
---|
1894 | Assert (!child->isWriteLockOnCurrentThread() || child->lockHandle() == lockHandle());
|
---|
1895 | removeDependentChild (ComPtr <IUnknown> (child));
|
---|
1896 | }
|
---|
1897 |
|
---|
1898 | protected:
|
---|
1899 |
|
---|
1900 | void uninitDependentChildren();
|
---|
1901 |
|
---|
1902 | VirtualBoxBase *getDependentChild (const ComPtr <IUnknown> &unk);
|
---|
1903 |
|
---|
1904 | private:
|
---|
1905 |
|
---|
1906 | void addDependentChild (const ComPtr <IUnknown> &unk, VirtualBoxBase *child);
|
---|
1907 | void removeDependentChild (const ComPtr <IUnknown> &unk);
|
---|
1908 |
|
---|
1909 | typedef std::map <IUnknown *, VirtualBoxBase *> DependentChildren;
|
---|
1910 | DependentChildren mDependentChildren;
|
---|
1911 |
|
---|
1912 | WriteLockHandle mMapLock;
|
---|
1913 |
|
---|
1914 | RTSEMEVENT mUninitDoneSem;
|
---|
1915 | unsigned mChildrenLeft;
|
---|
1916 | };
|
---|
1917 |
|
---|
1918 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1919 |
|
---|
1920 | /**
|
---|
1921 | * Base class to track VirtualBoxBaseNEXT chlidren of the component.
|
---|
1922 | *
|
---|
1923 | * This class is a preferrable VirtualBoxBase replacement for components that
|
---|
1924 | * operate with collections of child components. It gives two useful
|
---|
1925 | * possibilities:
|
---|
1926 | *
|
---|
1927 | * <ol><li>
|
---|
1928 | * Given an IUnknown instance, it's possible to quickly determine
|
---|
1929 | * whether this instance represents a child object created by the given
|
---|
1930 | * component, and if so, get a valid VirtualBoxBase pointer to the child
|
---|
1931 | * object. The returned pointer can be then safely casted to the
|
---|
1932 | * actual class of the child object (to get access to its "internal"
|
---|
1933 | * non-interface methods) provided that no other child components implement
|
---|
1934 | * the same initial interface IUnknown is queried from.
|
---|
1935 | * </li><li>
|
---|
1936 | * When the parent object uninitializes itself, it can easily unintialize
|
---|
1937 | * all its VirtualBoxBase derived children (using their
|
---|
1938 | * VirtualBoxBase::uninit() implementations). This is done simply by
|
---|
1939 | * calling the #uninitDependentChildren() method.
|
---|
1940 | * </li></ol>
|
---|
1941 | *
|
---|
1942 | * In order to let the above work, the following must be done:
|
---|
1943 | * <ol><li>
|
---|
1944 | * When a child object is initialized, it calls #addDependentChild() of
|
---|
1945 | * its parent to register itself within the list of dependent children.
|
---|
1946 | * </li><li>
|
---|
1947 | * When a child object it is uninitialized, it calls
|
---|
1948 | * #removeDependentChild() to unregister itself. Since the child's
|
---|
1949 | * uninitialization may originate both from this method and from the child
|
---|
1950 | * itself calling its uninit() on another thread at the same time, please
|
---|
1951 | * make sure that #removeDependentChild() is called:
|
---|
1952 | * <ul><li>
|
---|
1953 | * after the child has successfully entered AutoUninitSpan -- to make
|
---|
1954 | * sure this method is called only once for the given child object
|
---|
1955 | * transitioning from Ready to NotReady. A failure to do so will at
|
---|
1956 | * least likely cause an assertion ("Failed to remove the child from
|
---|
1957 | * the map").
|
---|
1958 | * </li><li>
|
---|
1959 | * outside the child object's lock -- to avoid guaranteed deadlocks
|
---|
1960 | * caused by different lock order: (child_lock, map_lock) in uninit()
|
---|
1961 | * and (map_lock, child_lock) in this method.
|
---|
1962 | * </li></ul>
|
---|
1963 | * </li></ol>
|
---|
1964 | *
|
---|
1965 | * Note that children added by #addDependentChild() are <b>weakly</b> referenced
|
---|
1966 | * (i.e. AddRef() is not called), so when a child object is deleted externally
|
---|
1967 | * (because it's reference count goes to zero), it will automatically remove
|
---|
1968 | * itself from the map of dependent children provided that it follows the rules
|
---|
1969 | * described here.
|
---|
1970 | *
|
---|
1971 | * @note Once again: because of weak referencing, deadlocks and assertions are
|
---|
1972 | * very likely if #addDependentChild() or #removeDependentChild() are used
|
---|
1973 | * incorrectly (called at inappropriate times). Check the above rules once
|
---|
1974 | * more.
|
---|
1975 | *
|
---|
1976 | * @todo This is a VirtualBoxBaseWithChildren equivalent that uses the
|
---|
1977 | * VirtualBoxBaseNEXT implementation. Will completely supercede
|
---|
1978 | * VirtualBoxBaseWithChildren after the old VirtualBoxBase implementation
|
---|
1979 | * has gone.
|
---|
1980 | */
|
---|
1981 | class VirtualBoxBaseWithChildrenNEXT : public VirtualBoxBaseNEXT
|
---|
1982 | {
|
---|
1983 | public:
|
---|
1984 |
|
---|
1985 | VirtualBoxBaseWithChildrenNEXT()
|
---|
1986 | : mUninitDoneSem (NIL_RTSEMEVENT), mChildrenLeft (0)
|
---|
1987 | {}
|
---|
1988 |
|
---|
1989 | virtual ~VirtualBoxBaseWithChildrenNEXT()
|
---|
1990 | {}
|
---|
1991 |
|
---|
1992 | /**
|
---|
1993 | * Adds the given child to the map of dependent children.
|
---|
1994 | *
|
---|
1995 | * Typically called from the child's init() method, from within the
|
---|
1996 | * AutoInitSpan scope. Otherwise, VirtualBoxBase::AutoCaller must be
|
---|
1997 | * used on @a aChild to make sure it is not uninitialized during this
|
---|
1998 | * method's call.
|
---|
1999 | *
|
---|
2000 | * @param aChild Child object to add (must inherit VirtualBoxBase AND
|
---|
2001 | * implement some interface).
|
---|
2002 | */
|
---|
2003 | template <class C>
|
---|
2004 | void addDependentChild (C *aChild)
|
---|
2005 | {
|
---|
2006 | AssertReturnVoid (aChild);
|
---|
2007 | doAddDependentChild (ComPtr <IUnknown> (aChild), aChild);
|
---|
2008 | }
|
---|
2009 |
|
---|
2010 | /**
|
---|
2011 | * Equivalent to template <class C> void addDependentChild (C *aChild)
|
---|
2012 | * but takes a ComObjPtr <C> argument.
|
---|
2013 | */
|
---|
2014 | template <class C>
|
---|
2015 | void addDependentChild (const ComObjPtr <C> &aChild)
|
---|
2016 | {
|
---|
2017 | AssertReturnVoid (!aChild.isNull());
|
---|
2018 | doAddDependentChild (ComPtr <IUnknown> (static_cast <C *> (aChild)), aChild);
|
---|
2019 | }
|
---|
2020 |
|
---|
2021 | /**
|
---|
2022 | * Removes the given child from the map of dependent children.
|
---|
2023 | *
|
---|
2024 | * Typically called from from the child's uninit() method, from within the
|
---|
2025 | * AutoUninitSpan scope. Make sure te child is not locked for reading or
|
---|
2026 | * writing in this case.
|
---|
2027 | *
|
---|
2028 | * If called not from within the AutoUninitSpan scope,
|
---|
2029 | * VirtualBoxBase::AutoCaller must be used on @a aChild to make sure it is
|
---|
2030 | * not uninitialized during this method's call.
|
---|
2031 | *
|
---|
2032 | * @param aChild Child object to remove (must inherit VirtualBoxBase AND
|
---|
2033 | * implement some interface).
|
---|
2034 | */
|
---|
2035 | template <class C>
|
---|
2036 | void removeDependentChild (C *aChild)
|
---|
2037 | {
|
---|
2038 | AssertReturnVoid (aChild);
|
---|
2039 | doRemoveDependentChild (ComPtr <IUnknown> (aChild));
|
---|
2040 | }
|
---|
2041 |
|
---|
2042 | /**
|
---|
2043 | * Equivalent to template <class C> void removeDependentChild (C *aChild)
|
---|
2044 | * but takes a ComObjPtr <C> argument.
|
---|
2045 | */
|
---|
2046 | template <class C>
|
---|
2047 | void removeDependentChild (const ComObjPtr <C> &aChild)
|
---|
2048 | {
|
---|
2049 | AssertReturnVoid (!aChild.isNull());
|
---|
2050 | doRemoveDependentChild (ComPtr <IUnknown> (static_cast <C *> (aChild)));
|
---|
2051 | }
|
---|
2052 |
|
---|
2053 | protected:
|
---|
2054 |
|
---|
2055 | void uninitDependentChildren();
|
---|
2056 |
|
---|
2057 | VirtualBoxBaseNEXT *getDependentChild (const ComPtr <IUnknown> &aUnk);
|
---|
2058 |
|
---|
2059 | private:
|
---|
2060 |
|
---|
2061 | /// @todo temporarily reinterpret VirtualBoxBase * as VirtualBoxBaseNEXT *
|
---|
2062 | // until ported HardDisk and Progress to the new scheme.
|
---|
2063 | void doAddDependentChild (IUnknown *aUnk, VirtualBoxBase *aChild)
|
---|
2064 | {
|
---|
2065 | doAddDependentChild (aUnk,
|
---|
2066 | reinterpret_cast <VirtualBoxBaseNEXT *> (aChild));
|
---|
2067 | }
|
---|
2068 |
|
---|
2069 | void doAddDependentChild (IUnknown *aUnk, VirtualBoxBaseNEXT *aChild);
|
---|
2070 | void doRemoveDependentChild (IUnknown *aUnk);
|
---|
2071 |
|
---|
2072 | typedef std::map <IUnknown *, VirtualBoxBaseNEXT *> DependentChildren;
|
---|
2073 | DependentChildren mDependentChildren;
|
---|
2074 |
|
---|
2075 | RTSEMEVENT mUninitDoneSem;
|
---|
2076 | size_t mChildrenLeft;
|
---|
2077 |
|
---|
2078 | /* Protects all the fields above */
|
---|
2079 | RWLockHandle mMapLock;
|
---|
2080 | };
|
---|
2081 |
|
---|
2082 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2083 |
|
---|
2084 | /**
|
---|
2085 | * Base class to track component's children of some particular type.
|
---|
2086 | *
|
---|
2087 | * This class is similar to VirtualBoxBaseWithChildren, with the exception
|
---|
2088 | * that all children must be of the same type. For this reason, it's not
|
---|
2089 | * necessary to use a map to store children, so a list is used instead.
|
---|
2090 | *
|
---|
2091 | * As opposed to VirtualBoxBaseWithChildren, children added by
|
---|
2092 | * #addDependentChild() are <b>strongly</b> referenced, so that they cannot
|
---|
2093 | * be externally destructed until #removeDependentChild() is called.
|
---|
2094 | * For this reason, strict rules of calling #removeDependentChild() don't
|
---|
2095 | * apply to instances of this class -- it can be called anywhere in the
|
---|
2096 | * child's uninit() implementation.
|
---|
2097 | *
|
---|
2098 | * @param C type of child objects (must inherit VirtualBoxBase AND
|
---|
2099 | * implement some interface)
|
---|
2100 | *
|
---|
2101 | * @deprecated Use VirtualBoxBaseWithTypedChildrenNEXT for new classes.
|
---|
2102 | */
|
---|
2103 | template <class C>
|
---|
2104 | class VirtualBoxBaseWithTypedChildren : public VirtualBoxBase
|
---|
2105 | {
|
---|
2106 | public:
|
---|
2107 |
|
---|
2108 | typedef std::list <ComObjPtr <C> > DependentChildren;
|
---|
2109 |
|
---|
2110 | VirtualBoxBaseWithTypedChildren() : mInUninit (false) {}
|
---|
2111 |
|
---|
2112 | virtual ~VirtualBoxBaseWithTypedChildren() {}
|
---|
2113 |
|
---|
2114 | /**
|
---|
2115 | * Adds the given child to the list of dependent children.
|
---|
2116 | * Must be called from the child's init() method,
|
---|
2117 | * from under the child's lock.
|
---|
2118 | *
|
---|
2119 | * @param C the child object to add (must inherit VirtualBoxBase AND
|
---|
2120 | * implement some interface)
|
---|
2121 | */
|
---|
2122 | void addDependentChild (C *child)
|
---|
2123 | {
|
---|
2124 | AssertReturn (child, (void) 0);
|
---|
2125 |
|
---|
2126 | AutoWriteLock alock (mMapLock);
|
---|
2127 | if (mInUninit)
|
---|
2128 | return;
|
---|
2129 |
|
---|
2130 | mDependentChildren.push_back (child);
|
---|
2131 | }
|
---|
2132 |
|
---|
2133 | /**
|
---|
2134 | * Removes the given child from the list of dependent children.
|
---|
2135 | * Must be called from the child's uninit() method,
|
---|
2136 | * under the child's lock.
|
---|
2137 | *
|
---|
2138 | * @param C the child object to remove (must inherit VirtualBoxBase AND
|
---|
2139 | * implement some interface)
|
---|
2140 | */
|
---|
2141 | void removeDependentChild (C *child)
|
---|
2142 | {
|
---|
2143 | AssertReturn (child, (void) 0);
|
---|
2144 |
|
---|
2145 | AutoWriteLock alock (mMapLock);
|
---|
2146 | if (mInUninit)
|
---|
2147 | return;
|
---|
2148 |
|
---|
2149 | mDependentChildren.remove (child);
|
---|
2150 | }
|
---|
2151 |
|
---|
2152 | protected:
|
---|
2153 |
|
---|
2154 | /**
|
---|
2155 | * Returns an internal lock handle to lock the list of children
|
---|
2156 | * returned by #dependentChildren() using AutoReadLock/AutoWriteLock:
|
---|
2157 | * <code>
|
---|
2158 | * AutoReadLock alock (dependentChildrenLock());
|
---|
2159 | * </code>
|
---|
2160 | *
|
---|
2161 | * This is necessary for example to access the list of children returned by
|
---|
2162 | * #dependentChildren().
|
---|
2163 | */
|
---|
2164 | RWLockHandle *dependentChildrenLock() const { return &mMapLock; }
|
---|
2165 |
|
---|
2166 | /**
|
---|
2167 | * Returns the read-only list of all dependent children.
|
---|
2168 | * @note
|
---|
2169 | * Access the returned list (iterate, get size etc.) only after
|
---|
2170 | * doing |AutoWriteLock alock (dependentChildrenLock());|!
|
---|
2171 | */
|
---|
2172 | const DependentChildren &dependentChildren() const { return mDependentChildren; }
|
---|
2173 |
|
---|
2174 | /**
|
---|
2175 | * Uninitializes all dependent children registered with #addDependentChild().
|
---|
2176 | *
|
---|
2177 | * @note
|
---|
2178 | * This method will call uninit() methods of children. If these methods
|
---|
2179 | * access the parent object, uninitDependentChildren() must be called
|
---|
2180 | * either at the beginning of the parent uninitialization sequence (when
|
---|
2181 | * it is still operational) or after setReady(false) is called to
|
---|
2182 | * indicate the parent is out of action.
|
---|
2183 | */
|
---|
2184 | void uninitDependentChildren()
|
---|
2185 | {
|
---|
2186 | AutoWriteLock alock (this);
|
---|
2187 | AutoWriteLock mapLock (mMapLock);
|
---|
2188 |
|
---|
2189 | if (mDependentChildren.size())
|
---|
2190 | {
|
---|
2191 | // set flag to ignore #removeDependentChild() called from child->uninit()
|
---|
2192 | mInUninit = true;
|
---|
2193 |
|
---|
2194 | // leave the locks to let children waiting for #removeDependentChild() run
|
---|
2195 | mapLock.leave();
|
---|
2196 | alock.leave();
|
---|
2197 |
|
---|
2198 | for (typename DependentChildren::iterator it = mDependentChildren.begin();
|
---|
2199 | it != mDependentChildren.end(); ++ it)
|
---|
2200 | {
|
---|
2201 | C *child = (*it);
|
---|
2202 | Assert (child);
|
---|
2203 | if (child)
|
---|
2204 | child->uninit();
|
---|
2205 | }
|
---|
2206 | mDependentChildren.clear();
|
---|
2207 |
|
---|
2208 | alock.enter();
|
---|
2209 | mapLock.enter();
|
---|
2210 |
|
---|
2211 | mInUninit = false;
|
---|
2212 | }
|
---|
2213 | }
|
---|
2214 |
|
---|
2215 | /**
|
---|
2216 | * Removes (detaches) all dependent children registered with
|
---|
2217 | * #addDependentChild(), without uninitializing them.
|
---|
2218 | *
|
---|
2219 | * @note This method must be called from under the main object's lock
|
---|
2220 | */
|
---|
2221 | void removeDependentChildren()
|
---|
2222 | {
|
---|
2223 | AutoWriteLock alock (mMapLock);
|
---|
2224 | mDependentChildren.clear();
|
---|
2225 | }
|
---|
2226 |
|
---|
2227 | private:
|
---|
2228 |
|
---|
2229 | DependentChildren mDependentChildren;
|
---|
2230 |
|
---|
2231 | bool mInUninit;
|
---|
2232 | mutable RWLockHandle mMapLock;
|
---|
2233 | };
|
---|
2234 |
|
---|
2235 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2236 |
|
---|
2237 | /**
|
---|
2238 | * Base class to track component's chlidren of the particular type.
|
---|
2239 | *
|
---|
2240 | * This class is similar to VirtualBoxBaseWithChildren, with the exception that
|
---|
2241 | * all children must be of the same type. For this reason, it's not necessary to
|
---|
2242 | * use a map to store children -- a list is used instead.
|
---|
2243 | *
|
---|
2244 | * Also, as opposed to VirtualBoxBaseWithChildren, children added by
|
---|
2245 | * #addDependentChild() are <b>strongly</b> referenced, so that they cannot be
|
---|
2246 | * deleted until #removeDependentChild() is called on them.
|
---|
2247 | *
|
---|
2248 | * See individual method descriptions for more information.
|
---|
2249 | *
|
---|
2250 | * @param C Type of child objects (must inherit VirtualBoxBase AND implementsome
|
---|
2251 | * interface).
|
---|
2252 | *
|
---|
2253 | * @todo This is a VirtualBoxBaseWithChildren equivalent that uses the
|
---|
2254 | * VirtualBoxBaseNEXT implementation. Will completely supercede
|
---|
2255 | * VirtualBoxBaseWithChildren after the old VirtualBoxBase implementation
|
---|
2256 | * has gone.
|
---|
2257 | */
|
---|
2258 | template <class C>
|
---|
2259 | class VirtualBoxBaseWithTypedChildrenNEXT : public VirtualBoxBaseNEXT
|
---|
2260 | {
|
---|
2261 | public:
|
---|
2262 |
|
---|
2263 | typedef std::list <ComObjPtr <C> > DependentChildren;
|
---|
2264 |
|
---|
2265 | VirtualBoxBaseWithTypedChildrenNEXT() {}
|
---|
2266 |
|
---|
2267 | virtual ~VirtualBoxBaseWithTypedChildrenNEXT() {}
|
---|
2268 |
|
---|
2269 | /**
|
---|
2270 | * Adds the given child to the list of dependent children.
|
---|
2271 | *
|
---|
2272 | * Usually gets called from the child's init() method.
|
---|
2273 | *
|
---|
2274 | * @note Locks this object for writing.
|
---|
2275 | *
|
---|
2276 | * @note @a aChild (unless it is in InInit state) must be protected by
|
---|
2277 | * VirtualBoxBase::AutoCaller to make sure it is not uninitialized on
|
---|
2278 | * another thread during this method's call.
|
---|
2279 | *
|
---|
2280 | * @note If this method is called from under the child's read or write lock,
|
---|
2281 | * make sure the {parent, child} locking order is preserved by locking
|
---|
2282 | * the callee (this object) for writing before the child's lock.
|
---|
2283 | *
|
---|
2284 | * @param aChild Child object to add.
|
---|
2285 | */
|
---|
2286 | void addDependentChild (C *aChild)
|
---|
2287 | {
|
---|
2288 | AssertReturnVoid (aChild);
|
---|
2289 |
|
---|
2290 | AutoCaller autoCaller (this);
|
---|
2291 |
|
---|
2292 | /* sanity */
|
---|
2293 | AssertReturnVoid (autoCaller.state() == InInit ||
|
---|
2294 | autoCaller.state() == Ready ||
|
---|
2295 | autoCaller.state() == Limited);
|
---|
2296 |
|
---|
2297 | AutoWriteLock alock (this);
|
---|
2298 | mDependentChildren.push_back (aChild);
|
---|
2299 | }
|
---|
2300 |
|
---|
2301 | /**
|
---|
2302 | * Removes the given child from the list of dependent children.
|
---|
2303 | *
|
---|
2304 | * Usually gets called from the child's uninit() method.
|
---|
2305 | *
|
---|
2306 | * Note that once this method returns, the callee (this object) is not
|
---|
2307 | * guaranteed to be valid any more, so the caller must not call its
|
---|
2308 | * other methods.
|
---|
2309 | *
|
---|
2310 | * @note Locks this object for writing.
|
---|
2311 | *
|
---|
2312 | * @note @a aChild (unless it is in InUninit state) must be protected by
|
---|
2313 | * VirtualBoxBase::AutoCaller to make sure it is not uninitialized on
|
---|
2314 | * another thread during this method's call.
|
---|
2315 | *
|
---|
2316 | * @note If this method is called from under the child's read or write lock,
|
---|
2317 | * make sure the {parent, child} locking order is preserved by locking
|
---|
2318 | * the callee (this object) for writing before the child's lock.
|
---|
2319 | *
|
---|
2320 | * @param aChild Child object to remove.
|
---|
2321 | */
|
---|
2322 | void removeDependentChild (C *aChild)
|
---|
2323 | {
|
---|
2324 | AssertReturnVoid (aChild);
|
---|
2325 |
|
---|
2326 | AutoCaller autoCaller (this);
|
---|
2327 |
|
---|
2328 | /* return shortly; uninitDependentChildren() will do the job */
|
---|
2329 | if (autoCaller.state() == InUninit)
|
---|
2330 | return;
|
---|
2331 |
|
---|
2332 | AutoWriteLock alock (this);
|
---|
2333 | mDependentChildren.remove (aChild);
|
---|
2334 | }
|
---|
2335 |
|
---|
2336 | protected:
|
---|
2337 |
|
---|
2338 | /**
|
---|
2339 | * Returns the read-only list of all dependent children.
|
---|
2340 | *
|
---|
2341 | * @note Access the returned list (iterate, get size etc.) only after
|
---|
2342 | * locking this object for reading or for writing!
|
---|
2343 | */
|
---|
2344 | const DependentChildren &dependentChildren() const { return mDependentChildren; }
|
---|
2345 |
|
---|
2346 | /**
|
---|
2347 | * Uninitializes all dependent children registered on this object with
|
---|
2348 | * #addDependentChild().
|
---|
2349 | *
|
---|
2350 | * Must be called from within the VirtualBoxBaseProto::AutoUninitSpan (i.e.
|
---|
2351 | * typically from this object's uninit() method) to uninitialize children
|
---|
2352 | * before this object goes out of service and becomes unusable.
|
---|
2353 | *
|
---|
2354 | * Note that this method will call uninit() methods of child objects. If
|
---|
2355 | * these methods need to call the parent object during uninitialization,
|
---|
2356 | * #uninitDependentChildren() must be called before the relevant part of the
|
---|
2357 | * parent is uninitialized, usually at the begnning of the parent
|
---|
2358 | * uninitialization sequence.
|
---|
2359 | *
|
---|
2360 | * @note May lock this object through the called children.
|
---|
2361 | */
|
---|
2362 | void uninitDependentChildren()
|
---|
2363 | {
|
---|
2364 | AutoCaller autoCaller (this);
|
---|
2365 |
|
---|
2366 | /* We cannot hold the write lock (necessary here to protect
|
---|
2367 | * mDependentChildren) when uninitializing children because we want to
|
---|
2368 | * avoid a possible deadlock where we could get stuck in child->uninit()
|
---|
2369 | * blocked by AutoUninitSpan waiting for the number of child's callers
|
---|
2370 | * to drop to zero, while some caller is stuck in our
|
---|
2371 | * removeDependentChild() method waiting for the write lock.
|
---|
2372 | *
|
---|
2373 | * The only safe place to not lock and keep accessing our data members
|
---|
2374 | * is the InUninit state (no active call to our object may exist on
|
---|
2375 | * another thread when we are in InUinint, provided that all such calls
|
---|
2376 | * use the AutoCaller class of course). InUinint is also used as a flag
|
---|
2377 | * by removeDependentChild() that prevents touching mDependentChildren
|
---|
2378 | * from outside. Therefore, we assert.
|
---|
2379 | */
|
---|
2380 | AssertReturnVoid (autoCaller.state() == InUninit);
|
---|
2381 |
|
---|
2382 | if (mDependentChildren.size())
|
---|
2383 | {
|
---|
2384 | for (typename DependentChildren::iterator it = mDependentChildren.begin();
|
---|
2385 | it != mDependentChildren.end(); ++ it)
|
---|
2386 | {
|
---|
2387 | C *child = (*it);
|
---|
2388 | Assert (child);
|
---|
2389 |
|
---|
2390 | /* Note that if child->uninit() happens to be called on another
|
---|
2391 | * thread right before us and is not yet finished; the second
|
---|
2392 | * uninit() call will wait until the first one has done so
|
---|
2393 | * (thanks to AutoUninitSpan). */
|
---|
2394 | if (child)
|
---|
2395 | child->uninit();
|
---|
2396 | }
|
---|
2397 |
|
---|
2398 | /* release all strong references we hold */
|
---|
2399 | mDependentChildren.clear();
|
---|
2400 | }
|
---|
2401 | }
|
---|
2402 |
|
---|
2403 | /**
|
---|
2404 | * Removes (detaches) all dependent children registered with
|
---|
2405 | * #addDependentChild(), without uninitializing them.
|
---|
2406 | *
|
---|
2407 | * @note @a |this| (unless it is in InUninit state) must be protected by
|
---|
2408 | * VirtualBoxBase::AutoCaller to make sure it is not uninitialized on
|
---|
2409 | * another thread during this method's call.
|
---|
2410 | *
|
---|
2411 | * @note Locks this object for writing.
|
---|
2412 | */
|
---|
2413 | void removeDependentChildren()
|
---|
2414 | {
|
---|
2415 | AutoWriteLock alock (this);
|
---|
2416 | mDependentChildren.clear();
|
---|
2417 | }
|
---|
2418 |
|
---|
2419 | private:
|
---|
2420 |
|
---|
2421 | DependentChildren mDependentChildren;
|
---|
2422 | };
|
---|
2423 |
|
---|
2424 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2425 |
|
---|
2426 | /// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
|
---|
2427 | /**
|
---|
2428 | * Simple template that manages data structure allocation/deallocation
|
---|
2429 | * and supports data pointer sharing (the instance that shares the pointer is
|
---|
2430 | * not responsible for memory deallocation as opposed to the instance that
|
---|
2431 | * owns it).
|
---|
2432 | */
|
---|
2433 | template <class D>
|
---|
2434 | class Shareable
|
---|
2435 | {
|
---|
2436 | public:
|
---|
2437 |
|
---|
2438 | Shareable() : mData (NULL), mIsShared (FALSE) {}
|
---|
2439 | ~Shareable() { free(); }
|
---|
2440 |
|
---|
2441 | void allocate() { attach (new D); }
|
---|
2442 |
|
---|
2443 | virtual void free() {
|
---|
2444 | if (mData) {
|
---|
2445 | if (!mIsShared)
|
---|
2446 | delete mData;
|
---|
2447 | mData = NULL;
|
---|
2448 | mIsShared = false;
|
---|
2449 | }
|
---|
2450 | }
|
---|
2451 |
|
---|
2452 | void attach (D *data) {
|
---|
2453 | AssertMsg (data, ("new data must not be NULL"));
|
---|
2454 | if (data && mData != data) {
|
---|
2455 | if (mData && !mIsShared)
|
---|
2456 | delete mData;
|
---|
2457 | mData = data;
|
---|
2458 | mIsShared = false;
|
---|
2459 | }
|
---|
2460 | }
|
---|
2461 |
|
---|
2462 | void attach (Shareable &data) {
|
---|
2463 | AssertMsg (
|
---|
2464 | data.mData == mData || !data.mIsShared,
|
---|
2465 | ("new data must not be shared")
|
---|
2466 | );
|
---|
2467 | if (this != &data && !data.mIsShared) {
|
---|
2468 | attach (data.mData);
|
---|
2469 | data.mIsShared = true;
|
---|
2470 | }
|
---|
2471 | }
|
---|
2472 |
|
---|
2473 | void share (D *data) {
|
---|
2474 | AssertMsg (data, ("new data must not be NULL"));
|
---|
2475 | if (mData != data) {
|
---|
2476 | if (mData && !mIsShared)
|
---|
2477 | delete mData;
|
---|
2478 | mData = data;
|
---|
2479 | mIsShared = true;
|
---|
2480 | }
|
---|
2481 | }
|
---|
2482 |
|
---|
2483 | void share (const Shareable &data) { share (data.mData); }
|
---|
2484 |
|
---|
2485 | void attachCopy (const D *data) {
|
---|
2486 | AssertMsg (data, ("data to copy must not be NULL"));
|
---|
2487 | if (data)
|
---|
2488 | attach (new D (*data));
|
---|
2489 | }
|
---|
2490 |
|
---|
2491 | void attachCopy (const Shareable &data) {
|
---|
2492 | attachCopy (data.mData);
|
---|
2493 | }
|
---|
2494 |
|
---|
2495 | virtual D *detach() {
|
---|
2496 | D *d = mData;
|
---|
2497 | mData = NULL;
|
---|
2498 | mIsShared = false;
|
---|
2499 | return d;
|
---|
2500 | }
|
---|
2501 |
|
---|
2502 | D *data() const {
|
---|
2503 | return mData;
|
---|
2504 | }
|
---|
2505 |
|
---|
2506 | D *operator->() const {
|
---|
2507 | AssertMsg (mData, ("data must not be NULL"));
|
---|
2508 | return mData;
|
---|
2509 | }
|
---|
2510 |
|
---|
2511 | bool isNull() const { return mData == NULL; }
|
---|
2512 | bool operator!() const { return isNull(); }
|
---|
2513 |
|
---|
2514 | bool isShared() const { return mIsShared; }
|
---|
2515 |
|
---|
2516 | protected:
|
---|
2517 |
|
---|
2518 | D *mData;
|
---|
2519 | bool mIsShared;
|
---|
2520 | };
|
---|
2521 |
|
---|
2522 | /// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
|
---|
2523 | /**
|
---|
2524 | * Simple template that enhances Shareable<> and supports data
|
---|
2525 | * backup/rollback/commit (using the copy constructor of the managed data
|
---|
2526 | * structure).
|
---|
2527 | */
|
---|
2528 | template <class D>
|
---|
2529 | class Backupable : public Shareable <D>
|
---|
2530 | {
|
---|
2531 | public:
|
---|
2532 |
|
---|
2533 | Backupable() : Shareable <D> (), mBackupData (NULL) {}
|
---|
2534 |
|
---|
2535 | void free()
|
---|
2536 | {
|
---|
2537 | AssertMsg (this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
|
---|
2538 | rollback();
|
---|
2539 | Shareable <D>::free();
|
---|
2540 | }
|
---|
2541 |
|
---|
2542 | D *detach()
|
---|
2543 | {
|
---|
2544 | AssertMsg (this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
|
---|
2545 | rollback();
|
---|
2546 | return Shareable <D>::detach();
|
---|
2547 | }
|
---|
2548 |
|
---|
2549 | void share (const Backupable &data)
|
---|
2550 | {
|
---|
2551 | AssertMsg (!data.isBackedUp(), ("data to share must not be backed up"));
|
---|
2552 | if (!data.isBackedUp())
|
---|
2553 | Shareable <D>::share (data.mData);
|
---|
2554 | }
|
---|
2555 |
|
---|
2556 | /**
|
---|
2557 | * Stores the current data pointer in the backup area, allocates new data
|
---|
2558 | * using the copy constructor on current data and makes new data active.
|
---|
2559 | */
|
---|
2560 | void backup()
|
---|
2561 | {
|
---|
2562 | AssertMsg (this->mData, ("data must not be NULL"));
|
---|
2563 | if (this->mData && !mBackupData)
|
---|
2564 | {
|
---|
2565 | mBackupData = this->mData;
|
---|
2566 | this->mData = new D (*mBackupData);
|
---|
2567 | }
|
---|
2568 | }
|
---|
2569 |
|
---|
2570 | /**
|
---|
2571 | * Deletes new data created by #backup() and restores previous data pointer
|
---|
2572 | * stored in the backup area, making it active again.
|
---|
2573 | */
|
---|
2574 | void rollback()
|
---|
2575 | {
|
---|
2576 | if (this->mData && mBackupData)
|
---|
2577 | {
|
---|
2578 | delete this->mData;
|
---|
2579 | this->mData = mBackupData;
|
---|
2580 | mBackupData = NULL;
|
---|
2581 | }
|
---|
2582 | }
|
---|
2583 |
|
---|
2584 | /**
|
---|
2585 | * Commits current changes by deleting backed up data and clearing up the
|
---|
2586 | * backup area. The new data pointer created by #backup() remains active
|
---|
2587 | * and becomes the only managed pointer.
|
---|
2588 | *
|
---|
2589 | * This method is much faster than #commitCopy() (just a single pointer
|
---|
2590 | * assignment operation), but makes the previous data pointer invalid
|
---|
2591 | * (because it is freed). For this reason, this method must not be
|
---|
2592 | * used if it's possible that data managed by this instance is shared with
|
---|
2593 | * some other Shareable instance. See #commitCopy().
|
---|
2594 | */
|
---|
2595 | void commit()
|
---|
2596 | {
|
---|
2597 | if (this->mData && mBackupData)
|
---|
2598 | {
|
---|
2599 | if (!this->mIsShared)
|
---|
2600 | delete mBackupData;
|
---|
2601 | mBackupData = NULL;
|
---|
2602 | this->mIsShared = false;
|
---|
2603 | }
|
---|
2604 | }
|
---|
2605 |
|
---|
2606 | /**
|
---|
2607 | * Commits current changes by assigning new data to the previous data
|
---|
2608 | * pointer stored in the backup area using the assignment operator.
|
---|
2609 | * New data is deleted, the backup area is cleared and the previous data
|
---|
2610 | * pointer becomes active and the only managed pointer.
|
---|
2611 | *
|
---|
2612 | * This method is slower than #commit(), but it keeps the previous data
|
---|
2613 | * pointer valid (i.e. new data is copied to the same memory location).
|
---|
2614 | * For that reason it's safe to use this method on instances that share
|
---|
2615 | * managed data with other Shareable instances.
|
---|
2616 | */
|
---|
2617 | void commitCopy()
|
---|
2618 | {
|
---|
2619 | if (this->mData && mBackupData)
|
---|
2620 | {
|
---|
2621 | *mBackupData = *(this->mData);
|
---|
2622 | delete this->mData;
|
---|
2623 | this->mData = mBackupData;
|
---|
2624 | mBackupData = NULL;
|
---|
2625 | }
|
---|
2626 | }
|
---|
2627 |
|
---|
2628 | void assignCopy (const D *data)
|
---|
2629 | {
|
---|
2630 | AssertMsg (this->mData, ("data must not be NULL"));
|
---|
2631 | AssertMsg (data, ("data to copy must not be NULL"));
|
---|
2632 | if (this->mData && data)
|
---|
2633 | {
|
---|
2634 | if (!mBackupData)
|
---|
2635 | {
|
---|
2636 | mBackupData = this->mData;
|
---|
2637 | this->mData = new D (*data);
|
---|
2638 | }
|
---|
2639 | else
|
---|
2640 | *this->mData = *data;
|
---|
2641 | }
|
---|
2642 | }
|
---|
2643 |
|
---|
2644 | void assignCopy (const Backupable &data)
|
---|
2645 | {
|
---|
2646 | assignCopy (data.mData);
|
---|
2647 | }
|
---|
2648 |
|
---|
2649 | bool isBackedUp() const
|
---|
2650 | {
|
---|
2651 | return mBackupData != NULL;
|
---|
2652 | }
|
---|
2653 |
|
---|
2654 | bool hasActualChanges() const
|
---|
2655 | {
|
---|
2656 | AssertMsg (this->mData, ("data must not be NULL"));
|
---|
2657 | return this->mData != NULL && mBackupData != NULL &&
|
---|
2658 | !(*this->mData == *mBackupData);
|
---|
2659 | }
|
---|
2660 |
|
---|
2661 | D *backedUpData() const
|
---|
2662 | {
|
---|
2663 | return mBackupData;
|
---|
2664 | }
|
---|
2665 |
|
---|
2666 | protected:
|
---|
2667 |
|
---|
2668 | D *mBackupData;
|
---|
2669 | };
|
---|
2670 |
|
---|
2671 | #if defined VBOX_MAIN_SETTINGS_ADDONS
|
---|
2672 |
|
---|
2673 | /**
|
---|
2674 | * Settinsg API additions.
|
---|
2675 | */
|
---|
2676 | namespace settings
|
---|
2677 | {
|
---|
2678 |
|
---|
2679 | /// @todo once string data in Bstr and Utf8Str is auto_ref_ptr, enable the
|
---|
2680 | /// code below
|
---|
2681 |
|
---|
2682 | #if 0
|
---|
2683 |
|
---|
2684 | /** Specialization of FromString for Bstr. */
|
---|
2685 | template<> com::Bstr FromString <com::Bstr> (const char *aValue);
|
---|
2686 |
|
---|
2687 | #endif
|
---|
2688 |
|
---|
2689 | /** Specialization of ToString for Bstr. */
|
---|
2690 | template<> stdx::char_auto_ptr
|
---|
2691 | ToString <com::Bstr> (const com::Bstr &aValue, unsigned int aExtra);
|
---|
2692 |
|
---|
2693 | /** Specialization of FromString for Guid. */
|
---|
2694 | template<> com::Guid FromString <com::Guid> (const char *aValue);
|
---|
2695 |
|
---|
2696 | /** Specialization of ToString for Guid. */
|
---|
2697 | template<> stdx::char_auto_ptr
|
---|
2698 | ToString <com::Guid> (const com::Guid &aValue, unsigned int aExtra);
|
---|
2699 |
|
---|
2700 | } /* namespace settings */
|
---|
2701 |
|
---|
2702 | #endif /* VBOX_MAIN_SETTINGS_ADDONS */
|
---|
2703 |
|
---|
2704 | #endif // ____H_VIRTUALBOXBASEIMPL
|
---|