VariableGuard< T > Class Template Reference

This reference page is linked to from the following overview topics: Exception Safety.



Detailed Description

template<typename T>
class MaxSDK::VariableGuard< T >

Resets a variable when the object goes out of scope.

Following the principle of resource acquisition is allocation, this class, which should always be allocated on the stack, will set a variable to the given value, then set the variable back to its original value when it goes out of score. This is safer than setting things by hand since the destructor will still be executed if an exception is thrown from the guarded block. An example given a member variable mRecursionLocked:

 void SomeClass::SomeFunction() {
   if (!mRecursionLocked) {
     VariableGuard<bool> guard(mRecursionLocked, true);
     // some possibly recursive code...
   }
 } 

#include <VariableGuard.h>

Inheritance diagram for VariableGuard< T >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

  VariableGuard (T &variable)
  Guards the given variable, restoring its value when this object instance goes out of scope.
  VariableGuard (T &variable, T value)
  Guards the given variable, and sets it to the specified value.
  ~VariableGuard ()
  Restores the variable to its original value.
GetCurrentValue () const
  Retrieves the current value of the guarded variable.
void  SetCurrentValue (T value)
  Sets the variable to the specified value.

Constructor & Destructor Documentation

VariableGuard ( T &  variable ) [inline]

Guards the given variable, restoring its value when this object instance goes out of scope.

Parameters:
variable - the variable to be guarded (whose value needs to be restored)
                                   :
                mVariable(variable),
                    mOriginalValue(variable)
                { }
VariableGuard ( T &  variable,
value 
) [inline]

Guards the given variable, and sets it to the specified value.

The variable is restored to its original value when this object instance goes out of scope.

Parameters:
variable - the variable to be guarded (whose value needs to be restored)
value - the new value the variable should be set to right away
                                                    :
                mVariable(variable),
                    mOriginalValue(variable)
                {
                    SetCurrentValue(value);
                }
~VariableGuard ( ) [inline]

Restores the variable to its original value.

                {
                    mVariable = mOriginalValue;
                }

Member Function Documentation

T GetCurrentValue ( ) const [inline]

Retrieves the current value of the guarded variable.

                {
                    return mVariable;
                }
void SetCurrentValue ( value ) [inline]

Sets the variable to the specified value.

Upon this guard's destruction, the variable will be reset to the original value found during the guard's construction.

Parameters:
value - the new value the variable should be set to
                {
                    mVariable = value;
                }