[llvm] 1dee803 - [SaveAndRestore] Upgrade this to support non-copyable types.

Chris Lattner via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 14 08:14:25 PDT 2022


Author: Chris Lattner
Date: 2022-10-14T08:14:12-07:00
New Revision: 1dee803afab1afdd122f217680dbbd35826799a3

URL: https://github.com/llvm/llvm-project/commit/1dee803afab1afdd122f217680dbbd35826799a3
DIFF: https://github.com/llvm/llvm-project/commit/1dee803afab1afdd122f217680dbbd35826799a3.diff

LOG: [SaveAndRestore] Upgrade this to support non-copyable types.

This adds a constructor and upgrades the dtor to work with
move-only types.

Differential Revision: https://reviews.llvm.org/D135940

Added: 
    

Modified: 
    llvm/include/llvm/Support/SaveAndRestore.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/SaveAndRestore.h b/llvm/include/llvm/Support/SaveAndRestore.h
index 3c0333b7119a6..2f5dc04e4bede 100644
--- a/llvm/include/llvm/Support/SaveAndRestore.h
+++ b/llvm/include/llvm/Support/SaveAndRestore.h
@@ -20,11 +20,12 @@ namespace llvm {
 /// A utility class that uses RAII to save and restore the value of a variable.
 template <typename T> struct SaveAndRestore {
   SaveAndRestore(T &X) : X(X), OldValue(X) {}
-  SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) {
-    X = NewValue;
+  SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) { X = NewValue; }
+  SaveAndRestore(T &X, T &&NewValue) : X(X), OldValue(std::move(X)) {
+    X = std::move(NewValue);
   }
-  ~SaveAndRestore() { X = OldValue; }
-  T get() { return OldValue; }
+  ~SaveAndRestore() { X = std::move(OldValue); }
+  const T &get() { return OldValue; }
 
 private:
   T &X;


        


More information about the llvm-commits mailing list