1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox COM base classes definition
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
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 "AutoLock.h"
|
---|
33 |
|
---|
34 | using namespace com;
|
---|
35 | using util::AutoLock;
|
---|
36 | using util::AutoReaderLock;
|
---|
37 | using util::AutoMultiLock;
|
---|
38 |
|
---|
39 | #include <iprt/cdefs.h>
|
---|
40 | #include <iprt/critsect.h>
|
---|
41 |
|
---|
42 | #include <list>
|
---|
43 | #include <map>
|
---|
44 |
|
---|
45 | #if defined (__WIN__)
|
---|
46 |
|
---|
47 | #include <atlcom.h>
|
---|
48 |
|
---|
49 | // use a special version of the singleton class factory,
|
---|
50 | // see KB811591 in msdn for more info.
|
---|
51 |
|
---|
52 | #undef DECLARE_CLASSFACTORY_SINGLETON
|
---|
53 | #define DECLARE_CLASSFACTORY_SINGLETON(obj) DECLARE_CLASSFACTORY_EX(CMyComClassFactorySingleton<obj>)
|
---|
54 |
|
---|
55 | template <class T>
|
---|
56 | class CMyComClassFactorySingleton : public CComClassFactory
|
---|
57 | {
|
---|
58 | public:
|
---|
59 | CMyComClassFactorySingleton() : m_hrCreate(S_OK){}
|
---|
60 | virtual ~CMyComClassFactorySingleton(){}
|
---|
61 | // IClassFactory
|
---|
62 | STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObj)
|
---|
63 | {
|
---|
64 | HRESULT hRes = E_POINTER;
|
---|
65 | if (ppvObj != NULL)
|
---|
66 | {
|
---|
67 | *ppvObj = NULL;
|
---|
68 | // Aggregation is not supported in singleton objects.
|
---|
69 | ATLASSERT(pUnkOuter == NULL);
|
---|
70 | if (pUnkOuter != NULL)
|
---|
71 | hRes = CLASS_E_NOAGGREGATION;
|
---|
72 | else
|
---|
73 | {
|
---|
74 | if (m_hrCreate == S_OK && m_spObj == NULL)
|
---|
75 | {
|
---|
76 | Lock();
|
---|
77 | __try
|
---|
78 | {
|
---|
79 | // Fix: The following If statement was moved inside the __try statement.
|
---|
80 | // Did another thread arrive here first?
|
---|
81 | if (m_hrCreate == S_OK && m_spObj == NULL)
|
---|
82 | {
|
---|
83 | // lock the module to indicate activity
|
---|
84 | // (necessary for the monitor shutdown thread to correctly
|
---|
85 | // terminate the module in case when CreateInstance() fails)
|
---|
86 | _pAtlModule->Lock();
|
---|
87 | CComObjectCached<T> *p;
|
---|
88 | m_hrCreate = CComObjectCached<T>::CreateInstance(&p);
|
---|
89 | if (SUCCEEDED(m_hrCreate))
|
---|
90 | {
|
---|
91 | m_hrCreate = p->QueryInterface(IID_IUnknown, (void**)&m_spObj);
|
---|
92 | if (FAILED(m_hrCreate))
|
---|
93 | {
|
---|
94 | delete p;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | _pAtlModule->Unlock();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | __finally
|
---|
101 | {
|
---|
102 | Unlock();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | if (m_hrCreate == S_OK)
|
---|
106 | {
|
---|
107 | hRes = m_spObj->QueryInterface(riid, ppvObj);
|
---|
108 | }
|
---|
109 | else
|
---|
110 | {
|
---|
111 | hRes = m_hrCreate;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | return hRes;
|
---|
116 | }
|
---|
117 | HRESULT m_hrCreate;
|
---|
118 | CComPtr<IUnknown> m_spObj;
|
---|
119 | };
|
---|
120 |
|
---|
121 | #endif // defined (__WIN__)
|
---|
122 |
|
---|
123 | // macros
|
---|
124 | ////////////////////////////////////////////////////////////////////////////////
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * A special version of the Assert macro to be used within VirtualBoxBase
|
---|
128 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
129 | *
|
---|
130 | * In the debug build, this macro is equivalent to Assert.
|
---|
131 | * In the release build, this macro uses |setError (E_FAIL, ...)| to set the
|
---|
132 | * error info from the asserted expression.
|
---|
133 | *
|
---|
134 | * @see VirtualBoxSupportErrorInfoImpl::setError
|
---|
135 | *
|
---|
136 | * @param expr Expression which should be true.
|
---|
137 | */
|
---|
138 | #if defined (DEBUG)
|
---|
139 | #define ComAssert(expr) Assert (expr)
|
---|
140 | #else
|
---|
141 | #define ComAssert(expr) \
|
---|
142 | do { \
|
---|
143 | if (!(expr)) \
|
---|
144 | setError (E_FAIL, "Assertion failed: [%s] at '%s' (%d) in %s.\n" \
|
---|
145 | "Please contact the product vendor!", \
|
---|
146 | #expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
|
---|
147 | } while (0)
|
---|
148 | #endif
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * A special version of the AssertMsg macro to be used within VirtualBoxBase
|
---|
152 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
153 | *
|
---|
154 | * See ComAssert for more info.
|
---|
155 | *
|
---|
156 | * @param expr Expression which should be true.
|
---|
157 | * @param a printf argument list (in parenthesis).
|
---|
158 | */
|
---|
159 | #if defined (DEBUG)
|
---|
160 | #define ComAssertMsg(expr, a) AssertMsg (expr, a)
|
---|
161 | #else
|
---|
162 | #define ComAssertMsg(expr, a) \
|
---|
163 | do { \
|
---|
164 | if (!(expr)) \
|
---|
165 | setError (E_FAIL, "Assertion failed: [%s] at '%s' (%d) in %s.\n" \
|
---|
166 | "%s.\n" \
|
---|
167 | "Please contact the product vendor!", \
|
---|
168 | #expr, __FILE__, __LINE__, __PRETTY_FUNCTION__, Utf8StrFmt a .raw()); \
|
---|
169 | } while (0)
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * A special version of the AssertRC macro to be used within VirtualBoxBase
|
---|
174 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
175 | *
|
---|
176 | * See ComAssert for more info.
|
---|
177 | *
|
---|
178 | * @param vrc VBox status code.
|
---|
179 | */
|
---|
180 | #if defined (DEBUG)
|
---|
181 | #define ComAssertRC(vrc) AssertRC (vrc)
|
---|
182 | #else
|
---|
183 | #define ComAssertRC(vrc) ComAssertMsgRC (vrc, ("%Vra", vrc))
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * A special version of the AssertMsgRC 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 | * @param msg printf argument list (in parenthesis).
|
---|
194 | */
|
---|
195 | #if defined (DEBUG)
|
---|
196 | #define ComAssertMsgRC(vrc, msg) AssertMsgRC (vrc, msg)
|
---|
197 | #else
|
---|
198 | #define ComAssertMsgRC(vrc, msg) ComAssertMsg (VBOX_SUCCESS (vrc), msg)
|
---|
199 | #endif
|
---|
200 |
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * A special version of the AssertFailed macro to be used within VirtualBoxBase
|
---|
204 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
205 | *
|
---|
206 | * See ComAssert for more info.
|
---|
207 | */
|
---|
208 | #if defined (DEBUG)
|
---|
209 | #define ComAssertFailed() AssertFailed()
|
---|
210 | #else
|
---|
211 | #define ComAssertFailed() \
|
---|
212 | do { \
|
---|
213 | setError (E_FAIL, "Assertion failed at '%s' (%d) in %s.\n" \
|
---|
214 | "Please contact the product vendor!", \
|
---|
215 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \
|
---|
216 | } while (0)
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * A special version of the AssertMsgFailed macro to be used within VirtualBoxBase
|
---|
221 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
222 | *
|
---|
223 | * See ComAssert for more info.
|
---|
224 | *
|
---|
225 | * @param a printf argument list (in parenthesis).
|
---|
226 | */
|
---|
227 | #if defined (DEBUG)
|
---|
228 | #define ComAssertMsgFailed(a) AssertMsgFailed(a)
|
---|
229 | #else
|
---|
230 | #define ComAssertMsgFailed(a) \
|
---|
231 | do { \
|
---|
232 | setError (E_FAIL, "Assertion failed at '%s' (%d) in %s.\n" \
|
---|
233 | "%s.\n" \
|
---|
234 | "Please contact the product vendor!", \
|
---|
235 | __FILE__, __LINE__, __PRETTY_FUNCTION__, Utf8StrFmt a .raw()); \
|
---|
236 | } while (0)
|
---|
237 | #endif
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * A special version of the AssertComRC macro to be used within VirtualBoxBase
|
---|
241 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
242 | *
|
---|
243 | * See ComAssert for more info.
|
---|
244 | *
|
---|
245 | * @param rc COM result code
|
---|
246 | */
|
---|
247 | #if defined (DEBUG)
|
---|
248 | #define ComAssertComRC(rc) AssertComRC (rc)
|
---|
249 | #else
|
---|
250 | #define ComAssertComRC(rc) ComAssertMsg (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc))
|
---|
251 | #endif
|
---|
252 |
|
---|
253 |
|
---|
254 | /** Special version of ComAssert that returns ret if expr fails */
|
---|
255 | #define ComAssertRet(expr, ret) \
|
---|
256 | do { ComAssert (expr); if (!(expr)) return (ret); } while (0)
|
---|
257 | /** Special version of ComAssertMsg that returns ret if expr fails */
|
---|
258 | #define ComAssertMsgRet(expr, a, ret) \
|
---|
259 | do { ComAssertMsg (expr, a); if (!(expr)) return (ret); } while (0)
|
---|
260 | /** Special version of ComAssertRC that returns ret if vrc does not succeed */
|
---|
261 | #define ComAssertRCRet(vrc, ret) \
|
---|
262 | do { ComAssertRC (vrc); if (!VBOX_SUCCESS (vrc)) return (ret); } while (0)
|
---|
263 | /** Special version of ComAssertMsgRC that returns ret if vrc does not succeed */
|
---|
264 | #define ComAssertMsgRCRet(vrc, msg, ret) \
|
---|
265 | do { ComAssertMsgRC (vrc, msg); if (!VBOX_SUCCESS (vrc)) return (ret); } while (0)
|
---|
266 | /** Special version of ComAssertFailed that returns ret */
|
---|
267 | #define ComAssertFailedRet(ret) \
|
---|
268 | do { ComAssertFailed(); return (ret); } while (0)
|
---|
269 | /** Special version of ComAssertMsgFailed that returns ret */
|
---|
270 | #define ComAssertMsgFailedRet(msg, ret) \
|
---|
271 | do { ComAssertMsgFailed (msg); return (ret); } while (0)
|
---|
272 | /** Special version of ComAssertComRC that returns ret if rc does not succeed */
|
---|
273 | #define ComAssertComRCRet(rc, ret) \
|
---|
274 | do { ComAssertComRC (rc); if (!SUCCEEDED (rc)) return (ret); } while (0)
|
---|
275 | /** Special version of ComAssertComRC that returns rc if rc does not succeed */
|
---|
276 | #define ComAssertComRCRetRC(rc) \
|
---|
277 | do { ComAssertComRC (rc); if (!SUCCEEDED (rc)) return (rc); } while (0)
|
---|
278 |
|
---|
279 |
|
---|
280 | /** Special version of ComAssert that evaulates eval and breaks if expr fails */
|
---|
281 | #define ComAssertBreak(expr, eval) \
|
---|
282 | if (1) { ComAssert (expr); if (!(expr)) { eval; break; } } else do {} while (0)
|
---|
283 | /** Special version of ComAssertMsg that evaulates eval and breaks if expr fails */
|
---|
284 | #define ComAssertMsgBreak(expr, a, eval) \
|
---|
285 | if (1) { ComAssertMsg (expr, a); if (!(expr)) { eval; break; } } else do {} while (0)
|
---|
286 | /** Special version of ComAssertRC that evaulates eval and breaks if vrc does not succeed */
|
---|
287 | #define ComAssertRCBreak(vrc, eval) \
|
---|
288 | if (1) { ComAssertRC (vrc); if (!VBOX_SUCCESS (vrc)) { eval; break; } } else do {} while (0)
|
---|
289 | /** Special version of ComAssertMsgRC that evaulates eval and breaks if vrc does not succeed */
|
---|
290 | #define ComAssertMsgRCBreak(vrc, msg, eval) \
|
---|
291 | if (1) { ComAssertMsgRC (vrc, msg); if (!VBOX_SUCCESS (vrc)) { eval; break; } } else do {} while (0)
|
---|
292 | /** Special version of ComAssertFailed that evaulates eval and breaks */
|
---|
293 | #define ComAssertFailedBreak(eval) \
|
---|
294 | if (1) { ComAssertFailed(); { eval; break; } } else do {} while (0)
|
---|
295 | /** Special version of ComAssertMsgFailed that evaulates eval and breaks */
|
---|
296 | #define ComAssertMsgFailedBreak(msg, eval) \
|
---|
297 | if (1) { ComAssertMsgFailed (msg); { eval; break; } } else do {} while (0)
|
---|
298 | /** Special version of ComAssertComRC that evaulates eval and breaks if rc does not succeed */
|
---|
299 | #define ComAssertComRCBreak(rc, eval) \
|
---|
300 | if (1) { ComAssertComRC (rc); if (!SUCCEEDED (rc)) { eval; break; } } else do {} while (0)
|
---|
301 | /** Special version of ComAssertComRC that just breaks if rc does not succeed */
|
---|
302 | #define ComAssertComRCBreakRC(rc) \
|
---|
303 | if (1) { ComAssertComRC (rc); if (!SUCCEEDED (rc)) { break; } } else do {} while (0)
|
---|
304 |
|
---|
305 | /// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
|
---|
306 | /**
|
---|
307 | * Checks whether this object is ready or not. Objects are typically ready
|
---|
308 | * after they are successfully created by their parent objects and become
|
---|
309 | * not ready when the respective parent itsef becomes not ready or gets
|
---|
310 | * destroyed while a reference to the child is still held by the caller
|
---|
311 | * (which prevents it from destruction).
|
---|
312 | *
|
---|
313 | * When this object is not ready, the macro sets error info and returns
|
---|
314 | * E_UNEXPECTED (the translatable error message is defined in null context).
|
---|
315 | * Otherwise, the macro does nothing.
|
---|
316 | *
|
---|
317 | * This macro <b>must</b> be used at the beginning of all interface methods
|
---|
318 | * (right after entering the class lock) in classes derived from both
|
---|
319 | * VirtualBoxBase and VirtualBoxSupportErrorInfoImpl.
|
---|
320 | */
|
---|
321 | #define CHECK_READY() \
|
---|
322 | do { \
|
---|
323 | if (!isReady()) \
|
---|
324 | return setError (E_UNEXPECTED, tr ("The object is not ready")); \
|
---|
325 | } while (0)
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Declares an empty construtor and destructor for the given class.
|
---|
329 | * This is useful to prevent the compiler from generating the default
|
---|
330 | * ctor and dtor, which in turn allows to use forward class statements
|
---|
331 | * (instead of including their header files) when declaring data members of
|
---|
332 | * non-fundamental types with constructors (which are always called implicitly
|
---|
333 | * by constructors and by the destructor of the class).
|
---|
334 | *
|
---|
335 | * This macro is to be palced within (the public section of) the class
|
---|
336 | * declaration. Its counterpart, DEFINE_EMPTY_CTOR_DTOR, must be placed
|
---|
337 | * somewhere in one of the translation units (usually .cpp source files).
|
---|
338 | *
|
---|
339 | * @param cls class to declare a ctor and dtor for
|
---|
340 | */
|
---|
341 | #define DECLARE_EMPTY_CTOR_DTOR(cls) cls(); ~cls();
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Defines an empty construtor and destructor for the given class.
|
---|
345 | * See DECLARE_EMPTY_CTOR_DTOR for more info.
|
---|
346 | */
|
---|
347 | #define DEFINE_EMPTY_CTOR_DTOR(cls) \
|
---|
348 | cls::cls () {}; cls::~cls () {};
|
---|
349 |
|
---|
350 | ////////////////////////////////////////////////////////////////////////////////
|
---|
351 |
|
---|
352 | namespace stdx
|
---|
353 | {
|
---|
354 | /**
|
---|
355 | * A wrapper around the container that owns pointers it stores.
|
---|
356 | *
|
---|
357 | * @note
|
---|
358 | * Ownership is recognized only when destructing the container!
|
---|
359 | * Pointers are not deleted when erased using erase() etc.
|
---|
360 | *
|
---|
361 | * @param container
|
---|
362 | * class that meets Container requirements (for example, an instance of
|
---|
363 | * std::list<>, std::vector<> etc.). The given class must store
|
---|
364 | * pointers (for example, std::list <MyType *>).
|
---|
365 | */
|
---|
366 | template <typename container>
|
---|
367 | class ptr_container : public container
|
---|
368 | {
|
---|
369 | public:
|
---|
370 | ~ptr_container()
|
---|
371 | {
|
---|
372 | for (typename container::iterator it = container::begin();
|
---|
373 | it != container::end();
|
---|
374 | ++ it)
|
---|
375 | delete (*it);
|
---|
376 | }
|
---|
377 | };
|
---|
378 | };
|
---|
379 |
|
---|
380 | ////////////////////////////////////////////////////////////////////////////////
|
---|
381 |
|
---|
382 | class ATL_NO_VTABLE VirtualBoxBaseNEXT_base
|
---|
383 | #ifdef __WIN__
|
---|
384 | : public CComObjectRootEx <CComMultiThreadModel>
|
---|
385 | #else
|
---|
386 | : public CComObjectRootEx
|
---|
387 | #endif
|
---|
388 | , public AutoLock::Lockable
|
---|
389 | {
|
---|
390 | public:
|
---|
391 |
|
---|
392 | enum State { NotReady, Ready, InInit, InUninit, InitFailed, Limited };
|
---|
393 |
|
---|
394 | protected:
|
---|
395 |
|
---|
396 | VirtualBoxBaseNEXT_base();
|
---|
397 | virtual ~VirtualBoxBaseNEXT_base();
|
---|
398 |
|
---|
399 | public:
|
---|
400 |
|
---|
401 | // AutoLock::Lockable interface
|
---|
402 | virtual AutoLock::Handle *lockHandle() const;
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * Virtual unintialization method.
|
---|
406 | * Must be called by all implementations (COM classes) when the last
|
---|
407 | * reference to the object is released, before calling the destructor.
|
---|
408 | * Also, this method is called automatically by the uninit() method of the
|
---|
409 | * parent of this object, when this object is a dependent child of a class
|
---|
410 | * derived from VirtualBoxBaseWithChildren (@sa
|
---|
411 | * VirtualBoxBaseWithChildren::addDependentChild).
|
---|
412 | */
|
---|
413 | virtual void uninit() {}
|
---|
414 |
|
---|
415 | virtual HRESULT addCaller (State *aState = NULL, bool aLimited = false);
|
---|
416 | virtual void releaseCaller();
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Adds a limited caller. This method is equivalent to doing
|
---|
420 | * <tt>addCaller (aState, true)</tt>, but it is preferred because
|
---|
421 | * provides better self-descriptiveness. See #addCaller() for more info.
|
---|
422 | */
|
---|
423 | HRESULT addLimitedCaller (State *aState = NULL)
|
---|
424 | {
|
---|
425 | return addCaller (aState, true /* aLimited */);
|
---|
426 | }
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * Smart class that automatically increases the number of callers of the
|
---|
430 | * given VirtualBoxBase object when an instance is constructed and decreases
|
---|
431 | * it back when the created instance goes out of scope (i.e. gets destroyed).
|
---|
432 | *
|
---|
433 | * If #rc() returns a failure after the instance creation, it means that
|
---|
434 | * the managed VirtualBoxBase object is not Ready, or in any other invalid
|
---|
435 | * state, so that the caller must not use the object and can return this
|
---|
436 | * failed result code to the upper level.
|
---|
437 | *
|
---|
438 | * See VirtualBoxBase::addCaller(), VirtualBoxBase::addLimitedCaller() and
|
---|
439 | * VirtualBoxBase::releaseCaller() for more details about object callers.
|
---|
440 | *
|
---|
441 | * @param aLimited |false| if this template should use
|
---|
442 | * VirtualiBoxBase::addCaller() calls to add callers, or
|
---|
443 | * |true| if VirtualiBoxBase::addLimitedCaller() should be
|
---|
444 | * used.
|
---|
445 | *
|
---|
446 | * @note It is preferrable to use the AutoCaller and AutoLimitedCaller
|
---|
447 | * classes than specify the @a aLimited argument, for better
|
---|
448 | * self-descriptiveness.
|
---|
449 | */
|
---|
450 | template <bool aLimited>
|
---|
451 | class AutoCallerBase
|
---|
452 | {
|
---|
453 | public:
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Increases the number of callers of the given object
|
---|
457 | * by calling VirtualBoxBase::addCaller().
|
---|
458 | */
|
---|
459 | AutoCallerBase (VirtualBoxBaseNEXT_base *aObj) : mObj (aObj)
|
---|
460 | {
|
---|
461 | Assert (aObj);
|
---|
462 | mRC = mObj->addCaller (&mState, aLimited);
|
---|
463 | }
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * If the number of callers was successfully increased,
|
---|
467 | * decreases it using VirtualBoxBase::releaseCaller(), otherwise
|
---|
468 | * does nothing.
|
---|
469 | */
|
---|
470 | ~AutoCallerBase()
|
---|
471 | {
|
---|
472 | if (SUCCEEDED (mRC))
|
---|
473 | mObj->releaseCaller();
|
---|
474 | }
|
---|
475 |
|
---|
476 | /**
|
---|
477 | * Stores the result code returned by VirtualBoxBase::addCaller()
|
---|
478 | * after instance creation or after the last #add() call. A successful
|
---|
479 | * result code means the number of callers was successfully increased.
|
---|
480 | */
|
---|
481 | HRESULT rc() const { return mRC; }
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Returns |true| if |SUCCEEDED (rc())| is |true|, for convenience.
|
---|
485 | * |true| means the number of callers was successfully increased.
|
---|
486 | */
|
---|
487 | bool isOk() const { return SUCCEEDED (mRC); }
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * Stores the object state returned by VirtualBoxBase::addCaller()
|
---|
491 | * after instance creation or after the last #add() call.
|
---|
492 | */
|
---|
493 | State state() const { return mState; }
|
---|
494 |
|
---|
495 | /**
|
---|
496 | * Temporarily decreases the number of callers of the managed object.
|
---|
497 | * May only be called if #isOk() returns |true|. Note that #rc() will
|
---|
498 | * return E_FAIL after this method succeeds.
|
---|
499 | */
|
---|
500 | void release()
|
---|
501 | {
|
---|
502 | Assert (SUCCEEDED (mRC));
|
---|
503 | if (SUCCEEDED (mRC))
|
---|
504 | {
|
---|
505 | mObj->releaseCaller();
|
---|
506 | mRC = E_FAIL;
|
---|
507 | }
|
---|
508 | }
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Restores the number of callers decreased by #release(). May only
|
---|
512 | * be called after #release().
|
---|
513 | */
|
---|
514 | void add()
|
---|
515 | {
|
---|
516 | Assert (!SUCCEEDED (mRC));
|
---|
517 | if (!SUCCEEDED (mRC))
|
---|
518 | mRC = mObj->addCaller (&mState, aLimited);
|
---|
519 | }
|
---|
520 |
|
---|
521 | private:
|
---|
522 |
|
---|
523 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoCallerBase)
|
---|
524 | DECLARE_CLS_NEW_DELETE_NOOP (AutoCallerBase)
|
---|
525 |
|
---|
526 | VirtualBoxBaseNEXT_base *mObj;
|
---|
527 | HRESULT mRC;
|
---|
528 | State mState;
|
---|
529 | };
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Smart class that automatically increases the number of normal
|
---|
533 | * (non-limited) callers of the given VirtualBoxBase object when an
|
---|
534 | * instance is constructed and decreases it back when the created instance
|
---|
535 | * goes out of scope (i.e. gets destroyed).
|
---|
536 | *
|
---|
537 | * A typical usage pattern to declare a normal method of some object
|
---|
538 | * (i.e. a method that is valid only when the object provides its
|
---|
539 | * full functionality) is:
|
---|
540 | * <code>
|
---|
541 | * STDMETHODIMP Component::Foo()
|
---|
542 | * {
|
---|
543 | * AutoCaller autoCaller (this);
|
---|
544 | * CheckComRCReturnRC (autoCaller.rc());
|
---|
545 | * ...
|
---|
546 | * </code>
|
---|
547 | *
|
---|
548 | * Using this class is equivalent to using the AutoCallerBase template
|
---|
549 | * with the @a aLimited argument set to |false|, but this class is
|
---|
550 | * preferred because provides better self-descriptiveness.
|
---|
551 | *
|
---|
552 | * See AutoCallerBase for more information about auto caller functionality.
|
---|
553 | */
|
---|
554 | typedef AutoCallerBase <false> AutoCaller;
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Smart class that automatically increases the number of limited callers
|
---|
558 | * of the given VirtualBoxBase object when an instance is constructed and
|
---|
559 | * decreases it back when the created instance goes out of scope (i.e.
|
---|
560 | * gets destroyed).
|
---|
561 | *
|
---|
562 | * A typical usage pattern to declare a limited method of some object
|
---|
563 | * (i.e. a method that is valid even if the object doesn't provide its
|
---|
564 | * full functionality) is:
|
---|
565 | * <code>
|
---|
566 | * STDMETHODIMP Component::Bar()
|
---|
567 | * {
|
---|
568 | * AutoLimitedCaller autoCaller (this);
|
---|
569 | * CheckComRCReturnRC (autoCaller.rc());
|
---|
570 | * ...
|
---|
571 | * </code>
|
---|
572 | *
|
---|
573 | * Using this class is equivalent to using the AutoCallerBase template
|
---|
574 | * with the @a aLimited argument set to |true|, but this class is
|
---|
575 | * preferred because provides better self-descriptiveness.
|
---|
576 | *
|
---|
577 | * See AutoCallerBase for more information about auto caller functionality.
|
---|
578 | */
|
---|
579 | typedef AutoCallerBase <true> AutoLimitedCaller;
|
---|
580 |
|
---|
581 | protected:
|
---|
582 |
|
---|
583 | /**
|
---|
584 | * Smart class to enclose the state transition NotReady->InInit->Ready.
|
---|
585 | *
|
---|
586 | * Instances must be created at the beginning of init() methods of
|
---|
587 | * VirtualBoxBase subclasses as a stack-based variable using |this| pointer
|
---|
588 | * as the argument. When this variable is created it automatically places
|
---|
589 | * the object to the InInit state.
|
---|
590 | *
|
---|
591 | * When the created variable goes out of scope (i.e. gets destroyed),
|
---|
592 | * depending on the success status of this initialization span, it either
|
---|
593 | * places the object to the Ready state or calls the object's
|
---|
594 | * VirtualBoxBase::uninit() method which is supposed to place the object
|
---|
595 | * back to the NotReady state using the AutoUninitSpan class.
|
---|
596 | *
|
---|
597 | * The initial success status of the initialization span is determined by
|
---|
598 | * the @a aSuccess argument of the AutoInitSpan constructor (|false| by
|
---|
599 | * default). Inside the initialization span, the success status can be set
|
---|
600 | * to |true| using #setSucceeded() or to |false| using #setFailed(). Please
|
---|
601 | * don't forget to set the correct success status before letting the
|
---|
602 | * AutoInitSpan variable go out of scope (for example, by performing an
|
---|
603 | * early return from the init() method)!
|
---|
604 | *
|
---|
605 | * Note that if an instance of this class gets constructed when the
|
---|
606 | * object is in the state other than NotReady, #isOk() returns |false| and
|
---|
607 | * methods of this class do nothing: the state transition is not performed.
|
---|
608 | *
|
---|
609 | * A typical usage pattern is:
|
---|
610 | * <code>
|
---|
611 | * HRESULT Component::init()
|
---|
612 | * {
|
---|
613 | * AutoInitSpan autoInitSpan (this);
|
---|
614 | * AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
615 | * ...
|
---|
616 | * if (FAILED (rc))
|
---|
617 | * return rc;
|
---|
618 | * ...
|
---|
619 | * if (SUCCEEDED (rc))
|
---|
620 | * autoInitSpan.setSucceeded();
|
---|
621 | * return rc;
|
---|
622 | * }
|
---|
623 | * </code>
|
---|
624 | *
|
---|
625 | * @note Never create instances of this class outside init() methods of
|
---|
626 | * VirtualBoxBase subclasses and never pass anything other than |this| as
|
---|
627 | * the argument to the constructor!
|
---|
628 | */
|
---|
629 | class AutoInitSpan
|
---|
630 | {
|
---|
631 | public:
|
---|
632 |
|
---|
633 | enum Status { Failed = 0x0, Succeeded = 0x1, Limited = 0x2 };
|
---|
634 |
|
---|
635 | AutoInitSpan (VirtualBoxBaseNEXT_base *aObj, Status aStatus = Failed);
|
---|
636 | ~AutoInitSpan();
|
---|
637 |
|
---|
638 | /**
|
---|
639 | * Returns |true| if this instance has been created at the right moment
|
---|
640 | * (when the object was in the NotReady state) and |false| otherwise.
|
---|
641 | */
|
---|
642 | bool isOk() const { return mOk; }
|
---|
643 |
|
---|
644 | /**
|
---|
645 | * Sets the initialization status to Succeeded to indicates successful
|
---|
646 | * initialization. The AutoInitSpan destructor will place the managed
|
---|
647 | * VirtualBoxBase object to the Ready state.
|
---|
648 | */
|
---|
649 | void setSucceeded() { mStatus = Succeeded; }
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Sets the initialization status to Succeeded to indicate limited
|
---|
653 | * (partly successful) initialization. The AutoInitSpan destructor will
|
---|
654 | * place the managed VirtualBoxBase object to the Limited state.
|
---|
655 | */
|
---|
656 | void setLimited() { mStatus = Limited; }
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Sets the initialization status to Failure to indicates failed
|
---|
660 | * initialization. The AutoInitSpan destructor will place the managed
|
---|
661 | * VirtualBoxBase object to the InitFailed state and will automatically
|
---|
662 | * call its uninit() method which is supposed to place the object back
|
---|
663 | * to the NotReady state using AutoUninitSpan.
|
---|
664 | */
|
---|
665 | void setFailed() { mStatus = Failed; }
|
---|
666 |
|
---|
667 | /** Returns the current initialization status. */
|
---|
668 | Status status() { return mStatus; }
|
---|
669 |
|
---|
670 | private:
|
---|
671 |
|
---|
672 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoInitSpan)
|
---|
673 | DECLARE_CLS_NEW_DELETE_NOOP (AutoInitSpan)
|
---|
674 |
|
---|
675 | VirtualBoxBaseNEXT_base *mObj;
|
---|
676 | Status mStatus : 3; // must be at least total number of bits + 1 (sign)
|
---|
677 | bool mOk : 1;
|
---|
678 | };
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Smart class to enclose the state transition Limited->InInit->Ready.
|
---|
682 | *
|
---|
683 | * Instances must be created at the beginning of methods of VirtualBoxBase
|
---|
684 | * subclasses that try to re-initialize the object to bring it to the
|
---|
685 | * Ready state (full functionality) after partial initialization
|
---|
686 | * (limited functionality)>, as a stack-based variable using |this| pointer
|
---|
687 | * as the argument. When this variable is created it automatically places
|
---|
688 | * the object to the InInit state.
|
---|
689 | *
|
---|
690 | * When the created variable goes out of scope (i.e. gets destroyed),
|
---|
691 | * depending on the success status of this initialization span, it either
|
---|
692 | * places the object to the Ready state or brings it back to the Limited
|
---|
693 | * state.
|
---|
694 | *
|
---|
695 | * The initial success status of the re-initialization span is |false|.
|
---|
696 | * In order to make it successful, #setSucceeded() must be called before
|
---|
697 | * the instance is destroyed.
|
---|
698 | *
|
---|
699 | * Note that if an instance of this class gets constructed when the
|
---|
700 | * object is in the state other than Limited, #isOk() returns |false| and
|
---|
701 | * methods of this class do nothing: the state transition is not performed.
|
---|
702 | *
|
---|
703 | * A typical usage pattern is:
|
---|
704 | * <code>
|
---|
705 | * HRESULT Component::reinit()
|
---|
706 | * {
|
---|
707 | * AutoReadySpan autoReadySpan (this);
|
---|
708 | * AssertReturn (autoReadySpan.isOk(), E_UNEXPECTED);
|
---|
709 | * ...
|
---|
710 | * if (FAILED (rc))
|
---|
711 | * return rc;
|
---|
712 | * ...
|
---|
713 | * if (SUCCEEDED (rc))
|
---|
714 | * autoReadySpan.setSucceeded();
|
---|
715 | * return rc;
|
---|
716 | * }
|
---|
717 | * </code>
|
---|
718 | *
|
---|
719 | * @note Never create instances of this class outside re-initialization
|
---|
720 | * methods of VirtualBoxBase subclasses and never pass anything other than
|
---|
721 | * |this| as the argument to the constructor!
|
---|
722 | */
|
---|
723 | class AutoReadySpan
|
---|
724 | {
|
---|
725 | public:
|
---|
726 |
|
---|
727 | AutoReadySpan (VirtualBoxBaseNEXT_base *aObj);
|
---|
728 | ~AutoReadySpan();
|
---|
729 |
|
---|
730 | /**
|
---|
731 | * Returns |true| if this instance has been created at the right moment
|
---|
732 | * (when the object was in the Limited state) and |false| otherwise.
|
---|
733 | */
|
---|
734 | bool isOk() const { return mOk; }
|
---|
735 |
|
---|
736 | /**
|
---|
737 | * Sets the re-initialization status to Succeeded to indicates
|
---|
738 | * successful re-initialization. The AutoReadySpan destructor will
|
---|
739 | * place the managed VirtualBoxBase object to the Ready state.
|
---|
740 | */
|
---|
741 | void setSucceeded() { mSucceeded = true; }
|
---|
742 |
|
---|
743 | private:
|
---|
744 |
|
---|
745 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoReadySpan)
|
---|
746 | DECLARE_CLS_NEW_DELETE_NOOP (AutoReadySpan)
|
---|
747 |
|
---|
748 | VirtualBoxBaseNEXT_base *mObj;
|
---|
749 | bool mSucceeded : 1;
|
---|
750 | bool mOk : 1;
|
---|
751 | };
|
---|
752 |
|
---|
753 | /**
|
---|
754 | * Smart class to enclose the state transition Ready->InUnnit->NotReady or
|
---|
755 | * InitFailed->InUnnit->NotReady.
|
---|
756 | *
|
---|
757 | * Must be created at the beginning of uninit() methods of VirtualBoxBase
|
---|
758 | * subclasses as a stack-based variable using |this| pointer as the argument.
|
---|
759 | * When this variable is created it automatically places the object to the
|
---|
760 | * InUninit state, unless it is already in the NotReady state as indicated
|
---|
761 | * by #uninitDone() returning |true|. In the latter case, the uninit()
|
---|
762 | * method must immediately return because there should be nothing to
|
---|
763 | * uninitialize.
|
---|
764 | *
|
---|
765 | * When this variable goes out of scope (i.e. gets destroyed), it places
|
---|
766 | * the object to the NotReady state.
|
---|
767 | *
|
---|
768 | * A typical usage pattern is:
|
---|
769 | * <code>
|
---|
770 | * void Component::uninit()
|
---|
771 | * {
|
---|
772 | * AutoUninitSpan autoUninitSpan (this);
|
---|
773 | * if (autoUninitSpan.uninitDone())
|
---|
774 | * retrun;
|
---|
775 | * ...
|
---|
776 | * </code>
|
---|
777 | *
|
---|
778 | * @note Never create instances of this class outside uninit() methods and
|
---|
779 | * never pass anything other than |this| as the argument to the constructor!
|
---|
780 | */
|
---|
781 | class AutoUninitSpan
|
---|
782 | {
|
---|
783 | public:
|
---|
784 |
|
---|
785 | AutoUninitSpan (VirtualBoxBaseNEXT_base *aObj);
|
---|
786 | ~AutoUninitSpan();
|
---|
787 |
|
---|
788 | /** |true| when uninit() is called as a result of init() failure */
|
---|
789 | bool initFailed() { return mInitFailed; }
|
---|
790 |
|
---|
791 | /** |true| when uninit() has already been called (so the object is NotReady) */
|
---|
792 | bool uninitDone() { return mUninitDone; }
|
---|
793 |
|
---|
794 | private:
|
---|
795 |
|
---|
796 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoUninitSpan)
|
---|
797 | DECLARE_CLS_NEW_DELETE_NOOP (AutoUninitSpan)
|
---|
798 |
|
---|
799 | VirtualBoxBaseNEXT_base *mObj;
|
---|
800 | bool mInitFailed : 1;
|
---|
801 | bool mUninitDone : 1;
|
---|
802 | };
|
---|
803 |
|
---|
804 | private:
|
---|
805 |
|
---|
806 | void setState (State aState)
|
---|
807 | {
|
---|
808 | Assert (mState != aState);
|
---|
809 | mState = aState;
|
---|
810 | mStateChangeThread = RTThreadSelf();
|
---|
811 | }
|
---|
812 |
|
---|
813 | /** Primary state of this object */
|
---|
814 | State mState;
|
---|
815 | /** Thread that caused the last state change */
|
---|
816 | RTTHREAD mStateChangeThread;
|
---|
817 | /** Total number of active calls to this object */
|
---|
818 | unsigned mCallers;
|
---|
819 | /** Semaphore posted when the number of callers drops to zero */
|
---|
820 | RTSEMEVENT mZeroCallersSem;
|
---|
821 | /** Semaphore posted when the object goes from InInit some other state */
|
---|
822 | RTSEMEVENTMULTI mInitDoneSem;
|
---|
823 | /** Number of threads waiting for mInitDoneSem */
|
---|
824 | unsigned mInitDoneSemUsers;
|
---|
825 |
|
---|
826 | /** Protects access to state related data members */
|
---|
827 | RTCRITSECT mStateLock;
|
---|
828 |
|
---|
829 | /** User-level object lock for subclasses */
|
---|
830 | mutable AutoLock::Handle *mObjectLock;
|
---|
831 | };
|
---|
832 |
|
---|
833 | /**
|
---|
834 | * This macro adds the error info support to methods of the VirtualBoxBase
|
---|
835 | * class (by overriding them). Place it to the public section of the
|
---|
836 | * VirtualBoxBase subclass and the following methods will set the extended
|
---|
837 | * error info in case of failure instead of just returning the result code:
|
---|
838 | *
|
---|
839 | * <ul>
|
---|
840 | * <li>VirtualBoxBase::addCaller()
|
---|
841 | * </ul>
|
---|
842 | *
|
---|
843 | * @note The given VirtualBoxBase subclass must also inherit from both
|
---|
844 | * VirtualBoxSupportErrorInfoImpl and VirtualBoxSupportTranslation templates!
|
---|
845 | *
|
---|
846 | * @param C VirtualBoxBase subclass to add the error info support to
|
---|
847 | */
|
---|
848 | #define VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(C) \
|
---|
849 | virtual HRESULT addCaller (VirtualBoxBaseNEXT_base::State *aState = NULL, \
|
---|
850 | bool aLimited = false) \
|
---|
851 | { \
|
---|
852 | VirtualBoxBaseNEXT_base::State state; \
|
---|
853 | HRESULT rc = VirtualBoxBaseNEXT_base::addCaller (&state, aLimited); \
|
---|
854 | if (FAILED (rc)) \
|
---|
855 | { \
|
---|
856 | if (state == VirtualBoxBaseNEXT_base::Limited) \
|
---|
857 | rc = setError (rc, tr ("The object functonality is limited")); \
|
---|
858 | else \
|
---|
859 | rc = setError (rc, tr ("The object is not ready")); \
|
---|
860 | } \
|
---|
861 | if (aState) \
|
---|
862 | *aState = state; \
|
---|
863 | return rc; \
|
---|
864 | } \
|
---|
865 |
|
---|
866 | ////////////////////////////////////////////////////////////////////////////////
|
---|
867 |
|
---|
868 | /// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
|
---|
869 | class ATL_NO_VTABLE VirtualBoxBase : public VirtualBoxBaseNEXT_base
|
---|
870 | //#ifdef __WIN__
|
---|
871 | // : public CComObjectRootEx<CComMultiThreadModel>
|
---|
872 | //#else
|
---|
873 | // : public CComObjectRootEx
|
---|
874 | //#endif
|
---|
875 | {
|
---|
876 |
|
---|
877 | public:
|
---|
878 | VirtualBoxBase()
|
---|
879 | {
|
---|
880 | mReady = false;
|
---|
881 | }
|
---|
882 | virtual ~VirtualBoxBase()
|
---|
883 | {
|
---|
884 | }
|
---|
885 |
|
---|
886 | /**
|
---|
887 | * Virtual unintialization method. Called during parent object's
|
---|
888 | * uninitialization, if the given subclass instance is a dependent child of
|
---|
889 | * a class dervived from VirtualBoxBaseWithChildren (@sa
|
---|
890 | * VirtualBoxBaseWithChildren::addDependentChild). In this case, this
|
---|
891 | * method's impelemtation must call setReady (false),
|
---|
892 | */
|
---|
893 | virtual void uninit() {}
|
---|
894 |
|
---|
895 |
|
---|
896 | // sets the ready state of the object
|
---|
897 | void setReady(bool isReady)
|
---|
898 | {
|
---|
899 | mReady = isReady;
|
---|
900 | }
|
---|
901 | // get the ready state of the object
|
---|
902 | bool isReady()
|
---|
903 | {
|
---|
904 | return mReady;
|
---|
905 | }
|
---|
906 |
|
---|
907 | static const char *translate (const char *context, const char *sourceText,
|
---|
908 | const char *comment = 0);
|
---|
909 |
|
---|
910 | private:
|
---|
911 |
|
---|
912 | // flag determining whether an object is ready
|
---|
913 | // for usage, i.e. methods may be called
|
---|
914 | bool mReady;
|
---|
915 | // mutex semaphore to lock the object
|
---|
916 | };
|
---|
917 |
|
---|
918 | /**
|
---|
919 | * Temporary class to disable deprecated methods of VirtualBoxBase.
|
---|
920 | * Can be used as a base for components that are completely switched to
|
---|
921 | * the new locking scheme (VirtualBoxBaseNEXT_base).
|
---|
922 | *
|
---|
923 | * @todo remove after we switch to VirtualBoxBaseNEXT completely.
|
---|
924 | */
|
---|
925 | class VirtualBoxBaseNEXT : public VirtualBoxBase
|
---|
926 | {
|
---|
927 | private:
|
---|
928 |
|
---|
929 | void lock();
|
---|
930 | void unlock();
|
---|
931 | void setReady (bool isReady);
|
---|
932 | bool isReady();
|
---|
933 | };
|
---|
934 |
|
---|
935 | ////////////////////////////////////////////////////////////////////////////////
|
---|
936 |
|
---|
937 | /** Helper for VirtualBoxSupportTranslation */
|
---|
938 | class VirtualBoxSupportTranslationBase
|
---|
939 | {
|
---|
940 | protected:
|
---|
941 | static bool cutClassNameFrom__PRETTY_FUNCTION__ (char *prettyFunctionName);
|
---|
942 | };
|
---|
943 |
|
---|
944 | /**
|
---|
945 | * This template implements the NLS string translation support for the
|
---|
946 | * given class by providing a #tr() function.
|
---|
947 | *
|
---|
948 | * @param C class that needs to support the string translation
|
---|
949 | *
|
---|
950 | * @note
|
---|
951 | * Every class that wants to use the #tr() function in its own methods must
|
---|
952 | * inherit from this template, regardless of whether its base class (if any)
|
---|
953 | * inherits from it or not! Otherwise, the translation service will not
|
---|
954 | * work correctly. However, the declaration of the resulting class must
|
---|
955 | * contain the VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(<ClassName>) macro
|
---|
956 | * if one of its base classes also inherits from this template (to resolve
|
---|
957 | * the ambiguity of the #tr() function).
|
---|
958 | */
|
---|
959 | template <class C>
|
---|
960 | class VirtualBoxSupportTranslation : virtual protected VirtualBoxSupportTranslationBase
|
---|
961 | {
|
---|
962 | public:
|
---|
963 |
|
---|
964 | /**
|
---|
965 | * Translates the given text string according to the currently installed
|
---|
966 | * translation table and current context, which is determined by the
|
---|
967 | * class name. See VirtualBoxBase::translate() for more info.
|
---|
968 | *
|
---|
969 | * @param sourceText the string to translate
|
---|
970 | * @param comment the comment to the string (NULL means no comment)
|
---|
971 | *
|
---|
972 | * @return
|
---|
973 | * the translated version of the source string in UTF-8 encoding,
|
---|
974 | * or the source string itself if the translation is not found in
|
---|
975 | * the current context.
|
---|
976 | */
|
---|
977 | inline static const char *tr (const char *sourceText, const char *comment = 0)
|
---|
978 | {
|
---|
979 | return VirtualBoxBase::translate (getClassName(), sourceText, comment);
|
---|
980 | }
|
---|
981 |
|
---|
982 | protected:
|
---|
983 |
|
---|
984 | static const char *getClassName()
|
---|
985 | {
|
---|
986 | static char fn [sizeof (__PRETTY_FUNCTION__) + 1];
|
---|
987 | if (!className)
|
---|
988 | {
|
---|
989 | strcpy (fn, __PRETTY_FUNCTION__);
|
---|
990 | cutClassNameFrom__PRETTY_FUNCTION__ (fn);
|
---|
991 | className = fn;
|
---|
992 | }
|
---|
993 | return className;
|
---|
994 | }
|
---|
995 |
|
---|
996 | private:
|
---|
997 |
|
---|
998 | static const char *className;
|
---|
999 | };
|
---|
1000 |
|
---|
1001 | template <class C>
|
---|
1002 | const char *VirtualBoxSupportTranslation <C>::className = NULL;
|
---|
1003 |
|
---|
1004 | /**
|
---|
1005 | * This macro must be invoked inside the public section of the declaration of
|
---|
1006 | * the class inherited from the VirtualBoxSupportTranslation template, in case
|
---|
1007 | * when one of its other base classes also inherits from that template. This is
|
---|
1008 | * necessary to resolve the ambiguity of the #tr() function.
|
---|
1009 | *
|
---|
1010 | * @param C class that inherits from the VirtualBoxSupportTranslation template
|
---|
1011 | * more than once (through its other base clases)
|
---|
1012 | */
|
---|
1013 | #define VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(C) \
|
---|
1014 | inline static const char *tr (const char *sourceText, const char *comment = 0) \
|
---|
1015 | { \
|
---|
1016 | return VirtualBoxSupportTranslation <C>::tr (sourceText, comment); \
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /**
|
---|
1020 | * A dummy macro that is used to shut down Qt's lupdate tool warnings
|
---|
1021 | * in some situations. This macro needs to be present inside (better at the
|
---|
1022 | * very beginning) of the declaration of the class that inherits from
|
---|
1023 | * VirtualBoxSupportTranslation template, to make lupdate happy.
|
---|
1024 | */
|
---|
1025 | #define Q_OBJECT
|
---|
1026 |
|
---|
1027 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1028 |
|
---|
1029 | /**
|
---|
1030 | * Helper for the VirtualBoxSupportErrorInfoImpl template.
|
---|
1031 | */
|
---|
1032 | class VirtualBoxSupportErrorInfoImplBase
|
---|
1033 | {
|
---|
1034 | protected:
|
---|
1035 |
|
---|
1036 | static HRESULT setError (HRESULT resultCode, const GUID &iid,
|
---|
1037 | const Bstr &component,
|
---|
1038 | const Bstr &text);
|
---|
1039 |
|
---|
1040 | static HRESULT setError (HRESULT resultCode, const GUID &iid,
|
---|
1041 | const Bstr &component,
|
---|
1042 | const char *text, va_list args);
|
---|
1043 |
|
---|
1044 | inline static HRESULT setError (const com::ErrorInfo &ei)
|
---|
1045 | {
|
---|
1046 | if (!ei.isBasicAvailable())
|
---|
1047 | return S_OK;
|
---|
1048 |
|
---|
1049 | Assert (FAILED (ei.getResultCode()));
|
---|
1050 | Assert (!ei.getText().isEmpty());
|
---|
1051 | return setError (ei.getResultCode(), ei.getInterfaceID(),
|
---|
1052 | ei.getComponent(), ei.getText());
|
---|
1053 | }
|
---|
1054 | };
|
---|
1055 |
|
---|
1056 | /**
|
---|
1057 | * This template implements ISupportErrorInfo for the given component class
|
---|
1058 | * and provides the #setError() method to conveniently set the error information
|
---|
1059 | * from within interface methods' implementations.
|
---|
1060 | *
|
---|
1061 | * On Windows, the template argument must define a COM interface map using
|
---|
1062 | * BEGIN_COM_MAP / END_COM_MAP macros and this map must contain a
|
---|
1063 | * COM_INTERFACE_ENTRY(ISupportErrorInfo) definition. All interface entries
|
---|
1064 | * that follow it will be considered to support IErrorInfo, i.e. the
|
---|
1065 | * InterfaceSupportsErrorInfo() implementation will return S_OK for the
|
---|
1066 | * corresponding IID.
|
---|
1067 | *
|
---|
1068 | * On all platforms, the template argument must also define the following
|
---|
1069 | * method: |public static const wchar_t *C::getComponentName()|. See
|
---|
1070 | * #setError (HRESULT, const char *, ...) for a description on how it is
|
---|
1071 | * used.
|
---|
1072 | *
|
---|
1073 | * @param C
|
---|
1074 | * component class that implements one or more COM interfaces
|
---|
1075 | * @param I
|
---|
1076 | * default interface for the component. This interface's IID is used
|
---|
1077 | * by the shortest form of #setError, for convenience.
|
---|
1078 | */
|
---|
1079 | template <class C, class I>
|
---|
1080 | class ATL_NO_VTABLE VirtualBoxSupportErrorInfoImpl
|
---|
1081 | : protected VirtualBoxSupportErrorInfoImplBase
|
---|
1082 | #if defined (__WIN__)
|
---|
1083 | , public ISupportErrorInfo
|
---|
1084 | #else
|
---|
1085 | #endif
|
---|
1086 | {
|
---|
1087 | public:
|
---|
1088 |
|
---|
1089 | #if defined (__WIN__)
|
---|
1090 | STDMETHOD(InterfaceSupportsErrorInfo) (REFIID riid)
|
---|
1091 | {
|
---|
1092 | const _ATL_INTMAP_ENTRY* pEntries = C::_GetEntries();
|
---|
1093 | Assert (pEntries);
|
---|
1094 | if (!pEntries)
|
---|
1095 | return S_FALSE;
|
---|
1096 |
|
---|
1097 | BOOL bSupports = FALSE;
|
---|
1098 | BOOL bISupportErrorInfoFound = FALSE;
|
---|
1099 |
|
---|
1100 | while (pEntries->pFunc != NULL && !bSupports)
|
---|
1101 | {
|
---|
1102 | if (!bISupportErrorInfoFound)
|
---|
1103 | {
|
---|
1104 | // skip the com map entries until ISupportErrorInfo is found
|
---|
1105 | bISupportErrorInfoFound =
|
---|
1106 | InlineIsEqualGUID (*(pEntries->piid), IID_ISupportErrorInfo);
|
---|
1107 | }
|
---|
1108 | else
|
---|
1109 | {
|
---|
1110 | // look for the requested interface in the rest of the com map
|
---|
1111 | bSupports = InlineIsEqualGUID (*(pEntries->piid), riid);
|
---|
1112 | }
|
---|
1113 | pEntries++;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | Assert (bISupportErrorInfoFound);
|
---|
1117 |
|
---|
1118 | return bSupports ? S_OK : S_FALSE;
|
---|
1119 | }
|
---|
1120 | #endif // defined (__WIN__)
|
---|
1121 |
|
---|
1122 | protected:
|
---|
1123 |
|
---|
1124 | /**
|
---|
1125 | * Sets the error information for the current thread.
|
---|
1126 | * This information can be retrieved by a caller of an interface method
|
---|
1127 | * using IErrorInfo on Windows or nsIException on Linux, or the cross-platform
|
---|
1128 | * IVirtualBoxErrorInfo interface that provides extended error info (only
|
---|
1129 | * for components from the VirtualBox COM library). Alternatively, the
|
---|
1130 | * platform-independent class com::ErrorInfo (defined in VBox[XP]COM.lib)
|
---|
1131 | * can be used to retrieve error info in a convenient way.
|
---|
1132 | *
|
---|
1133 | * It is assumed that the interface method that uses this function returns
|
---|
1134 | * an unsuccessful result code to the caller (otherwise, there is no reason
|
---|
1135 | * for the caller to try to retrieve error info after method invocation).
|
---|
1136 | *
|
---|
1137 | * Here is a table of correspondence between this method's arguments
|
---|
1138 | * and IErrorInfo/nsIException/IVirtualBoxErrorInfo attributes/methods:
|
---|
1139 | *
|
---|
1140 | * argument IErrorInfo nsIException IVirtualBoxErrorInfo
|
---|
1141 | * ----------------------------------------------------------------
|
---|
1142 | * resultCode -- result resultCode
|
---|
1143 | * iid GetGUID -- interfaceID
|
---|
1144 | * component GetSource -- component
|
---|
1145 | * text GetDescription message text
|
---|
1146 | *
|
---|
1147 | * This method is rarely needs to be used though. There are more convenient
|
---|
1148 | * overloaded versions, that automatically substitute some arguments
|
---|
1149 | * taking their values from the template parameters. See
|
---|
1150 | * #setError (HRESULT, const char *, ...) for an example.
|
---|
1151 | *
|
---|
1152 | * @param resultCode result (error) code, must not be S_OK
|
---|
1153 | * @param iid IID of the intrface that defines the error
|
---|
1154 | * @param component name of the component that generates the error
|
---|
1155 | * @param text error message (must not be null), an RTStrPrintf-like
|
---|
1156 | * format string in UTF-8 encoding
|
---|
1157 | * @param ... list of arguments for the format string
|
---|
1158 | *
|
---|
1159 | * @return
|
---|
1160 | * the error argument, for convenience, If an error occures while
|
---|
1161 | * creating error info itself, that error is returned instead of the
|
---|
1162 | * error argument.
|
---|
1163 | */
|
---|
1164 | inline static HRESULT setError (HRESULT resultCode, const GUID &iid,
|
---|
1165 | const wchar_t *component,
|
---|
1166 | const char *text, ...)
|
---|
1167 | {
|
---|
1168 | va_list args;
|
---|
1169 | va_start (args, text);
|
---|
1170 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError (
|
---|
1171 | resultCode, iid, component, text, args);
|
---|
1172 | va_end (args);
|
---|
1173 | return rc;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | /**
|
---|
1177 | * Sets the error information for the current thread.
|
---|
1178 | * A convenience method that automatically sets the default interface
|
---|
1179 | * ID (taken from the I template argument) and the component name
|
---|
1180 | * (a value of C::getComponentName()).
|
---|
1181 | *
|
---|
1182 | * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
|
---|
1183 | * for details.
|
---|
1184 | *
|
---|
1185 | * This method is the most common (and convenient) way to set error
|
---|
1186 | * information from within interface methods. A typical pattern of usage
|
---|
1187 | * is looks like this:
|
---|
1188 | *
|
---|
1189 | * <code>
|
---|
1190 | * return setError (E_FAIL, "Terrible Error");
|
---|
1191 | * </code>
|
---|
1192 | * or
|
---|
1193 | * <code>
|
---|
1194 | * HRESULT rc = setError (E_FAIL, "Terrible Error");
|
---|
1195 | * ...
|
---|
1196 | * return rc;
|
---|
1197 | * </code>
|
---|
1198 | */
|
---|
1199 | inline static HRESULT setError (HRESULT resultCode, const char *text, ...)
|
---|
1200 | {
|
---|
1201 | va_list args;
|
---|
1202 | va_start (args, text);
|
---|
1203 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError (
|
---|
1204 | resultCode, COM_IIDOF(I), C::getComponentName(), text, args);
|
---|
1205 | va_end (args);
|
---|
1206 | return rc;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | /**
|
---|
1210 | * Sets the error information for the current thread, va_list variant.
|
---|
1211 | * A convenience method that automatically sets the default interface
|
---|
1212 | * ID (taken from the I template argument) and the component name
|
---|
1213 | * (a value of C::getComponentName()).
|
---|
1214 | *
|
---|
1215 | * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
|
---|
1216 | * for details.
|
---|
1217 | *
|
---|
1218 | * This method is the most common (and convenient) way to set error
|
---|
1219 | * information from within interface methods. A typical pattern of usage
|
---|
1220 | * is looks like this:
|
---|
1221 | *
|
---|
1222 | * <code>
|
---|
1223 | * return setErrorV (E_FAIL, pszFormat, args);
|
---|
1224 | * </code>
|
---|
1225 | * or
|
---|
1226 | * <code>
|
---|
1227 | * HRESULT rc = setErrorV (E_FAIL, pszFormat, args);
|
---|
1228 | * ...
|
---|
1229 | * return rc;
|
---|
1230 | * </code>
|
---|
1231 | */
|
---|
1232 | inline static HRESULT setErrorV (HRESULT resultCode, const char *text, va_list args)
|
---|
1233 | {
|
---|
1234 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError (
|
---|
1235 | resultCode, COM_IIDOF(I), C::getComponentName(), text, args);
|
---|
1236 | return rc;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | /**
|
---|
1240 | * Sets the error information for the current thread.
|
---|
1241 | * A convenience method that automatically sets the component name
|
---|
1242 | * (a value of C::getComponentName()), but allows to specify the interface
|
---|
1243 | * id manually.
|
---|
1244 | *
|
---|
1245 | * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
|
---|
1246 | * for details.
|
---|
1247 | */
|
---|
1248 | inline static HRESULT setError (HRESULT resultCode, const GUID &iid,
|
---|
1249 | const char *text, ...)
|
---|
1250 | {
|
---|
1251 | va_list args;
|
---|
1252 | va_start (args, text);
|
---|
1253 | HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError (
|
---|
1254 | resultCode, iid, C::getComponentName(), text, args);
|
---|
1255 | va_end (args);
|
---|
1256 | return rc;
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | /**
|
---|
1260 | * Sets the error information for the current thread using the error
|
---|
1261 | * information previously fetched using an ErrorInfo instance.
|
---|
1262 | * This is useful, for instance, if you want to keep the error information
|
---|
1263 | * returned by some interface's method, but need to call another method
|
---|
1264 | * of some interface prior to returning from your method (calling
|
---|
1265 | * another method will normally reset the current error information).
|
---|
1266 | */
|
---|
1267 | inline static HRESULT setError (const com::ErrorInfo &ei)
|
---|
1268 | {
|
---|
1269 | return VirtualBoxSupportErrorInfoImplBase::setError (ei);
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | private:
|
---|
1273 |
|
---|
1274 | };
|
---|
1275 |
|
---|
1276 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1277 |
|
---|
1278 | /**
|
---|
1279 | * Base class to track VirtualBoxBase chlidren of the component.
|
---|
1280 | *
|
---|
1281 | * This class is a preferrable VirtualBoxBase replacement for components
|
---|
1282 | * that operate with collections of child components. It gives two useful
|
---|
1283 | * possibilities:
|
---|
1284 | *
|
---|
1285 | * <ol><li>
|
---|
1286 | * Given an IUnknown instance, it's possible to quickly determine
|
---|
1287 | * whether this instance represents a child object created by the given
|
---|
1288 | * component, and if so, get a valid VirtualBoxBase pointer to the child
|
---|
1289 | * object. The returned pointer can be then safely casted to the
|
---|
1290 | * actual class of the child object (to get access to its "internal"
|
---|
1291 | * non-interface methods) provided that no other child components implement
|
---|
1292 | * the same initial interface IUnknown is queried from.
|
---|
1293 | * </li><li>
|
---|
1294 | * When the parent object uninitializes itself, it can easily unintialize
|
---|
1295 | * all its VirtualBoxBase derived children (using their
|
---|
1296 | * VirtualBoxBase::uninit() implementations). This is done simply by
|
---|
1297 | * calling the #uninitDependentChildren() method.
|
---|
1298 | * </li><ol>
|
---|
1299 | *
|
---|
1300 | * In order to let the above work, the following must be done:
|
---|
1301 | * <ol><li>
|
---|
1302 | * When a child object is initialized, it calls #addDependentChild() of
|
---|
1303 | * its parent to register itself within the list of dependent children.
|
---|
1304 | * </li><li>
|
---|
1305 | * When a child object it is uninitialized, it calls #removeDependentChild()
|
---|
1306 | * to unregister itself. This must be done <b>after</b> the child has called
|
---|
1307 | * setReady(false) to indicate it is no more valid, and <b>not</b> from under
|
---|
1308 | * the child object's lock. Note also, that the first action the child's
|
---|
1309 | * uninit() implementation must do is to check for readiness after acquiring
|
---|
1310 | * the object's lock and return immediately if not ready.
|
---|
1311 | * </li><ol>
|
---|
1312 | *
|
---|
1313 | * Children added by #addDependentChild() are <b>weakly</b> referenced
|
---|
1314 | * (i.e. AddRef() is not called), so when a child is externally destructed
|
---|
1315 | * (i.e. its reference count goes to zero), it will automatically remove
|
---|
1316 | * itself from a map of dependent children, provided that it follows the
|
---|
1317 | * rules described here.
|
---|
1318 | *
|
---|
1319 | * @note
|
---|
1320 | * Because of weak referencing, deadlocks and assertions are very likely
|
---|
1321 | * if #addDependentChild() or #removeDependentChild() are used incorrectly
|
---|
1322 | * (called at inappropriate times). Check the above rules once more.
|
---|
1323 | */
|
---|
1324 | class VirtualBoxBaseWithChildren : public VirtualBoxBase
|
---|
1325 | {
|
---|
1326 | public:
|
---|
1327 |
|
---|
1328 | VirtualBoxBaseWithChildren()
|
---|
1329 | : mUninitDoneSem (NIL_RTSEMEVENT), mChildrenLeft (0)
|
---|
1330 | {
|
---|
1331 | RTCritSectInit (&mMapLock);
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | virtual ~VirtualBoxBaseWithChildren()
|
---|
1335 | {
|
---|
1336 | RTCritSectDelete (&mMapLock);
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | /**
|
---|
1340 | * Adds the given child to the map of dependent children.
|
---|
1341 | * Intended to be called from the child's init() method,
|
---|
1342 | * from under the child's lock.
|
---|
1343 | *
|
---|
1344 | * @param C the child object to add (must inherit VirtualBoxBase AND
|
---|
1345 | * implement some interface)
|
---|
1346 | */
|
---|
1347 | template <class C>
|
---|
1348 | void addDependentChild (C *child)
|
---|
1349 | {
|
---|
1350 | AssertReturn (child, (void) 0);
|
---|
1351 | addDependentChild (child, child);
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | /**
|
---|
1355 | * Removes the given child from the map of dependent children.
|
---|
1356 | * Must be called <b>after<b> the child has called setReady(false), and
|
---|
1357 | * <b>not</b> from under the child object's lock.
|
---|
1358 | *
|
---|
1359 | * @param C the child object to remove (must inherit VirtualBoxBase AND
|
---|
1360 | * implement some interface)
|
---|
1361 | */
|
---|
1362 | template <class C>
|
---|
1363 | void removeDependentChild (C *child)
|
---|
1364 | {
|
---|
1365 | AssertReturn (child, (void) 0);
|
---|
1366 | /// @todo (r=dmik) the below check (and the relevant comment above)
|
---|
1367 | // seems to be not necessary any more once we completely switch to
|
---|
1368 | // the NEXT locking scheme. This requires altering removeDependentChild()
|
---|
1369 | // and uninitDependentChildren() as well (due to the new state scheme,
|
---|
1370 | // there is a separate mutex for state transition, so calling the
|
---|
1371 | // child's uninit() from under the children map lock should not produce
|
---|
1372 | // dead-locks any more).
|
---|
1373 | Assert (!child->isLockedOnCurrentThread());
|
---|
1374 | removeDependentChild (ComPtr <IUnknown> (child));
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | protected:
|
---|
1378 |
|
---|
1379 | void uninitDependentChildren();
|
---|
1380 |
|
---|
1381 | VirtualBoxBase *getDependentChild (const ComPtr <IUnknown> &unk);
|
---|
1382 |
|
---|
1383 | private:
|
---|
1384 |
|
---|
1385 | void addDependentChild (const ComPtr <IUnknown> &unk, VirtualBoxBase *child);
|
---|
1386 | void removeDependentChild (const ComPtr <IUnknown> &unk);
|
---|
1387 |
|
---|
1388 | typedef std::map <IUnknown *, VirtualBoxBase *> DependentChildren;
|
---|
1389 | DependentChildren mDependentChildren;
|
---|
1390 |
|
---|
1391 | RTCRITSECT mMapLock;
|
---|
1392 | RTSEMEVENT mUninitDoneSem;
|
---|
1393 | unsigned mChildrenLeft;
|
---|
1394 | };
|
---|
1395 |
|
---|
1396 | /**
|
---|
1397 | * Temporary class to disable deprecated methods of VirtualBoxBase.
|
---|
1398 | * Can be used as a base for components that are completely switched to
|
---|
1399 | * the new locking scheme (VirtualBoxBaseNEXT_base).
|
---|
1400 | *
|
---|
1401 | * @todo remove after we switch to VirtualBoxBaseNEXT completely.
|
---|
1402 | */
|
---|
1403 | class VirtualBoxBaseWithChildrenNEXT : public VirtualBoxBaseWithChildren
|
---|
1404 | {
|
---|
1405 | private:
|
---|
1406 |
|
---|
1407 | void lock();
|
---|
1408 | void unlock();
|
---|
1409 | void setReady (bool isReady);
|
---|
1410 | bool isReady();
|
---|
1411 | };
|
---|
1412 |
|
---|
1413 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1414 |
|
---|
1415 | /**
|
---|
1416 | * Base class to track component's chlidren of some particular type.
|
---|
1417 | *
|
---|
1418 | * This class is similar to VirtualBoxBaseWithChildren, with the exception
|
---|
1419 | * that all children must be of the same type. For this reason, it's not
|
---|
1420 | * necessary to use a map to store children, so a list is used instead.
|
---|
1421 | *
|
---|
1422 | * As opposed to VirtualBoxBaseWithChildren, children added by
|
---|
1423 | * #addDependentChild() are <b>strongly</b> referenced, so that they cannot
|
---|
1424 | * be externally destructed until #removeDependentChild() is called.
|
---|
1425 | * For this reason, strict rules of calling #removeDependentChild() don't
|
---|
1426 | * apply to instances of this class -- it can be called anywhere in the
|
---|
1427 | * child's uninit() implementation.
|
---|
1428 | *
|
---|
1429 | * @param C type of child objects (must inherit VirtualBoxBase AND
|
---|
1430 | * implement some interface)
|
---|
1431 | */
|
---|
1432 | template <class C>
|
---|
1433 | class VirtualBoxBaseWithTypedChildren : public VirtualBoxBase
|
---|
1434 | {
|
---|
1435 | public:
|
---|
1436 |
|
---|
1437 | typedef std::list <ComObjPtr <C> > DependentChildren;
|
---|
1438 |
|
---|
1439 | VirtualBoxBaseWithTypedChildren() : mInUninit (false) {}
|
---|
1440 |
|
---|
1441 | virtual ~VirtualBoxBaseWithTypedChildren() {}
|
---|
1442 |
|
---|
1443 | /**
|
---|
1444 | * Adds the given child to the list of dependent children.
|
---|
1445 | * Must be called from the child's init() method,
|
---|
1446 | * from under the child's lock.
|
---|
1447 | *
|
---|
1448 | * @param C the child object to add (must inherit VirtualBoxBase AND
|
---|
1449 | * implement some interface)
|
---|
1450 | */
|
---|
1451 | void addDependentChild (C *child)
|
---|
1452 | {
|
---|
1453 | AssertReturn (child, (void) 0);
|
---|
1454 |
|
---|
1455 | AutoLock alock (mMapLock);
|
---|
1456 | if (mInUninit)
|
---|
1457 | return;
|
---|
1458 |
|
---|
1459 | mDependentChildren.push_back (child);
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | /**
|
---|
1463 | * Removes the given child from the list of dependent children.
|
---|
1464 | * Must be called from the child's uninit() method,
|
---|
1465 | * under the child's lock.
|
---|
1466 | *
|
---|
1467 | * @param C the child object to remove (must inherit VirtualBoxBase AND
|
---|
1468 | * implement some interface)
|
---|
1469 | */
|
---|
1470 | void removeDependentChild (C *child)
|
---|
1471 | {
|
---|
1472 | AssertReturn (child, (void) 0);
|
---|
1473 |
|
---|
1474 | AutoLock alock (mMapLock);
|
---|
1475 | if (mInUninit)
|
---|
1476 | return;
|
---|
1477 |
|
---|
1478 | mDependentChildren.remove (child);
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | protected:
|
---|
1482 |
|
---|
1483 | /**
|
---|
1484 | * Returns an internal lock handle to lock the list of children
|
---|
1485 | * returned by #dependentChildren() using AutoLock:
|
---|
1486 | * <code>
|
---|
1487 | * AutoLock alock (dependentChildrenLock());
|
---|
1488 | * </code>
|
---|
1489 | */
|
---|
1490 | AutoLock::Handle &dependentChildrenLock() const { return mMapLock; }
|
---|
1491 |
|
---|
1492 | /**
|
---|
1493 | * Returns the read-only list of all dependent children.
|
---|
1494 | * @note
|
---|
1495 | * Access the returned list (iterate, get size etc.) only after
|
---|
1496 | * doing |AutoLock alock (dependentChildrenLock());|!
|
---|
1497 | */
|
---|
1498 | const DependentChildren &dependentChildren() const { return mDependentChildren; }
|
---|
1499 |
|
---|
1500 | /**
|
---|
1501 | * Uninitializes all dependent children registered with #addDependentChild().
|
---|
1502 | *
|
---|
1503 | * @note
|
---|
1504 | * This method will call uninit() methods of children. If these methods
|
---|
1505 | * access the parent object, uninitDependentChildren() must be called
|
---|
1506 | * either at the beginning of the parent uninitialization sequence (when
|
---|
1507 | * it is still operational) or after setReady(false) is called to
|
---|
1508 | * indicate the parent is out of action.
|
---|
1509 | */
|
---|
1510 | void uninitDependentChildren()
|
---|
1511 | {
|
---|
1512 | AutoLock alock (this);
|
---|
1513 | AutoLock mapLock (mMapLock);
|
---|
1514 |
|
---|
1515 | if (mDependentChildren.size())
|
---|
1516 | {
|
---|
1517 | // set flag to ignore #removeDependentChild() called from child->uninit()
|
---|
1518 | mInUninit = true;
|
---|
1519 |
|
---|
1520 | // leave the locks to let children waiting for #removeDependentChild() run
|
---|
1521 | mapLock.leave();
|
---|
1522 | alock.leave();
|
---|
1523 |
|
---|
1524 | for (typename DependentChildren::iterator it = mDependentChildren.begin();
|
---|
1525 | it != mDependentChildren.end(); ++ it)
|
---|
1526 | {
|
---|
1527 | C *child = (*it);
|
---|
1528 | Assert (child);
|
---|
1529 | if (child)
|
---|
1530 | child->uninit();
|
---|
1531 | }
|
---|
1532 | mDependentChildren.clear();
|
---|
1533 |
|
---|
1534 | alock.enter();
|
---|
1535 | mapLock.enter();
|
---|
1536 |
|
---|
1537 | mInUninit = false;
|
---|
1538 | }
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | /**
|
---|
1542 | * Removes (detaches) all dependent children registered with
|
---|
1543 | * #addDependentChild(), without uninitializing them.
|
---|
1544 | *
|
---|
1545 | * @note This method must be called from under the main object's lock
|
---|
1546 | */
|
---|
1547 | void removeDependentChildren()
|
---|
1548 | {
|
---|
1549 | AutoLock alock (mMapLock);
|
---|
1550 | mDependentChildren.clear();
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | private:
|
---|
1554 |
|
---|
1555 | DependentChildren mDependentChildren;
|
---|
1556 |
|
---|
1557 | bool mInUninit;
|
---|
1558 | mutable AutoLock::Handle mMapLock;
|
---|
1559 | };
|
---|
1560 |
|
---|
1561 | /**
|
---|
1562 | * Temporary class to disable deprecated methods of VirtualBoxBase.
|
---|
1563 | * Can be used as a base for components that are completely switched to
|
---|
1564 | * the new locking scheme (VirtualBoxBaseNEXT_base).
|
---|
1565 | *
|
---|
1566 | * @todo remove after we switch to VirtualBoxBaseNEXT completely.
|
---|
1567 | */
|
---|
1568 | template <class C>
|
---|
1569 | class VirtualBoxBaseWithTypedChildrenNEXT : public VirtualBoxBaseWithTypedChildren <C>
|
---|
1570 | {
|
---|
1571 | public:
|
---|
1572 |
|
---|
1573 | typedef util::AutoLock AutoLock;
|
---|
1574 |
|
---|
1575 | private:
|
---|
1576 |
|
---|
1577 | void lock();
|
---|
1578 | void unlock();
|
---|
1579 | bool isLockedOnCurrentThread();
|
---|
1580 | void setReady (bool isReady);
|
---|
1581 | bool isReady();
|
---|
1582 | };
|
---|
1583 |
|
---|
1584 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1585 |
|
---|
1586 | /// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
|
---|
1587 | /**
|
---|
1588 | * Simple template that manages data structure allocation/deallocation
|
---|
1589 | * and supports data pointer sharing (the instance that shares the pointer is
|
---|
1590 | * not responsible for memory deallocation as opposed to the instance that
|
---|
1591 | * owns it).
|
---|
1592 | */
|
---|
1593 | template <class D>
|
---|
1594 | class Shareable
|
---|
1595 | {
|
---|
1596 | public:
|
---|
1597 |
|
---|
1598 | Shareable() : mData (NULL), mIsShared (FALSE) {}
|
---|
1599 | ~Shareable() { free(); }
|
---|
1600 |
|
---|
1601 | void allocate() { attach (new D); }
|
---|
1602 |
|
---|
1603 | virtual void free() {
|
---|
1604 | if (mData) {
|
---|
1605 | if (!mIsShared)
|
---|
1606 | delete mData;
|
---|
1607 | mData = NULL;
|
---|
1608 | mIsShared = false;
|
---|
1609 | }
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | void attach (D *data) {
|
---|
1613 | AssertMsg (data, ("new data must not be NULL"));
|
---|
1614 | if (data && mData != data) {
|
---|
1615 | if (mData && !mIsShared)
|
---|
1616 | delete mData;
|
---|
1617 | mData = data;
|
---|
1618 | mIsShared = false;
|
---|
1619 | }
|
---|
1620 | }
|
---|
1621 |
|
---|
1622 | void attach (Shareable &data) {
|
---|
1623 | AssertMsg (
|
---|
1624 | data.mData == mData || !data.mIsShared,
|
---|
1625 | ("new data must not be shared")
|
---|
1626 | );
|
---|
1627 | if (this != &data && !data.mIsShared) {
|
---|
1628 | attach (data.mData);
|
---|
1629 | data.mIsShared = true;
|
---|
1630 | }
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | void share (D *data) {
|
---|
1634 | AssertMsg (data, ("new data must not be NULL"));
|
---|
1635 | if (mData != data) {
|
---|
1636 | if (mData && !mIsShared)
|
---|
1637 | delete mData;
|
---|
1638 | mData = data;
|
---|
1639 | mIsShared = true;
|
---|
1640 | }
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | void share (const Shareable &data) { share (data.mData); }
|
---|
1644 |
|
---|
1645 | void attachCopy (const D *data) {
|
---|
1646 | AssertMsg (data, ("data to copy must not be NULL"));
|
---|
1647 | if (data)
|
---|
1648 | attach (new D (*data));
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | void attachCopy (const Shareable &data) {
|
---|
1652 | attachCopy (data.mData);
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | virtual D *detach() {
|
---|
1656 | D *d = mData;
|
---|
1657 | mData = NULL;
|
---|
1658 | mIsShared = false;
|
---|
1659 | return d;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | D *data() const {
|
---|
1663 | return mData;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | D *operator->() const {
|
---|
1667 | AssertMsg (mData, ("data must not be NULL"));
|
---|
1668 | return mData;
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | bool isNull() const { return mData == NULL; }
|
---|
1672 | bool operator!() const { return isNull(); }
|
---|
1673 |
|
---|
1674 | bool isShared() const { return mIsShared; }
|
---|
1675 |
|
---|
1676 | protected:
|
---|
1677 |
|
---|
1678 | D *mData;
|
---|
1679 | bool mIsShared;
|
---|
1680 | };
|
---|
1681 |
|
---|
1682 | /// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
|
---|
1683 | /**
|
---|
1684 | * Simple template that enhances Shareable<> and supports data
|
---|
1685 | * backup/rollback/commit (using the copy constructor of the managed data
|
---|
1686 | * structure).
|
---|
1687 | */
|
---|
1688 | template <class D>
|
---|
1689 | class Backupable : public Shareable <D>
|
---|
1690 | {
|
---|
1691 | public:
|
---|
1692 |
|
---|
1693 | Backupable() : Shareable <D> (), mBackupData (NULL) {}
|
---|
1694 |
|
---|
1695 | void free()
|
---|
1696 | {
|
---|
1697 | AssertMsg (this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
|
---|
1698 | rollback();
|
---|
1699 | Shareable <D>::free();
|
---|
1700 | }
|
---|
1701 |
|
---|
1702 | D *detach()
|
---|
1703 | {
|
---|
1704 | AssertMsg (this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
|
---|
1705 | rollback();
|
---|
1706 | return Shareable <D>::detach();
|
---|
1707 | }
|
---|
1708 |
|
---|
1709 | void share (const Backupable &data)
|
---|
1710 | {
|
---|
1711 | AssertMsg (!data.isBackedUp(), ("data to share must not be backed up"));
|
---|
1712 | if (!data.isBackedUp())
|
---|
1713 | Shareable <D>::share (data.mData);
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 | /**
|
---|
1717 | * Stores the current data pointer in the backup area, allocates new data
|
---|
1718 | * using the copy constructor on current data and makes new data active.
|
---|
1719 | */
|
---|
1720 | void backup()
|
---|
1721 | {
|
---|
1722 | AssertMsg (this->mData, ("data must not be NULL"));
|
---|
1723 | if (this->mData && !mBackupData)
|
---|
1724 | {
|
---|
1725 | mBackupData = this->mData;
|
---|
1726 | this->mData = new D (*mBackupData);
|
---|
1727 | }
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | /**
|
---|
1731 | * Deletes new data created by #backup() and restores previous data pointer
|
---|
1732 | * stored in the backup area, making it active again.
|
---|
1733 | */
|
---|
1734 | void rollback()
|
---|
1735 | {
|
---|
1736 | if (this->mData && mBackupData)
|
---|
1737 | {
|
---|
1738 | delete this->mData;
|
---|
1739 | this->mData = mBackupData;
|
---|
1740 | mBackupData = NULL;
|
---|
1741 | }
|
---|
1742 | }
|
---|
1743 |
|
---|
1744 | /**
|
---|
1745 | * Commits current changes by deleting backed up data and clearing up the
|
---|
1746 | * backup area. The new data pointer created by #backup() remains active
|
---|
1747 | * and becomes the only managed pointer.
|
---|
1748 | *
|
---|
1749 | * This method is much faster than #commitCopy() (just a single pointer
|
---|
1750 | * assignment operation), but makes the previous data pointer invalid
|
---|
1751 | * (because it is freed). For this reason, this method must not be
|
---|
1752 | * used if it's possible that data managed by this instance is shared with
|
---|
1753 | * some other Shareable instance. See #commitCopy().
|
---|
1754 | */
|
---|
1755 | void commit()
|
---|
1756 | {
|
---|
1757 | if (this->mData && mBackupData)
|
---|
1758 | {
|
---|
1759 | if (!this->mIsShared)
|
---|
1760 | delete mBackupData;
|
---|
1761 | mBackupData = NULL;
|
---|
1762 | this->mIsShared = false;
|
---|
1763 | }
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | /**
|
---|
1767 | * Commits current changes by assigning new data to the previous data
|
---|
1768 | * pointer stored in the backup area using the assignment operator.
|
---|
1769 | * New data is deleted, the backup area is cleared and the previous data
|
---|
1770 | * pointer becomes active and the only managed pointer.
|
---|
1771 | *
|
---|
1772 | * This method is slower than #commit(), but it keeps the previous data
|
---|
1773 | * pointer valid (i.e. new data is copied to the same memory location).
|
---|
1774 | * For that reason it's safe to use this method on instances that share
|
---|
1775 | * managed data with other Shareable instances.
|
---|
1776 | */
|
---|
1777 | void commitCopy()
|
---|
1778 | {
|
---|
1779 | if (this->mData && mBackupData)
|
---|
1780 | {
|
---|
1781 | *mBackupData = *(this->mData);
|
---|
1782 | delete this->mData;
|
---|
1783 | this->mData = mBackupData;
|
---|
1784 | mBackupData = NULL;
|
---|
1785 | }
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | void assignCopy (const D *data)
|
---|
1789 | {
|
---|
1790 | AssertMsg (this->mData, ("data must not be NULL"));
|
---|
1791 | AssertMsg (data, ("data to copy must not be NULL"));
|
---|
1792 | if (this->mData && data)
|
---|
1793 | {
|
---|
1794 | if (!mBackupData)
|
---|
1795 | {
|
---|
1796 | mBackupData = this->mData;
|
---|
1797 | this->mData = new D (*data);
|
---|
1798 | }
|
---|
1799 | else
|
---|
1800 | *this->mData = *data;
|
---|
1801 | }
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | void assignCopy (const Backupable &data)
|
---|
1805 | {
|
---|
1806 | assignCopy (data.mData);
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | bool isBackedUp() const
|
---|
1810 | {
|
---|
1811 | return mBackupData != NULL;
|
---|
1812 | }
|
---|
1813 |
|
---|
1814 | bool hasActualChanges() const
|
---|
1815 | {
|
---|
1816 | AssertMsg (this->mData, ("data must not be NULL"));
|
---|
1817 | return this->mData != NULL && mBackupData != NULL &&
|
---|
1818 | !(*this->mData == *mBackupData);
|
---|
1819 | }
|
---|
1820 |
|
---|
1821 | D *backedUpData() const
|
---|
1822 | {
|
---|
1823 | return mBackupData;
|
---|
1824 | }
|
---|
1825 |
|
---|
1826 | protected:
|
---|
1827 |
|
---|
1828 | D *mBackupData;
|
---|
1829 | };
|
---|
1830 |
|
---|
1831 | #endif // ____H_VIRTUALBOXBASEIMPL
|
---|