CopperSpice API  1.7.4
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 a copy of the current value of var and then assigns value to var.

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

Restores the original value of the managed variable. The value was stored during construction or when the last call to commit() was made.

Method Documentation

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

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