1 //===-- SaveAndRestore.h - Utility -------------------------------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file provides utility classes that use RAII to save and restore
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SUPPORT_SAVEANDRESTORE_H
17 #define LLVM_SUPPORT_SAVEANDRESTORE_H
21 /// A utility class that uses RAII to save and restore the value of a variable.
22 template <typename T> struct SaveAndRestore {
23 SaveAndRestore(T &X) : X(X), OldValue(X) {}
24 SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) {
27 ~SaveAndRestore() { X = OldValue; }
28 T get() { return OldValue; }
35 /// Similar to \c SaveAndRestore. Operates only on bools; the old value of a
36 /// variable is saved, and during the dstor the old value is or'ed with the new
39 SaveOr(bool &X) : X(X), OldValue(X) { X = false; }
40 ~SaveOr() { X |= OldValue; }