scoped_restore.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* scoped_restore, a simple class for saving and restoring a value
  2. Copyright (C) 2016-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_SCOPED_RESTORE_H
  15. #define COMMON_SCOPED_RESTORE_H
  16. /* Base class for scoped_restore_tmpl. */
  17. class scoped_restore_base
  18. {
  19. public:
  20. /* This informs the (scoped_restore_tmpl<T>) dtor that you no longer
  21. want the original value restored. */
  22. void release () const
  23. { m_saved_var = NULL; }
  24. protected:
  25. scoped_restore_base (void *saved_var)
  26. : m_saved_var (saved_var)
  27. {}
  28. /* The type-erased saved variable. This is here so that clients can
  29. call release() on a "scoped_restore" local, which is a typedef to
  30. a scoped_restore_base. See below. */
  31. mutable void *m_saved_var;
  32. };
  33. /* A convenience typedef. Users of make_scoped_restore declare the
  34. local RAII object as having this type. */
  35. typedef const scoped_restore_base &scoped_restore;
  36. /* An RAII-based object that saves a variable's value, and then
  37. restores it again when this object is destroyed. */
  38. template<typename T>
  39. class scoped_restore_tmpl : public scoped_restore_base
  40. {
  41. public:
  42. /* Create a new scoped_restore object that saves the current value
  43. of *VAR. *VAR will be restored when this scoped_restore object
  44. is destroyed. */
  45. scoped_restore_tmpl (T *var)
  46. : scoped_restore_base (var),
  47. m_saved_value (*var)
  48. {
  49. }
  50. /* Create a new scoped_restore object that saves the current value
  51. of *VAR, and sets *VAR to VALUE. *VAR will be restored when this
  52. scoped_restore object is destroyed. This is templated on T2 to
  53. allow passing VALUEs of types convertible to T.
  54. E.g.: T='base'; T2='derived'. */
  55. template <typename T2>
  56. scoped_restore_tmpl (T *var, T2 value)
  57. : scoped_restore_base (var),
  58. m_saved_value (*var)
  59. {
  60. *var = value;
  61. }
  62. scoped_restore_tmpl (const scoped_restore_tmpl<T> &other)
  63. : scoped_restore_base {other.m_saved_var},
  64. m_saved_value (other.m_saved_value)
  65. {
  66. other.m_saved_var = NULL;
  67. }
  68. ~scoped_restore_tmpl ()
  69. {
  70. if (saved_var () != NULL)
  71. *saved_var () = m_saved_value;
  72. }
  73. private:
  74. /* Return a pointer to the saved variable with its type
  75. restored. */
  76. T *saved_var ()
  77. { return static_cast<T *> (m_saved_var); }
  78. /* No need for this. It is intentionally not defined anywhere. */
  79. scoped_restore_tmpl &operator= (const scoped_restore_tmpl &);
  80. /* The saved value. */
  81. const T m_saved_value;
  82. };
  83. /* Make a scoped_restore. This is useful because it lets template
  84. argument deduction work. */
  85. template<typename T>
  86. scoped_restore_tmpl<T> make_scoped_restore (T *var)
  87. {
  88. return scoped_restore_tmpl<T> (var);
  89. }
  90. /* Make a scoped_restore. This is useful because it lets template
  91. argument deduction work. */
  92. template<typename T, typename T2>
  93. scoped_restore_tmpl<T> make_scoped_restore (T *var, T2 value)
  94. {
  95. return scoped_restore_tmpl<T> (var, value);
  96. }
  97. #endif /* COMMON_SCOPED_RESTORE_H */