torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
torc::generic::TemporaryAssignment< TYPE > Class Template Reference

Template class that stores the current value of a variable and restores that value when this object goes out of scope. More...

#include <TemporaryAssignment.hpp>

Public Member Functions

 TemporaryAssignment (TYPE &inVariable)
 Public constructor that does not assign a new value. More...
 
 TemporaryAssignment (TYPE &inVariable, TYPE inValue)
 Public constructor that assigns a new value. More...
 
 ~TemporaryAssignment (void)
 Public destructor to restore the original value. More...
 

Protected Attributes

TYPE & mVariable
 Reference to the variable. More...
 
TYPE mOriginalValue
 Original value of the variable. More...
 

Detailed Description

template<typename TYPE>
class torc::generic::TemporaryAssignment< TYPE >

Template class that stores the current value of a variable and restores that value when this object goes out of scope.

Note
This class inspired a StackOverflow question, and there may be more general ways to solve the problem.
See Also
http://stackoverflow.com/questions/13383683/reverting-temporary-assignment-in-c

Definition at line 30 of file TemporaryAssignment.hpp.

Constructor & Destructor Documentation

template<typename TYPE>
torc::generic::TemporaryAssignment< TYPE >::TemporaryAssignment ( TYPE &  inVariable)
inline

Public constructor that does not assign a new value.

Parameters
inVariableThe variable to save and restore.

Definition at line 41 of file TemporaryAssignment.hpp.

42  : mVariable(inVariable), mOriginalValue(inVariable) {
43  }
TYPE mOriginalValue
Original value of the variable.
TYPE & mVariable
Reference to the variable.
template<typename TYPE>
torc::generic::TemporaryAssignment< TYPE >::TemporaryAssignment ( TYPE &  inVariable,
TYPE  inValue 
)
inline

Public constructor that assigns a new value.

Parameters
inVariableThe variable to save and restore.
inValueThe new value to use.

Definition at line 47 of file TemporaryAssignment.hpp.

48  : mVariable(inVariable), mOriginalValue(inVariable) {
49  mVariable = inValue;
50  }
TYPE mOriginalValue
Original value of the variable.
TYPE & mVariable
Reference to the variable.
template<typename TYPE>
torc::generic::TemporaryAssignment< TYPE >::~TemporaryAssignment ( void  )
inline

Public destructor to restore the original value.

Definition at line 52 of file TemporaryAssignment.hpp.

52  {
54  }
TYPE mOriginalValue
Original value of the variable.
TYPE & mVariable
Reference to the variable.

Field Documentation

template<typename TYPE>
TYPE torc::generic::TemporaryAssignment< TYPE >::mOriginalValue
protected

Original value of the variable.

Definition at line 36 of file TemporaryAssignment.hpp.

template<typename TYPE>
TYPE& torc::generic::TemporaryAssignment< TYPE >::mVariable
protected

Reference to the variable.

Definition at line 34 of file TemporaryAssignment.hpp.


The documentation for this class was generated from the following file: