VirtualBox

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

Last change on this file since 13835 was 13835, checked in by vboxsync, 16 years ago

s/VBOX_SUCCESS/RT_SUCCESS/g s/VBOX_FAILURE/RT_FAILURE/g - VBOX_SUCCESS and VBOX_FAILURE have *NOT* been retired (because old habbits die hard) just sligtly deprecated.

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