1 //===-- SaveAndRestore.h - Utility -------------------------------*- C++ -*-=//
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
7 //===----------------------------------------------------------------------===//
10 /// This file provides utility classes that use RAII to save and restore
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_SAVEANDRESTORE_H
16 #define LLVM_SUPPORT_SAVEANDRESTORE_H
20 /// A utility class that uses RAII to save and restore the value of a variable.
21 template <typename T
> struct SaveAndRestore
{
22 SaveAndRestore(T
&X
) : X(X
), OldValue(X
) {}
23 SaveAndRestore(T
&X
, const T
&NewValue
) : X(X
), OldValue(X
) {
26 ~SaveAndRestore() { X
= OldValue
; }
27 T
get() { return OldValue
; }