VirtualBox

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

Last change on this file since 40418 was 40418, checked in by vboxsync, 13 years ago

Main: Extended IMachine and the settings XML with three tracing related properties.

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