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//===----------------------------------------------------------------------===//
9276479Sdim///
10276479Sdim/// \file
11276479Sdim/// This file provides utility classes that use RAII to save and restore
12276479Sdim/// values.
13276479Sdim///
14234285Sdim//===----------------------------------------------------------------------===//
15234285Sdim
16249423Sdim#ifndef LLVM_SUPPORT_SAVEANDRESTORE_H
17249423Sdim#define LLVM_SUPPORT_SAVEANDRESTORE_H
18234285Sdim
19234285Sdimnamespace llvm {
20234285Sdim
21276479Sdim/// A utility class that uses RAII to save and restore the value of a variable.
22276479Sdimtemplate <typename T> struct SaveAndRestore {
23276479Sdim  SaveAndRestore(T &X) : X(X), OldValue(X) {}
24276479Sdim  SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) {
25276479Sdim    X = NewValue;
26234285Sdim  }
27276479Sdim  ~SaveAndRestore() { X = OldValue; }
28276479Sdim  T get() { return OldValue; }
29276479Sdim
30234285Sdimprivate:
31276479Sdim  T &X;
32276479Sdim  T OldValue;
33234285Sdim};
34234285Sdim
35276479Sdim/// Similar to \c SaveAndRestore.  Operates only on bools; the old value of a
36276479Sdim/// variable is saved, and during the dstor the old value is or'ed with the new
37276479Sdim/// value.
38234285Sdimstruct SaveOr {
39276479Sdim  SaveOr(bool &X) : X(X), OldValue(X) { X = false; }
40276479Sdim  ~SaveOr() { X |= OldValue; }
41276479Sdim
42234285Sdimprivate:
43276479Sdim  bool &X;
44276479Sdim  const bool OldValue;
45234285Sdim};
46234285Sdim
47276479Sdim} // namespace llvm
48276479Sdim
49234285Sdim#endif
50