[SaveAndRestore] Upgrade this to support non-copyable types.
authorChris Lattner <clattner@nondot.org>
Fri, 14 Oct 2022 05:01:14 +0000 (22:01 -0700)
committerChris Lattner <clattner@nondot.org>
Fri, 14 Oct 2022 15:14:12 +0000 (08:14 -0700)
This adds a constructor and upgrades the dtor to work with
move-only types.

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

llvm/include/llvm/Support/SaveAndRestore.h

index 3c0333b..2f5dc04 100644 (file)
@@ -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; }
-  get() { return OldValue; }
+  ~SaveAndRestore() { X = std::move(OldValue); }
+  const T &get() { return OldValue; }
 
 private:
   T &X;