LLVM 20.0.0git
SaveAndRestore.h
Go to the documentation of this file.
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#include <utility>
19
20namespace llvm {
21
22/// A utility class that uses RAII to save and restore the value of a variable.
23template <typename T> struct SaveAndRestore {
24 SaveAndRestore(T &X) : X(X), OldValue(X) {}
25 SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) { X = NewValue; }
26 SaveAndRestore(T &X, T &&NewValue) : X(X), OldValue(std::move(X)) {
27 X = std::move(NewValue);
28 }
29 ~SaveAndRestore() { X = std::move(OldValue); }
30 const T &get() { return OldValue; }
31
32private:
33 T &X;
34 T OldValue;
35};
36
37// User-defined CTAD guides.
38template <typename T> SaveAndRestore(T &) -> SaveAndRestore<T>;
39template <typename T> SaveAndRestore(T &, const T &) -> SaveAndRestore<T>;
40template <typename T> SaveAndRestore(T &, T &&) -> SaveAndRestore<T>;
41
42} // namespace llvm
43
44#endif
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
A utility class that uses RAII to save and restore the value of a variable.
SaveAndRestore(T &X, const T &NewValue)
SaveAndRestore(T &X, T &&NewValue)