QScopedValueRollback Class

template <typename T> class QScopedValueRollback

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

Header: #include <QScopedValueRollback>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

Public Functions

Detailed Description

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 and QScopeGuard.

Member Function Documentation

[explicit constexpr] QScopedValueRollback::QScopedValueRollback(T &var)

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

[explicit constexpr] QScopedValueRollback::QScopedValueRollback(T &var, T value)

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

[constexpr] QScopedValueRollback::~QScopedValueRollback()

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

[constexpr] void QScopedValueRollback::commit()

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

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.