VirtualBox

Ignore:
Timestamp:
Jan 26, 2010 2:06:05 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
56959
Message:

Main: move Auto*StateDependency templates out of MachineImpl.h

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/include/MachineImpl.h

    r26044 r26046  
    9494
    9595    enum InitMode { Init_New, Init_Import, Init_Registered };
     96
     97    enum StateDependency
     98    {
     99        AnyStateDep = 0, MutableStateDep, MutableOrSavedStateDep
     100    };
    96101
    97102    /**
     
    334339        AttachmentList mAttachments;
    335340    };
    336 
    337     enum StateDependency
    338     {
    339         AnyStateDep = 0, MutableStateDep, MutableOrSavedStateDep
    340     };
    341 
    342     /**
    343      *  Helper class that safely manages the machine state dependency by
    344      *  calling Machine::addStateDependency() on construction and
    345      *  Machine::releaseStateDependency() on destruction. Intended for Machine
    346      *  children. The usage pattern is:
    347      *
    348      *  @code
    349      *      AutoCaller autoCaller(this);
    350      *      if (FAILED(autoCaller.rc())) return autoCaller.rc();
    351      *
    352      *      Machine::AutoStateDependency<MutableStateDep> adep(mParent);
    353      *      if (FAILED(stateDep.rc())) return stateDep.rc();
    354      *      ...
    355      *      // code that depends on the particular machine state
    356      *      ...
    357      *  @endcode
    358      *
    359      *  Note that it is more convenient to use the following individual
    360      *  shortcut classes instead of using this template directly:
    361      *  AutoAnyStateDependency, AutoMutableStateDependency and
    362      *  AutoMutableOrSavedStateDependency. The usage pattern is exactly the
    363      *  same as above except that there is no need to specify the template
    364      *  argument because it is already done by the shortcut class.
    365      *
    366      *  @param taDepType    Dependecy type to manage.
    367      */
    368     template <StateDependency taDepType = AnyStateDep>
    369     class AutoStateDependency
    370     {
    371     public:
    372 
    373         AutoStateDependency(Machine *aThat)
    374             : mThat(aThat), mRC(S_OK)
    375             , mMachineState(MachineState_Null)
    376             , mRegistered(FALSE)
    377         {
    378             Assert(aThat);
    379             mRC = aThat->addStateDependency(taDepType, &mMachineState,
    380                                             &mRegistered);
    381         }
    382         ~AutoStateDependency()
    383         {
    384             if (SUCCEEDED(mRC))
    385                 mThat->releaseStateDependency();
    386         }
    387 
    388         /** Decreases the number of dependencies before the instance is
    389          *  destroyed. Note that will reset #rc() to E_FAIL. */
    390         void release()
    391         {
    392             AssertReturnVoid(SUCCEEDED(mRC));
    393             mThat->releaseStateDependency();
    394             mRC = E_FAIL;
    395         }
    396 
    397         /** Restores the number of callers after by #release(). #rc() will be
    398          *  reset to the result of calling addStateDependency() and must be
    399          *  rechecked to ensure the operation succeeded. */
    400         void add()
    401         {
    402             AssertReturnVoid(!SUCCEEDED(mRC));
    403             mRC = mThat->addStateDependency(taDepType, &mMachineState,
    404                                             &mRegistered);
    405         }
    406 
    407         /** Returns the result of Machine::addStateDependency(). */
    408         HRESULT rc() const { return mRC; }
    409 
    410         /** Shortcut to SUCCEEDED(rc()). */
    411         bool isOk() const { return SUCCEEDED(mRC); }
    412 
    413         /** Returns the machine state value as returned by
    414          *  Machine::addStateDependency(). */
    415         MachineState_T machineState() const { return mMachineState; }
    416 
    417         /** Returns the machine state value as returned by
    418          *  Machine::addStateDependency(). */
    419         BOOL machineRegistered() const { return mRegistered; }
    420 
    421     protected:
    422 
    423         Machine *mThat;
    424         HRESULT mRC;
    425         MachineState_T mMachineState;
    426         BOOL mRegistered;
    427 
    428     private:
    429 
    430         DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(AutoStateDependency)
    431         DECLARE_CLS_NEW_DELETE_NOOP(AutoStateDependency)
    432     };
    433 
    434     /**
    435      *  Shortcut to AutoStateDependency<AnyStateDep>.
    436      *  See AutoStateDependency to get the usage pattern.
    437      *
    438      *  Accepts any machine state and guarantees the state won't change before
    439      *  this object is destroyed. If the machine state cannot be protected (as
    440      *  a result of the state change currently in progress), this instance's
    441      *  #rc() method will indicate a failure, and the caller is not allowed to
    442      *  rely on any particular machine state and should return the failed
    443      *  result code to the upper level.
    444      */
    445     typedef AutoStateDependency<AnyStateDep> AutoAnyStateDependency;
    446 
    447     /**
    448      *  Shortcut to AutoStateDependency<MutableStateDep>.
    449      *  See AutoStateDependency to get the usage pattern.
    450      *
    451      *  Succeeds only if the machine state is in one of the mutable states, and
    452      *  guarantees the given mutable state won't change before this object is
    453      *  destroyed. If the machine is not mutable, this instance's #rc() method
    454      *  will indicate a failure, and the caller is not allowed to rely on any
    455      *  particular machine state and should return the failed result code to
    456      *  the upper level.
    457      *
    458      *  Intended to be used within all setter methods of IMachine
    459      *  children objects (DVDDrive, NetworkAdapter, AudioAdapter, etc.) to
    460      *  provide data protection and consistency.
    461      */
    462     typedef AutoStateDependency<MutableStateDep> AutoMutableStateDependency;
    463 
    464     /**
    465      *  Shortcut to AutoStateDependency<MutableOrSavedStateDep>.
    466      *  See AutoStateDependency to get the usage pattern.
    467      *
    468      *  Succeeds only if the machine state is in one of the mutable states, or
    469      *  if the machine is in the Saved state, and guarantees the given mutable
    470      *  state won't change before this object is destroyed. If the machine is
    471      *  not mutable, this instance's #rc() method will indicate a failure, and
    472      *  the caller is not allowed to rely on any particular machine state and
    473      *  should return the failed result code to the upper level.
    474      *
    475      *  Intended to be used within setter methods of IMachine
    476      *  children objects that may also operate on Saved machines.
    477      */
    478     typedef AutoStateDependency<MutableOrSavedStateDep> AutoMutableOrSavedStateDependency;
    479 
    480341
    481342    VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(Machine)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette