1 //===-- SaveAndRestore.h - Utility -------------------------------*- C++ -*-=// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file provides utility classes that use 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 /// A utility class that uses RAII to save and restore the value of a variable. 21 template <typename T> struct SaveAndRestore { SaveAndRestoreSaveAndRestore22 SaveAndRestore(T &X) : X(X), OldValue(X) {} SaveAndRestoreSaveAndRestore23 SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) { 24 X = NewValue; 25 } ~SaveAndRestoreSaveAndRestore26 ~SaveAndRestore() { X = OldValue; } getSaveAndRestore27 T get() { return OldValue; } 28 29 private: 30 T &X; 31 T OldValue; 32 }; 33 34 } // namespace llvm 35 36 #endif 37