3ds Max C++ API Reference
VariableGuard< T > Class Template Reference

Resets a variable when the object goes out of scope. More...

#include <VariableGuard.h>

+ Inheritance diagram for VariableGuard< T >:

Public Member Functions

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

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...
}
}

Constructor & Destructor Documentation

◆ VariableGuard() [1/2]

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)
45  :
46  mVariable(variable),
47  mOriginalValue(variable)
48  { }

◆ VariableGuard() [2/2]

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
56  :
57  mVariable(variable),
58  mOriginalValue(variable)
59  {
60  SetCurrentValue(value);
61  }
void SetCurrentValue(T value)
Sets the variable to the specified value.
Definition: VariableGuard.h:83

◆ ~VariableGuard()

~VariableGuard ( )
inline

Restores the variable to its original value.

66  {
67  RestoreValue();
68  }
void RestoreValue()
Restores the variable to its original value Note that the variable will be restored again to its orig...
Definition: VariableGuard.h:91

Member Function Documentation

◆ GetCurrentValue()

T GetCurrentValue ( ) const
inline

Retrieves the current value of the guarded variable.

73  {
74  return mVariable;
75  }

◆ SetCurrentValue()

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
84  {
85  mVariable = value;
86  }

◆ RestoreValue()

void RestoreValue ( )
inline

Restores the variable to its original value Note that the variable will be restored again to its original value when the object instance goes out of scope.

92  {
93  mVariable = mOriginalValue;
94  }