SaveAndRestore.h revision 276479
1193323Sed//===-- SaveAndRestore.h - Utility  -------------------------------*- C++ -*-=//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed///
10193323Sed/// \file
11193323Sed/// This file provides utility classes that use RAII to save and restore
12193323Sed/// values.
13193323Sed///
14193323Sed//===----------------------------------------------------------------------===//
15193323Sed
16193323Sed#ifndef LLVM_SUPPORT_SAVEANDRESTORE_H
17193323Sed#define LLVM_SUPPORT_SAVEANDRESTORE_H
18193323Sed
19193323Sednamespace llvm {
20218893Sdim
21193323Sed/// A utility class that uses RAII to save and restore the value of a variable.
22193323Sedtemplate <typename T> struct SaveAndRestore {
23193323Sed  SaveAndRestore(T &X) : X(X), OldValue(X) {}
24193323Sed  SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) {
25193323Sed    X = NewValue;
26193323Sed  }
27193323Sed  ~SaveAndRestore() { X = OldValue; }
28193323Sed  T get() { return OldValue; }
29193323Sed
30193323Sedprivate:
31193323Sed  T &X;
32193323Sed  T OldValue;
33193323Sed};
34218893Sdim
35193323Sed/// Similar to \c SaveAndRestore.  Operates only on bools; the old value of a
36193323Sed/// variable is saved, and during the dstor the old value is or'ed with the new
37193323Sed/// value.
38193323Sedstruct SaveOr {
39193323Sed  SaveOr(bool &X) : X(X), OldValue(X) { X = false; }
40198090Srdivacky  ~SaveOr() { X |= OldValue; }
41193323Sed
42198090Srdivackyprivate:
43193323Sed  bool &X;
44193323Sed  const bool OldValue;
45193323Sed};
46193323Sed
47224145Sdim} // namespace llvm
48224145Sdim
49224145Sdim#endif
50193323Sed