1 | /** @file
|
---|
2 | * VirtualBox COM base classes definition
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #ifndef ____H_VIRTUALBOXBASEIMPL
|
---|
18 | #define ____H_VIRTUALBOXBASEIMPL
|
---|
19 |
|
---|
20 | #include <iprt/cdefs.h>
|
---|
21 | #include <iprt/thread.h>
|
---|
22 |
|
---|
23 | #include <list>
|
---|
24 | #include <map>
|
---|
25 |
|
---|
26 | #include "VBox/com/ErrorInfo.h"
|
---|
27 | #include "VBox/com/SupportErrorInfo.h"
|
---|
28 | #include "VBox/com/AutoLock.h"
|
---|
29 |
|
---|
30 | #include "VBox/com/VirtualBox.h"
|
---|
31 |
|
---|
32 | // avoid including VBox/settings.h and VBox/xml.h;
|
---|
33 | // only declare the classes
|
---|
34 | namespace xml
|
---|
35 | {
|
---|
36 | class File;
|
---|
37 | }
|
---|
38 |
|
---|
39 | using namespace com;
|
---|
40 | using namespace util;
|
---|
41 |
|
---|
42 | class AutoInitSpan;
|
---|
43 | class AutoUninitSpan;
|
---|
44 |
|
---|
45 | class VirtualBox;
|
---|
46 | class Machine;
|
---|
47 | class Medium;
|
---|
48 | class Host;
|
---|
49 | typedef std::list< ComObjPtr<Medium> > MediaList;
|
---|
50 |
|
---|
51 | ////////////////////////////////////////////////////////////////////////////////
|
---|
52 | //
|
---|
53 | // COM helpers
|
---|
54 | //
|
---|
55 | ////////////////////////////////////////////////////////////////////////////////
|
---|
56 |
|
---|
57 | #if !defined (VBOX_WITH_XPCOM)
|
---|
58 |
|
---|
59 | #include <atlcom.h>
|
---|
60 |
|
---|
61 | /* use a special version of the singleton class factory,
|
---|
62 | * see KB811591 in msdn for more info. */
|
---|
63 |
|
---|
64 | #undef DECLARE_CLASSFACTORY_SINGLETON
|
---|
65 | #define DECLARE_CLASSFACTORY_SINGLETON(obj) DECLARE_CLASSFACTORY_EX(CMyComClassFactorySingleton<obj>)
|
---|
66 |
|
---|
67 | template <class T>
|
---|
68 | class CMyComClassFactorySingleton : public CComClassFactory
|
---|
69 | {
|
---|
70 | public:
|
---|
71 | CMyComClassFactorySingleton() : m_hrCreate(S_OK){}
|
---|
72 | virtual ~CMyComClassFactorySingleton(){}
|
---|
73 | // IClassFactory
|
---|
74 | STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObj)
|
---|
75 | {
|
---|
76 | HRESULT hRes = E_POINTER;
|
---|
77 | if (ppvObj != NULL)
|
---|
78 | {
|
---|
79 | *ppvObj = NULL;
|
---|
80 | // Aggregation is not supported in singleton objects.
|
---|
81 | ATLASSERT(pUnkOuter == NULL);
|
---|
82 | if (pUnkOuter != NULL)
|
---|
83 | hRes = CLASS_E_NOAGGREGATION;
|
---|
84 | else
|
---|
85 | {
|
---|
86 | if (m_hrCreate == S_OK && m_spObj == NULL)
|
---|
87 | {
|
---|
88 | Lock();
|
---|
89 | __try
|
---|
90 | {
|
---|
91 | // Fix: The following If statement was moved inside the __try statement.
|
---|
92 | // Did another thread arrive here first?
|
---|
93 | if (m_hrCreate == S_OK && m_spObj == NULL)
|
---|
94 | {
|
---|
95 | // lock the module to indicate activity
|
---|
96 | // (necessary for the monitor shutdown thread to correctly
|
---|
97 | // terminate the module in case when CreateInstance() fails)
|
---|
98 | _pAtlModule->Lock();
|
---|
99 | CComObjectCached<T> *p;
|
---|
100 | m_hrCreate = CComObjectCached<T>::CreateInstance(&p);
|
---|
101 | if (SUCCEEDED(m_hrCreate))
|
---|
102 | {
|
---|
103 | m_hrCreate = p->QueryInterface(IID_IUnknown, (void**)&m_spObj);
|
---|
104 | if (FAILED(m_hrCreate))
|
---|
105 | {
|
---|
106 | delete p;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | _pAtlModule->Unlock();
|
---|
110 | }
|
---|
111 | }
|
---|
112 | __finally
|
---|
113 | {
|
---|
114 | Unlock();
|
---|
115 | }
|
---|
116 | }
|
---|
117 | if (m_hrCreate == S_OK)
|
---|
118 | {
|
---|
119 | hRes = m_spObj->QueryInterface(riid, ppvObj);
|
---|
120 | }
|
---|
121 | else
|
---|
122 | {
|
---|
123 | hRes = m_hrCreate;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | return hRes;
|
---|
128 | }
|
---|
129 | HRESULT m_hrCreate;
|
---|
130 | CComPtr<IUnknown> m_spObj;
|
---|
131 | };
|
---|
132 |
|
---|
133 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
134 |
|
---|
135 | ////////////////////////////////////////////////////////////////////////////////
|
---|
136 | //
|
---|
137 | // Macros
|
---|
138 | //
|
---|
139 | ////////////////////////////////////////////////////////////////////////////////
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Special version of the Assert macro to be used within VirtualBoxBase
|
---|
143 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
144 | *
|
---|
145 | * In the debug build, this macro is equivalent to Assert.
|
---|
146 | * In the release build, this macro uses |setError(E_FAIL, ...)| to set the
|
---|
147 | * error info from the asserted expression.
|
---|
148 | *
|
---|
149 | * @see VirtualBoxSupportErrorInfoImpl::setError
|
---|
150 | *
|
---|
151 | * @param expr Expression which should be true.
|
---|
152 | */
|
---|
153 | #if defined (DEBUG)
|
---|
154 | #define ComAssert(expr) Assert(expr)
|
---|
155 | #else
|
---|
156 | #define ComAssert(expr) \
|
---|
157 | do { \
|
---|
158 | if (RT_UNLIKELY(!(expr))) \
|
---|
159 | setError(E_FAIL, \
|
---|
160 | "Assertion failed: [%s] at '%s' (%d) in %s.\nPlease contact the product vendor!", \
|
---|
161 | #expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
|
---|
162 | } while (0)
|
---|
163 | #endif
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Special version of the AssertMsg macro to be used within VirtualBoxBase
|
---|
167 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
168 | *
|
---|
169 | * See ComAssert for more info.
|
---|
170 | *
|
---|
171 | * @param expr Expression which should be true.
|
---|
172 | * @param a printf argument list (in parenthesis).
|
---|
173 | */
|
---|
174 | #if defined (DEBUG)
|
---|
175 | #define ComAssertMsg(expr, a) AssertMsg(expr, a)
|
---|
176 | #else
|
---|
177 | #define ComAssertMsg(expr, a) \
|
---|
178 | do { \
|
---|
179 | if (RT_UNLIKELY(!(expr))) \
|
---|
180 | setError(E_FAIL, \
|
---|
181 | "Assertion failed: [%s] at '%s' (%d) in %s.\n%s.\nPlease contact the product vendor!", \
|
---|
182 | #expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
|
---|
183 | } while (0)
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Special version of the AssertRC macro to be used within VirtualBoxBase
|
---|
188 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
189 | *
|
---|
190 | * See ComAssert for more info.
|
---|
191 | *
|
---|
192 | * @param vrc VBox status code.
|
---|
193 | */
|
---|
194 | #if defined (DEBUG)
|
---|
195 | #define ComAssertRC(vrc) AssertRC(vrc)
|
---|
196 | #else
|
---|
197 | #define ComAssertRC(vrc) ComAssertMsgRC(vrc, ("%Rra", vrc))
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Special version of the AssertMsgRC macro to be used within VirtualBoxBase
|
---|
202 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
203 | *
|
---|
204 | * See ComAssert for more info.
|
---|
205 | *
|
---|
206 | * @param vrc VBox status code.
|
---|
207 | * @param msg printf argument list (in parenthesis).
|
---|
208 | */
|
---|
209 | #if defined (DEBUG)
|
---|
210 | #define ComAssertMsgRC(vrc, msg) AssertMsgRC(vrc, msg)
|
---|
211 | #else
|
---|
212 | #define ComAssertMsgRC(vrc, msg) ComAssertMsg(RT_SUCCESS(vrc), msg)
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Special version of the AssertComRC macro to be used within VirtualBoxBase
|
---|
217 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
218 | *
|
---|
219 | * See ComAssert for more info.
|
---|
220 | *
|
---|
221 | * @param rc COM result code
|
---|
222 | */
|
---|
223 | #if defined (DEBUG)
|
---|
224 | #define ComAssertComRC(rc) AssertComRC(rc)
|
---|
225 | #else
|
---|
226 | #define ComAssertComRC(rc) ComAssertMsg(SUCCEEDED(rc), ("COM RC = %Rhrc (0x%08X)", (rc), (rc)))
|
---|
227 | #endif
|
---|
228 |
|
---|
229 |
|
---|
230 | /** Special version of ComAssert that returns ret if expr fails */
|
---|
231 | #define ComAssertRet(expr, ret) \
|
---|
232 | do { ComAssert(expr); if (!(expr)) return (ret); } while (0)
|
---|
233 | /** Special version of ComAssertMsg that returns ret if expr fails */
|
---|
234 | #define ComAssertMsgRet(expr, a, ret) \
|
---|
235 | do { ComAssertMsg(expr, a); if (!(expr)) return (ret); } while (0)
|
---|
236 | /** Special version of ComAssertRC that returns ret if vrc does not succeed */
|
---|
237 | #define ComAssertRCRet(vrc, ret) \
|
---|
238 | do { ComAssertRC(vrc); if (!RT_SUCCESS(vrc)) return (ret); } while (0)
|
---|
239 | /** Special version of ComAssertMsgRC that returns ret if vrc does not succeed */
|
---|
240 | #define ComAssertMsgRCRet(vrc, msg, ret) \
|
---|
241 | do { ComAssertMsgRC(vrc, msg); if (!RT_SUCCESS(vrc)) return (ret); } while (0)
|
---|
242 | /** Special version of ComAssertFailed that returns ret */
|
---|
243 | #define ComAssertFailedRet(ret) \
|
---|
244 | do { ComAssertFailed(); return (ret); } while (0)
|
---|
245 | /** Special version of ComAssertMsgFailed that returns ret */
|
---|
246 | #define ComAssertMsgFailedRet(msg, ret) \
|
---|
247 | do { ComAssertMsgFailed(msg); return (ret); } while (0)
|
---|
248 | /** Special version of ComAssertComRC that returns ret if rc does not succeed */
|
---|
249 | #define ComAssertComRCRet(rc, ret) \
|
---|
250 | do { ComAssertComRC(rc); if (!SUCCEEDED(rc)) return (ret); } while (0)
|
---|
251 | /** Special version of ComAssertComRC that returns rc if rc does not succeed */
|
---|
252 | #define ComAssertComRCRetRC(rc) \
|
---|
253 | do { ComAssertComRC(rc); if (!SUCCEEDED(rc)) return (rc); } while (0)
|
---|
254 |
|
---|
255 |
|
---|
256 | /** Special version of ComAssert that evaluates eval and breaks if expr fails */
|
---|
257 | #define ComAssertBreak(expr, eval) \
|
---|
258 | if (1) { ComAssert(expr); if (!(expr)) { eval; break; } } else do {} while (0)
|
---|
259 | /** Special version of ComAssertMsg that evaluates eval and breaks if expr fails */
|
---|
260 | #define ComAssertMsgBreak(expr, a, eval) \
|
---|
261 | if (1) { ComAssertMsg(expr, a); if (!(expr)) { eval; break; } } else do {} while (0)
|
---|
262 | /** Special version of ComAssertRC that evaluates eval and breaks if vrc does not succeed */
|
---|
263 | #define ComAssertRCBreak(vrc, eval) \
|
---|
264 | if (1) { ComAssertRC(vrc); if (!RT_SUCCESS(vrc)) { eval; break; } } else do {} while (0)
|
---|
265 | /** Special version of ComAssertMsgRC that evaluates eval and breaks if vrc does not succeed */
|
---|
266 | #define ComAssertMsgRCBreak(vrc, msg, eval) \
|
---|
267 | if (1) { ComAssertMsgRC(vrc, msg); if (!RT_SUCCESS(vrc)) { eval; break; } } else do {} while (0)
|
---|
268 | /** Special version of ComAssertFailed that evaluates eval and breaks */
|
---|
269 | #define ComAssertFailedBreak(eval) \
|
---|
270 | if (1) { ComAssertFailed(); { eval; break; } } else do {} while (0)
|
---|
271 | /** Special version of ComAssertMsgFailed that evaluates eval and breaks */
|
---|
272 | #define ComAssertMsgFailedBreak(msg, eval) \
|
---|
273 | if (1) { ComAssertMsgFailed (msg); { eval; break; } } else do {} while (0)
|
---|
274 | /** Special version of ComAssertComRC that evaluates eval and breaks if rc does not succeed */
|
---|
275 | #define ComAssertComRCBreak(rc, eval) \
|
---|
276 | if (1) { ComAssertComRC(rc); if (!SUCCEEDED(rc)) { eval; break; } } else do {} while (0)
|
---|
277 | /** Special version of ComAssertComRC that just breaks if rc does not succeed */
|
---|
278 | #define ComAssertComRCBreakRC(rc) \
|
---|
279 | if (1) { ComAssertComRC(rc); if (!SUCCEEDED(rc)) { break; } } else do {} while (0)
|
---|
280 |
|
---|
281 |
|
---|
282 | /** Special version of ComAssert that evaluates eval and throws it if expr fails */
|
---|
283 | #define ComAssertThrow(expr, eval) \
|
---|
284 | if (1) { ComAssert(expr); if (!(expr)) { throw (eval); } } else do {} while (0)
|
---|
285 | /** Special version of ComAssertMsg that evaluates eval and throws it if expr fails */
|
---|
286 | #define ComAssertMsgThrow(expr, a, eval) \
|
---|
287 | if (1) { ComAssertMsg(expr, a); if (!(expr)) { throw (eval); } } else do {} while (0)
|
---|
288 | /** Special version of ComAssertRC that evaluates eval and throws it if vrc does not succeed */
|
---|
289 | #define ComAssertRCThrow(vrc, eval) \
|
---|
290 | if (1) { ComAssertRC(vrc); if (!RT_SUCCESS(vrc)) { throw (eval); } } else do {} while (0)
|
---|
291 | /** Special version of ComAssertMsgRC that evaluates eval and throws it if vrc does not succeed */
|
---|
292 | #define ComAssertMsgRCThrow(vrc, msg, eval) \
|
---|
293 | if (1) { ComAssertMsgRC(vrc, msg); if (!RT_SUCCESS(vrc)) { throw (eval); } } else do {} while (0)
|
---|
294 | /** Special version of ComAssertFailed that evaluates eval and throws it */
|
---|
295 | #define ComAssertFailedThrow(eval) \
|
---|
296 | if (1) { ComAssertFailed(); { throw (eval); } } else do {} while (0)
|
---|
297 | /** Special version of ComAssertMsgFailed that evaluates eval and throws it */
|
---|
298 | #define ComAssertMsgFailedThrow(msg, eval) \
|
---|
299 | if (1) { ComAssertMsgFailed (msg); { throw (eval); } } else do {} while (0)
|
---|
300 | /** Special version of ComAssertComRC that evaluates eval and throws it if rc does not succeed */
|
---|
301 | #define ComAssertComRCThrow(rc, eval) \
|
---|
302 | if (1) { ComAssertComRC(rc); if (!SUCCEEDED(rc)) { throw (eval); } } else do {} while (0)
|
---|
303 | /** Special version of ComAssertComRC that just throws rc if rc does not succeed */
|
---|
304 | #define ComAssertComRCThrowRC(rc) \
|
---|
305 | if (1) { ComAssertComRC(rc); if (!SUCCEEDED(rc)) { throw rc; } } else do {} while (0)
|
---|
306 |
|
---|
307 | ////////////////////////////////////////////////////////////////////////////////
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Checks that the pointer argument is not NULL and returns E_INVALIDARG +
|
---|
311 | * extended error info on failure.
|
---|
312 | * @param arg Input pointer-type argument (strings, interface pointers...)
|
---|
313 | */
|
---|
314 | #define CheckComArgNotNull(arg) \
|
---|
315 | do { \
|
---|
316 | if (RT_UNLIKELY((arg) == NULL)) \
|
---|
317 | return setError(E_INVALIDARG, tr("Argument %s is NULL"), #arg); \
|
---|
318 | } while (0)
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Checks that safe array argument is not NULL and returns E_INVALIDARG +
|
---|
322 | * extended error info on failure.
|
---|
323 | * @param arg Input safe array argument (strings, interface pointers...)
|
---|
324 | */
|
---|
325 | #define CheckComArgSafeArrayNotNull(arg) \
|
---|
326 | do { \
|
---|
327 | if (RT_UNLIKELY(ComSafeArrayInIsNull(arg))) \
|
---|
328 | return setError(E_INVALIDARG, tr("Argument %s is NULL"), #arg); \
|
---|
329 | } while (0)
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Checks that the string argument is not a NULL or empty string and returns
|
---|
333 | * E_INVALIDARG + extended error info on failure.
|
---|
334 | * @param arg Input string argument (BSTR etc.).
|
---|
335 | */
|
---|
336 | #define CheckComArgStrNotEmptyOrNull(arg) \
|
---|
337 | do { \
|
---|
338 | if (RT_UNLIKELY((arg) == NULL || *(arg) == '\0')) \
|
---|
339 | return setError(E_INVALIDARG, \
|
---|
340 | tr("Argument %s is empty or NULL"), #arg); \
|
---|
341 | } while (0)
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Checks that the given expression (that must involve the argument) is true and
|
---|
345 | * returns E_INVALIDARG + extended error info on failure.
|
---|
346 | * @param arg Argument.
|
---|
347 | * @param expr Expression to evaluate.
|
---|
348 | */
|
---|
349 | #define CheckComArgExpr(arg, expr) \
|
---|
350 | do { \
|
---|
351 | if (RT_UNLIKELY(!(expr))) \
|
---|
352 | return setError(E_INVALIDARG, \
|
---|
353 | tr("Argument %s is invalid (must be %s)"), #arg, #expr); \
|
---|
354 | } while (0)
|
---|
355 |
|
---|
356 | /**
|
---|
357 | * Checks that the given expression (that must involve the argument) is true and
|
---|
358 | * returns E_INVALIDARG + extended error info on failure. The error message must
|
---|
359 | * be customized.
|
---|
360 | * @param arg Argument.
|
---|
361 | * @param expr Expression to evaluate.
|
---|
362 | * @param msg Parenthesized printf-like expression (must start with a verb,
|
---|
363 | * like "must be one of...", "is not within...").
|
---|
364 | */
|
---|
365 | #define CheckComArgExprMsg(arg, expr, msg) \
|
---|
366 | do { \
|
---|
367 | if (RT_UNLIKELY(!(expr))) \
|
---|
368 | return setError(E_INVALIDARG, tr ("Argument %s %s"), \
|
---|
369 | #arg, Utf8StrFmt msg .raw()); \
|
---|
370 | } while (0)
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Checks that the given pointer to an output argument is valid and returns
|
---|
374 | * E_POINTER + extended error info otherwise.
|
---|
375 | * @param arg Pointer argument.
|
---|
376 | */
|
---|
377 | #define CheckComArgOutPointerValid(arg) \
|
---|
378 | do { \
|
---|
379 | if (RT_UNLIKELY(!VALID_PTR(arg))) \
|
---|
380 | return setError(E_POINTER, \
|
---|
381 | tr("Output argument %s points to invalid memory location (%p)"), \
|
---|
382 | #arg, (void *) (arg)); \
|
---|
383 | } while (0)
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Checks that the given pointer to an output safe array argument is valid and
|
---|
387 | * returns E_POINTER + extended error info otherwise.
|
---|
388 | * @param arg Safe array argument.
|
---|
389 | */
|
---|
390 | #define CheckComArgOutSafeArrayPointerValid(arg) \
|
---|
391 | do { \
|
---|
392 | if (RT_UNLIKELY(ComSafeArrayOutIsNull(arg))) \
|
---|
393 | return setError(E_POINTER, \
|
---|
394 | tr("Output argument %s points to invalid memory location (%p)"), \
|
---|
395 | #arg, (void*)(arg)); \
|
---|
396 | } while (0)
|
---|
397 |
|
---|
398 | /**
|
---|
399 | * Sets the extended error info and returns E_NOTIMPL.
|
---|
400 | */
|
---|
401 | #define ReturnComNotImplemented() \
|
---|
402 | do { \
|
---|
403 | return setError(E_NOTIMPL, tr("Method %s is not implemented"), __FUNCTION__); \
|
---|
404 | } while (0)
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Declares an empty constructor and destructor for the given class.
|
---|
408 | * This is useful to prevent the compiler from generating the default
|
---|
409 | * ctor and dtor, which in turn allows to use forward class statements
|
---|
410 | * (instead of including their header files) when declaring data members of
|
---|
411 | * non-fundamental types with constructors (which are always called implicitly
|
---|
412 | * by constructors and by the destructor of the class).
|
---|
413 | *
|
---|
414 | * This macro is to be placed within (the public section of) the class
|
---|
415 | * declaration. Its counterpart, DEFINE_EMPTY_CTOR_DTOR, must be placed
|
---|
416 | * somewhere in one of the translation units (usually .cpp source files).
|
---|
417 | *
|
---|
418 | * @param cls class to declare a ctor and dtor for
|
---|
419 | */
|
---|
420 | #define DECLARE_EMPTY_CTOR_DTOR(cls) cls(); ~cls();
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Defines an empty constructor and destructor for the given class.
|
---|
424 | * See DECLARE_EMPTY_CTOR_DTOR for more info.
|
---|
425 | */
|
---|
426 | #define DEFINE_EMPTY_CTOR_DTOR(cls) \
|
---|
427 | cls::cls() { /*empty*/ } \
|
---|
428 | cls::~cls() { /*empty*/ }
|
---|
429 |
|
---|
430 | ////////////////////////////////////////////////////////////////////////////////
|
---|
431 | //
|
---|
432 | // VirtualBoxBase
|
---|
433 | //
|
---|
434 | ////////////////////////////////////////////////////////////////////////////////
|
---|
435 |
|
---|
436 | #define VIRTUALBOXBASE_ADD_VIRTUAL_COMPONENT_METHODS(cls, iface) \
|
---|
437 | virtual const IID& getClassIID() const \
|
---|
438 | { \
|
---|
439 | return cls::getStaticClassIID(); \
|
---|
440 | } \
|
---|
441 | static const IID& getStaticClassIID() \
|
---|
442 | { \
|
---|
443 | return COM_IIDOF(iface); \
|
---|
444 | } \
|
---|
445 | virtual const char* getComponentName() const \
|
---|
446 | { \
|
---|
447 | return cls::getStaticComponentName(); \
|
---|
448 | } \
|
---|
449 | static const char* getStaticComponentName() \
|
---|
450 | { \
|
---|
451 | return #cls; \
|
---|
452 | }
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT:
|
---|
456 | * This macro must be used once in the declaration of any class derived
|
---|
457 | * from VirtualBoxBase. It implements the pure virtual getClassIID() and
|
---|
458 | * getComponentName() methods. If this macro is not present, instances
|
---|
459 | * of a class derived from VirtualBoxBase cannot be instantiated.
|
---|
460 | *
|
---|
461 | * @param X The class name, e.g. "Class".
|
---|
462 | * @param IX The interface name which this class implements, e.g. "IClass".
|
---|
463 | */
|
---|
464 | #ifdef VBOX_WITH_XPCOM
|
---|
465 | #define VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(cls, iface) \
|
---|
466 | VIRTUALBOXBASE_ADD_VIRTUAL_COMPONENT_METHODS(cls, iface)
|
---|
467 | #else // #ifdef VBOX_WITH_XPCOM
|
---|
468 | #define VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(cls, iface) \
|
---|
469 | VIRTUALBOXBASE_ADD_VIRTUAL_COMPONENT_METHODS(cls, iface) \
|
---|
470 | STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid) \
|
---|
471 | { \
|
---|
472 | const _ATL_INTMAP_ENTRY* pEntries = cls::_GetEntries(); \
|
---|
473 | Assert(pEntries); \
|
---|
474 | if (!pEntries) \
|
---|
475 | return S_FALSE; \
|
---|
476 | BOOL bSupports = FALSE; \
|
---|
477 | BOOL bISupportErrorInfoFound = FALSE; \
|
---|
478 | while (pEntries->pFunc != NULL && !bSupports) \
|
---|
479 | { \
|
---|
480 | if (!bISupportErrorInfoFound) \
|
---|
481 | bISupportErrorInfoFound = InlineIsEqualGUID(*(pEntries->piid), IID_ISupportErrorInfo); \
|
---|
482 | else \
|
---|
483 | bSupports = InlineIsEqualGUID(*(pEntries->piid), riid); \
|
---|
484 | pEntries++; \
|
---|
485 | } \
|
---|
486 | Assert(bISupportErrorInfoFound); \
|
---|
487 | return bSupports ? S_OK : S_FALSE; \
|
---|
488 | }
|
---|
489 | #endif // #ifdef VBOX_WITH_XPCOM
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Abstract base class for all component classes implementing COM
|
---|
493 | * interfaces of the VirtualBox COM library.
|
---|
494 | *
|
---|
495 | * Declares functionality that should be available in all components.
|
---|
496 | *
|
---|
497 | * Among the basic functionality implemented by this class is the primary object
|
---|
498 | * state that indicates if the object is ready to serve the calls, and if not,
|
---|
499 | * what stage it is currently at. Here is the primary state diagram:
|
---|
500 | *
|
---|
501 | * +-------------------------------------------------------+
|
---|
502 | * | |
|
---|
503 | * | (InitFailed) -----------------------+ |
|
---|
504 | * | ^ | |
|
---|
505 | * v | v |
|
---|
506 | * [*] ---> NotReady ----> (InInit) -----> Ready -----> (InUninit) ----+
|
---|
507 | * ^ |
|
---|
508 | * | v
|
---|
509 | * | Limited
|
---|
510 | * | |
|
---|
511 | * +-------+
|
---|
512 | *
|
---|
513 | * The object is fully operational only when its state is Ready. The Limited
|
---|
514 | * state means that only some vital part of the object is operational, and it
|
---|
515 | * requires some sort of reinitialization to become fully operational. The
|
---|
516 | * NotReady state means the object is basically dead: it either was not yet
|
---|
517 | * initialized after creation at all, or was uninitialized and is waiting to be
|
---|
518 | * destroyed when the last reference to it is released. All other states are
|
---|
519 | * transitional.
|
---|
520 | *
|
---|
521 | * The NotReady->InInit->Ready, NotReady->InInit->Limited and
|
---|
522 | * NotReady->InInit->InitFailed transition is done by the AutoInitSpan smart
|
---|
523 | * class.
|
---|
524 | *
|
---|
525 | * The Limited->InInit->Ready, Limited->InInit->Limited and
|
---|
526 | * Limited->InInit->InitFailed transition is done by the AutoReinitSpan smart
|
---|
527 | * class.
|
---|
528 | *
|
---|
529 | * The Ready->InUninit->NotReady and InitFailed->InUninit->NotReady
|
---|
530 | * transitions are done by the AutoUninitSpan smart class.
|
---|
531 | *
|
---|
532 | * In order to maintain the primary state integrity and declared functionality
|
---|
533 | * all subclasses must:
|
---|
534 | *
|
---|
535 | * 1) Use the above Auto*Span classes to perform state transitions. See the
|
---|
536 | * individual class descriptions for details.
|
---|
537 | *
|
---|
538 | * 2) All public methods of subclasses (i.e. all methods that can be called
|
---|
539 | * directly, not only from within other methods of the subclass) must have a
|
---|
540 | * standard prolog as described in the AutoCaller and AutoLimitedCaller
|
---|
541 | * documentation. Alternatively, they must use addCaller()/releaseCaller()
|
---|
542 | * directly (and therefore have both the prolog and the epilog), but this is
|
---|
543 | * not recommended.
|
---|
544 | */
|
---|
545 | class ATL_NO_VTABLE VirtualBoxBase
|
---|
546 | : public Lockable,
|
---|
547 | public CComObjectRootEx<CComMultiThreadModel>
|
---|
548 | #if !defined (VBOX_WITH_XPCOM)
|
---|
549 | , public ISupportErrorInfo
|
---|
550 | #endif
|
---|
551 | {
|
---|
552 | public:
|
---|
553 | enum State { NotReady, Ready, InInit, InUninit, InitFailed, Limited };
|
---|
554 |
|
---|
555 | VirtualBoxBase();
|
---|
556 | virtual ~VirtualBoxBase();
|
---|
557 |
|
---|
558 | static const char *translate(const char *context, const char *sourceText,
|
---|
559 | const char *comment = 0);
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * Unintialization method.
|
---|
563 | *
|
---|
564 | * Must be called by all final implementations (component classes) when the
|
---|
565 | * last reference to the object is released, before calling the destructor.
|
---|
566 | *
|
---|
567 | * This method is also automatically called by the uninit() method of this
|
---|
568 | * object's parent if this object is a dependent child of a class derived
|
---|
569 | * from VirtualBoxBaseWithChildren (see
|
---|
570 | * VirtualBoxBaseWithChildren::addDependentChild).
|
---|
571 | *
|
---|
572 | * @note Never call this method the AutoCaller scope or after the
|
---|
573 | * #addCaller() call not paired by #releaseCaller() because it is a
|
---|
574 | * guaranteed deadlock. See AutoUninitSpan for details.
|
---|
575 | */
|
---|
576 | virtual void uninit() {}
|
---|
577 |
|
---|
578 | virtual HRESULT addCaller(State *aState = NULL,
|
---|
579 | bool aLimited = false);
|
---|
580 | virtual void releaseCaller();
|
---|
581 |
|
---|
582 | /**
|
---|
583 | * Adds a limited caller. This method is equivalent to doing
|
---|
584 | * <tt>addCaller (aState, true)</tt>, but it is preferred because provides
|
---|
585 | * better self-descriptiveness. See #addCaller() for more info.
|
---|
586 | */
|
---|
587 | HRESULT addLimitedCaller(State *aState = NULL)
|
---|
588 | {
|
---|
589 | return addCaller(aState, true /* aLimited */);
|
---|
590 | }
|
---|
591 |
|
---|
592 | /**
|
---|
593 | * Pure virtual method for simple run-time type identification without
|
---|
594 | * having to enable C++ RTTI.
|
---|
595 | *
|
---|
596 | * This *must* be implemented by every subclass deriving from VirtualBoxBase;
|
---|
597 | * use the VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT macro to do that most easily.
|
---|
598 | */
|
---|
599 | virtual const IID& getClassIID() const = 0;
|
---|
600 |
|
---|
601 | /**
|
---|
602 | * Pure virtual method for simple run-time type identification without
|
---|
603 | * having to enable C++ RTTI.
|
---|
604 | *
|
---|
605 | * This *must* be implemented by every subclass deriving from VirtualBoxBase;
|
---|
606 | * use the VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT macro to do that most easily.
|
---|
607 | */
|
---|
608 | virtual const char* getComponentName() const = 0;
|
---|
609 |
|
---|
610 | /**
|
---|
611 | * Virtual method which determins the locking class to be used for validating
|
---|
612 | * lock order with the standard member lock handle. This method is overridden
|
---|
613 | * in a number of subclasses.
|
---|
614 | */
|
---|
615 | virtual VBoxLockingClass getLockingClass() const
|
---|
616 | {
|
---|
617 | return LOCKCLASS_OTHEROBJECT;
|
---|
618 | }
|
---|
619 |
|
---|
620 | virtual RWLockHandle *lockHandle() const;
|
---|
621 |
|
---|
622 | /**
|
---|
623 | * Returns a lock handle used to protect the primary state fields (used by
|
---|
624 | * #addCaller(), AutoInitSpan, AutoUninitSpan, etc.). Only intended to be
|
---|
625 | * used for similar purposes in subclasses. WARNING: NO any other locks may
|
---|
626 | * be requested while holding this lock!
|
---|
627 | */
|
---|
628 | WriteLockHandle *stateLockHandle() { return &mStateLock; }
|
---|
629 |
|
---|
630 | static HRESULT setErrorInternal(HRESULT aResultCode,
|
---|
631 | const GUID &aIID,
|
---|
632 | const char *aComponent,
|
---|
633 | const Utf8Str &aText,
|
---|
634 | bool aWarning,
|
---|
635 | bool aLogIt);
|
---|
636 |
|
---|
637 | HRESULT setError(HRESULT aResultCode, const char *pcsz, ...);
|
---|
638 | HRESULT setWarning(HRESULT aResultCode, const char *pcsz, ...);
|
---|
639 | HRESULT setErrorNoLog(HRESULT aResultCode, const char *pcsz, ...);
|
---|
640 |
|
---|
641 | private:
|
---|
642 |
|
---|
643 | void setState(State aState)
|
---|
644 | {
|
---|
645 | Assert(mState != aState);
|
---|
646 | mState = aState;
|
---|
647 | mStateChangeThread = RTThreadSelf();
|
---|
648 | }
|
---|
649 |
|
---|
650 | /** Primary state of this object */
|
---|
651 | State mState;
|
---|
652 | /** Thread that caused the last state change */
|
---|
653 | RTTHREAD mStateChangeThread;
|
---|
654 | /** Total number of active calls to this object */
|
---|
655 | unsigned mCallers;
|
---|
656 | /** Posted when the number of callers drops to zero */
|
---|
657 | RTSEMEVENT mZeroCallersSem;
|
---|
658 | /** Posted when the object goes from InInit/InUninit to some other state */
|
---|
659 | RTSEMEVENTMULTI mInitUninitSem;
|
---|
660 | /** Number of threads waiting for mInitUninitDoneSem */
|
---|
661 | unsigned mInitUninitWaiters;
|
---|
662 |
|
---|
663 | /** Protects access to state related data members */
|
---|
664 | WriteLockHandle mStateLock;
|
---|
665 |
|
---|
666 | /** User-level object lock for subclasses */
|
---|
667 | mutable RWLockHandle *mObjectLock;
|
---|
668 |
|
---|
669 | friend class AutoInitSpan;
|
---|
670 | friend class AutoReinitSpan;
|
---|
671 | friend class AutoUninitSpan;
|
---|
672 | };
|
---|
673 |
|
---|
674 | ////////////////////////////////////////////////////////////////////////////////
|
---|
675 |
|
---|
676 | /** Helper for VirtualBoxSupportTranslation. */
|
---|
677 | class VirtualBoxSupportTranslationBase
|
---|
678 | {
|
---|
679 | protected:
|
---|
680 | static bool cutClassNameFrom__PRETTY_FUNCTION__(char *aPrettyFunctionName);
|
---|
681 | };
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * The VirtualBoxSupportTranslation template implements the NLS string
|
---|
685 | * translation support for the given class.
|
---|
686 | *
|
---|
687 | * Translation support is provided by the static #tr() function. This function,
|
---|
688 | * given a string in UTF-8 encoding, looks up for a translation of the given
|
---|
689 | * string by calling the VirtualBoxBase::translate() global function which
|
---|
690 | * receives the name of the enclosing class ("context of translation") as the
|
---|
691 | * additional argument and returns a translated string based on the currently
|
---|
692 | * active language.
|
---|
693 | *
|
---|
694 | * @param C Class that needs to support the string translation.
|
---|
695 | *
|
---|
696 | * @note Every class that wants to use the #tr() function in its own methods
|
---|
697 | * must inherit from this template, regardless of whether its base class
|
---|
698 | * (if any) inherits from it or not. Otherwise, the translation service
|
---|
699 | * will not work correctly. However, the declaration of the derived
|
---|
700 | * class must contain
|
---|
701 | * the <tt>COM_SUPPORTTRANSLATION_OVERRIDE (<ClassName>)</tt> macro if one
|
---|
702 | * of its base classes also inherits from this template (to resolve the
|
---|
703 | * ambiguity of the #tr() function).
|
---|
704 | */
|
---|
705 | template<class C>
|
---|
706 | class VirtualBoxSupportTranslation : virtual protected VirtualBoxSupportTranslationBase
|
---|
707 | {
|
---|
708 | public:
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * Translates the given text string by calling VirtualBoxBase::translate()
|
---|
712 | * and passing the name of the C class as the first argument ("context of
|
---|
713 | * translation") See VirtualBoxBase::translate() for more info.
|
---|
714 | *
|
---|
715 | * @param aSourceText String to translate.
|
---|
716 | * @param aComment Comment to the string to resolve possible
|
---|
717 | * ambiguities (NULL means no comment).
|
---|
718 | *
|
---|
719 | * @return Translated version of the source string in UTF-8 encoding, or
|
---|
720 | * the source string itself if the translation is not found in the
|
---|
721 | * specified context.
|
---|
722 | */
|
---|
723 | inline static const char *tr(const char *aSourceText,
|
---|
724 | const char *aComment = NULL)
|
---|
725 | {
|
---|
726 | return VirtualBoxBase::translate(className(), aSourceText, aComment);
|
---|
727 | }
|
---|
728 |
|
---|
729 | protected:
|
---|
730 |
|
---|
731 | static const char *className()
|
---|
732 | {
|
---|
733 | static char fn[sizeof(__PRETTY_FUNCTION__) + 1];
|
---|
734 | if (!sClassName)
|
---|
735 | {
|
---|
736 | strcpy(fn, __PRETTY_FUNCTION__);
|
---|
737 | cutClassNameFrom__PRETTY_FUNCTION__(fn);
|
---|
738 | sClassName = fn;
|
---|
739 | }
|
---|
740 | return sClassName;
|
---|
741 | }
|
---|
742 |
|
---|
743 | private:
|
---|
744 |
|
---|
745 | static const char *sClassName;
|
---|
746 | };
|
---|
747 |
|
---|
748 | template<class C>
|
---|
749 | const char *VirtualBoxSupportTranslation<C>::sClassName = NULL;
|
---|
750 |
|
---|
751 | /**
|
---|
752 | * This macro must be invoked inside the public section of the declaration of
|
---|
753 | * the class inherited from the VirtualBoxSupportTranslation template in case
|
---|
754 | * if one of its other base classes also inherits from that template. This is
|
---|
755 | * necessary to resolve the ambiguity of the #tr() function.
|
---|
756 | *
|
---|
757 | * @param C Class that inherits the VirtualBoxSupportTranslation template
|
---|
758 | * more than once (through its other base clases).
|
---|
759 | */
|
---|
760 | #define VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(C) \
|
---|
761 | inline static const char *tr(const char *aSourceText, \
|
---|
762 | const char *aComment = NULL) \
|
---|
763 | { \
|
---|
764 | return VirtualBoxSupportTranslation<C>::tr(aSourceText, aComment); \
|
---|
765 | }
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * Dummy macro that is used to shut down Qt's lupdate tool warnings in some
|
---|
769 | * situations. This macro needs to be present inside (better at the very
|
---|
770 | * beginning) of the declaration of the class that inherits from
|
---|
771 | * VirtualBoxSupportTranslation template, to make lupdate happy.
|
---|
772 | */
|
---|
773 | #define Q_OBJECT
|
---|
774 |
|
---|
775 | ////////////////////////////////////////////////////////////////////////////////
|
---|
776 |
|
---|
777 | /**
|
---|
778 | * Base class to track VirtualBoxBaseNEXT chlidren of the component.
|
---|
779 | *
|
---|
780 | * This class is a preferrable VirtualBoxBase replacement for components that
|
---|
781 | * operate with collections of child components. It gives two useful
|
---|
782 | * possibilities:
|
---|
783 | *
|
---|
784 | * <ol><li>
|
---|
785 | * Given an IUnknown instance, it's possible to quickly determine
|
---|
786 | * whether this instance represents a child object that belongs to the
|
---|
787 | * given component, and if so, get a valid VirtualBoxBase pointer to the
|
---|
788 | * child object. The returned pointer can be then safely casted to the
|
---|
789 | * actual class of the child object (to get access to its "internal"
|
---|
790 | * non-interface methods) provided that no other child components implement
|
---|
791 | * the same original COM interface IUnknown is queried from.
|
---|
792 | * </li><li>
|
---|
793 | * When the parent object uninitializes itself, it can easily unintialize
|
---|
794 | * all its VirtualBoxBase derived children (using their
|
---|
795 | * VirtualBoxBase::uninit() implementations). This is done simply by
|
---|
796 | * calling the #uninitDependentChildren() method.
|
---|
797 | * </li></ol>
|
---|
798 | *
|
---|
799 | * In order to let the above work, the following must be done:
|
---|
800 | * <ol><li>
|
---|
801 | * When a child object is initialized, it calls #addDependentChild() of
|
---|
802 | * its parent to register itself within the list of dependent children.
|
---|
803 | * </li><li>
|
---|
804 | * When the child object it is uninitialized, it calls
|
---|
805 | * #removeDependentChild() to unregister itself.
|
---|
806 | * </li></ol>
|
---|
807 | *
|
---|
808 | * Note that if the parent object does not call #uninitDependentChildren() when
|
---|
809 | * it gets uninitialized, it must call uninit() methods of individual children
|
---|
810 | * manually to disconnect them; a failure to do so will cause crashes in these
|
---|
811 | * methods when children get destroyed. The same applies to children not calling
|
---|
812 | * #removeDependentChild() when getting destroyed.
|
---|
813 | *
|
---|
814 | * Note that children added by #addDependentChild() are <b>weakly</b> referenced
|
---|
815 | * (i.e. AddRef() is not called), so when a child object is deleted externally
|
---|
816 | * (because it's reference count goes to zero), it will automatically remove
|
---|
817 | * itself from the map of dependent children provided that it follows the rules
|
---|
818 | * described here.
|
---|
819 | *
|
---|
820 | * Access to the child list is serialized using the #childrenLock() lock handle
|
---|
821 | * (which defaults to the general object lock handle (see
|
---|
822 | * VirtualBoxBase::lockHandle()). This lock is used by all add/remove methods of
|
---|
823 | * this class so be aware of the need to preserve the {parent, child} lock order
|
---|
824 | * when calling these methods.
|
---|
825 | *
|
---|
826 | * Read individual method descriptions to get further information.
|
---|
827 | *
|
---|
828 | * @todo This is a VirtualBoxBaseWithChildren equivalent that uses the
|
---|
829 | * VirtualBoxBaseNEXT implementation. Will completely supersede
|
---|
830 | * VirtualBoxBaseWithChildren after the old VirtualBoxBase implementation
|
---|
831 | * has gone.
|
---|
832 | */
|
---|
833 | class VirtualBoxBaseWithChildrenNEXT : public VirtualBoxBase
|
---|
834 | {
|
---|
835 | public:
|
---|
836 |
|
---|
837 | VirtualBoxBaseWithChildrenNEXT()
|
---|
838 | {}
|
---|
839 |
|
---|
840 | virtual ~VirtualBoxBaseWithChildrenNEXT()
|
---|
841 | {}
|
---|
842 |
|
---|
843 | /**
|
---|
844 | * Lock handle to use when adding/removing child objects from the list of
|
---|
845 | * children. It is guaranteed that no any other lock is requested in methods
|
---|
846 | * of this class while holding this lock.
|
---|
847 | *
|
---|
848 | * @warning By default, this simply returns the general object's lock handle
|
---|
849 | * (see VirtualBoxBase::lockHandle()) which is sufficient for most
|
---|
850 | * cases.
|
---|
851 | */
|
---|
852 | virtual RWLockHandle *childrenLock() { return lockHandle(); }
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * Adds the given child to the list of dependent children.
|
---|
856 | *
|
---|
857 | * Usually gets called from the child's init() method.
|
---|
858 | *
|
---|
859 | * @note @a aChild (unless it is in InInit state) must be protected by
|
---|
860 | * VirtualBoxBase::AutoCaller to make sure it is not uninitialized on
|
---|
861 | * another thread during this method's call.
|
---|
862 | *
|
---|
863 | * @note When #childrenLock() is not overloaded (returns the general object
|
---|
864 | * lock) and this method is called from under the child's read or
|
---|
865 | * write lock, make sure the {parent, child} locking order is
|
---|
866 | * preserved by locking the callee (this object) for writing before
|
---|
867 | * the child's lock.
|
---|
868 | *
|
---|
869 | * @param aChild Child object to add (must inherit VirtualBoxBase AND
|
---|
870 | * implement some interface).
|
---|
871 | *
|
---|
872 | * @note Locks #childrenLock() for writing.
|
---|
873 | */
|
---|
874 | template<class C>
|
---|
875 | void addDependentChild(C *aChild)
|
---|
876 | {
|
---|
877 | AssertReturnVoid(aChild != NULL);
|
---|
878 | doAddDependentChild(ComPtr<IUnknown>(aChild), aChild);
|
---|
879 | }
|
---|
880 |
|
---|
881 | /**
|
---|
882 | * Equivalent to template <class C> void addDependentChild (C *aChild)
|
---|
883 | * but takes a ComObjPtr<C> argument.
|
---|
884 | */
|
---|
885 | template<class C>
|
---|
886 | void addDependentChild(const ComObjPtr<C> &aChild)
|
---|
887 | {
|
---|
888 | AssertReturnVoid(!aChild.isNull());
|
---|
889 | doAddDependentChild(ComPtr<IUnknown>(static_cast<C *>(aChild)), aChild);
|
---|
890 | }
|
---|
891 |
|
---|
892 | /**
|
---|
893 | * Removes the given child from the list of dependent children.
|
---|
894 | *
|
---|
895 | * Usually gets called from the child's uninit() method.
|
---|
896 | *
|
---|
897 | * Keep in mind that the called (parent) object may be no longer available
|
---|
898 | * (i.e. may be deleted deleted) after this method returns, so you must not
|
---|
899 | * call any other parent's methods after that!
|
---|
900 | *
|
---|
901 | * @note Locks #childrenLock() for writing.
|
---|
902 | *
|
---|
903 | * @note @a aChild (unless it is in InUninit state) must be protected by
|
---|
904 | * VirtualBoxBase::AutoCaller to make sure it is not uninitialized on
|
---|
905 | * another thread during this method's call.
|
---|
906 | *
|
---|
907 | * @note When #childrenLock() is not overloaded (returns the general object
|
---|
908 | * lock) and this method is called from under the child's read or
|
---|
909 | * write lock, make sure the {parent, child} locking order is
|
---|
910 | * preserved by locking the callee (this object) for writing before
|
---|
911 | * the child's lock. This is irrelevant when the method is called from
|
---|
912 | * under this object's VirtualBoxBaseProto::AutoUninitSpan (i.e. in
|
---|
913 | * InUninit state) since in this case no locking is done.
|
---|
914 | *
|
---|
915 | * @param aChild Child object to remove.
|
---|
916 | *
|
---|
917 | * @note Locks #childrenLock() for writing.
|
---|
918 | */
|
---|
919 | template<class C>
|
---|
920 | void removeDependentChild(C *aChild)
|
---|
921 | {
|
---|
922 | AssertReturnVoid(aChild != NULL);
|
---|
923 | doRemoveDependentChild(ComPtr<IUnknown>(aChild));
|
---|
924 | }
|
---|
925 |
|
---|
926 | /**
|
---|
927 | * Equivalent to template <class C> void removeDependentChild (C *aChild)
|
---|
928 | * but takes a ComObjPtr<C> argument.
|
---|
929 | */
|
---|
930 | template<class C>
|
---|
931 | void removeDependentChild(const ComObjPtr<C> &aChild)
|
---|
932 | {
|
---|
933 | AssertReturnVoid(!aChild.isNull());
|
---|
934 | doRemoveDependentChild(ComPtr<IUnknown>(static_cast<C *>(aChild)));
|
---|
935 | }
|
---|
936 |
|
---|
937 | protected:
|
---|
938 |
|
---|
939 | void uninitDependentChildren();
|
---|
940 |
|
---|
941 | VirtualBoxBase *getDependentChild(const ComPtr<IUnknown> &aUnk);
|
---|
942 |
|
---|
943 | private:
|
---|
944 | void doAddDependentChild(IUnknown *aUnk, VirtualBoxBase *aChild);
|
---|
945 | void doRemoveDependentChild(IUnknown *aUnk);
|
---|
946 |
|
---|
947 | typedef std::map<IUnknown*, VirtualBoxBase*> DependentChildren;
|
---|
948 | DependentChildren mDependentChildren;
|
---|
949 | };
|
---|
950 |
|
---|
951 | ////////////////////////////////////////////////////////////////////////////////
|
---|
952 |
|
---|
953 | ////////////////////////////////////////////////////////////////////////////////
|
---|
954 |
|
---|
955 |
|
---|
956 | /**
|
---|
957 | * Simple template that manages data structure allocation/deallocation
|
---|
958 | * and supports data pointer sharing (the instance that shares the pointer is
|
---|
959 | * not responsible for memory deallocation as opposed to the instance that
|
---|
960 | * owns it).
|
---|
961 | */
|
---|
962 | template <class D>
|
---|
963 | class Shareable
|
---|
964 | {
|
---|
965 | public:
|
---|
966 |
|
---|
967 | Shareable() : mData (NULL), mIsShared(FALSE) {}
|
---|
968 | ~Shareable() { free(); }
|
---|
969 |
|
---|
970 | void allocate() { attach(new D); }
|
---|
971 |
|
---|
972 | virtual void free() {
|
---|
973 | if (mData) {
|
---|
974 | if (!mIsShared)
|
---|
975 | delete mData;
|
---|
976 | mData = NULL;
|
---|
977 | mIsShared = false;
|
---|
978 | }
|
---|
979 | }
|
---|
980 |
|
---|
981 | void attach(D *d) {
|
---|
982 | AssertMsg(d, ("new data must not be NULL"));
|
---|
983 | if (d && mData != d) {
|
---|
984 | if (mData && !mIsShared)
|
---|
985 | delete mData;
|
---|
986 | mData = d;
|
---|
987 | mIsShared = false;
|
---|
988 | }
|
---|
989 | }
|
---|
990 |
|
---|
991 | void attach(Shareable &d) {
|
---|
992 | AssertMsg(
|
---|
993 | d.mData == mData || !d.mIsShared,
|
---|
994 | ("new data must not be shared")
|
---|
995 | );
|
---|
996 | if (this != &d && !d.mIsShared) {
|
---|
997 | attach(d.mData);
|
---|
998 | d.mIsShared = true;
|
---|
999 | }
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | void share(D *d) {
|
---|
1003 | AssertMsg(d, ("new data must not be NULL"));
|
---|
1004 | if (mData != d) {
|
---|
1005 | if (mData && !mIsShared)
|
---|
1006 | delete mData;
|
---|
1007 | mData = d;
|
---|
1008 | mIsShared = true;
|
---|
1009 | }
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | void share(const Shareable &d) { share(d.mData); }
|
---|
1013 |
|
---|
1014 | void attachCopy(const D *d) {
|
---|
1015 | AssertMsg(d, ("data to copy must not be NULL"));
|
---|
1016 | if (d)
|
---|
1017 | attach(new D(*d));
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | void attachCopy(const Shareable &d) {
|
---|
1021 | attachCopy(d.mData);
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | virtual D *detach() {
|
---|
1025 | D *d = mData;
|
---|
1026 | mData = NULL;
|
---|
1027 | mIsShared = false;
|
---|
1028 | return d;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | D *data() const {
|
---|
1032 | return mData;
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | D *operator->() const {
|
---|
1036 | AssertMsg(mData, ("data must not be NULL"));
|
---|
1037 | return mData;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | bool isNull() const { return mData == NULL; }
|
---|
1041 | bool operator!() const { return isNull(); }
|
---|
1042 |
|
---|
1043 | bool isShared() const { return mIsShared; }
|
---|
1044 |
|
---|
1045 | protected:
|
---|
1046 |
|
---|
1047 | D *mData;
|
---|
1048 | bool mIsShared;
|
---|
1049 | };
|
---|
1050 |
|
---|
1051 | /// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
|
---|
1052 | /**
|
---|
1053 | * Simple template that enhances Shareable<> and supports data
|
---|
1054 | * backup/rollback/commit (using the copy constructor of the managed data
|
---|
1055 | * structure).
|
---|
1056 | */
|
---|
1057 | template<class D>
|
---|
1058 | class Backupable : public Shareable<D>
|
---|
1059 | {
|
---|
1060 | public:
|
---|
1061 |
|
---|
1062 | Backupable() : Shareable<D> (), mBackupData(NULL) {}
|
---|
1063 |
|
---|
1064 | void free()
|
---|
1065 | {
|
---|
1066 | AssertMsg(this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
|
---|
1067 | rollback();
|
---|
1068 | Shareable<D>::free();
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | D *detach()
|
---|
1072 | {
|
---|
1073 | AssertMsg(this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
|
---|
1074 | rollback();
|
---|
1075 | return Shareable<D>::detach();
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | void share(const Backupable &d)
|
---|
1079 | {
|
---|
1080 | AssertMsg(!d.isBackedUp(), ("data to share must not be backed up"));
|
---|
1081 | if (!d.isBackedUp())
|
---|
1082 | Shareable<D>::share(d.mData);
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | /**
|
---|
1086 | * Stores the current data pointer in the backup area, allocates new data
|
---|
1087 | * using the copy constructor on current data and makes new data active.
|
---|
1088 | */
|
---|
1089 | void backup()
|
---|
1090 | {
|
---|
1091 | AssertMsg(this->mData, ("data must not be NULL"));
|
---|
1092 | if (this->mData && !mBackupData)
|
---|
1093 | {
|
---|
1094 | D *pNewData = new D(*this->mData);
|
---|
1095 | mBackupData = this->mData;
|
---|
1096 | this->mData = pNewData;
|
---|
1097 | }
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | /**
|
---|
1101 | * Deletes new data created by #backup() and restores previous data pointer
|
---|
1102 | * stored in the backup area, making it active again.
|
---|
1103 | */
|
---|
1104 | void rollback()
|
---|
1105 | {
|
---|
1106 | if (this->mData && mBackupData)
|
---|
1107 | {
|
---|
1108 | delete this->mData;
|
---|
1109 | this->mData = mBackupData;
|
---|
1110 | mBackupData = NULL;
|
---|
1111 | }
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | /**
|
---|
1115 | * Commits current changes by deleting backed up data and clearing up the
|
---|
1116 | * backup area. The new data pointer created by #backup() remains active
|
---|
1117 | * and becomes the only managed pointer.
|
---|
1118 | *
|
---|
1119 | * This method is much faster than #commitCopy() (just a single pointer
|
---|
1120 | * assignment operation), but makes the previous data pointer invalid
|
---|
1121 | * (because it is freed). For this reason, this method must not be
|
---|
1122 | * used if it's possible that data managed by this instance is shared with
|
---|
1123 | * some other Shareable instance. See #commitCopy().
|
---|
1124 | */
|
---|
1125 | void commit()
|
---|
1126 | {
|
---|
1127 | if (this->mData && mBackupData)
|
---|
1128 | {
|
---|
1129 | if (!this->mIsShared)
|
---|
1130 | delete mBackupData;
|
---|
1131 | mBackupData = NULL;
|
---|
1132 | this->mIsShared = false;
|
---|
1133 | }
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | /**
|
---|
1137 | * Commits current changes by assigning new data to the previous data
|
---|
1138 | * pointer stored in the backup area using the assignment operator.
|
---|
1139 | * New data is deleted, the backup area is cleared and the previous data
|
---|
1140 | * pointer becomes active and the only managed pointer.
|
---|
1141 | *
|
---|
1142 | * This method is slower than #commit(), but it keeps the previous data
|
---|
1143 | * pointer valid (i.e. new data is copied to the same memory location).
|
---|
1144 | * For that reason it's safe to use this method on instances that share
|
---|
1145 | * managed data with other Shareable instances.
|
---|
1146 | */
|
---|
1147 | void commitCopy()
|
---|
1148 | {
|
---|
1149 | if (this->mData && mBackupData)
|
---|
1150 | {
|
---|
1151 | *mBackupData = *(this->mData);
|
---|
1152 | delete this->mData;
|
---|
1153 | this->mData = mBackupData;
|
---|
1154 | mBackupData = NULL;
|
---|
1155 | }
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | void assignCopy(const D *pData)
|
---|
1159 | {
|
---|
1160 | AssertMsg(this->mData, ("data must not be NULL"));
|
---|
1161 | AssertMsg(pData, ("data to copy must not be NULL"));
|
---|
1162 | if (this->mData && pData)
|
---|
1163 | {
|
---|
1164 | if (!mBackupData)
|
---|
1165 | {
|
---|
1166 | D *pNewData = new D(*pData);
|
---|
1167 | mBackupData = this->mData;
|
---|
1168 | this->mData = pNewData;
|
---|
1169 | }
|
---|
1170 | else
|
---|
1171 | *this->mData = *pData;
|
---|
1172 | }
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | void assignCopy(const Backupable &d)
|
---|
1176 | {
|
---|
1177 | assignCopy(d.mData);
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | bool isBackedUp() const
|
---|
1181 | {
|
---|
1182 | return mBackupData != NULL;
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | D *backedUpData() const
|
---|
1186 | {
|
---|
1187 | return mBackupData;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | protected:
|
---|
1191 |
|
---|
1192 | D *mBackupData;
|
---|
1193 | };
|
---|
1194 |
|
---|
1195 | #endif // !____H_VIRTUALBOXBASEIMPL
|
---|
1196 |
|
---|