VirtualBox

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

Last change on this file since 98262 was 98262, checked in by vboxsync, 17 months ago

Main: rc() -> hrc()/vrc(). bugref:10223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/* $Id: AutoStateDep.h 98262 2023-01-24 01:42:14Z vboxsync $ */
2
3#ifndef MAIN_INCLUDED_AutoStateDep_h
4#define MAIN_INCLUDED_AutoStateDep_h
5#ifndef RT_WITHOUT_PRAGMA_ONCE
6# pragma once
7#endif
8
9/** @file
10 *
11 * AutoStateDep template classes, formerly in MachineImpl.h. Use these if
12 * you need to ensure that the machine state does not change over a certain
13 * period of time.
14 */
15
16/*
17 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
18 *
19 * This file is part of VirtualBox base platform packages, as
20 * available from https://www.virtualbox.org.
21 *
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation, in version 3 of the
25 * License.
26 *
27 * This program is distributed in the hope that it will be useful, but
28 * WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, see <https://www.gnu.org/licenses>.
34 *
35 * SPDX-License-Identifier: GPL-3.0-only
36 */
37
38 /**
39 * Helper class that safely manages the machine state dependency by
40 * calling Machine::addStateDependency() on construction and
41 * Machine::releaseStateDependency() on destruction. Intended for Machine
42 * children. The usage pattern is:
43 *
44 * @code
45 * AutoCaller autoCaller(this);
46 * if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
47 *
48 * Machine::AutoStateDependency<MutableStateDep> adep(mParent);
49 * if (FAILED(stateDep.hrc())) return stateDep.hrc();
50 * ...
51 * // code that depends on the particular machine state
52 * ...
53 * @endcode
54 *
55 * Note that it is more convenient to use the following individual
56 * shortcut classes instead of using this template directly:
57 * AutoAnyStateDependency, AutoMutableStateDependency,
58 * AutoMutableOrSavedStateDependency, AutoMutableOrRunningStateDependency
59 * or AutoMutableOrSavedOrRunningStateDependency. The usage pattern is
60 * exactly the same as above except that there is no need to specify the
61 * template argument because it is already done by the shortcut class.
62 *
63 * @param taDepType Dependency type to manage.
64 */
65 template <Machine::StateDependency taDepType = Machine::AnyStateDep>
66 class AutoStateDependency
67 {
68 public:
69
70 AutoStateDependency(Machine *aThat)
71 : mThat(aThat), mRC(S_OK),
72 mMachineState(MachineState_Null),
73 mRegistered(FALSE)
74 {
75 Assert(aThat);
76 mRC = aThat->i_addStateDependency(taDepType, &mMachineState,
77 &mRegistered);
78 }
79 ~AutoStateDependency()
80 {
81 if (SUCCEEDED(mRC))
82 mThat->i_releaseStateDependency();
83 }
84
85 /** Decreases the number of dependencies before the instance is
86 * destroyed. Note that will reset #rc() to E_FAIL. */
87 void release()
88 {
89 AssertReturnVoid(SUCCEEDED(mRC));
90 mThat->i_releaseStateDependency();
91 mRC = E_FAIL;
92 }
93
94 /** Restores the number of callers after by #release(). #rc() will be
95 * reset to the result of calling addStateDependency() and must be
96 * rechecked to ensure the operation succeeded. */
97 void add()
98 {
99 AssertReturnVoid(!SUCCEEDED(mRC));
100 mRC = mThat->i_addStateDependency(taDepType, &mMachineState,
101 &mRegistered);
102 }
103
104 /** Returns the result of Machine::addStateDependency(). */
105 HRESULT hrc() const { return mRC; }
106 /** Returns the result of Machine::addStateDependency().
107 * @deprecated Use #hrc() instead. */
108 HRESULT rc() const { return mRC; }
109
110 /** Shortcut to SUCCEEDED(hrc()). */
111 bool isOk() const { return SUCCEEDED(mRC); }
112
113 /** Returns the machine state value as returned by
114 * Machine::addStateDependency(). */
115 MachineState_T machineState() const { return mMachineState; }
116
117 /** Returns the machine state value as returned by
118 * Machine::addStateDependency(). */
119 BOOL machineRegistered() const { return mRegistered; }
120
121 protected:
122
123 Machine *mThat;
124 HRESULT mRC;
125 MachineState_T mMachineState;
126 BOOL mRegistered;
127
128 private:
129
130 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoStateDependency);
131 DECLARE_CLS_NEW_DELETE_NOOP(AutoStateDependency);
132 };
133
134 /**
135 * Shortcut to AutoStateDependency<AnyStateDep>.
136 * See AutoStateDependency to get the usage pattern.
137 *
138 * Accepts any machine state and guarantees the state won't change before
139 * this object is destroyed. If the machine state cannot be protected (as
140 * a result of the state change currently in progress), this instance's
141 * #hrc() method will indicate a failure, and the caller is not allowed to
142 * rely on any particular machine state and should return the failed
143 * result code to the upper level.
144 */
145 typedef AutoStateDependency<Machine::AnyStateDep> AutoAnyStateDependency;
146
147 /**
148 * Shortcut to AutoStateDependency<MutableStateDep>.
149 * See AutoStateDependency to get the usage pattern.
150 *
151 * Succeeds only if the machine state is in one of the mutable states, and
152 * guarantees the given mutable state won't change before this object is
153 * destroyed. If the machine is not mutable, this instance's #hrc() method
154 * will indicate a failure, and the caller is not allowed to rely on any
155 * particular machine state and should return the failed result code to
156 * the upper level.
157 *
158 * Intended to be used within all setter methods of IMachine
159 * children objects (DVDDrive, NetworkAdapter, AudioAdapter, etc.) to
160 * provide data protection and consistency. There must be no VM process,
161 * i.e. use for settings changes which are valid when the VM is shut down.
162 */
163 typedef AutoStateDependency<Machine::MutableStateDep> AutoMutableStateDependency;
164
165 /**
166 * Shortcut to AutoStateDependency<MutableOrSavedStateDep>.
167 * See AutoStateDependency to get the usage pattern.
168 *
169 * Succeeds only if the machine state is in one of the mutable states, or
170 * if the machine is in the Saved state, and guarantees the given mutable
171 * state won't change before this object is destroyed. If the machine is
172 * not mutable, this instance's #hrc() method will indicate a failure, and
173 * the caller is not allowed to rely on any particular machine state and
174 * should return the failed result code to the upper level.
175 *
176 * Intended to be used within setter methods of IMachine
177 * children objects that may operate on shut down or Saved machines.
178 */
179 typedef AutoStateDependency<Machine::MutableOrSavedStateDep> AutoMutableOrSavedStateDependency;
180
181 /**
182 * Shortcut to AutoStateDependency<MutableOrRunningStateDep>.
183 * See AutoStateDependency to get the usage pattern.
184 *
185 * Succeeds only if the machine state is in one of the mutable states, or
186 * if the machine is in the Running or Paused state, and guarantees the
187 * given mutable state won't change before this object is destroyed. If
188 * the machine is not mutable, this instance's #hrc() method will indicate
189 * a failure, and the caller is not allowed to rely on any particular
190 * machine state and should return the failed result code to the upper
191 * level.
192 *
193 * Intended to be used within setter methods of IMachine
194 * children objects that may operate on shut down or running machines.
195 */
196 typedef AutoStateDependency<Machine::MutableOrRunningStateDep> AutoMutableOrRunningStateDependency;
197
198 /**
199 * Shortcut to AutoStateDependency<MutableOrSavedOrRunningStateDep>.
200 * See AutoStateDependency to get the usage pattern.
201 *
202 * Succeeds only if the machine state is in one of the mutable states, or
203 * if the machine is in the Running, Paused or Saved state, and guarantees
204 * the given mutable state won't change before this object is destroyed.
205 * If the machine is not mutable, this instance's #hrc() method will
206 * indicate a failure, and the caller is not allowed to rely on any
207 * particular machine state and should return the failed result code to
208 * the upper level.
209 *
210 * Intended to be used within setter methods of IMachine
211 * children objects that may operate on shut down, running or saved
212 * machines.
213 */
214 typedef AutoStateDependency<Machine::MutableOrSavedOrRunningStateDep> AutoMutableOrSavedOrRunningStateDependency;
215
216#endif /* !MAIN_INCLUDED_AutoStateDep_h */
217
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use