Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QScopedValueRollback< T > Class Template Reference

\inmodule QtCore More...

#include <qscopedvaluerollback.h>

+ Collaboration diagram for QScopedValueRollback< T >:

Public Member Functions

Q_NODISCARD_CTOR constexpr QScopedValueRollback (T &var)
 Stores the previous value of var internally, for revert on destruction.
 
Q_NODISCARD_CTOR constexpr QScopedValueRollback (T &var, T value)
 Assigns value to \ var and stores the previous value of var internally, for revert on destruction.
 
Q_DECL_CONSTEXPR_DTOR ~QScopedValueRollback ()
 Assigns the previous value to the managed variable.
 
constexpr void commit ()
 Updates the previous value of the managed variable to its current value.
 

Detailed Description

template<typename T>
class QScopedValueRollback< T >

\inmodule QtCore

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

Since
4.8

The QScopedValueRollback class can be used to revert state when an exception is thrown without needing to write try-catch blocks.

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

The template can only be instantiated with a type that supports assignment.

See also
QScopedPointer, QScopeGuard

Definition at line 14 of file qscopedvaluerollback.h.

Constructor & Destructor Documentation

◆ QScopedValueRollback() [1/2]

template<typename T>
Q_NODISCARD_CTOR constexpr QScopedValueRollback< T >::QScopedValueRollback ( T & var)
inlineexplicitconstexpr

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

Definition at line 18 of file qscopedvaluerollback.h.

◆ QScopedValueRollback() [2/2]

template<typename T>
Q_NODISCARD_CTOR constexpr QScopedValueRollback< T >::QScopedValueRollback ( T & var,
T value )
inlineexplicitconstexpr

Assigns value to \ var and stores the previous value of var internally, for revert on destruction.

Since
5.4

Definition at line 24 of file qscopedvaluerollback.h.

◆ ~QScopedValueRollback()

template<typename T>
Q_DECL_CONSTEXPR_DTOR 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()

Definition at line 30 of file qscopedvaluerollback.h.

Member Function Documentation

◆ commit()

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

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

Definition at line 35 of file qscopedvaluerollback.h.


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