VirtualBox

source: vbox/trunk/src/VBox/Main/include/AutoCaller.h@ 61644

Last change on this file since 61644 was 59996, checked in by vboxsync, 9 years ago

Main/VirtualBox: postpone the error reporting from VirtualBox object creation to the method calls of the object. COM loses the error (replaces it by REGDB_E_CLASSNOTREG), making troubleshooting very difficult. XPCOM wouldn't need this, but it is applied everywhere for maximum consistency. Many changes elsewhere to propagate the information correctly, and also fixes many outdated comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.6 KB
Line 
1/* $Id: AutoCaller.h 59996 2016-03-11 15:27:55Z vboxsync $ */
2/** @file
3 *
4 * VirtualBox object caller handling definitions
5 */
6
7/*
8 * Copyright (C) 2006-2016 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.
27class 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 */
67class AutoCaller
68{
69public:
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
170protected:
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
191private:
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 */
223class AutoLimitedCaller : public AutoCaller
224{
225public:
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 */
300class AutoInitSpan
301{
302public:
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(HRESULT rc = E_ACCESSDENIED)
337 {
338 mResult = Failed;
339 mFailedRC = rc;
340 mpFailedEI = new ErrorInfo();
341 }
342
343 /** Returns the current initialization result. */
344 Result result() { return mResult; }
345
346private:
347
348 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoInitSpan)
349 DECLARE_CLS_NEW_DELETE_NOOP(AutoInitSpan)
350
351 VirtualBoxBase *mObj;
352 Result mResult : 3; // must be at least total number of bits + 1 (sign)
353 bool mOk : 1;
354 HRESULT mFailedRC;
355 ErrorInfo *mpFailedEI;
356};
357
358/**
359 * Smart class to enclose the state transition Limited->InInit->Ready.
360 *
361 * The purpose of this span is to protect object re-initialization.
362 *
363 * Instances must be created as a stack-based variable taking |this| pointer
364 * as the argument at the beginning of methods of VirtualBoxBase
365 * subclasses that try to re-initialize the object to bring it to the Ready
366 * state (full functionality) after partial initialization (limited
367 * functionality). When this variable is created, it automatically places
368 * the object to the InInit state.
369 *
370 * When the created variable goes out of scope (i.e. gets destroyed),
371 * depending on the success status of this initialization span, it either
372 * places the object to the Ready state or brings it back to the Limited
373 * state.
374 *
375 * The initial success status of the re-initialization span is |false|. In
376 * order to make it successful, #setSucceeded() must be called before the
377 * instance is destroyed.
378 *
379 * Note that if an instance of this class gets constructed when the object
380 * is in the state other than Limited, #isOk() returns |false| and methods
381 * of this class do nothing: the state transition is not performed.
382 *
383 * A typical usage pattern is:
384 * <code>
385 * HRESULT Component::reinit()
386 * {
387 * AutoReinitSpan autoReinitSpan(this);
388 * AssertReturn(autoReinitSpan.isOk(), E_FAIL);
389 * ...
390 * if (FAILED(rc))
391 * return rc;
392 * ...
393 * if (SUCCEEDED(rc))
394 * autoReinitSpan.setSucceeded();
395 * return rc;
396 * }
397 * </code>
398 *
399 * @note Never create instances of this class outside re-initialization
400 * methods of VirtualBoxBase subclasses and never pass anything other than
401 * |this| as the argument to the constructor!
402 */
403class AutoReinitSpan
404{
405public:
406
407 AutoReinitSpan(VirtualBoxBase *aObj);
408 ~AutoReinitSpan();
409
410 /**
411 * Returns |true| if this instance has been created at the right moment
412 * (when the object was in the Limited state) and |false| otherwise.
413 */
414 bool isOk() const { return mOk; }
415
416 /**
417 * Sets the re-initialization status to Succeeded to indicates
418 * successful re-initialization. The AutoReinitSpan destructor will place
419 * the managed VirtualBoxBase object to the Ready state.
420 */
421 void setSucceeded() { mSucceeded = true; }
422
423private:
424
425 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoReinitSpan)
426 DECLARE_CLS_NEW_DELETE_NOOP(AutoReinitSpan)
427
428 VirtualBoxBase *mObj;
429 bool mSucceeded : 1;
430 bool mOk : 1;
431};
432
433/**
434 * Smart class to enclose the state transition Ready->InUninit->NotReady,
435 * InitFailed->InUninit->NotReady.
436 *
437 * The purpose of this span is to protect object uninitialization.
438 *
439 * Instances must be created as a stack-based variable taking |this| pointer
440 * as the argument at the beginning of uninit() methods of VirtualBoxBase
441 * subclasses. When this variable is created it automatically places the
442 * object to the InUninit state, unless it is already in the NotReady state
443 * as indicated by #uninitDone() returning |true|. In the latter case, the
444 * uninit() method must immediately return because there should be nothing
445 * to uninitialize.
446 *
447 * When this variable goes out of scope (i.e. gets destroyed), it places the
448 * object to NotReady state.
449 *
450 * A typical usage pattern is:
451 * <code>
452 * void Component::uninit()
453 * {
454 * AutoUninitSpan autoUninitSpan(this);
455 * if (autoUninitSpan.uninitDone())
456 * return;
457 * ...
458 * }
459 * </code>
460 *
461 * @note The constructor of this class blocks the current thread execution
462 * until the number of callers added to the object using
463 * ObjectState::addCaller() or AutoCaller drops to zero. For this reason,
464 * it is forbidden to create instances of this class (or call uninit())
465 * within the AutoCaller or ObjectState::addCaller() scope because it is
466 * a guaranteed deadlock.
467 *
468 * @note Never create instances of this class outside uninit() methods and
469 * never pass anything other than |this| as the argument to the
470 * constructor!
471 */
472class AutoUninitSpan
473{
474public:
475
476 AutoUninitSpan(VirtualBoxBase *aObj);
477 ~AutoUninitSpan();
478
479 /** |true| when uninit() is called as a result of init() failure */
480 bool initFailed() { return mInitFailed; }
481
482 /** |true| when uninit() has already been called (so the object is NotReady) */
483 bool uninitDone() { return mUninitDone; }
484
485 void setSucceeded();
486
487private:
488
489 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoUninitSpan)
490 DECLARE_CLS_NEW_DELETE_NOOP(AutoUninitSpan)
491
492 VirtualBoxBase *mObj;
493 bool mInitFailed : 1;
494 bool mUninitDone : 1;
495};
496
497#endif // !____H_AUTOCALLER
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