2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2024-08-22 23:05:35 +08:00
|
|
|
* Copyright (C) 2018-2024 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2020-02-23 05:21:06 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2024-08-22 23:05:35 +08:00
|
|
|
#include <memory>
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class VariableBackup {
|
|
|
|
public:
|
|
|
|
VariableBackup(T *ptr) : pValue(ptr) {
|
|
|
|
oldValue = *ptr;
|
|
|
|
}
|
2018-03-05 20:43:24 +08:00
|
|
|
VariableBackup(T *ptr, T &&newValue) : pValue(ptr) {
|
|
|
|
oldValue = *ptr;
|
|
|
|
*pValue = newValue;
|
|
|
|
}
|
|
|
|
VariableBackup(T *ptr, T &newValue) : pValue(ptr) {
|
|
|
|
oldValue = *ptr;
|
|
|
|
*pValue = newValue;
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
~VariableBackup() {
|
|
|
|
*pValue = oldValue;
|
|
|
|
}
|
|
|
|
void operator=(const T &val) {
|
|
|
|
*pValue = val;
|
|
|
|
}
|
2020-11-06 18:50:35 +08:00
|
|
|
template <typename T2>
|
|
|
|
bool operator==(const T2 &val) const {
|
|
|
|
return *pValue == val;
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
T oldValue;
|
|
|
|
T *pValue;
|
|
|
|
};
|
2024-08-22 23:05:35 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class NonCopyableVariableBackup {
|
|
|
|
public:
|
|
|
|
NonCopyableVariableBackup(T *ptr, T &&newValue) : pValue(ptr) {
|
|
|
|
oldValue = std::move(*ptr);
|
|
|
|
*pValue = std::move(newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
~NonCopyableVariableBackup() {
|
|
|
|
*pValue = std::move(oldValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T oldValue;
|
|
|
|
T *pValue;
|
|
|
|
};
|