LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SaveAndRestore.h
Go to the documentation of this file.
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_SUPPORT_SAVEANDRESTORE_H
16 #define LLVM_SUPPORT_SAVEANDRESTORE_H
17 
18 namespace llvm {
19 
20 // SaveAndRestore - A utility class that uses RAII to save and restore
21 // the value of a variable.
22 template<typename T>
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; }
30 private:
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.
38 struct SaveOr {
39  SaveOr(bool& x) : X(x), old_value(x) { x = false; }
40  ~SaveOr() { X |= old_value; }
41 private:
42  bool& X;
43  const bool old_value;
44 };
45 
46 }
47 #endif
SaveOr(bool &x)
SaveAndRestore(T &x, const T &new_value)