VirtualBox

source: vbox/trunk/src/VBox/Main/include/VirtualBoxBase.h@ 3668

Last change on this file since 3668 was 3668, checked in by vboxsync, 17 years ago

replace underscore symbols in Main/

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.7 KB
Line 
1/** @file
2 *
3 * VirtualBox COM base classes definition
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek 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
34using namespace com;
35using util::AutoLock;
36using util::AutoReaderLock;
37using util::AutoMultiLock;
38
39#include <iprt/cdefs.h>
40#include <iprt/critsect.h>
41
42#include <list>
43#include <map>
44
45#if defined (RT_OS_WINDOWS)
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
55template <class T>
56class CMyComClassFactorySingleton : public CComClassFactory
57{
58public:
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 (RT_OS_WINDOWS)
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
352namespace 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
382class ATL_NO_VTABLE VirtualBoxBaseNEXT_base
383#ifdef RT_OS_WINDOWS
384 : public CComObjectRootEx <CComMultiThreadModel>
385#else
386 : public CComObjectRootEx
387#endif
388 , public AutoLock::Lockable
389{
390public:
391
392 enum State { NotReady, Ready, InInit, InUninit, InitFailed, Limited };
393
394protected:
395
396 VirtualBoxBaseNEXT_base();
397 virtual ~VirtualBoxBaseNEXT_base();
398
399public:
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 * @param aObj Object to add a caller to. If NULL, this
460 * instance is effectively turned to no-op (where
461 * rc() will return S_OK and state() will be
462 * NotReady).
463 */
464 AutoCallerBase (VirtualBoxBaseNEXT_base *aObj)
465 : mObj (aObj)
466 , mRC (S_OK)
467 , mState (NotReady)
468 {
469 if (mObj)
470 mRC = mObj->addCaller (&mState, aLimited);
471 }
472
473 /**
474 * If the number of callers was successfully increased,
475 * decreases it using VirtualBoxBase::releaseCaller(), otherwise
476 * does nothing.
477 */
478 ~AutoCallerBase()
479 {
480 if (mObj && SUCCEEDED (mRC))
481 mObj->releaseCaller();
482 }
483
484 /**
485 * Stores the result code returned by VirtualBoxBase::addCaller()
486 * after instance creation or after the last #add() call. A successful
487 * result code means the number of callers was successfully increased.
488 */
489 HRESULT rc() const { return mRC; }
490
491 /**
492 * Returns |true| if |SUCCEEDED (rc())| is |true|, for convenience.
493 * |true| means the number of callers was successfully increased.
494 */
495 bool isOk() const { return SUCCEEDED (mRC); }
496
497 /**
498 * Stores the object state returned by VirtualBoxBase::addCaller()
499 * after instance creation or after the last #add() call.
500 */
501 State state() const { return mState; }
502
503 /**
504 * Temporarily decreases the number of callers of the managed object.
505 * May only be called if #isOk() returns |true|. Note that #rc() will
506 * return E_FAIL after this method succeeds.
507 */
508 void release()
509 {
510 Assert (SUCCEEDED (mRC));
511 if (SUCCEEDED (mRC))
512 {
513 if (mObj)
514 mObj->releaseCaller();
515 mRC = E_FAIL;
516 }
517 }
518
519 /**
520 * Restores the number of callers decreased by #release(). May only
521 * be called after #release().
522 */
523 void add()
524 {
525 Assert (!SUCCEEDED (mRC));
526 if (mObj && !SUCCEEDED (mRC))
527 mRC = mObj->addCaller (&mState, aLimited);
528 }
529
530 private:
531
532 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoCallerBase)
533 DECLARE_CLS_NEW_DELETE_NOOP (AutoCallerBase)
534
535 VirtualBoxBaseNEXT_base *mObj;
536 HRESULT mRC;
537 State mState;
538 };
539
540 /**
541 * Smart class that automatically increases the number of normal
542 * (non-limited) callers of the given VirtualBoxBase object when an
543 * instance is constructed and decreases it back when the created instance
544 * goes out of scope (i.e. gets destroyed).
545 *
546 * A typical usage pattern to declare a normal method of some object
547 * (i.e. a method that is valid only when the object provides its
548 * full functionality) is:
549 * <code>
550 * STDMETHODIMP Component::Foo()
551 * {
552 * AutoCaller autoCaller (this);
553 * CheckComRCReturnRC (autoCaller.rc());
554 * ...
555 * </code>
556 *
557 * Using this class is equivalent to using the AutoCallerBase template
558 * with the @a aLimited argument set to |false|, but this class is
559 * preferred because provides better self-descriptiveness.
560 *
561 * See AutoCallerBase for more information about auto caller functionality.
562 */
563 typedef AutoCallerBase <false> AutoCaller;
564
565 /**
566 * Smart class that automatically increases the number of limited callers
567 * of the given VirtualBoxBase object when an instance is constructed and
568 * decreases it back when the created instance goes out of scope (i.e.
569 * gets destroyed).
570 *
571 * A typical usage pattern to declare a limited method of some object
572 * (i.e. a method that is valid even if the object doesn't provide its
573 * full functionality) is:
574 * <code>
575 * STDMETHODIMP Component::Bar()
576 * {
577 * AutoLimitedCaller autoCaller (this);
578 * CheckComRCReturnRC (autoCaller.rc());
579 * ...
580 * </code>
581 *
582 * Using this class is equivalent to using the AutoCallerBase template
583 * with the @a aLimited argument set to |true|, but this class is
584 * preferred because provides better self-descriptiveness.
585 *
586 * See AutoCallerBase for more information about auto caller functionality.
587 */
588 typedef AutoCallerBase <true> AutoLimitedCaller;
589
590protected:
591
592 /**
593 * Smart class to enclose the state transition NotReady->InInit->Ready.
594 *
595 * Instances must be created at the beginning of init() methods of
596 * VirtualBoxBase subclasses as a stack-based variable using |this| pointer
597 * as the argument. When this variable is created it automatically places
598 * the object to the InInit state.
599 *
600 * When the created variable goes out of scope (i.e. gets destroyed),
601 * depending on the success status of this initialization span, it either
602 * places the object to the Ready state or calls the object's
603 * VirtualBoxBase::uninit() method which is supposed to place the object
604 * back to the NotReady state using the AutoUninitSpan class.
605 *
606 * The initial success status of the initialization span is determined by
607 * the @a aSuccess argument of the AutoInitSpan constructor (|false| by
608 * default). Inside the initialization span, the success status can be set
609 * to |true| using #setSucceeded() or to |false| using #setFailed(). Please
610 * don't forget to set the correct success status before letting the
611 * AutoInitSpan variable go out of scope (for example, by performing an
612 * early return from the init() method)!
613 *
614 * Note that if an instance of this class gets constructed when the
615 * object is in the state other than NotReady, #isOk() returns |false| and
616 * methods of this class do nothing: the state transition is not performed.
617 *
618 * A typical usage pattern is:
619 * <code>
620 * HRESULT Component::init()
621 * {
622 * AutoInitSpan autoInitSpan (this);
623 * AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
624 * ...
625 * if (FAILED (rc))
626 * return rc;
627 * ...
628 * if (SUCCEEDED (rc))
629 * autoInitSpan.setSucceeded();
630 * return rc;
631 * }
632 * </code>
633 *
634 * @note Never create instances of this class outside init() methods of
635 * VirtualBoxBase subclasses and never pass anything other than |this| as
636 * the argument to the constructor!
637 */
638 class AutoInitSpan
639 {
640 public:
641
642 enum Status { Failed = 0x0, Succeeded = 0x1, Limited = 0x2 };
643
644 AutoInitSpan (VirtualBoxBaseNEXT_base *aObj, Status aStatus = Failed);
645 ~AutoInitSpan();
646
647 /**
648 * Returns |true| if this instance has been created at the right moment
649 * (when the object was in the NotReady state) and |false| otherwise.
650 */
651 bool isOk() const { return mOk; }
652
653 /**
654 * Sets the initialization status to Succeeded to indicates successful
655 * initialization. The AutoInitSpan destructor will place the managed
656 * VirtualBoxBase object to the Ready state.
657 */
658 void setSucceeded() { mStatus = Succeeded; }
659
660 /**
661 * Sets the initialization status to Succeeded to indicate limited
662 * (partly successful) initialization. The AutoInitSpan destructor will
663 * place the managed VirtualBoxBase object to the Limited state.
664 */
665 void setLimited() { mStatus = Limited; }
666
667 /**
668 * Sets the initialization status to Failure to indicates failed
669 * initialization. The AutoInitSpan destructor will place the managed
670 * VirtualBoxBase object to the InitFailed state and will automatically
671 * call its uninit() method which is supposed to place the object back
672 * to the NotReady state using AutoUninitSpan.
673 */
674 void setFailed() { mStatus = Failed; }
675
676 /** Returns the current initialization status. */
677 Status status() { return mStatus; }
678
679 private:
680
681 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoInitSpan)
682 DECLARE_CLS_NEW_DELETE_NOOP (AutoInitSpan)
683
684 VirtualBoxBaseNEXT_base *mObj;
685 Status mStatus : 3; // must be at least total number of bits + 1 (sign)
686 bool mOk : 1;
687 };
688
689 /**
690 * Smart class to enclose the state transition Limited->InInit->Ready.
691 *
692 * Instances must be created at the beginning of methods of VirtualBoxBase
693 * subclasses that try to re-initialize the object to bring it to the
694 * Ready state (full functionality) after partial initialization
695 * (limited functionality)>, as a stack-based variable using |this| pointer
696 * as the argument. When this variable is created it automatically places
697 * the object to the InInit state.
698 *
699 * When the created variable goes out of scope (i.e. gets destroyed),
700 * depending on the success status of this initialization span, it either
701 * places the object to the Ready state or brings it back to the Limited
702 * state.
703 *
704 * The initial success status of the re-initialization span is |false|.
705 * In order to make it successful, #setSucceeded() must be called before
706 * the instance is destroyed.
707 *
708 * Note that if an instance of this class gets constructed when the
709 * object is in the state other than Limited, #isOk() returns |false| and
710 * methods of this class do nothing: the state transition is not performed.
711 *
712 * A typical usage pattern is:
713 * <code>
714 * HRESULT Component::reinit()
715 * {
716 * AutoReadySpan autoReadySpan (this);
717 * AssertReturn (autoReadySpan.isOk(), E_UNEXPECTED);
718 * ...
719 * if (FAILED (rc))
720 * return rc;
721 * ...
722 * if (SUCCEEDED (rc))
723 * autoReadySpan.setSucceeded();
724 * return rc;
725 * }
726 * </code>
727 *
728 * @note Never create instances of this class outside re-initialization
729 * methods of VirtualBoxBase subclasses and never pass anything other than
730 * |this| as the argument to the constructor!
731 */
732 class AutoReadySpan
733 {
734 public:
735
736 AutoReadySpan (VirtualBoxBaseNEXT_base *aObj);
737 ~AutoReadySpan();
738
739 /**
740 * Returns |true| if this instance has been created at the right moment
741 * (when the object was in the Limited state) and |false| otherwise.
742 */
743 bool isOk() const { return mOk; }
744
745 /**
746 * Sets the re-initialization status to Succeeded to indicates
747 * successful re-initialization. The AutoReadySpan destructor will
748 * place the managed VirtualBoxBase object to the Ready state.
749 */
750 void setSucceeded() { mSucceeded = true; }
751
752 private:
753
754 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoReadySpan)
755 DECLARE_CLS_NEW_DELETE_NOOP (AutoReadySpan)
756
757 VirtualBoxBaseNEXT_base *mObj;
758 bool mSucceeded : 1;
759 bool mOk : 1;
760 };
761
762 /**
763 * Smart class to enclose the state transition Ready->InUnnit->NotReady or
764 * InitFailed->InUnnit->NotReady.
765 *
766 * Must be created at the beginning of uninit() methods of VirtualBoxBase
767 * subclasses as a stack-based variable using |this| pointer as the argument.
768 * When this variable is created it automatically places the object to the
769 * InUninit state, unless it is already in the NotReady state as indicated
770 * by #uninitDone() returning |true|. In the latter case, the uninit()
771 * method must immediately return because there should be nothing to
772 * uninitialize.
773 *
774 * When this variable goes out of scope (i.e. gets destroyed), it places
775 * the object to the NotReady state.
776 *
777 * A typical usage pattern is:
778 * <code>
779 * void Component::uninit()
780 * {
781 * AutoUninitSpan autoUninitSpan (this);
782 * if (autoUninitSpan.uninitDone())
783 * retrun;
784 * ...
785 * </code>
786 *
787 * @note Never create instances of this class outside uninit() methods and
788 * never pass anything other than |this| as the argument to the constructor!
789 */
790 class AutoUninitSpan
791 {
792 public:
793
794 AutoUninitSpan (VirtualBoxBaseNEXT_base *aObj);
795 ~AutoUninitSpan();
796
797 /** |true| when uninit() is called as a result of init() failure */
798 bool initFailed() { return mInitFailed; }
799
800 /** |true| when uninit() has already been called (so the object is NotReady) */
801 bool uninitDone() { return mUninitDone; }
802
803 private:
804
805 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoUninitSpan)
806 DECLARE_CLS_NEW_DELETE_NOOP (AutoUninitSpan)
807
808 VirtualBoxBaseNEXT_base *mObj;
809 bool mInitFailed : 1;
810 bool mUninitDone : 1;
811 };
812
813private:
814
815 void setState (State aState)
816 {
817 Assert (mState != aState);
818 mState = aState;
819 mStateChangeThread = RTThreadSelf();
820 }
821
822 /** Primary state of this object */
823 State mState;
824 /** Thread that caused the last state change */
825 RTTHREAD mStateChangeThread;
826 /** Total number of active calls to this object */
827 unsigned mCallers;
828 /** Semaphore posted when the number of callers drops to zero */
829 RTSEMEVENT mZeroCallersSem;
830 /** Semaphore posted when the object goes from InInit some other state */
831 RTSEMEVENTMULTI mInitDoneSem;
832 /** Number of threads waiting for mInitDoneSem */
833 unsigned mInitDoneSemUsers;
834
835 /** Protects access to state related data members */
836 RTCRITSECT mStateLock;
837
838 /** User-level object lock for subclasses */
839 mutable AutoLock::Handle *mObjectLock;
840};
841
842/**
843 * This macro adds the error info support to methods of the VirtualBoxBase
844 * class (by overriding them). Place it to the public section of the
845 * VirtualBoxBase subclass and the following methods will set the extended
846 * error info in case of failure instead of just returning the result code:
847 *
848 * <ul>
849 * <li>VirtualBoxBase::addCaller()
850 * </ul>
851 *
852 * @note The given VirtualBoxBase subclass must also inherit from both
853 * VirtualBoxSupportErrorInfoImpl and VirtualBoxSupportTranslation templates!
854 *
855 * @param C VirtualBoxBase subclass to add the error info support to
856 */
857#define VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(C) \
858 virtual HRESULT addCaller (VirtualBoxBaseNEXT_base::State *aState = NULL, \
859 bool aLimited = false) \
860 { \
861 VirtualBoxBaseNEXT_base::State state; \
862 HRESULT rc = VirtualBoxBaseNEXT_base::addCaller (&state, aLimited); \
863 if (FAILED (rc)) \
864 { \
865 if (state == VirtualBoxBaseNEXT_base::Limited) \
866 rc = setError (rc, tr ("The object functonality is limited")); \
867 else \
868 rc = setError (rc, tr ("The object is not ready")); \
869 } \
870 if (aState) \
871 *aState = state; \
872 return rc; \
873 } \
874
875////////////////////////////////////////////////////////////////////////////////
876
877/// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
878class ATL_NO_VTABLE VirtualBoxBase : public VirtualBoxBaseNEXT_base
879//#ifdef RT_OS_WINDOWS
880// : public CComObjectRootEx<CComMultiThreadModel>
881//#else
882// : public CComObjectRootEx
883//#endif
884{
885
886public:
887 VirtualBoxBase()
888 {
889 mReady = false;
890 }
891 virtual ~VirtualBoxBase()
892 {
893 }
894
895 /**
896 * Virtual unintialization method. Called during parent object's
897 * uninitialization, if the given subclass instance is a dependent child of
898 * a class dervived from VirtualBoxBaseWithChildren (@sa
899 * VirtualBoxBaseWithChildren::addDependentChild). In this case, this
900 * method's impelemtation must call setReady (false),
901 */
902 virtual void uninit() {}
903
904
905 // sets the ready state of the object
906 void setReady(bool isReady)
907 {
908 mReady = isReady;
909 }
910 // get the ready state of the object
911 bool isReady()
912 {
913 return mReady;
914 }
915
916 static const char *translate (const char *context, const char *sourceText,
917 const char *comment = 0);
918
919private:
920
921 // flag determining whether an object is ready
922 // for usage, i.e. methods may be called
923 bool mReady;
924 // mutex semaphore to lock the object
925};
926
927/**
928 * Temporary class to disable deprecated methods of VirtualBoxBase.
929 * Can be used as a base for components that are completely switched to
930 * the new locking scheme (VirtualBoxBaseNEXT_base).
931 *
932 * @todo remove after we switch to VirtualBoxBaseNEXT completely.
933 */
934class VirtualBoxBaseNEXT : public VirtualBoxBase
935{
936private:
937
938 void lock();
939 void unlock();
940 void setReady (bool isReady);
941 bool isReady();
942};
943
944////////////////////////////////////////////////////////////////////////////////
945
946/** Helper for VirtualBoxSupportTranslation */
947class VirtualBoxSupportTranslationBase
948{
949protected:
950 static bool cutClassNameFrom__PRETTY_FUNCTION__ (char *prettyFunctionName);
951};
952
953/**
954 * This template implements the NLS string translation support for the
955 * given class by providing a #tr() function.
956 *
957 * @param C class that needs to support the string translation
958 *
959 * @note
960 * Every class that wants to use the #tr() function in its own methods must
961 * inherit from this template, regardless of whether its base class (if any)
962 * inherits from it or not! Otherwise, the translation service will not
963 * work correctly. However, the declaration of the resulting class must
964 * contain the VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(<ClassName>) macro
965 * if one of its base classes also inherits from this template (to resolve
966 * the ambiguity of the #tr() function).
967 */
968template <class C>
969class VirtualBoxSupportTranslation : virtual protected VirtualBoxSupportTranslationBase
970{
971public:
972
973 /**
974 * Translates the given text string according to the currently installed
975 * translation table and current context, which is determined by the
976 * class name. See VirtualBoxBase::translate() for more info.
977 *
978 * @param sourceText the string to translate
979 * @param comment the comment to the string (NULL means no comment)
980 *
981 * @return
982 * the translated version of the source string in UTF-8 encoding,
983 * or the source string itself if the translation is not found in
984 * the current context.
985 */
986 inline static const char *tr (const char *sourceText, const char *comment = 0)
987 {
988 return VirtualBoxBase::translate (getClassName(), sourceText, comment);
989 }
990
991protected:
992
993 static const char *getClassName()
994 {
995 static char fn [sizeof (__PRETTY_FUNCTION__) + 1];
996 if (!className)
997 {
998 strcpy (fn, __PRETTY_FUNCTION__);
999 cutClassNameFrom__PRETTY_FUNCTION__ (fn);
1000 className = fn;
1001 }
1002 return className;
1003 }
1004
1005private:
1006
1007 static const char *className;
1008};
1009
1010template <class C>
1011const char *VirtualBoxSupportTranslation <C>::className = NULL;
1012
1013/**
1014 * This macro must be invoked inside the public section of the declaration of
1015 * the class inherited from the VirtualBoxSupportTranslation template, in case
1016 * when one of its other base classes also inherits from that template. This is
1017 * necessary to resolve the ambiguity of the #tr() function.
1018 *
1019 * @param C class that inherits from the VirtualBoxSupportTranslation template
1020 * more than once (through its other base clases)
1021 */
1022#define VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(C) \
1023 inline static const char *tr (const char *sourceText, const char *comment = 0) \
1024 { \
1025 return VirtualBoxSupportTranslation <C>::tr (sourceText, comment); \
1026 }
1027
1028/**
1029 * A dummy macro that is used to shut down Qt's lupdate tool warnings
1030 * in some situations. This macro needs to be present inside (better at the
1031 * very beginning) of the declaration of the class that inherits from
1032 * VirtualBoxSupportTranslation template, to make lupdate happy.
1033 */
1034#define Q_OBJECT
1035
1036////////////////////////////////////////////////////////////////////////////////
1037
1038/**
1039 * Helper for the VirtualBoxSupportErrorInfoImpl template.
1040 */
1041class VirtualBoxSupportErrorInfoImplBase
1042{
1043 static HRESULT setErrorInternal (HRESULT aResultCode, const GUID &aIID,
1044 const Bstr &aComponent, const Bstr &aText,
1045 bool aPreserve);
1046
1047protected:
1048
1049 inline static HRESULT setError (HRESULT aResultCode, const GUID &aIID,
1050 const Bstr &aComponent,
1051 const Bstr &aText)
1052 {
1053 return setErrorInternal (aResultCode, aIID, aComponent, aText,
1054 false /* aPreserve */);
1055 }
1056
1057 inline static HRESULT addError (HRESULT aResultCode, const GUID &aIID,
1058 const Bstr &aComponent,
1059 const Bstr &aText)
1060 {
1061 return setErrorInternal (aResultCode, aIID, aComponent, aText,
1062 true /* aPreserve */);
1063 }
1064
1065 static HRESULT setError (HRESULT aResultCode, const GUID &aIID,
1066 const Bstr &aComponent,
1067 const char *aText, va_list aArgs)
1068 {
1069 return setErrorInternal (aResultCode, aIID, aComponent,
1070 Utf8StrFmt (aText, aArgs),
1071 false /* aPreserve */);
1072 }
1073
1074 static HRESULT addError (HRESULT aResultCode, const GUID &aIID,
1075 const Bstr &aComponent,
1076 const char *aText, va_list aArgs)
1077 {
1078 return setErrorInternal (aResultCode, aIID, aComponent,
1079 Utf8StrFmt (aText, aArgs),
1080 true /* aPreserve */);
1081 }
1082};
1083
1084/**
1085 * This template implements ISupportErrorInfo for the given component class
1086 * and provides the #setError() method to conveniently set the error information
1087 * from within interface methods' implementations.
1088 *
1089 * On Windows, the template argument must define a COM interface map using
1090 * BEGIN_COM_MAP / END_COM_MAP macros and this map must contain a
1091 * COM_INTERFACE_ENTRY(ISupportErrorInfo) definition. All interface entries
1092 * that follow it will be considered to support IErrorInfo, i.e. the
1093 * InterfaceSupportsErrorInfo() implementation will return S_OK for the
1094 * corresponding IID.
1095 *
1096 * On all platforms, the template argument must also define the following
1097 * method: |public static const wchar_t *C::getComponentName()|. See
1098 * #setError (HRESULT, const char *, ...) for a description on how it is
1099 * used.
1100 *
1101 * @param C
1102 * component class that implements one or more COM interfaces
1103 * @param I
1104 * default interface for the component. This interface's IID is used
1105 * by the shortest form of #setError, for convenience.
1106 */
1107template <class C, class I>
1108class ATL_NO_VTABLE VirtualBoxSupportErrorInfoImpl
1109 : protected VirtualBoxSupportErrorInfoImplBase
1110#if defined (RT_OS_WINDOWS)
1111 , public ISupportErrorInfo
1112#else
1113#endif
1114{
1115public:
1116
1117#if defined (RT_OS_WINDOWS)
1118 STDMETHOD(InterfaceSupportsErrorInfo) (REFIID riid)
1119 {
1120 const _ATL_INTMAP_ENTRY* pEntries = C::_GetEntries();
1121 Assert (pEntries);
1122 if (!pEntries)
1123 return S_FALSE;
1124
1125 BOOL bSupports = FALSE;
1126 BOOL bISupportErrorInfoFound = FALSE;
1127
1128 while (pEntries->pFunc != NULL && !bSupports)
1129 {
1130 if (!bISupportErrorInfoFound)
1131 {
1132 // skip the com map entries until ISupportErrorInfo is found
1133 bISupportErrorInfoFound =
1134 InlineIsEqualGUID (*(pEntries->piid), IID_ISupportErrorInfo);
1135 }
1136 else
1137 {
1138 // look for the requested interface in the rest of the com map
1139 bSupports = InlineIsEqualGUID (*(pEntries->piid), riid);
1140 }
1141 pEntries++;
1142 }
1143
1144 Assert (bISupportErrorInfoFound);
1145
1146 return bSupports ? S_OK : S_FALSE;
1147 }
1148#endif // defined (RT_OS_WINDOWS)
1149
1150protected:
1151
1152 /**
1153 * Sets the error information for the current thread.
1154 * This information can be retrieved by a caller of an interface method
1155 * using IErrorInfo on Windows or nsIException on Linux, or the cross-platform
1156 * IVirtualBoxErrorInfo interface that provides extended error info (only
1157 * for components from the VirtualBox COM library). Alternatively, the
1158 * platform-independent class com::ErrorInfo (defined in VBox[XP]COM.lib)
1159 * can be used to retrieve error info in a convenient way.
1160 *
1161 * It is assumed that the interface method that uses this function returns
1162 * an unsuccessful result code to the caller (otherwise, there is no reason
1163 * for the caller to try to retrieve error info after method invocation).
1164 *
1165 * Here is a table of correspondence between this method's arguments
1166 * and IErrorInfo/nsIException/IVirtualBoxErrorInfo attributes/methods:
1167 *
1168 * argument IErrorInfo nsIException IVirtualBoxErrorInfo
1169 * ----------------------------------------------------------------
1170 * resultCode -- result resultCode
1171 * iid GetGUID -- interfaceID
1172 * component GetSource -- component
1173 * text GetDescription message text
1174 *
1175 * This method is rarely needs to be used though. There are more convenient
1176 * overloaded versions, that automatically substitute some arguments
1177 * taking their values from the template parameters. See
1178 * #setError (HRESULT, const char *, ...) for an example.
1179 *
1180 * @param aResultCode result (error) code, must not be S_OK
1181 * @param aIID IID of the intrface that defines the error
1182 * @param aComponent name of the component that generates the error
1183 * @param aText error message (must not be null), an RTStrPrintf-like
1184 * format string in UTF-8 encoding
1185 * @param ... list of arguments for the format string
1186 *
1187 * @return
1188 * the error argument, for convenience, If an error occures while
1189 * creating error info itself, that error is returned instead of the
1190 * error argument.
1191 */
1192 inline static HRESULT setError (HRESULT aResultCode, const GUID &aIID,
1193 const wchar_t *aComponent,
1194 const char *aText, ...)
1195 {
1196 va_list args;
1197 va_start (args, aText);
1198 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
1199 (aResultCode, aIID, aComponent, aText, args);
1200 va_end (args);
1201 return rc;
1202 }
1203
1204 /**
1205 * This method is the same as #setError() except that it preserves the
1206 * error info object (if any) set for the current thread before this
1207 * method is called by storing it in the IVirtualBoxErrorInfo::next
1208 * attribute of the new error info object.
1209 */
1210 inline static HRESULT addError (HRESULT aResultCode, const GUID &aIID,
1211 const wchar_t *aComponent,
1212 const char *aText, ...)
1213 {
1214 va_list args;
1215 va_start (args, aText);
1216 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::addError
1217 (aResultCode, aIID, aComponent, aText, args);
1218 va_end (args);
1219 return rc;
1220 }
1221
1222 /**
1223 * Sets the error information for the current thread.
1224 * A convenience method that automatically sets the default interface
1225 * ID (taken from the I template argument) and the component name
1226 * (a value of C::getComponentName()).
1227 *
1228 * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
1229 * for details.
1230 *
1231 * This method is the most common (and convenient) way to set error
1232 * information from within interface methods. A typical pattern of usage
1233 * is looks like this:
1234 *
1235 * <code>
1236 * return setError (E_FAIL, "Terrible Error");
1237 * </code>
1238 * or
1239 * <code>
1240 * HRESULT rc = setError (E_FAIL, "Terrible Error");
1241 * ...
1242 * return rc;
1243 * </code>
1244 */
1245 inline static HRESULT setError (HRESULT aResultCode, const char *aText, ...)
1246 {
1247 va_list args;
1248 va_start (args, aText);
1249 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
1250 (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, args);
1251 va_end (args);
1252 return rc;
1253 }
1254
1255 /**
1256 * This method is the same as #setError() except that it preserves the
1257 * error info object (if any) set for the current thread before this
1258 * method is called by storing it in the IVirtualBoxErrorInfo::next
1259 * attribute of the new error info object.
1260 */
1261 inline static HRESULT addError (HRESULT aResultCode, const char *aText, ...)
1262 {
1263 va_list args;
1264 va_start (args, aText);
1265 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::addError
1266 (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, args);
1267 va_end (args);
1268 return rc;
1269 }
1270
1271 /**
1272 * Sets the error information for the current thread, va_list variant.
1273 * A convenience method that automatically sets the default interface
1274 * ID (taken from the I template argument) and the component name
1275 * (a value of C::getComponentName()).
1276 *
1277 * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
1278 * and #setError (HRESULT, const char *, ...) for details.
1279 */
1280 inline static HRESULT setErrorV (HRESULT aResultCode, const char *aText,
1281 va_list aArgs)
1282 {
1283 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
1284 (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, aArgs);
1285 return rc;
1286 }
1287
1288 /**
1289 * This method is the same as #setErrorV() except that it preserves the
1290 * error info object (if any) set for the current thread before this
1291 * method is called by storing it in the IVirtualBoxErrorInfo::next
1292 * attribute of the new error info object.
1293 */
1294 inline static HRESULT addErrorV (HRESULT aResultCode, const char *aText,
1295 va_list aArgs)
1296 {
1297 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::addError
1298 (aResultCode, COM_IIDOF(I), C::getComponentName(), aText, aArgs);
1299 return rc;
1300 }
1301
1302 /**
1303 * Sets the error information for the current thread, BStr variant.
1304 * A convenience method that automatically sets the default interface
1305 * ID (taken from the I template argument) and the component name
1306 * (a value of C::getComponentName()).
1307 *
1308 * This method is preferred iy you have a ready (translated and formatted)
1309 * Bstr string, because it omits an extra conversion Utf8Str -> Bstr.
1310 *
1311 * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
1312 * and #setError (HRESULT, const char *, ...) for details.
1313 */
1314 inline static HRESULT setErrorBstr (HRESULT aResultCode, const Bstr &aText)
1315 {
1316 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
1317 (aResultCode, COM_IIDOF(I), C::getComponentName(), aText);
1318 return rc;
1319 }
1320
1321 /**
1322 * This method is the same as #setErrorBstr() except that it preserves the
1323 * error info object (if any) set for the current thread before this
1324 * method is called by storing it in the IVirtualBoxErrorInfo::next
1325 * attribute of the new error info object.
1326 */
1327 inline static HRESULT addErrorBstr (HRESULT aResultCode, const Bstr &aText)
1328 {
1329 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::addError
1330 (aResultCode, COM_IIDOF(I), C::getComponentName(), aText);
1331 return rc;
1332 }
1333
1334 /**
1335 * Sets the error information for the current thread.
1336 * A convenience method that automatically sets the component name
1337 * (a value of C::getComponentName()), but allows to specify the interface
1338 * id manually.
1339 *
1340 * See #setError (HRESULT, const GUID &, const wchar_t *, const char *text, ...)
1341 * for details.
1342 */
1343 inline static HRESULT setError (HRESULT aResultCode, const GUID &aIID,
1344 const char *aText, ...)
1345 {
1346 va_list args;
1347 va_start (args, aText);
1348 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::setError
1349 (aResultCode, aIID, C::getComponentName(), aText, args);
1350 va_end (args);
1351 return rc;
1352 }
1353
1354 /**
1355 * This method is the same as #setError() except that it preserves the
1356 * error info object (if any) set for the current thread before this
1357 * method is called by storing it in the IVirtualBoxErrorInfo::next
1358 * attribute of the new error info object.
1359 */
1360 inline static HRESULT addError (HRESULT aResultCode, const GUID &aIID,
1361 const char *aText, ...)
1362 {
1363 va_list args;
1364 va_start (args, aText);
1365 HRESULT rc = VirtualBoxSupportErrorInfoImplBase::addError
1366 (aResultCode, aIID, C::getComponentName(), aText, args);
1367 va_end (args);
1368 return rc;
1369 }
1370
1371private:
1372
1373};
1374
1375////////////////////////////////////////////////////////////////////////////////
1376
1377/**
1378 * Base class to track VirtualBoxBase chlidren of the component.
1379 *
1380 * This class is a preferrable VirtualBoxBase replacement for components
1381 * that operate with collections of child components. It gives two useful
1382 * possibilities:
1383 *
1384 * <ol><li>
1385 * Given an IUnknown instance, it's possible to quickly determine
1386 * whether this instance represents a child object created by the given
1387 * component, and if so, get a valid VirtualBoxBase pointer to the child
1388 * object. The returned pointer can be then safely casted to the
1389 * actual class of the child object (to get access to its "internal"
1390 * non-interface methods) provided that no other child components implement
1391 * the same initial interface IUnknown is queried from.
1392 * </li><li>
1393 * When the parent object uninitializes itself, it can easily unintialize
1394 * all its VirtualBoxBase derived children (using their
1395 * VirtualBoxBase::uninit() implementations). This is done simply by
1396 * calling the #uninitDependentChildren() method.
1397 * </li><ol>
1398 *
1399 * In order to let the above work, the following must be done:
1400 * <ol><li>
1401 * When a child object is initialized, it calls #addDependentChild() of
1402 * its parent to register itself within the list of dependent children.
1403 * </li><li>
1404 * When a child object it is uninitialized, it calls #removeDependentChild()
1405 * to unregister itself. This must be done <b>after</b> the child has called
1406 * setReady(false) to indicate it is no more valid, and <b>not</b> from under
1407 * the child object's lock. Note also, that the first action the child's
1408 * uninit() implementation must do is to check for readiness after acquiring
1409 * the object's lock and return immediately if not ready.
1410 * </li><ol>
1411 *
1412 * Children added by #addDependentChild() are <b>weakly</b> referenced
1413 * (i.e. AddRef() is not called), so when a child is externally destructed
1414 * (i.e. its reference count goes to zero), it will automatically remove
1415 * itself from a map of dependent children, provided that it follows the
1416 * rules described here.
1417 *
1418 * @note
1419 * Because of weak referencing, deadlocks and assertions are very likely
1420 * if #addDependentChild() or #removeDependentChild() are used incorrectly
1421 * (called at inappropriate times). Check the above rules once more.
1422 */
1423class VirtualBoxBaseWithChildren : public VirtualBoxBase
1424{
1425public:
1426
1427 VirtualBoxBaseWithChildren()
1428 : mUninitDoneSem (NIL_RTSEMEVENT), mChildrenLeft (0)
1429 {
1430 RTCritSectInit (&mMapLock);
1431 }
1432
1433 virtual ~VirtualBoxBaseWithChildren()
1434 {
1435 RTCritSectDelete (&mMapLock);
1436 }
1437
1438 /**
1439 * Adds the given child to the map of dependent children.
1440 * Intended to be called from the child's init() method,
1441 * from under the child's lock.
1442 *
1443 * @param C the child object to add (must inherit VirtualBoxBase AND
1444 * implement some interface)
1445 */
1446 template <class C>
1447 void addDependentChild (C *child)
1448 {
1449 AssertReturn (child, (void) 0);
1450 addDependentChild (child, child);
1451 }
1452
1453 /**
1454 * Removes the given child from the map of dependent children.
1455 * Must be called <b>after<b> the child has called setReady(false), and
1456 * <b>not</b> from under the child object's lock.
1457 *
1458 * @param C the child object to remove (must inherit VirtualBoxBase AND
1459 * implement some interface)
1460 */
1461 template <class C>
1462 void removeDependentChild (C *child)
1463 {
1464 AssertReturn (child, (void) 0);
1465 /// @todo (r=dmik) the below check (and the relevant comment above)
1466 // seems to be not necessary any more once we completely switch to
1467 // the NEXT locking scheme. This requires altering removeDependentChild()
1468 // and uninitDependentChildren() as well (due to the new state scheme,
1469 // there is a separate mutex for state transition, so calling the
1470 // child's uninit() from under the children map lock should not produce
1471 // dead-locks any more).
1472 Assert (!child->isLockedOnCurrentThread());
1473 removeDependentChild (ComPtr <IUnknown> (child));
1474 }
1475
1476protected:
1477
1478 void uninitDependentChildren();
1479
1480 VirtualBoxBase *getDependentChild (const ComPtr <IUnknown> &unk);
1481
1482private:
1483
1484 void addDependentChild (const ComPtr <IUnknown> &unk, VirtualBoxBase *child);
1485 void removeDependentChild (const ComPtr <IUnknown> &unk);
1486
1487 typedef std::map <IUnknown *, VirtualBoxBase *> DependentChildren;
1488 DependentChildren mDependentChildren;
1489
1490 RTCRITSECT mMapLock;
1491 RTSEMEVENT mUninitDoneSem;
1492 unsigned mChildrenLeft;
1493};
1494
1495/**
1496 * Temporary class to disable deprecated methods of VirtualBoxBase.
1497 * Can be used as a base for components that are completely switched to
1498 * the new locking scheme (VirtualBoxBaseNEXT_base).
1499 *
1500 * @todo remove after we switch to VirtualBoxBaseNEXT completely.
1501 */
1502class VirtualBoxBaseWithChildrenNEXT : public VirtualBoxBaseWithChildren
1503{
1504private:
1505
1506 void lock();
1507 void unlock();
1508 void setReady (bool isReady);
1509 bool isReady();
1510};
1511
1512////////////////////////////////////////////////////////////////////////////////
1513
1514/**
1515 * Base class to track component's chlidren of some particular type.
1516 *
1517 * This class is similar to VirtualBoxBaseWithChildren, with the exception
1518 * that all children must be of the same type. For this reason, it's not
1519 * necessary to use a map to store children, so a list is used instead.
1520 *
1521 * As opposed to VirtualBoxBaseWithChildren, children added by
1522 * #addDependentChild() are <b>strongly</b> referenced, so that they cannot
1523 * be externally destructed until #removeDependentChild() is called.
1524 * For this reason, strict rules of calling #removeDependentChild() don't
1525 * apply to instances of this class -- it can be called anywhere in the
1526 * child's uninit() implementation.
1527 *
1528 * @param C type of child objects (must inherit VirtualBoxBase AND
1529 * implement some interface)
1530 */
1531template <class C>
1532class VirtualBoxBaseWithTypedChildren : public VirtualBoxBase
1533{
1534public:
1535
1536 typedef std::list <ComObjPtr <C> > DependentChildren;
1537
1538 VirtualBoxBaseWithTypedChildren() : mInUninit (false) {}
1539
1540 virtual ~VirtualBoxBaseWithTypedChildren() {}
1541
1542 /**
1543 * Adds the given child to the list of dependent children.
1544 * Must be called from the child's init() method,
1545 * from under the child's lock.
1546 *
1547 * @param C the child object to add (must inherit VirtualBoxBase AND
1548 * implement some interface)
1549 */
1550 void addDependentChild (C *child)
1551 {
1552 AssertReturn (child, (void) 0);
1553
1554 AutoLock alock (mMapLock);
1555 if (mInUninit)
1556 return;
1557
1558 mDependentChildren.push_back (child);
1559 }
1560
1561 /**
1562 * Removes the given child from the list of dependent children.
1563 * Must be called from the child's uninit() method,
1564 * under the child's lock.
1565 *
1566 * @param C the child object to remove (must inherit VirtualBoxBase AND
1567 * implement some interface)
1568 */
1569 void removeDependentChild (C *child)
1570 {
1571 AssertReturn (child, (void) 0);
1572
1573 AutoLock alock (mMapLock);
1574 if (mInUninit)
1575 return;
1576
1577 mDependentChildren.remove (child);
1578 }
1579
1580protected:
1581
1582 /**
1583 * Returns an internal lock handle to lock the list of children
1584 * returned by #dependentChildren() using AutoLock:
1585 * <code>
1586 * AutoLock alock (dependentChildrenLock());
1587 * </code>
1588 */
1589 AutoLock::Handle &dependentChildrenLock() const { return mMapLock; }
1590
1591 /**
1592 * Returns the read-only list of all dependent children.
1593 * @note
1594 * Access the returned list (iterate, get size etc.) only after
1595 * doing |AutoLock alock (dependentChildrenLock());|!
1596 */
1597 const DependentChildren &dependentChildren() const { return mDependentChildren; }
1598
1599 /**
1600 * Uninitializes all dependent children registered with #addDependentChild().
1601 *
1602 * @note
1603 * This method will call uninit() methods of children. If these methods
1604 * access the parent object, uninitDependentChildren() must be called
1605 * either at the beginning of the parent uninitialization sequence (when
1606 * it is still operational) or after setReady(false) is called to
1607 * indicate the parent is out of action.
1608 */
1609 void uninitDependentChildren()
1610 {
1611 AutoLock alock (this);
1612 AutoLock mapLock (mMapLock);
1613
1614 if (mDependentChildren.size())
1615 {
1616 // set flag to ignore #removeDependentChild() called from child->uninit()
1617 mInUninit = true;
1618
1619 // leave the locks to let children waiting for #removeDependentChild() run
1620 mapLock.leave();
1621 alock.leave();
1622
1623 for (typename DependentChildren::iterator it = mDependentChildren.begin();
1624 it != mDependentChildren.end(); ++ it)
1625 {
1626 C *child = (*it);
1627 Assert (child);
1628 if (child)
1629 child->uninit();
1630 }
1631 mDependentChildren.clear();
1632
1633 alock.enter();
1634 mapLock.enter();
1635
1636 mInUninit = false;
1637 }
1638 }
1639
1640 /**
1641 * Removes (detaches) all dependent children registered with
1642 * #addDependentChild(), without uninitializing them.
1643 *
1644 * @note This method must be called from under the main object's lock
1645 */
1646 void removeDependentChildren()
1647 {
1648 AutoLock alock (mMapLock);
1649 mDependentChildren.clear();
1650 }
1651
1652private:
1653
1654 DependentChildren mDependentChildren;
1655
1656 bool mInUninit;
1657 mutable AutoLock::Handle mMapLock;
1658};
1659
1660/**
1661 * Temporary class to disable deprecated methods of VirtualBoxBase.
1662 * Can be used as a base for components that are completely switched to
1663 * the new locking scheme (VirtualBoxBaseNEXT_base).
1664 *
1665 * @todo remove after we switch to VirtualBoxBaseNEXT completely.
1666 */
1667template <class C>
1668class VirtualBoxBaseWithTypedChildrenNEXT : public VirtualBoxBaseWithTypedChildren <C>
1669{
1670public:
1671
1672 typedef util::AutoLock AutoLock;
1673
1674private:
1675
1676 void lock();
1677 void unlock();
1678 bool isLockedOnCurrentThread();
1679 void setReady (bool isReady);
1680 bool isReady();
1681};
1682
1683////////////////////////////////////////////////////////////////////////////////
1684
1685/// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
1686/**
1687 * Simple template that manages data structure allocation/deallocation
1688 * and supports data pointer sharing (the instance that shares the pointer is
1689 * not responsible for memory deallocation as opposed to the instance that
1690 * owns it).
1691 */
1692template <class D>
1693class Shareable
1694{
1695public:
1696
1697 Shareable() : mData (NULL), mIsShared (FALSE) {}
1698 ~Shareable() { free(); }
1699
1700 void allocate() { attach (new D); }
1701
1702 virtual void free() {
1703 if (mData) {
1704 if (!mIsShared)
1705 delete mData;
1706 mData = NULL;
1707 mIsShared = false;
1708 }
1709 }
1710
1711 void attach (D *data) {
1712 AssertMsg (data, ("new data must not be NULL"));
1713 if (data && mData != data) {
1714 if (mData && !mIsShared)
1715 delete mData;
1716 mData = data;
1717 mIsShared = false;
1718 }
1719 }
1720
1721 void attach (Shareable &data) {
1722 AssertMsg (
1723 data.mData == mData || !data.mIsShared,
1724 ("new data must not be shared")
1725 );
1726 if (this != &data && !data.mIsShared) {
1727 attach (data.mData);
1728 data.mIsShared = true;
1729 }
1730 }
1731
1732 void share (D *data) {
1733 AssertMsg (data, ("new data must not be NULL"));
1734 if (mData != data) {
1735 if (mData && !mIsShared)
1736 delete mData;
1737 mData = data;
1738 mIsShared = true;
1739 }
1740 }
1741
1742 void share (const Shareable &data) { share (data.mData); }
1743
1744 void attachCopy (const D *data) {
1745 AssertMsg (data, ("data to copy must not be NULL"));
1746 if (data)
1747 attach (new D (*data));
1748 }
1749
1750 void attachCopy (const Shareable &data) {
1751 attachCopy (data.mData);
1752 }
1753
1754 virtual D *detach() {
1755 D *d = mData;
1756 mData = NULL;
1757 mIsShared = false;
1758 return d;
1759 }
1760
1761 D *data() const {
1762 return mData;
1763 }
1764
1765 D *operator->() const {
1766 AssertMsg (mData, ("data must not be NULL"));
1767 return mData;
1768 }
1769
1770 bool isNull() const { return mData == NULL; }
1771 bool operator!() const { return isNull(); }
1772
1773 bool isShared() const { return mIsShared; }
1774
1775protected:
1776
1777 D *mData;
1778 bool mIsShared;
1779};
1780
1781/// @todo (dmik) remove after we switch to VirtualBoxBaseNEXT completely
1782/**
1783 * Simple template that enhances Shareable<> and supports data
1784 * backup/rollback/commit (using the copy constructor of the managed data
1785 * structure).
1786 */
1787template <class D>
1788class Backupable : public Shareable <D>
1789{
1790public:
1791
1792 Backupable() : Shareable <D> (), mBackupData (NULL) {}
1793
1794 void free()
1795 {
1796 AssertMsg (this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
1797 rollback();
1798 Shareable <D>::free();
1799 }
1800
1801 D *detach()
1802 {
1803 AssertMsg (this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
1804 rollback();
1805 return Shareable <D>::detach();
1806 }
1807
1808 void share (const Backupable &data)
1809 {
1810 AssertMsg (!data.isBackedUp(), ("data to share must not be backed up"));
1811 if (!data.isBackedUp())
1812 Shareable <D>::share (data.mData);
1813 }
1814
1815 /**
1816 * Stores the current data pointer in the backup area, allocates new data
1817 * using the copy constructor on current data and makes new data active.
1818 */
1819 void backup()
1820 {
1821 AssertMsg (this->mData, ("data must not be NULL"));
1822 if (this->mData && !mBackupData)
1823 {
1824 mBackupData = this->mData;
1825 this->mData = new D (*mBackupData);
1826 }
1827 }
1828
1829 /**
1830 * Deletes new data created by #backup() and restores previous data pointer
1831 * stored in the backup area, making it active again.
1832 */
1833 void rollback()
1834 {
1835 if (this->mData && mBackupData)
1836 {
1837 delete this->mData;
1838 this->mData = mBackupData;
1839 mBackupData = NULL;
1840 }
1841 }
1842
1843 /**
1844 * Commits current changes by deleting backed up data and clearing up the
1845 * backup area. The new data pointer created by #backup() remains active
1846 * and becomes the only managed pointer.
1847 *
1848 * This method is much faster than #commitCopy() (just a single pointer
1849 * assignment operation), but makes the previous data pointer invalid
1850 * (because it is freed). For this reason, this method must not be
1851 * used if it's possible that data managed by this instance is shared with
1852 * some other Shareable instance. See #commitCopy().
1853 */
1854 void commit()
1855 {
1856 if (this->mData && mBackupData)
1857 {
1858 if (!this->mIsShared)
1859 delete mBackupData;
1860 mBackupData = NULL;
1861 this->mIsShared = false;
1862 }
1863 }
1864
1865 /**
1866 * Commits current changes by assigning new data to the previous data
1867 * pointer stored in the backup area using the assignment operator.
1868 * New data is deleted, the backup area is cleared and the previous data
1869 * pointer becomes active and the only managed pointer.
1870 *
1871 * This method is slower than #commit(), but it keeps the previous data
1872 * pointer valid (i.e. new data is copied to the same memory location).
1873 * For that reason it's safe to use this method on instances that share
1874 * managed data with other Shareable instances.
1875 */
1876 void commitCopy()
1877 {
1878 if (this->mData && mBackupData)
1879 {
1880 *mBackupData = *(this->mData);
1881 delete this->mData;
1882 this->mData = mBackupData;
1883 mBackupData = NULL;
1884 }
1885 }
1886
1887 void assignCopy (const D *data)
1888 {
1889 AssertMsg (this->mData, ("data must not be NULL"));
1890 AssertMsg (data, ("data to copy must not be NULL"));
1891 if (this->mData && data)
1892 {
1893 if (!mBackupData)
1894 {
1895 mBackupData = this->mData;
1896 this->mData = new D (*data);
1897 }
1898 else
1899 *this->mData = *data;
1900 }
1901 }
1902
1903 void assignCopy (const Backupable &data)
1904 {
1905 assignCopy (data.mData);
1906 }
1907
1908 bool isBackedUp() const
1909 {
1910 return mBackupData != NULL;
1911 }
1912
1913 bool hasActualChanges() const
1914 {
1915 AssertMsg (this->mData, ("data must not be NULL"));
1916 return this->mData != NULL && mBackupData != NULL &&
1917 !(*this->mData == *mBackupData);
1918 }
1919
1920 D *backedUpData() const
1921 {
1922 return mBackupData;
1923 }
1924
1925protected:
1926
1927 D *mBackupData;
1928};
1929
1930#endif // ____H_VIRTUALBOXBASEIMPL
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette