Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

  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.  
  20. namespace llvm {
  21.  
  22. /// A utility class that uses RAII to save and restore the value of a variable.
  23. template <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.  
  32. private:
  33.   T &X;
  34.   T OldValue;
  35. };
  36.  
  37. // User-defined CTAD guides.
  38. template <typename T> SaveAndRestore(T &) -> SaveAndRestore<T>;
  39. template <typename T> SaveAndRestore(T &, const T &) -> SaveAndRestore<T>;
  40. template <typename T> SaveAndRestore(T &, T &&) -> SaveAndRestore<T>;
  41.  
  42. } // namespace llvm
  43.  
  44. #endif
  45.