1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <cstddef> |
1
|
29 |
453
|
30 #include "CMatrix.h" |
1
|
31 |
1352
|
32 #include "error.h" |
1
|
33 #include "unwind-prot.h" |
|
34 #include "utils.h" |
|
35 |
4214
|
36 std::stack<unwind_elem> unwind_protect::elt_list; |
1
|
37 |
2985
|
38 class |
|
39 saved_variable |
1
|
40 { |
2985
|
41 public: |
1755
|
42 |
2985
|
43 enum var_type |
|
44 { |
|
45 boolean, |
|
46 integer, |
|
47 string_type, |
|
48 generic_ptr, |
|
49 generic |
|
50 }; |
1
|
51 |
|
52 saved_variable (void); |
2985
|
53 |
|
54 saved_variable (bool *p, bool v); |
|
55 |
1
|
56 saved_variable (int *p, int v); |
2985
|
57 |
3523
|
58 saved_variable (std::string *p, const std::string& v); |
2985
|
59 |
1
|
60 saved_variable (void **p, void *v); |
2985
|
61 |
1
|
62 ~saved_variable (void); |
|
63 |
|
64 void restore_value (void); |
|
65 |
2985
|
66 static void restore (void *s); |
|
67 |
|
68 private: |
|
69 |
1
|
70 union |
|
71 { |
2985
|
72 bool *ptr_to_bool; |
1
|
73 int *ptr_to_int; |
|
74 void *gen_ptr; |
|
75 void **ptr_to_gen_ptr; |
|
76 }; |
|
77 |
|
78 union |
|
79 { |
2985
|
80 bool bool_value; |
1
|
81 int int_value; |
5760
|
82 std::string *str_value; |
1
|
83 void *gen_ptr_value; |
|
84 }; |
|
85 |
|
86 var_type type_tag; |
2985
|
87 |
1
|
88 size_t size; |
|
89 }; |
|
90 |
|
91 saved_variable::saved_variable (void) |
|
92 { |
529
|
93 gen_ptr = 0; |
|
94 gen_ptr_value = 0; |
1
|
95 type_tag = generic; |
|
96 size = 0; |
|
97 } |
|
98 |
2985
|
99 saved_variable::saved_variable (bool *p, bool v) |
|
100 { |
3018
|
101 type_tag = boolean; |
2985
|
102 ptr_to_bool = p; |
|
103 bool_value = v; |
|
104 size = sizeof (bool); // Is this necessary? |
|
105 } |
|
106 |
1
|
107 saved_variable::saved_variable (int *p, int v) |
|
108 { |
|
109 type_tag = integer; |
|
110 ptr_to_int = p; |
|
111 int_value = v; |
1755
|
112 size = sizeof (int); // Is this necessary? |
|
113 } |
|
114 |
3523
|
115 saved_variable::saved_variable (std::string *p, const std::string& v) |
1755
|
116 { |
|
117 type_tag = string_type; |
|
118 gen_ptr = p; |
3523
|
119 str_value = new std::string (v); |
3549
|
120 size = sizeof (std::string); // Is this necessary? |
1
|
121 } |
|
122 |
|
123 saved_variable::saved_variable (void **p, void *v) |
|
124 { |
|
125 type_tag = generic_ptr; |
|
126 ptr_to_gen_ptr = p; |
|
127 gen_ptr_value = v; |
|
128 size = sizeof (void *); |
|
129 } |
|
130 |
|
131 saved_variable::~saved_variable (void) |
|
132 { |
1755
|
133 switch (type_tag) |
|
134 { |
|
135 case string_type: |
|
136 delete str_value; |
|
137 break; |
|
138 |
|
139 case generic: |
3263
|
140 // XXX FIXME XXX |
|
141 // delete [] gen_ptr_value; |
1755
|
142 break; |
|
143 |
|
144 default: |
|
145 break; |
|
146 } |
1
|
147 } |
|
148 |
|
149 void |
|
150 saved_variable::restore_value (void) |
|
151 { |
|
152 switch (type_tag) |
|
153 { |
2985
|
154 case boolean: |
|
155 *ptr_to_bool = bool_value; |
|
156 break; |
|
157 |
1
|
158 case integer: |
|
159 *ptr_to_int = int_value; |
|
160 break; |
777
|
161 |
1755
|
162 case string_type: |
3523
|
163 (static_cast<std::string *> (gen_ptr)) -> assign (*str_value); |
1755
|
164 break; |
|
165 |
1
|
166 case generic_ptr: |
|
167 *ptr_to_gen_ptr = gen_ptr_value; |
|
168 break; |
777
|
169 |
1
|
170 case generic: |
|
171 memcpy (gen_ptr, gen_ptr_value, size); |
|
172 break; |
777
|
173 |
1
|
174 default: |
|
175 panic_impossible (); |
777
|
176 break; |
1
|
177 } |
|
178 } |
|
179 |
2985
|
180 void |
|
181 saved_variable::restore (void *s) |
1
|
182 { |
2800
|
183 saved_variable *sv = static_cast<saved_variable *> (s); |
1
|
184 sv->restore_value (); |
|
185 delete sv; |
|
186 } |
|
187 |
|
188 void |
2985
|
189 unwind_protect::add (unwind_elem::cleanup_func fptr, void *ptr) |
|
190 { |
|
191 unwind_elem el (fptr, ptr); |
4214
|
192 elt_list.push (el); |
2985
|
193 } |
|
194 |
|
195 void |
|
196 unwind_protect::run (void) |
|
197 { |
4214
|
198 unwind_elem el = elt_list.top (); |
|
199 |
|
200 elt_list.pop (); |
2985
|
201 |
|
202 unwind_elem::cleanup_func f = el.fptr (); |
|
203 |
|
204 if (f) |
|
205 f (el.ptr ()); |
|
206 } |
|
207 |
|
208 void |
|
209 unwind_protect::discard (void) |
1
|
210 { |
4214
|
211 elt_list.pop (); |
2985
|
212 } |
|
213 |
|
214 void |
3523
|
215 unwind_protect::begin_frame (const std::string& tag) |
2985
|
216 { |
|
217 unwind_elem elem (tag); |
4214
|
218 elt_list.push (elem); |
2985
|
219 } |
|
220 |
|
221 void |
3523
|
222 unwind_protect::run_frame (const std::string& tag) |
2985
|
223 { |
4214
|
224 while (! elt_list.empty ()) |
2985
|
225 { |
4214
|
226 unwind_elem el = elt_list.top (); |
|
227 |
|
228 elt_list.pop (); |
2985
|
229 |
|
230 unwind_elem::cleanup_func f = el.fptr (); |
|
231 |
|
232 if (f) |
|
233 f (el.ptr ()); |
|
234 |
|
235 if (tag == el.tag ()) |
|
236 break; |
|
237 } |
1
|
238 } |
|
239 |
|
240 void |
3523
|
241 unwind_protect::discard_frame (const std::string& tag) |
1755
|
242 { |
4214
|
243 while (! elt_list.empty ()) |
2985
|
244 { |
4214
|
245 unwind_elem el = elt_list.top (); |
|
246 |
|
247 elt_list.pop (); |
2985
|
248 |
|
249 if (tag == el.tag ()) |
|
250 break; |
|
251 } |
|
252 } |
|
253 |
|
254 void |
|
255 unwind_protect::run_all (void) |
|
256 { |
4214
|
257 while (! elt_list.empty ()) |
2985
|
258 { |
4214
|
259 unwind_elem el = elt_list.top (); |
|
260 |
|
261 elt_list.pop (); |
2985
|
262 |
|
263 unwind_elem::cleanup_func f = el.fptr (); |
|
264 |
|
265 if (f) |
|
266 f (el.ptr ()); |
|
267 } |
1755
|
268 } |
|
269 |
|
270 void |
2985
|
271 unwind_protect::discard_all (void) |
|
272 { |
4214
|
273 while (! elt_list.empty ()) |
|
274 elt_list.pop (); |
2985
|
275 } |
|
276 |
|
277 void |
|
278 unwind_protect::save_bool (bool *ptr, bool value) |
|
279 { |
|
280 saved_variable *s = new saved_variable (ptr, value); |
|
281 add (saved_variable::restore, s); |
|
282 } |
|
283 |
|
284 void |
|
285 unwind_protect::save_int (int *ptr, int value) |
1
|
286 { |
|
287 saved_variable *s = new saved_variable (ptr, value); |
2985
|
288 add (saved_variable::restore, s); |
|
289 } |
|
290 |
|
291 void |
3523
|
292 unwind_protect::save_str (std::string *ptr, const std::string& value) |
2985
|
293 { |
|
294 saved_variable *s = new saved_variable (ptr, value); |
|
295 add (saved_variable::restore, s); |
|
296 } |
|
297 |
|
298 void |
|
299 unwind_protect::save_ptr (void **ptr, void *value) |
|
300 { |
|
301 saved_variable *s = new saved_variable (ptr, value); |
|
302 add (saved_variable::restore, s); |
1
|
303 } |
|
304 |
|
305 /* |
|
306 ;;; Local Variables: *** |
|
307 ;;; mode: C++ *** |
|
308 ;;; End: *** |
|
309 */ |