Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:33

0001 //===-- SaveAndRestore.h - Utility  -------------------------------*- C++ -*-=//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file provides utility classes that use RAII to save and restore
0011 /// values.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_SUPPORT_SAVEANDRESTORE_H
0016 #define LLVM_SUPPORT_SAVEANDRESTORE_H
0017 
0018 #include <utility>
0019 
0020 namespace llvm {
0021 
0022 /// A utility class that uses RAII to save and restore the value of a variable.
0023 template <typename T> struct SaveAndRestore {
0024   SaveAndRestore(T &X) : X(X), OldValue(X) {}
0025   SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) { X = NewValue; }
0026   SaveAndRestore(T &X, T &&NewValue) : X(X), OldValue(std::move(X)) {
0027     X = std::move(NewValue);
0028   }
0029   ~SaveAndRestore() { X = std::move(OldValue); }
0030   const T &get() { return OldValue; }
0031 
0032 private:
0033   T &X;
0034   T OldValue;
0035 };
0036 
0037 // User-defined CTAD guides.
0038 template <typename T> SaveAndRestore(T &) -> SaveAndRestore<T>;
0039 template <typename T> SaveAndRestore(T &, const T &) -> SaveAndRestore<T>;
0040 template <typename T> SaveAndRestore(T &, T &&) -> SaveAndRestore<T>;
0041 
0042 } // namespace llvm
0043 
0044 #endif