6.0.0-rc4Source

QAtomicScopedValueRollback Class

template <typename T> class QAtomicScopedValueRollback

The QAtomicScopedValueRollback class resets an atomic variable to its previous value on destruction. More…

Header#include <QAtomicScopedValueRollback>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QAtomicScopedValueRollback(std::atomic<T> &var, std::memory_order mo = std::memory_order_seq_cst)
QAtomicScopedValueRollback(std::atomic<T> &var, T value, std::memory_order mo = std::memory_order_seq_cst)
~QAtomicScopedValueRollback()
voidcommit()

Detailed Description

It is the counterpart of QScopedValueRollback for std::atomic variables. The object stores the variable's value when it is created, and writes it back when it goes out of scope, unless commit() was called:

std::atomic<int> state = 0;
{
    QAtomicScopedValueRollback<int> rollback(state, 1);
    // state is 1 here; it is 0 again after the block
}

See also QScopedValueRollback.

Differences from Qt

The variable is a std::atomic<T>; Qt also accepts a QAtomicInteger and a QAtomicPointer.

Member Function Documentation

[explicit] QAtomicScopedValueRollback::QAtomicScopedValueRollback(std::atomic<T> &var, std::memory_order mo = std::memory_order_seq_cst)

Constructs an object that restores var to its current value when it is destroyed. The value is read and written with the memory order mo.

QAtomicScopedValueRollback::QAtomicScopedValueRollback(std::atomic<T> &var, T value, std::memory_order mo = std::memory_order_seq_cst)

This is an overloaded function.

Stores value in var and restores the previous value when the object is destroyed.

QAtomicScopedValueRollback::~QAtomicScopedValueRollback()

Restores the saved value, unless commit() was called.

void QAtomicScopedValueRollback::commit()

Makes the variable keep its current value: nothing is restored when the object is destroyed.