1//===-- SaveAndRestore.h - Utility  -------------------------------*- C++ -*-=//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file provides utility classes that uses RAII to save and restore
11//  values.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_ANALYSIS_SAVERESTORE
16#define LLVM_CLANG_ANALYSIS_SAVERESTORE
17
18namespace clang {
19
20// SaveAndRestore - A utility class that uses RAII to save and restore
21//  the value of a variable.
22template<typename T>
23struct SaveAndRestore {
24  SaveAndRestore(T& x) : X(x), old_value(x) {}
25  SaveAndRestore(T& x, const T &new_value) : X(x), old_value(x) {
26    X = new_value;
27  }
28  ~SaveAndRestore() { X = old_value; }
29  T get() { return old_value; }
30private:
31  T& X;
32  T old_value;
33};
34
35// SaveOr - Similar to SaveAndRestore.  Operates only on bools; the old
36//  value of a variable is saved, and during the dstor the old value is
37//  or'ed with the new value.
38struct SaveOr {
39  SaveOr(bool& x) : X(x), old_value(x) { x = false; }
40  ~SaveOr() { X |= old_value; }
41private:
42  bool& X;
43  const bool old_value;
44};
45
46}
47#endif
48