llvm.org GIT mirror llvm / 945c2b3
Move "clang/Analysis/Support/SaveAndRestore.h" to "llvm/ADT/SaveAndRestore.h" to make it more widely available. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@151564 91177308-0d34-0410-b5e6-96231b3b80d8 Argyrios Kyrtzidis 8 years ago
1 changed file(s) with 47 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- SaveAndRestore.h - Utility -------------------------------*- C++ -*-=//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides utility classes that uses RAII to save and restore
10 // values.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_SAVERESTORE
15 #define LLVM_ADT_SAVERESTORE
16
17 namespace llvm {
18
19 // SaveAndRestore - A utility class that uses RAII to save and restore
20 // the value of a variable.
21 template
22 struct SaveAndRestore {
23 SaveAndRestore(T& x) : X(x), old_value(x) {}
24 SaveAndRestore(T& x, const T &new_value) : X(x), old_value(x) {
25 X = new_value;
26 }
27 ~SaveAndRestore() { X = old_value; }
28 T get() { return old_value; }
29 private:
30 T& X;
31 T old_value;
32 };
33
34 // SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
35 // value of a variable is saved, and during the dstor the old value is
36 // or'ed with the new value.
37 struct SaveOr {
38 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
39 ~SaveOr() { X |= old_value; }
40 private:
41 bool& X;
42 const bool old_value;
43 };
44
45 }
46 #endif