2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2014-03-13 14:37:22 +01:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2014-03-13 14:37:22 +01:00
|
|
|
|
2017-04-14 16:08:34 +03:00
|
|
|
#include <utility>
|
2014-03-13 14:37:22 +01:00
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
/// RAII object to save a value, and restore it when the scope is left.
|
|
|
|
|
template<typename T>
|
|
|
|
|
class ScopedSwap
|
|
|
|
|
{
|
|
|
|
|
T oldValue;
|
|
|
|
|
T &ref;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ScopedSwap(T &var, T newValue)
|
|
|
|
|
: oldValue(newValue)
|
|
|
|
|
, ref(var)
|
|
|
|
|
{
|
|
|
|
|
std::swap(ref, oldValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ScopedSwap()
|
|
|
|
|
{
|
|
|
|
|
std::swap(ref, oldValue);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-23 10:45:40 +02:00
|
|
|
using ScopedBoolSwap = ScopedSwap<bool>;
|
2014-03-13 14:37:22 +01:00
|
|
|
|
|
|
|
|
} // Utils namespace
|