1 | /* $Id: AutoCaller.h 55401 2015-04-23 10:03:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VirtualBox object caller handling definitions
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2014 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_AUTOCALLER
|
---|
20 | #define ____H_AUTOCALLER
|
---|
21 |
|
---|
22 | #include "ObjectState.h"
|
---|
23 |
|
---|
24 | #include "VBox/com/AutoLock.h"
|
---|
25 |
|
---|
26 | // Forward declaration needed, but nothing more.
|
---|
27 | class VirtualBoxBase;
|
---|
28 |
|
---|
29 |
|
---|
30 | ////////////////////////////////////////////////////////////////////////////////
|
---|
31 | //
|
---|
32 | // AutoCaller* classes
|
---|
33 | //
|
---|
34 | ////////////////////////////////////////////////////////////////////////////////
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Smart class that automatically increases the number of normal (non-limited)
|
---|
39 | * callers of the given VirtualBoxBase object when an instance is constructed
|
---|
40 | * and decreases it back when the created instance goes out of scope (i.e. gets
|
---|
41 | * destroyed).
|
---|
42 | *
|
---|
43 | * If #rc() returns a failure after the instance creation, it means that
|
---|
44 | * the managed VirtualBoxBase object is not Ready, or in any other invalid
|
---|
45 | * state, so that the caller must not use the object and can return this
|
---|
46 | * failed result code to the upper level.
|
---|
47 | *
|
---|
48 | * See ObjectState::addCaller() and ObjectState::releaseCaller() for more
|
---|
49 | * details about object callers.
|
---|
50 | *
|
---|
51 | * A typical usage pattern to declare a normal method of some object (i.e. a
|
---|
52 | * method that is valid only when the object provides its full
|
---|
53 | * functionality) is:
|
---|
54 | * <code>
|
---|
55 | * STDMETHODIMP Component::Foo()
|
---|
56 | * {
|
---|
57 | * AutoCaller autoCaller(this);
|
---|
58 | * HRESULT hrc = autoCaller.rc();
|
---|
59 | * if (SUCCEEDED(hrc))
|
---|
60 | * {
|
---|
61 | * ...
|
---|
62 | * }
|
---|
63 | * return hrc;
|
---|
64 | * }
|
---|
65 | * </code>
|
---|
66 | */
|
---|
67 | class AutoCaller
|
---|
68 | {
|
---|
69 | public:
|
---|
70 | /**
|
---|
71 | * Default constructor. Not terribly useful, but it's valid to create
|
---|
72 | * an instance without associating it with an object. It's a no-op,
|
---|
73 | * like the more useful constructor below when NULL is passed to it.
|
---|
74 | */
|
---|
75 | AutoCaller()
|
---|
76 | {
|
---|
77 | init(NULL, false);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Increases the number of callers of the given object by calling
|
---|
82 | * ObjectState::addCaller() for the corresponding member instance.
|
---|
83 | *
|
---|
84 | * @param aObj Object to add a normal caller to. If NULL, this
|
---|
85 | * instance is effectively turned to no-op (where
|
---|
86 | * rc() will return S_OK).
|
---|
87 | */
|
---|
88 | AutoCaller(VirtualBoxBase *aObj)
|
---|
89 | {
|
---|
90 | init(aObj, false);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * If the number of callers was successfully increased, decreases it
|
---|
95 | * using ObjectState::releaseCaller(), otherwise does nothing.
|
---|
96 | */
|
---|
97 | ~AutoCaller()
|
---|
98 | {
|
---|
99 | if (mObj && SUCCEEDED(mRC))
|
---|
100 | mObj->getObjectState().releaseCaller();
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Returns the stored result code returned by ObjectState::addCaller()
|
---|
105 | * after instance creation or after the last #add() call. A successful
|
---|
106 | * result code means the number of callers was successfully increased.
|
---|
107 | */
|
---|
108 | HRESULT rc() const { return mRC; }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Returns |true| if |SUCCEEDED(rc())| is |true|, for convenience.
|
---|
112 | * |true| means the number of callers was successfully increased.
|
---|
113 | */
|
---|
114 | bool isOk() const { return SUCCEEDED(mRC); }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Temporarily decreases the number of callers of the managed object.
|
---|
118 | * May only be called if #isOk() returns |true|. Note that #rc() will
|
---|
119 | * return E_FAIL after this method succeeds.
|
---|
120 | */
|
---|
121 | void release()
|
---|
122 | {
|
---|
123 | Assert(SUCCEEDED(mRC));
|
---|
124 | if (SUCCEEDED(mRC))
|
---|
125 | {
|
---|
126 | if (mObj)
|
---|
127 | mObj->getObjectState().releaseCaller();
|
---|
128 | mRC = E_FAIL;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Restores the number of callers decreased by #release(). May only be
|
---|
134 | * called after #release().
|
---|
135 | */
|
---|
136 | void add()
|
---|
137 | {
|
---|
138 | Assert(!SUCCEEDED(mRC));
|
---|
139 | if (mObj && !SUCCEEDED(mRC))
|
---|
140 | mRC = mObj->getObjectState().addCaller(mLimited);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Attaches another object to this caller instance.
|
---|
145 | * The previous object's caller is released before the new one is added.
|
---|
146 | *
|
---|
147 | * @param aObj New object to attach, may be @c NULL.
|
---|
148 | */
|
---|
149 | void attach(VirtualBoxBase *aObj)
|
---|
150 | {
|
---|
151 | /* detect simple self-reattachment */
|
---|
152 | if (mObj != aObj)
|
---|
153 | {
|
---|
154 | if (mObj && SUCCEEDED(mRC))
|
---|
155 | release();
|
---|
156 | else if (!mObj)
|
---|
157 | {
|
---|
158 | /* Fix up the success state when nothing is attached. Otherwise
|
---|
159 | * there are a couple of assertion which would trigger. */
|
---|
160 | mRC = E_FAIL;
|
---|
161 | }
|
---|
162 | mObj = aObj;
|
---|
163 | add();
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | /** Verbose equivalent to <tt>attach(NULL)</tt>. */
|
---|
168 | void detach() { attach(NULL); }
|
---|
169 |
|
---|
170 | protected:
|
---|
171 | /**
|
---|
172 | * Internal constructor: Increases the number of callers of the given
|
---|
173 | * object (either normal or limited variant) by calling
|
---|
174 | * ObjectState::addCaller() for the corresponding member instance.
|
---|
175 | *
|
---|
176 | * @param aObj Object to add a caller to. If NULL, this
|
---|
177 | * instance is effectively turned to no-op (where
|
---|
178 | * rc() will return S_OK).
|
---|
179 | * @param aLimited If |false|, then it's a regular caller, otherwise a
|
---|
180 | * limited caller.
|
---|
181 | */
|
---|
182 | void init(VirtualBoxBase *aObj, bool aLimited)
|
---|
183 | {
|
---|
184 | mObj = aObj;
|
---|
185 | mRC = S_OK;
|
---|
186 | mLimited = aLimited;
|
---|
187 | if (mObj)
|
---|
188 | mRC = mObj->getObjectState().addCaller(mLimited);
|
---|
189 | }
|
---|
190 |
|
---|
191 | private:
|
---|
192 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoCaller)
|
---|
193 | DECLARE_CLS_NEW_DELETE_NOOP(AutoCaller)
|
---|
194 |
|
---|
195 | VirtualBoxBase *mObj;
|
---|
196 | HRESULT mRC;
|
---|
197 | bool mLimited;
|
---|
198 | };
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Smart class that automatically increases the number of limited callers of
|
---|
202 | * the given VirtualBoxBase object when an instance is constructed and
|
---|
203 | * decreases it back when the created instance goes out of scope (i.e. gets
|
---|
204 | * destroyed).
|
---|
205 | *
|
---|
206 | * A typical usage pattern to declare a limited method of some object (i.e.
|
---|
207 | * a method that is valid even if the object doesn't provide its full
|
---|
208 | * functionality) is:
|
---|
209 | * <code>
|
---|
210 | * STDMETHODIMP Component::Bar()
|
---|
211 | * {
|
---|
212 | * AutoLimitedCaller autoCaller(this);
|
---|
213 | * HRESULT hrc = autoCaller.rc();
|
---|
214 | * if (SUCCEEDED(hrc))
|
---|
215 | * {
|
---|
216 | * ...
|
---|
217 | * }
|
---|
218 | * return hrc;
|
---|
219 | * </code>
|
---|
220 | *
|
---|
221 | * See AutoCaller for more information about auto caller functionality.
|
---|
222 | */
|
---|
223 | class AutoLimitedCaller : public AutoCaller
|
---|
224 | {
|
---|
225 | public:
|
---|
226 | /**
|
---|
227 | * Default constructor. Not terribly useful, but it's valid to create
|
---|
228 | * an instance without associating it with an object. It's a no-op,
|
---|
229 | * like the more useful constructor below when NULL is passed to it.
|
---|
230 | */
|
---|
231 | AutoLimitedCaller()
|
---|
232 | {
|
---|
233 | AutoCaller::init(NULL, true);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Increases the number of callers of the given object by calling
|
---|
238 | * ObjectState::addCaller() for the corresponding member instance.
|
---|
239 | *
|
---|
240 | * @param aObj Object to add a limited caller to. If NULL, this
|
---|
241 | * instance is effectively turned to no-op (where
|
---|
242 | * rc() will return S_OK).
|
---|
243 | */
|
---|
244 | AutoLimitedCaller(VirtualBoxBase *aObj)
|
---|
245 | {
|
---|
246 | AutoCaller::init(aObj, true);
|
---|
247 | }
|
---|
248 |
|
---|
249 | };
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Smart class to enclose the state transition NotReady->InInit->Ready.
|
---|
253 | *
|
---|
254 | * The purpose of this span is to protect object initialization.
|
---|
255 | *
|
---|
256 | * Instances must be created as a stack-based variable taking |this| pointer
|
---|
257 | * as the argument at the beginning of init() methods of VirtualBoxBase
|
---|
258 | * subclasses. When this variable is created it automatically places the
|
---|
259 | * object to the InInit state.
|
---|
260 | *
|
---|
261 | * When the created variable goes out of scope (i.e. gets destroyed) then,
|
---|
262 | * depending on the result status of this initialization span, it either
|
---|
263 | * places the object to Ready or Limited state or calls the object's
|
---|
264 | * VirtualBoxBase::uninit() method which is supposed to place the object
|
---|
265 | * back to the NotReady state using the AutoUninitSpan class.
|
---|
266 | *
|
---|
267 | * The initial result status of the initialization span is determined by the
|
---|
268 | * @a aResult argument of the AutoInitSpan constructor (Result::Failed by
|
---|
269 | * default). Inside the initialization span, the success status can be set
|
---|
270 | * to Result::Succeeded using #setSucceeded(), to to Result::Limited using
|
---|
271 | * #setLimited() or to Result::Failed using #setFailed(). Please don't
|
---|
272 | * forget to set the correct success status before getting the AutoInitSpan
|
---|
273 | * variable destroyed (for example, by performing an early return from
|
---|
274 | * the init() method)!
|
---|
275 | *
|
---|
276 | * Note that if an instance of this class gets constructed when the object
|
---|
277 | * is in the state other than NotReady, #isOk() returns |false| and methods
|
---|
278 | * of this class do nothing: the state transition is not performed.
|
---|
279 | *
|
---|
280 | * A typical usage pattern is:
|
---|
281 | * <code>
|
---|
282 | * HRESULT Component::init()
|
---|
283 | * {
|
---|
284 | * AutoInitSpan autoInitSpan(this);
|
---|
285 | * AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
286 | * ...
|
---|
287 | * if (FAILED(rc))
|
---|
288 | * return rc;
|
---|
289 | * ...
|
---|
290 | * if (SUCCEEDED(rc))
|
---|
291 | * autoInitSpan.setSucceeded();
|
---|
292 | * return rc;
|
---|
293 | * }
|
---|
294 | * </code>
|
---|
295 | *
|
---|
296 | * @note Never create instances of this class outside init() methods of
|
---|
297 | * VirtualBoxBase subclasses and never pass anything other than |this|
|
---|
298 | * as the argument to the constructor!
|
---|
299 | */
|
---|
300 | class AutoInitSpan
|
---|
301 | {
|
---|
302 | public:
|
---|
303 |
|
---|
304 | enum Result { Failed = 0x0, Succeeded = 0x1, Limited = 0x2 };
|
---|
305 |
|
---|
306 | AutoInitSpan(VirtualBoxBase *aObj, Result aResult = Failed);
|
---|
307 | ~AutoInitSpan();
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Returns |true| if this instance has been created at the right moment
|
---|
311 | * (when the object was in the NotReady state) and |false| otherwise.
|
---|
312 | */
|
---|
313 | bool isOk() const { return mOk; }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Sets the initialization status to Succeeded to indicates successful
|
---|
317 | * initialization. The AutoInitSpan destructor will place the managed
|
---|
318 | * VirtualBoxBase object to the Ready state.
|
---|
319 | */
|
---|
320 | void setSucceeded() { mResult = Succeeded; }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Sets the initialization status to Succeeded to indicate limited
|
---|
324 | * (partly successful) initialization. The AutoInitSpan destructor will
|
---|
325 | * place the managed VirtualBoxBase object to the Limited state.
|
---|
326 | */
|
---|
327 | void setLimited() { mResult = Limited; }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Sets the initialization status to Failure to indicates failed
|
---|
331 | * initialization. The AutoInitSpan destructor will place the managed
|
---|
332 | * VirtualBoxBase object to the InitFailed state and will automatically
|
---|
333 | * call its uninit() method which is supposed to place the object back
|
---|
334 | * to the NotReady state using AutoUninitSpan.
|
---|
335 | */
|
---|
336 | void setFailed() { mResult = Failed; }
|
---|
337 |
|
---|
338 | /** Returns the current initialization result. */
|
---|
339 | Result result() { return mResult; }
|
---|
340 |
|
---|
341 | private:
|
---|
342 |
|
---|
343 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoInitSpan)
|
---|
344 | DECLARE_CLS_NEW_DELETE_NOOP(AutoInitSpan)
|
---|
345 |
|
---|
346 | VirtualBoxBase *mObj;
|
---|
347 | Result mResult : 3; // must be at least total number of bits + 1 (sign)
|
---|
348 | bool mOk : 1;
|
---|
349 | };
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * Smart class to enclose the state transition Limited->InInit->Ready.
|
---|
353 | *
|
---|
354 | * The purpose of this span is to protect object re-initialization.
|
---|
355 | *
|
---|
356 | * Instances must be created as a stack-based variable taking |this| pointer
|
---|
357 | * as the argument at the beginning of methods of VirtualBoxBase
|
---|
358 | * subclasses that try to re-initialize the object to bring it to the Ready
|
---|
359 | * state (full functionality) after partial initialization (limited
|
---|
360 | * functionality). When this variable is created, it automatically places
|
---|
361 | * the object to the InInit state.
|
---|
362 | *
|
---|
363 | * When the created variable goes out of scope (i.e. gets destroyed),
|
---|
364 | * depending on the success status of this initialization span, it either
|
---|
365 | * places the object to the Ready state or brings it back to the Limited
|
---|
366 | * state.
|
---|
367 | *
|
---|
368 | * The initial success status of the re-initialization span is |false|. In
|
---|
369 | * order to make it successful, #setSucceeded() must be called before the
|
---|
370 | * instance is destroyed.
|
---|
371 | *
|
---|
372 | * Note that if an instance of this class gets constructed when the object
|
---|
373 | * is in the state other than Limited, #isOk() returns |false| and methods
|
---|
374 | * of this class do nothing: the state transition is not performed.
|
---|
375 | *
|
---|
376 | * A typical usage pattern is:
|
---|
377 | * <code>
|
---|
378 | * HRESULT Component::reinit()
|
---|
379 | * {
|
---|
380 | * AutoReinitSpan autoReinitSpan(this);
|
---|
381 | * AssertReturn(autoReinitSpan.isOk(), E_FAIL);
|
---|
382 | * ...
|
---|
383 | * if (FAILED(rc))
|
---|
384 | * return rc;
|
---|
385 | * ...
|
---|
386 | * if (SUCCEEDED(rc))
|
---|
387 | * autoReinitSpan.setSucceeded();
|
---|
388 | * return rc;
|
---|
389 | * }
|
---|
390 | * </code>
|
---|
391 | *
|
---|
392 | * @note Never create instances of this class outside re-initialization
|
---|
393 | * methods of VirtualBoxBase subclasses and never pass anything other than
|
---|
394 | * |this| as the argument to the constructor!
|
---|
395 | */
|
---|
396 | class AutoReinitSpan
|
---|
397 | {
|
---|
398 | public:
|
---|
399 |
|
---|
400 | AutoReinitSpan(VirtualBoxBase *aObj);
|
---|
401 | ~AutoReinitSpan();
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Returns |true| if this instance has been created at the right moment
|
---|
405 | * (when the object was in the Limited state) and |false| otherwise.
|
---|
406 | */
|
---|
407 | bool isOk() const { return mOk; }
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Sets the re-initialization status to Succeeded to indicates
|
---|
411 | * successful re-initialization. The AutoReinitSpan destructor will place
|
---|
412 | * the managed VirtualBoxBase object to the Ready state.
|
---|
413 | */
|
---|
414 | void setSucceeded() { mSucceeded = true; }
|
---|
415 |
|
---|
416 | private:
|
---|
417 |
|
---|
418 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoReinitSpan)
|
---|
419 | DECLARE_CLS_NEW_DELETE_NOOP(AutoReinitSpan)
|
---|
420 |
|
---|
421 | VirtualBoxBase *mObj;
|
---|
422 | bool mSucceeded : 1;
|
---|
423 | bool mOk : 1;
|
---|
424 | };
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Smart class to enclose the state transition Ready->InUninit->NotReady,
|
---|
428 | * InitFailed->InUninit->NotReady.
|
---|
429 | *
|
---|
430 | * The purpose of this span is to protect object uninitialization.
|
---|
431 | *
|
---|
432 | * Instances must be created as a stack-based variable taking |this| pointer
|
---|
433 | * as the argument at the beginning of uninit() methods of VirtualBoxBase
|
---|
434 | * subclasses. When this variable is created it automatically places the
|
---|
435 | * object to the InUninit state, unless it is already in the NotReady state
|
---|
436 | * as indicated by #uninitDone() returning |true|. In the latter case, the
|
---|
437 | * uninit() method must immediately return because there should be nothing
|
---|
438 | * to uninitialize.
|
---|
439 | *
|
---|
440 | * When this variable goes out of scope (i.e. gets destroyed), it places the
|
---|
441 | * object to NotReady state.
|
---|
442 | *
|
---|
443 | * A typical usage pattern is:
|
---|
444 | * <code>
|
---|
445 | * void Component::uninit()
|
---|
446 | * {
|
---|
447 | * AutoUninitSpan autoUninitSpan(this);
|
---|
448 | * if (autoUninitSpan.uninitDone())
|
---|
449 | * return;
|
---|
450 | * ...
|
---|
451 | * }
|
---|
452 | * </code>
|
---|
453 | *
|
---|
454 | * @note The constructor of this class blocks the current thread execution
|
---|
455 | * until the number of callers added to the object using
|
---|
456 | * ObjectState::addCaller() or AutoCaller drops to zero. For this reason,
|
---|
457 | * it is forbidden to create instances of this class (or call uninit())
|
---|
458 | * within the AutoCaller or ObjectState::addCaller() scope because it is
|
---|
459 | * a guaranteed deadlock.
|
---|
460 | *
|
---|
461 | * @note Never create instances of this class outside uninit() methods and
|
---|
462 | * never pass anything other than |this| as the argument to the
|
---|
463 | * constructor!
|
---|
464 | */
|
---|
465 | class AutoUninitSpan
|
---|
466 | {
|
---|
467 | public:
|
---|
468 |
|
---|
469 | AutoUninitSpan(VirtualBoxBase *aObj);
|
---|
470 | ~AutoUninitSpan();
|
---|
471 |
|
---|
472 | /** |true| when uninit() is called as a result of init() failure */
|
---|
473 | bool initFailed() { return mInitFailed; }
|
---|
474 |
|
---|
475 | /** |true| when uninit() has already been called (so the object is NotReady) */
|
---|
476 | bool uninitDone() { return mUninitDone; }
|
---|
477 |
|
---|
478 | void setSucceeded();
|
---|
479 |
|
---|
480 | private:
|
---|
481 |
|
---|
482 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoUninitSpan)
|
---|
483 | DECLARE_CLS_NEW_DELETE_NOOP(AutoUninitSpan)
|
---|
484 |
|
---|
485 | VirtualBoxBase *mObj;
|
---|
486 | bool mInitFailed : 1;
|
---|
487 | bool mUninitDone : 1;
|
---|
488 | };
|
---|
489 |
|
---|
490 | #endif // !____H_AUTOCALLER
|
---|