1 /* 2 This file is part of GammaRay, the Qt application inspection and 3 manipulation tool. 4 5 Copyright (C) 2013-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com 6 Author: Milian Wolff <milian.wolff@kdab.com> 7 8 Licensees holding valid commercial KDAB GammaRay licenses may use this file in 9 accordance with GammaRay Commercial License Agreement provided with the Software. 10 11 Contact info@kdab.com if any conditions of this licensing are not clear to you. 12 13 This program is free software; you can redistribute it and/or modify 14 it under the terms of the GNU General Public License as published by 15 the Free Software Foundation, either version 2 of the License, or 16 (at your option) any later version. 17 18 This program is distributed in the hope that it will be useful, 19 but WITHOUT ANY WARRANTY; without even the implied warranty of 20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 GNU General Public License for more details. 22 23 You should have received a copy of the GNU General Public License 24 along with this program. If not, see <http://www.gnu.org/licenses/>. 25 */ 26 27 #ifndef GAMMARAY_SETTEMPVALUE_H 28 #define GAMMARAY_SETTEMPVALUE_H 29 30 #include <qglobal.h> 31 32 namespace GammaRay { 33 /** 34 * @brief GammaRay utilities. 35 */ 36 namespace Util { 37 /** 38 * Temporarily overwrite a given object with a new value and reset the value 39 * when the scope is exited. 40 */ 41 template<class T> 42 struct SetTempValue 43 { SetTempValueSetTempValue44 SetTempValue(T &obj, T newValue) 45 : obj(obj) 46 , oldValue(obj) 47 { 48 obj = newValue; 49 } 50 ~SetTempValueSetTempValue51 ~SetTempValue() 52 { 53 obj = oldValue; 54 } 55 56 Q_DISABLE_COPY(SetTempValue) 57 T& obj; 58 T oldValue; 59 }; 60 } 61 } 62 63 #endif // GAMMARAY_SETTEMPVALUE_H 64