CopperSpice API  1.7.2
QScopedValueRollback< T > Class Template Reference

QScopedValueRollback class resets a variable to its previous value on destruction. More...

Public Methods

 QScopedValueRollback (T &var)
 
 QScopedValueRollback (T &var, T value)
 
 ~QScopedValueRollback ()
 
void commit ()
 

Detailed Description

template<typename T>
class QScopedValueRollback< T >

The QScopedValueRollback class resets a variable to its previous value on destruction. The QScopedAssignment class can be used to revert state when an exception is thrown without needing to write try-catch blocks.

This class can also be used to manage variables that are temporarily set, such as reentrancy guards. The variable will be reset whether the function is exited normally, exited early by a return statement, or exited by an exception.

The T for this templated class must be a data type which supports assignment.

See also
QScopedPointer

Constructor & Destructor Documentation

template<typename T >
QScopedValueRollback< T >::QScopedValueRollback ( T &  var)
inlineexplicit

Stores the previous value of var internally, for revert on destruction.

template<typename T >
QScopedValueRollback< T >::QScopedValueRollback ( T &  var,
value 
)
inlineexplicit

Stores the current value of var and then assigns value to var.

template<typename T >
QScopedValueRollback< T >::~QScopedValueRollback ( )
inline

Assigns the previous value to the managed variable. This is the value at construction time or at the last call to commit()

Method Documentation

template<typename T >
void QScopedValueRollback< T >::commit ( )
inline

Updates the previous value of the managed variable to its current value.