VirtualBox

source: vbox/trunk/src/VBox/Main/include/AutoStateDep.h@ 26088

Last change on this file since 26088 was 26088, checked in by vboxsync, 15 years ago

Main: debugging notes, coding style

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1#ifndef ____H_AUTOSTATEDEP
2#define ____H_AUTOSTATEDEP
3
4/** @file
5 *
6 * AutoStateDep template classes, formerly in MachineImpl.h. Use these if
7 * you need to ensure that the machine state does not change over a certain
8 * period of time.
9 */
10
11/*
12 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
13 *
14 * This file is part of VirtualBox Open Source Edition (OSE), as
15 * available from http://www.virtualbox.org. This file is free software;
16 * you can redistribute it and/or modify it under the terms of the GNU
17 * General Public License (GPL) as published by the Free Software
18 * Foundation, in version 2 as it comes in the "COPYING" file of the
19 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
20 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23 * Clara, CA 95054 USA or visit http://www.sun.com if you need
24 * additional information or have any questions.
25 */
26
27 /**
28 * Helper class that safely manages the machine state dependency by
29 * calling Machine::addStateDependency() on construction and
30 * Machine::releaseStateDependency() on destruction. Intended for Machine
31 * children. The usage pattern is:
32 *
33 * @code
34 * AutoCaller autoCaller(this);
35 * if (FAILED(autoCaller.rc())) return autoCaller.rc();
36 *
37 * Machine::AutoStateDependency<MutableStateDep> adep(mParent);
38 * if (FAILED(stateDep.rc())) return stateDep.rc();
39 * ...
40 * // code that depends on the particular machine state
41 * ...
42 * @endcode
43 *
44 * Note that it is more convenient to use the following individual
45 * shortcut classes instead of using this template directly:
46 * AutoAnyStateDependency, AutoMutableStateDependency and
47 * AutoMutableOrSavedStateDependency. The usage pattern is exactly the
48 * same as above except that there is no need to specify the template
49 * argument because it is already done by the shortcut class.
50 *
51 * @param taDepType Dependecy type to manage.
52 */
53 template <Machine::StateDependency taDepType = Machine::AnyStateDep>
54 class AutoStateDependency
55 {
56 public:
57
58 AutoStateDependency(Machine *aThat)
59 : mThat(aThat), mRC(S_OK),
60 mMachineState(MachineState_Null),
61 mRegistered(FALSE)
62 {
63 Assert(aThat);
64 mRC = aThat->addStateDependency(taDepType, &mMachineState,
65 &mRegistered);
66 }
67 ~AutoStateDependency()
68 {
69 if (SUCCEEDED(mRC))
70 mThat->releaseStateDependency();
71 }
72
73 /** Decreases the number of dependencies before the instance is
74 * destroyed. Note that will reset #rc() to E_FAIL. */
75 void release()
76 {
77 AssertReturnVoid(SUCCEEDED(mRC));
78 mThat->releaseStateDependency();
79 mRC = E_FAIL;
80 }
81
82 /** Restores the number of callers after by #release(). #rc() will be
83 * reset to the result of calling addStateDependency() and must be
84 * rechecked to ensure the operation succeeded. */
85 void add()
86 {
87 AssertReturnVoid(!SUCCEEDED(mRC));
88 mRC = mThat->addStateDependency(taDepType, &mMachineState,
89 &mRegistered);
90 }
91
92 /** Returns the result of Machine::addStateDependency(). */
93 HRESULT rc() const { return mRC; }
94
95 /** Shortcut to SUCCEEDED(rc()). */
96 bool isOk() const { return SUCCEEDED(mRC); }
97
98 /** Returns the machine state value as returned by
99 * Machine::addStateDependency(). */
100 MachineState_T machineState() const { return mMachineState; }
101
102 /** Returns the machine state value as returned by
103 * Machine::addStateDependency(). */
104 BOOL machineRegistered() const { return mRegistered; }
105
106 protected:
107
108 Machine *mThat;
109 HRESULT mRC;
110 MachineState_T mMachineState;
111 BOOL mRegistered;
112
113 private:
114
115 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoStateDependency)
116 DECLARE_CLS_NEW_DELETE_NOOP(AutoStateDependency)
117 };
118
119 /**
120 * Shortcut to AutoStateDependency<AnyStateDep>.
121 * See AutoStateDependency to get the usage pattern.
122 *
123 * Accepts any machine state and guarantees the state won't change before
124 * this object is destroyed. If the machine state cannot be protected (as
125 * a result of the state change currently in progress), this instance's
126 * #rc() method will indicate a failure, and the caller is not allowed to
127 * rely on any particular machine state and should return the failed
128 * result code to the upper level.
129 */
130 typedef AutoStateDependency<Machine::AnyStateDep> AutoAnyStateDependency;
131
132 /**
133 * Shortcut to AutoStateDependency<MutableStateDep>.
134 * See AutoStateDependency to get the usage pattern.
135 *
136 * Succeeds only if the machine state is in one of the mutable states, and
137 * guarantees the given mutable state won't change before this object is
138 * destroyed. If the machine is not mutable, this instance's #rc() method
139 * will indicate a failure, and the caller is not allowed to rely on any
140 * particular machine state and should return the failed result code to
141 * the upper level.
142 *
143 * Intended to be used within all setter methods of IMachine
144 * children objects (DVDDrive, NetworkAdapter, AudioAdapter, etc.) to
145 * provide data protection and consistency.
146 */
147 typedef AutoStateDependency<Machine::MutableStateDep> AutoMutableStateDependency;
148
149 /**
150 * Shortcut to AutoStateDependency<MutableOrSavedStateDep>.
151 * See AutoStateDependency to get the usage pattern.
152 *
153 * Succeeds only if the machine state is in one of the mutable states, or
154 * if the machine is in the Saved state, and guarantees the given mutable
155 * state won't change before this object is destroyed. If the machine is
156 * not mutable, this instance's #rc() method will indicate a failure, and
157 * the caller is not allowed to rely on any particular machine state and
158 * should return the failed result code to the upper level.
159 *
160 * Intended to be used within setter methods of IMachine
161 * children objects that may also operate on Saved machines.
162 */
163 typedef AutoStateDependency<Machine::MutableOrSavedStateDep> AutoMutableOrSavedStateDependency;
164
165#endif // ____H_AUTOSTATEDEP
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