1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSCOPEDVALUEROLLBACK_H
5#define QSCOPEDVALUEROLLBACK_H
6
7#include <QtCore/qglobal.h>
8
9QT_BEGIN_NAMESPACE
10
11template <typename T>
12class QScopedValueRollback
13{
14public:
15 Q_NODISCARD_CTOR
16 explicit constexpr QScopedValueRollback(T &var)
17 : varRef(var), oldValue(var)
18 {
19 }
20
21 Q_NODISCARD_CTOR
22 explicit constexpr QScopedValueRollback(T &var, T value)
23 : varRef(var), oldValue(std::move(var)) // ### C++20: std::exchange(var, std::move(value))
24 {
25 var = std::move(value);
26 }
27
28#if __cpp_constexpr >= 201907L
29 constexpr
30#endif
31 ~QScopedValueRollback()
32 {
33 varRef = std::move(oldValue);
34 }
35
36 constexpr void commit()
37 {
38 oldValue = varRef;
39 }
40
41private:
42 T &varRef;
43 T oldValue;
44
45 Q_DISABLE_COPY_MOVE(QScopedValueRollback)
46};
47
48QT_END_NAMESPACE
49
50#endif // QSCOPEDVALUEROLLBACK_H
51

source code of qtbase/src/corelib/tools/qscopedvaluerollback.h