|
| 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...
|
|
T | 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...
|
|
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);
}
}