1234285Sdim//===-- SaveAndRestore.h - Utility -------------------------------*- C++ -*-=// 2234285Sdim// 3234285Sdim// The LLVM Compiler Infrastructure 4234285Sdim// 5234285Sdim// This file is distributed under the University of Illinois Open Source 6234285Sdim// License. See LICENSE.TXT for details. 7234285Sdim// 8234285Sdim//===----------------------------------------------------------------------===// 9234285Sdim// 10234285Sdim// This file provides utility classes that uses RAII to save and restore 11234285Sdim// values. 12234285Sdim// 13234285Sdim//===----------------------------------------------------------------------===// 14234285Sdim 15252723Sdim#ifndef LLVM_SUPPORT_SAVEANDRESTORE_H 16252723Sdim#define LLVM_SUPPORT_SAVEANDRESTORE_H 17234285Sdim 18234285Sdimnamespace llvm { 19234285Sdim 20234285Sdim// SaveAndRestore - A utility class that uses RAII to save and restore 21234285Sdim// the value of a variable. 22234285Sdimtemplate<typename T> 23234285Sdimstruct SaveAndRestore { 24234285Sdim SaveAndRestore(T& x) : X(x), old_value(x) {} 25234285Sdim SaveAndRestore(T& x, const T &new_value) : X(x), old_value(x) { 26234285Sdim X = new_value; 27234285Sdim } 28234285Sdim ~SaveAndRestore() { X = old_value; } 29234285Sdim T get() { return old_value; } 30234285Sdimprivate: 31234285Sdim T& X; 32234285Sdim T old_value; 33234285Sdim}; 34234285Sdim 35234285Sdim// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old 36234285Sdim// value of a variable is saved, and during the dstor the old value is 37234285Sdim// or'ed with the new value. 38234285Sdimstruct SaveOr { 39234285Sdim SaveOr(bool& x) : X(x), old_value(x) { x = false; } 40234285Sdim ~SaveOr() { X |= old_value; } 41234285Sdimprivate: 42234285Sdim bool& X; 43234285Sdim const bool old_value; 44234285Sdim}; 45234285Sdim 46234285Sdim} 47234285Sdim#endif 48