1 //===-- SaveAndRestore.h - Utility -------------------------------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides utility classes that uses RAII to save and restore
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_SAVEANDRESTORE_H
16 #define LLVM_SUPPORT_SAVEANDRESTORE_H
20 // SaveAndRestore - A utility class that uses RAII to save and restore
21 // the value of a variable.
23 struct SaveAndRestore {
24 SaveAndRestore(T& x) : X(x), old_value(x) {}
25 SaveAndRestore(T& x, const T &new_value) : X(x), old_value(x) {
28 ~SaveAndRestore() { X = old_value; }
29 T get() { return old_value; }
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.
39 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
40 ~SaveOr() { X |= old_value; }