SaveAndRestore.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- SaveAndRestore.h - Utility -------------------------------*- C++ -*-=//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file provides utility classes that use RAII to save and restore
  16. /// values.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_SUPPORT_SAVEANDRESTORE_H
  20. #define LLVM_SUPPORT_SAVEANDRESTORE_H
  21. namespace llvm {
  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) {
  26. X = NewValue;
  27. }
  28. ~SaveAndRestore() { X = OldValue; }
  29. T get() { return OldValue; }
  30. private:
  31. T &X;
  32. T OldValue;
  33. };
  34. } // namespace llvm
  35. #endif
  36. #ifdef __GNUC__
  37. #pragma GCC diagnostic pop
  38. #endif