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