clang API Documentation

SaveAndRestore.h
Go to the documentation of this file.
00001 //===-- SaveAndRestore.h - Utility  -------------------------------*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file provides utility classes that uses RAII to save and restore
00011 //  values.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_ANALYSIS_SAVERESTORE
00016 #define LLVM_CLANG_ANALYSIS_SAVERESTORE
00017 
00018 namespace clang {
00019 
00020 // SaveAndRestore - A utility class that uses RAII to save and restore
00021 //  the value of a variable.
00022 template<typename T>
00023 struct SaveAndRestore {
00024   SaveAndRestore(T& x) : X(x), old_value(x) {}
00025   SaveAndRestore(T& x, const T &new_value) : X(x), old_value(x) {
00026     X = new_value;
00027   }
00028   ~SaveAndRestore() { X = old_value; }
00029   T get() { return old_value; }
00030 private:
00031   T& X;
00032   T old_value;
00033 };
00034 
00035 // SaveOr - Similar to SaveAndRestore.  Operates only on bools; the old
00036 //  value of a variable is saved, and during the dstor the old value is
00037 //  or'ed with the new value.
00038 struct SaveOr {
00039   SaveOr(bool& x) : X(x), old_value(x) { x = false; }
00040   ~SaveOr() { X |= old_value; }
00041 private:
00042   bool& X;
00043   const bool old_value;
00044 };
00045 
00046 }
00047 #endif