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