1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Basic Frontend (BFE):
|
---|
4 | * Declarations of the BFE base classes
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #ifndef ____H_VIRTUALBOXBASEIMPL
|
---|
20 | #define ____H_VIRTUALBOXBASEIMPL
|
---|
21 |
|
---|
22 | #ifdef VBOXBFE_WITHOUT_COM
|
---|
23 | # include "COMDefs.h" // Our wrapper for COM definitions left in the code
|
---|
24 | # include <iprt/uuid.h>
|
---|
25 | #else
|
---|
26 | # include <VBox/com/defs.h>
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | #include <VBox/com/assert.h> // For the AssertComRC macro
|
---|
30 |
|
---|
31 | #include <iprt/alloc.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/critsect.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/asm.h> // for ASMReturnAddress
|
---|
36 |
|
---|
37 | #include <list>
|
---|
38 | #include <map>
|
---|
39 |
|
---|
40 | // defines
|
---|
41 | ////////////////////////////////////////////////////////////////////////////////
|
---|
42 |
|
---|
43 | #define VBOX_E_OBJECT_NOT_FOUND 0x80BB0001
|
---|
44 | #define VBOX_E_INVALID_VM_STATE 0x80BB0002
|
---|
45 | #define VBOX_E_VM_ERROR 0x80BB0003
|
---|
46 | #define VBOX_E_FILE_ERROR 0x80BB0004
|
---|
47 | #define VBOX_E_IPRT_ERROR 0x80BB0005
|
---|
48 | #define VBOX_E_PDM_ERROR 0x80BB0006
|
---|
49 | #define VBOX_E_INVALID_OBJECT_STATE 0x80BB0007
|
---|
50 | #define VBOX_E_HOST_ERROR 0x80BB0008
|
---|
51 | #define VBOX_E_NOT_SUPPORTED 0x80BB0009
|
---|
52 | #define VBOX_E_XML_ERROR 0x80BB000A
|
---|
53 | #define VBOX_E_INVALID_SESSION_STATE 0x80BB000B
|
---|
54 | #define VBOX_E_OBJECT_IN_USE 0x80BB000C
|
---|
55 |
|
---|
56 | // macros and inlines
|
---|
57 | ////////////////////////////////////////////////////////////////////////////////
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * A lightweight replacement for the COM setError function. I am
|
---|
61 | * assuming that this is only called in circumstances justifying
|
---|
62 | * an assertion.
|
---|
63 | *
|
---|
64 | * @returns error number
|
---|
65 | * @param iNum error number - this is simply returned
|
---|
66 | * @param pszFormat formatted error message
|
---|
67 | */
|
---|
68 | static inline int setError(int iNum, const char *pszFormat, ...)
|
---|
69 | {
|
---|
70 | va_list args;
|
---|
71 | va_start(args, pszFormat);
|
---|
72 | AssertMsgFailed((pszFormat, args));
|
---|
73 | va_end(args);
|
---|
74 | return iNum;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Translate an error string. We do not do translation.
|
---|
79 | */
|
---|
80 | #define tr(a) a
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * A special version of the Assert macro to be used within VirtualBoxBase
|
---|
84 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
85 | *
|
---|
86 | * In the debug build, this macro is equivalent to Assert.
|
---|
87 | * In the release build, this macro uses |setError (E_FAIL, ...)| to set the
|
---|
88 | * error info from the asserted expression.
|
---|
89 | *
|
---|
90 | * @see VirtualBoxSupportErrorInfoImpl::setError
|
---|
91 | *
|
---|
92 | * @param expr Expression which should be true.
|
---|
93 | */
|
---|
94 | #if defined (DEBUG)
|
---|
95 | #define ComAssert(expr) Assert (expr)
|
---|
96 | #else
|
---|
97 | #define ComAssert(expr) \
|
---|
98 | do { } while (0)
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * A special version of the AssertMsg macro to be used within VirtualBoxBase
|
---|
103 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
104 | *
|
---|
105 | * See ComAssert for more info.
|
---|
106 | *
|
---|
107 | * @param expr Expression which should be true.
|
---|
108 | * @param a printf argument list (in parenthesis).
|
---|
109 | */
|
---|
110 | #if defined (DEBUG)
|
---|
111 | #define ComAssertMsg(expr, a) AssertMsg (expr, a)
|
---|
112 | #else
|
---|
113 | #define ComAssertMsg(expr, a) \
|
---|
114 | do { } while (0)
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * A special version of the AssertRC macro to be used within VirtualBoxBase
|
---|
119 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
120 | *
|
---|
121 | * See ComAssert for more info.
|
---|
122 | *
|
---|
123 | * @param vrc VBox status code.
|
---|
124 | */
|
---|
125 | #if defined (DEBUG)
|
---|
126 | #define ComAssertRC(vrc) AssertRC (vrc)
|
---|
127 | #else
|
---|
128 | #define ComAssertRC(vrc) ComAssertMsgRC (vrc, ("%Rra", vrc))
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * A special version of the AssertMsgRC macro to be used within VirtualBoxBase
|
---|
133 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
134 | *
|
---|
135 | * See ComAssert for more info.
|
---|
136 | *
|
---|
137 | * @param vrc VBox status code.
|
---|
138 | * @param msg printf argument list (in parenthesis).
|
---|
139 | */
|
---|
140 | #if defined (DEBUG)
|
---|
141 | #define ComAssertMsgRC(vrc, msg) AssertMsgRC (vrc, msg)
|
---|
142 | #else
|
---|
143 | #define ComAssertMsgRC(vrc, msg) ComAssertMsg (RT_SUCCESS (vrc), msg)
|
---|
144 | #endif
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * A special version of the AssertFailed macro to be used within VirtualBoxBase
|
---|
149 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
150 | *
|
---|
151 | * See ComAssert for more info.
|
---|
152 | */
|
---|
153 | #if defined (DEBUG)
|
---|
154 | #define ComAssertFailed() AssertFailed()
|
---|
155 | #else
|
---|
156 | #define ComAssertFailed() \
|
---|
157 | do { } while (0)
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * A special version of the AssertMsgFailed macro to be used within VirtualBoxBase
|
---|
162 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
163 | *
|
---|
164 | * See ComAssert for more info.
|
---|
165 | *
|
---|
166 | * @param a printf argument list (in parenthesis).
|
---|
167 | */
|
---|
168 | #if defined (DEBUG)
|
---|
169 | #define ComAssertMsgFailed(a) AssertMsgFailed(a)
|
---|
170 | #else
|
---|
171 | #define ComAssertMsgFailed(a) \
|
---|
172 | do { } while (0)
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * A special version of the AssertComRC macro to be used within VirtualBoxBase
|
---|
177 | * subclasses that also inherit the VirtualBoxSupportErrorInfoImpl template.
|
---|
178 | *
|
---|
179 | * See ComAssert for more info.
|
---|
180 | *
|
---|
181 | * @param rc COM result code
|
---|
182 | */
|
---|
183 | #if defined (DEBUG)
|
---|
184 | #define ComAssertComRC(rc) AssertComRC (rc)
|
---|
185 | #else
|
---|
186 | #define ComAssertComRC(rc) ComAssertMsg (SUCCEEDED (rc), ("COM RC = 0x%08X\n", rc))
|
---|
187 | #endif
|
---|
188 |
|
---|
189 |
|
---|
190 | /** Special version of ComAssert that returns ret if expr fails */
|
---|
191 | #define ComAssertRet(expr, ret) \
|
---|
192 | do { ComAssert (expr); if (!(expr)) return (ret); } while (0)
|
---|
193 | /** Special version of ComAssertMsg that returns ret if expr fails */
|
---|
194 | #define ComAssertMsgRet(expr, a, ret) \
|
---|
195 | do { ComAssertMsg (expr, a); if (!(expr)) return (ret); } while (0)
|
---|
196 | /** Special version of ComAssertRC that returns ret if vrc does not succeed */
|
---|
197 | #define ComAssertRCRet(vrc, ret) \
|
---|
198 | do { ComAssertRC (vrc); if (!RT_SUCCESS (vrc)) return (ret); } while (0)
|
---|
199 | /** Special version of ComAssertMsgRC that returns ret if vrc does not succeed */
|
---|
200 | #define ComAssertMsgRCRet(vrc, msg, ret) \
|
---|
201 | do { ComAssertMsgRC (vrc, msg); if (!RT_SUCCESS (vrc)) return (ret); } while (0)
|
---|
202 | /** Special version of ComAssertFailed that returns ret */
|
---|
203 | #define ComAssertFailedRet(ret) \
|
---|
204 | do { ComAssertFailed(); return (ret); } while (0)
|
---|
205 | /** Special version of ComAssertMsgFailed that returns ret */
|
---|
206 | #define ComAssertMsgFailedRet(msg, ret) \
|
---|
207 | do { ComAssertMsgFailed (msg); return (ret); } while (0)
|
---|
208 | /** Special version of ComAssertComRC that returns ret if rc does not succeed */
|
---|
209 | #define ComAssertComRCRet(rc, ret) \
|
---|
210 | do { ComAssertComRC (rc); if (!SUCCEEDED (rc)) return (ret); } while (0)
|
---|
211 |
|
---|
212 |
|
---|
213 | /** Special version of ComAssert that evaulates eval and breaks if expr fails */
|
---|
214 | #define ComAssertBreak(expr, eval) \
|
---|
215 | if (1) { ComAssert (expr); if (!(expr)) { eval; break; } } else do {} while (0)
|
---|
216 | /** Special version of ComAssertMsg that evaulates eval and breaks if expr fails */
|
---|
217 | #define ComAssertMsgBreak(expr, a, eval) \
|
---|
218 | if (1) { ComAssertMsg (expr, a); if (!(expr)) { eval; break; } } else do {} while (0)
|
---|
219 | /** Special version of ComAssertRC that evaulates eval and breaks if vrc does not succeed */
|
---|
220 | #define ComAssertRCBreak(vrc, eval) \
|
---|
221 | if (1) { ComAssertRC (vrc); if (!RT_SUCCESS (vrc)) { eval; break; } } else do {} while (0)
|
---|
222 | /** Special version of ComAssertMsgRC that evaulates eval and breaks if vrc does not succeed */
|
---|
223 | #define ComAssertMsgRCBreak(vrc, msg, eval) \
|
---|
224 | if (1) { ComAssertMsgRC (vrc, msg); if (!RT_SUCCESS (vrc)) { eval; break; } } else do {} while (0)
|
---|
225 | /** Special version of ComAssertFailed that vaulates eval and breaks */
|
---|
226 | #define ComAssertFailedBreak(eval) \
|
---|
227 | if (1) { ComAssertFailed(); { eval; break; } } else do {} while (0)
|
---|
228 | /** Special version of ComAssertMsgFailed that vaulates eval and breaks */
|
---|
229 | #define ComAssertMsgFailedBreak(msg, eval) \
|
---|
230 | if (1) { ComAssertMsgFailed (msg); { eval; break; } } else do {} while (0)
|
---|
231 | /** Special version of ComAssertComRC that vaulates eval and breaks if rc does not succeed */
|
---|
232 | #define ComAssertComRCBreak(rc, eval) \
|
---|
233 | if (1) { ComAssertComRC (rc); if (!SUCCEEDED (rc)) { eval; break; } } else do {} while (0)
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Checks whether this object is ready or not. Objects are typically ready
|
---|
237 | * after they are successfully created by their parent objects and become
|
---|
238 | * not ready when the respective parent itsef becomes not ready or gets
|
---|
239 | * destroyed while a reference to the child is still held by the caller
|
---|
240 | * (which prevents it from destruction).
|
---|
241 | *
|
---|
242 | * When this object is not ready, the macro sets error info and returns
|
---|
243 | * E_UNEXPECTED (the translatable error message is defined in null context).
|
---|
244 | * Otherwise, the macro does nothing.
|
---|
245 | *
|
---|
246 | * This macro <b>must</b> be used at the beginning of all interface methods
|
---|
247 | * (right after entering the class lock) in classes derived from both
|
---|
248 | * VirtualBoxBase and VirtualBoxSupportErrorInfoImpl.
|
---|
249 | */
|
---|
250 | #define CHECK_READY() \
|
---|
251 | do { \
|
---|
252 | if (!isReady()) \
|
---|
253 | return setError (E_UNEXPECTED, tr ("The object is not ready")); \
|
---|
254 | } while (0)
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Declares an empty constructor and destructor for the given class.
|
---|
258 | * This is useful to prevent the compiler from generating the default
|
---|
259 | * ctor and dtor, which in turn allows to use forward class statements
|
---|
260 | * (instead of including their header files) when declaring data members of
|
---|
261 | * non-fundamental types with constructors (which are always called implicitly
|
---|
262 | * by constructors and by the destructor of the class).
|
---|
263 | *
|
---|
264 | * This macro is to be palced within (the public section of) the class
|
---|
265 | * declaration. Its counterpart, DEFINE_EMPTY_CTOR_DTOR, must be placed
|
---|
266 | * somewhere in one of the translation units (usually .cpp source files).
|
---|
267 | *
|
---|
268 | * @param cls class to declare a ctor and dtor for
|
---|
269 | */
|
---|
270 | #define DECLARE_EMPTY_CTOR_DTOR(cls) cls(); ~cls();
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Defines an empty constructor and destructor for the given class.
|
---|
274 | * See DECLARE_EMPTY_CTOR_DTOR for more info.
|
---|
275 | */
|
---|
276 | #define DEFINE_EMPTY_CTOR_DTOR(cls) \
|
---|
277 | cls::cls () {}; cls::~cls () {};
|
---|
278 |
|
---|
279 | #define VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(cls, iface)
|
---|
280 |
|
---|
281 | ////////////////////////////////////////////////////////////////////////////////
|
---|
282 |
|
---|
283 | namespace stdx
|
---|
284 | {
|
---|
285 | /**
|
---|
286 | * A wrapper around the container that owns pointers it stores.
|
---|
287 | *
|
---|
288 | * @note
|
---|
289 | * Ownership is recognized only when destructing the container!
|
---|
290 | * Pointers are not deleted when erased using erase() etc.
|
---|
291 | *
|
---|
292 | * @param container
|
---|
293 | * class that meets Container requirements (for example, an instance of
|
---|
294 | * std::list<>, std::vector<> etc.). The given class must store
|
---|
295 | * pointers (for example, std::list <MyType *>).
|
---|
296 | */
|
---|
297 | template <typename container>
|
---|
298 | class ptr_container : public container
|
---|
299 | {
|
---|
300 | public:
|
---|
301 | ~ptr_container()
|
---|
302 | {
|
---|
303 | for (typename container::iterator it = container::begin();
|
---|
304 | it != container::end();
|
---|
305 | ++ it)
|
---|
306 | delete (*it);
|
---|
307 | }
|
---|
308 | };
|
---|
309 | };
|
---|
310 |
|
---|
311 | ////////////////////////////////////////////////////////////////////////////////
|
---|
312 |
|
---|
313 | class ATL_NO_VTABLE VirtualBoxBase
|
---|
314 | {
|
---|
315 |
|
---|
316 | public:
|
---|
317 | VirtualBoxBase()
|
---|
318 | {
|
---|
319 | mReady = false;
|
---|
320 | RTCritSectInit(&mCritSec);
|
---|
321 | }
|
---|
322 | virtual ~VirtualBoxBase()
|
---|
323 | {
|
---|
324 | RTCritSectDelete(&mCritSec);
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Virtual uninitialization method. Called during parent object's
|
---|
329 | * uninitialization, if the given subclass instance is a dependent child of
|
---|
330 | * a class derived from VirtualBoxBaseWithChildren (@sa
|
---|
331 | * VirtualBoxBaseWithChildren::addDependentChild). In this case, this
|
---|
332 | * method's implementation must call setReady (false),
|
---|
333 | */
|
---|
334 | virtual void uninit() {}
|
---|
335 |
|
---|
336 | // lock the object
|
---|
337 | void lock()
|
---|
338 | {
|
---|
339 | RTCritSectEnter(&mCritSec);
|
---|
340 | }
|
---|
341 | // unlock the object
|
---|
342 | void unlock()
|
---|
343 | {
|
---|
344 | RTCritSectLeave(&mCritSec);
|
---|
345 | }
|
---|
346 |
|
---|
347 | /** Returns true when the current thread owns this object's lock. */
|
---|
348 | bool isLockedOnCurrentThread()
|
---|
349 | {
|
---|
350 | return RTCritSectIsOwner (&mCritSec);
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Helper class to make safe locking / unlocking.
|
---|
355 | * The constructor, given the VirtualBoxBase pointer, safely acquires the
|
---|
356 | * lock protecting its data. This lock will be released automatically
|
---|
357 | * when the instance goes out of scope (block, function etc.).
|
---|
358 | *
|
---|
359 | * @note
|
---|
360 | * An instance of this class must be declared as a local variable,
|
---|
361 | * otherwise the optimizer will most likely destruct it right after
|
---|
362 | * creation (but not at the end of the block), so the lock will be
|
---|
363 | * released immediately.
|
---|
364 | */
|
---|
365 | class AutoLock
|
---|
366 | {
|
---|
367 | public:
|
---|
368 |
|
---|
369 | #if defined(RT_STRICT)
|
---|
370 | # define ___CritSectEnter(cs) RTCritSectEnterDebug((cs), (RTUINTPTR)ASMReturnAddress(), "return address >>>", 0, __PRETTY_FUNCTION__)
|
---|
371 | #else
|
---|
372 | # define ___CritSectEnter(cs) RTCritSectEnter((cs))
|
---|
373 | #endif
|
---|
374 |
|
---|
375 | /** Internal lock handle */
|
---|
376 | class Handle
|
---|
377 | {
|
---|
378 | public:
|
---|
379 | Handle (RTCRITSECT &critSect) : lock (critSect) {}
|
---|
380 | private:
|
---|
381 | RTCRITSECT &lock;
|
---|
382 | friend class AutoLock;
|
---|
383 | };
|
---|
384 |
|
---|
385 | AutoLock() : mLock (NULL), mLevel (0), mLeftLevel (0) {}
|
---|
386 |
|
---|
387 | AutoLock (VirtualBoxBase *that)
|
---|
388 | : mLock (that ? &that->mCritSec : NULL)
|
---|
389 | , mLevel (0), mLeftLevel (0)
|
---|
390 | {
|
---|
391 | if (mLock)
|
---|
392 | {
|
---|
393 | ___CritSectEnter (mLock);
|
---|
394 | ++ mLevel;
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | AutoLock (RTCRITSECT &critSect)
|
---|
399 | : mLock (&critSect), mLevel (0), mLeftLevel (0)
|
---|
400 | {
|
---|
401 | if (mLock)
|
---|
402 | {
|
---|
403 | ___CritSectEnter (mLock);
|
---|
404 | ++ mLevel;
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | AutoLock (const Handle &handle)
|
---|
409 | : mLock (&handle.lock), mLevel (0), mLeftLevel (0)
|
---|
410 | {
|
---|
411 | if (mLock)
|
---|
412 | {
|
---|
413 | ___CritSectEnter (mLock);
|
---|
414 | ++ mLevel;
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | ~AutoLock()
|
---|
419 | {
|
---|
420 | if (mLock)
|
---|
421 | {
|
---|
422 | if (mLeftLevel)
|
---|
423 | {
|
---|
424 | mLeftLevel -= mLevel;
|
---|
425 | mLevel = 0;
|
---|
426 | for (; mLeftLevel; -- mLeftLevel)
|
---|
427 | RTCritSectEnter (mLock);
|
---|
428 | }
|
---|
429 | AssertMsg (mLevel <= 1, ("Lock level > 1: %d\n", mLevel));
|
---|
430 | for (; mLevel; -- mLevel)
|
---|
431 | RTCritSectLeave (mLock);
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Tries to acquire the lock or increases the lock level
|
---|
437 | * if the lock is already owned by this thread.
|
---|
438 | */
|
---|
439 | void lock()
|
---|
440 | {
|
---|
441 | if (mLock)
|
---|
442 | {
|
---|
443 | AssertMsgReturn (mLeftLevel == 0, ("lock() after leave()\n"), (void) 0);
|
---|
444 | ___CritSectEnter (mLock);
|
---|
445 | ++ mLevel;
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | /**
|
---|
450 | * Decreases the lock level. If the level goes to zero, the lock
|
---|
451 | * is released by the current thread.
|
---|
452 | */
|
---|
453 | void unlock()
|
---|
454 | {
|
---|
455 | if (mLock)
|
---|
456 | {
|
---|
457 | AssertMsgReturn (mLevel > 0, ("Lock level is zero\n"), (void) 0);
|
---|
458 | AssertMsgReturn (mLeftLevel == 0, ("lock() after leave()\n"), (void) 0);
|
---|
459 | -- mLevel;
|
---|
460 | RTCritSectLeave (mLock);
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * Causes the current thread to completely release the lock
|
---|
466 | * (including locks acquired by all other instances of this class
|
---|
467 | * referring to the same object or handle). #enter() must be called
|
---|
468 | * to acquire the lock back and restore all lock levels.
|
---|
469 | */
|
---|
470 | void leave()
|
---|
471 | {
|
---|
472 | if (mLock)
|
---|
473 | {
|
---|
474 | AssertMsg (mLevel > 0, ("Lock level is zero\n"));
|
---|
475 | AssertMsgReturn (mLeftLevel == 0, ("leave() w/o enter()\n"), (void) 0);
|
---|
476 | mLeftLevel = RTCritSectGetRecursion (mLock);
|
---|
477 | for (uint32_t left = mLeftLevel; left; -- left)
|
---|
478 | RTCritSectLeave (mLock);
|
---|
479 | Assert (mLeftLevel >= mLevel);
|
---|
480 | }
|
---|
481 | }
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Causes the current thread to acquire the lock again and restore
|
---|
485 | * all lock levels after calling #leave().
|
---|
486 | */
|
---|
487 | void enter()
|
---|
488 | {
|
---|
489 | if (mLock)
|
---|
490 | {
|
---|
491 | AssertMsg (mLevel > 0, ("Lock level is zero\n"));
|
---|
492 | AssertMsgReturn (mLeftLevel > 0, ("enter() w/o leave()\n"), (void) 0);
|
---|
493 | for (; mLeftLevel; -- mLeftLevel)
|
---|
494 | ___CritSectEnter (mLock);
|
---|
495 | }
|
---|
496 | }
|
---|
497 |
|
---|
498 | uint32_t level() const { return mLevel; }
|
---|
499 |
|
---|
500 | bool isNull() const { return mLock == NULL; }
|
---|
501 | bool operator !() const { return isNull(); }
|
---|
502 |
|
---|
503 | bool belongsTo (VirtualBoxBase *that) const
|
---|
504 | {
|
---|
505 | return that && &that->mCritSec == mLock;
|
---|
506 | }
|
---|
507 |
|
---|
508 | private:
|
---|
509 |
|
---|
510 | AutoLock (const AutoLock &that); // disabled
|
---|
511 | AutoLock &operator = (const AutoLock &that); // disabled
|
---|
512 |
|
---|
513 | RTCRITSECT *mLock;
|
---|
514 | uint32_t mLevel;
|
---|
515 | uint32_t mLeftLevel;
|
---|
516 |
|
---|
517 | #undef ___CritSectEnter
|
---|
518 | };
|
---|
519 |
|
---|
520 | // sets the ready state of the object
|
---|
521 | void setReady(bool isReady)
|
---|
522 | {
|
---|
523 | mReady = isReady;
|
---|
524 | }
|
---|
525 | // get the ready state of the object
|
---|
526 | bool isReady()
|
---|
527 | {
|
---|
528 | return mReady;
|
---|
529 | }
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Translates the given text string according to the currently installed
|
---|
533 | * translation table and current context. The current context is determined
|
---|
534 | * by the context parameter. Additionally, a comment to the source text
|
---|
535 | * string text can be given. This comment (which is NULL by default)
|
---|
536 | * is helpful in situations where it is necessary to distinguish between
|
---|
537 | * two or more semantically different roles of the same source text in the
|
---|
538 | * same context.
|
---|
539 | *
|
---|
540 | * @param context the context of the the translation
|
---|
541 | * @param sourceText the string to translate
|
---|
542 | * @param comment the comment to the string (NULL means no comment)
|
---|
543 | *
|
---|
544 | * @return
|
---|
545 | * the translated version of the source string in UTF-8 encoding,
|
---|
546 | * or the source string itself if the translation is not found
|
---|
547 | * in the given context.
|
---|
548 | */
|
---|
549 | static const char *translate (const char *context, const char *sourceText,
|
---|
550 | const char *comment = 0);
|
---|
551 |
|
---|
552 | private:
|
---|
553 |
|
---|
554 | // flag determining whether an object is ready
|
---|
555 | // for usage, i.e. methods may be called
|
---|
556 | bool mReady;
|
---|
557 | // mutex semaphore to lock the object
|
---|
558 | RTCRITSECT mCritSec;
|
---|
559 | };
|
---|
560 |
|
---|
561 | ////////////////////////////////////////////////////////////////////////////////
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * Simple template that manages data structure allocation/deallocation
|
---|
565 | * and supports data pointer sharing (the instance that shares the pointer is
|
---|
566 | * not responsible for memory deallocation as opposed to the instance that
|
---|
567 | * owns it).
|
---|
568 | */
|
---|
569 | template <class D>
|
---|
570 | class Shareable
|
---|
571 | {
|
---|
572 | public:
|
---|
573 |
|
---|
574 | Shareable() : mData (NULL), mIsShared (FALSE) {}
|
---|
575 | ~Shareable() { free(); }
|
---|
576 |
|
---|
577 | void allocate() { attach (new D); }
|
---|
578 |
|
---|
579 | virtual void free() {
|
---|
580 | if (mData) {
|
---|
581 | if (!mIsShared)
|
---|
582 | delete mData;
|
---|
583 | mData = NULL;
|
---|
584 | mIsShared = false;
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | void attach (D *data) {
|
---|
589 | AssertMsg (data, ("new data must not be NULL"));
|
---|
590 | if (data && mData != data) {
|
---|
591 | if (mData && !mIsShared)
|
---|
592 | delete mData;
|
---|
593 | mData = data;
|
---|
594 | mIsShared = false;
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | void attach (Shareable &data) {
|
---|
599 | AssertMsg (
|
---|
600 | data.mData == mData || !data.mIsShared,
|
---|
601 | ("new data must not be shared")
|
---|
602 | );
|
---|
603 | if (this != &data && !data.mIsShared) {
|
---|
604 | attach (data.mData);
|
---|
605 | data.mIsShared = true;
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 | void share (D *data) {
|
---|
610 | AssertMsg (data, ("new data must not be NULL"));
|
---|
611 | if (mData != data) {
|
---|
612 | if (mData && !mIsShared)
|
---|
613 | delete mData;
|
---|
614 | mData = data;
|
---|
615 | mIsShared = true;
|
---|
616 | }
|
---|
617 | }
|
---|
618 |
|
---|
619 | void share (const Shareable &data) { share (data.mData); }
|
---|
620 |
|
---|
621 | void attachCopy (const D *data) {
|
---|
622 | AssertMsg (data, ("data to copy must not be NULL"));
|
---|
623 | if (data)
|
---|
624 | attach (new D (*data));
|
---|
625 | }
|
---|
626 |
|
---|
627 | void attachCopy (const Shareable &data) {
|
---|
628 | attachCopy (data.mData);
|
---|
629 | }
|
---|
630 |
|
---|
631 | virtual D *detach() {
|
---|
632 | D *d = mData;
|
---|
633 | mData = NULL;
|
---|
634 | mIsShared = false;
|
---|
635 | return d;
|
---|
636 | }
|
---|
637 |
|
---|
638 | D *data() const {
|
---|
639 | return mData;
|
---|
640 | }
|
---|
641 |
|
---|
642 | D *operator->() const {
|
---|
643 | AssertMsg (mData, ("data must not be NULL"));
|
---|
644 | return mData;
|
---|
645 | }
|
---|
646 |
|
---|
647 | bool isNull() const { return mData == NULL; }
|
---|
648 | bool operator!() const { return isNull(); }
|
---|
649 |
|
---|
650 | bool isShared() const { return mIsShared; }
|
---|
651 |
|
---|
652 | protected:
|
---|
653 |
|
---|
654 | D *mData;
|
---|
655 | bool mIsShared;
|
---|
656 | };
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Simple template that enhances Shareable<> and supports data
|
---|
660 | * backup/rollback/commit (using the copy constructor of the managed data
|
---|
661 | * structure).
|
---|
662 | */
|
---|
663 | template <class D>
|
---|
664 | class Backupable : public Shareable <D>
|
---|
665 | {
|
---|
666 | public:
|
---|
667 |
|
---|
668 | Backupable() : Shareable <D> (), mBackupData (NULL) {}
|
---|
669 |
|
---|
670 | void free()
|
---|
671 | {
|
---|
672 | AssertMsg (this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
|
---|
673 | rollback();
|
---|
674 | Shareable <D>::free();
|
---|
675 | }
|
---|
676 |
|
---|
677 | D *detach()
|
---|
678 | {
|
---|
679 | AssertMsg (this->mData || !mBackupData, ("backup must be NULL if data is NULL"));
|
---|
680 | rollback();
|
---|
681 | return Shareable <D>::detach();
|
---|
682 | }
|
---|
683 |
|
---|
684 | void share (const Backupable &data)
|
---|
685 | {
|
---|
686 | AssertMsg (!data.isBackedUp(), ("data to share must not be backed up"));
|
---|
687 | if (!data.isBackedUp())
|
---|
688 | Shareable <D>::share (data.mData);
|
---|
689 | }
|
---|
690 |
|
---|
691 | /**
|
---|
692 | * Stores the current data pointer in the backup area, allocates new data
|
---|
693 | * using the copy constructor on current data and makes new data active.
|
---|
694 | */
|
---|
695 | void backup()
|
---|
696 | {
|
---|
697 | AssertMsg (this->mData, ("data must not be NULL"));
|
---|
698 | if (this->mData && !mBackupData)
|
---|
699 | {
|
---|
700 | mBackupData = this->mData;
|
---|
701 | this->mData = new D (*mBackupData);
|
---|
702 | }
|
---|
703 | }
|
---|
704 |
|
---|
705 | /**
|
---|
706 | * Deletes new data created by #backup() and restores previous data pointer
|
---|
707 | * stored in the backup area, making it active again.
|
---|
708 | */
|
---|
709 | void rollback()
|
---|
710 | {
|
---|
711 | if (this->mData && mBackupData)
|
---|
712 | {
|
---|
713 | delete this->mData;
|
---|
714 | this->mData = mBackupData;
|
---|
715 | mBackupData = NULL;
|
---|
716 | }
|
---|
717 | }
|
---|
718 |
|
---|
719 | /**
|
---|
720 | * Commits current changes by deleting backed up data and clearing up the
|
---|
721 | * backup area. The new data pointer created by #backup() remains active
|
---|
722 | * and becomes the only managed pointer.
|
---|
723 | *
|
---|
724 | * This method is much faster than #commitCopy() (just a single pointer
|
---|
725 | * assignment operation), but makes the previous data pointer invalid
|
---|
726 | * (because it is freed). For this reason, this method must not be
|
---|
727 | * used if it's possible that data managed by this instance is shared with
|
---|
728 | * some other Shareable instance. See #commitCopy().
|
---|
729 | */
|
---|
730 | void commit()
|
---|
731 | {
|
---|
732 | if (this->mData && mBackupData)
|
---|
733 | {
|
---|
734 | if (!this->mIsShared)
|
---|
735 | delete mBackupData;
|
---|
736 | mBackupData = NULL;
|
---|
737 | this->mIsShared = false;
|
---|
738 | }
|
---|
739 | }
|
---|
740 |
|
---|
741 | /**
|
---|
742 | * Commits current changes by assigning new data to the previous data
|
---|
743 | * pointer stored in the backup area using the assignment operator.
|
---|
744 | * New data is deleted, the backup area is cleared and the previous data
|
---|
745 | * pointer becomes active and the only managed pointer.
|
---|
746 | *
|
---|
747 | * This method is slower than #commit(), but it keeps the previous data
|
---|
748 | * pointer valid (i.e. new data is copied to the same memory location).
|
---|
749 | * For that reason it's safe to use this method on instances that share
|
---|
750 | * managed data with other Shareable instances.
|
---|
751 | */
|
---|
752 | void commitCopy()
|
---|
753 | {
|
---|
754 | if (this->mData && mBackupData)
|
---|
755 | {
|
---|
756 | *mBackupData = *(this->mData);
|
---|
757 | delete this->mData;
|
---|
758 | this->mData = mBackupData;
|
---|
759 | mBackupData = NULL;
|
---|
760 | }
|
---|
761 | }
|
---|
762 |
|
---|
763 | void assignCopy (const D *data)
|
---|
764 | {
|
---|
765 | AssertMsg (this->mData, ("data must not be NULL"));
|
---|
766 | AssertMsg (data, ("data to copy must not be NULL"));
|
---|
767 | if (this->mData && data)
|
---|
768 | {
|
---|
769 | if (!mBackupData)
|
---|
770 | {
|
---|
771 | mBackupData = this->mData;
|
---|
772 | this->mData = new D (*data);
|
---|
773 | }
|
---|
774 | else
|
---|
775 | *this->mData = *data;
|
---|
776 | }
|
---|
777 | }
|
---|
778 |
|
---|
779 | void assignCopy (const Backupable &data)
|
---|
780 | {
|
---|
781 | assignCopy (data.mData);
|
---|
782 | }
|
---|
783 |
|
---|
784 | bool isBackedUp() const
|
---|
785 | {
|
---|
786 | return mBackupData != NULL;
|
---|
787 | }
|
---|
788 |
|
---|
789 | bool hasActualChanges() const
|
---|
790 | {
|
---|
791 | AssertMsg (this->mData, ("data must not be NULL"));
|
---|
792 | return this->mData != NULL && mBackupData != NULL &&
|
---|
793 | !(*this->mData == *mBackupData);
|
---|
794 | }
|
---|
795 |
|
---|
796 | D *backedUpData() const
|
---|
797 | {
|
---|
798 | return mBackupData;
|
---|
799 | }
|
---|
800 |
|
---|
801 | protected:
|
---|
802 |
|
---|
803 | D *mBackupData;
|
---|
804 | };
|
---|
805 |
|
---|
806 | #endif // ____H_VIRTUALBOXBASEIMPL
|
---|