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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1343
|
31 #include <cstddef> |
1
|
32 |
1755
|
33 #include <string> |
|
34 |
1
|
35 #include "SLStack.h" |
|
36 |
453
|
37 #include "CMatrix.h" |
1
|
38 |
1352
|
39 #include "error.h" |
1
|
40 #include "unwind-prot.h" |
|
41 #include "utils.h" |
|
42 |
2985
|
43 SLStack<unwind_elem> unwind_protect::list; |
1
|
44 |
2985
|
45 class |
|
46 saved_variable |
1
|
47 { |
2985
|
48 public: |
1755
|
49 |
2985
|
50 enum var_type |
|
51 { |
|
52 boolean, |
|
53 integer, |
|
54 string_type, |
|
55 generic_ptr, |
|
56 generic |
|
57 }; |
1
|
58 |
|
59 saved_variable (void); |
2985
|
60 |
|
61 saved_variable (bool *p, bool v); |
|
62 |
1
|
63 saved_variable (int *p, int v); |
2985
|
64 |
3523
|
65 saved_variable (std::string *p, const std::string& v); |
2985
|
66 |
1
|
67 saved_variable (void **p, void *v); |
2985
|
68 |
1
|
69 ~saved_variable (void); |
|
70 |
|
71 void restore_value (void); |
|
72 |
2985
|
73 static void restore (void *s); |
|
74 |
|
75 private: |
|
76 |
1
|
77 union |
|
78 { |
2985
|
79 bool *ptr_to_bool; |
1
|
80 int *ptr_to_int; |
|
81 void *gen_ptr; |
|
82 void **ptr_to_gen_ptr; |
|
83 }; |
|
84 |
|
85 union |
|
86 { |
2985
|
87 bool bool_value; |
1
|
88 int int_value; |
3523
|
89 const std::string *str_value; |
1
|
90 void *gen_ptr_value; |
|
91 }; |
|
92 |
|
93 var_type type_tag; |
2985
|
94 |
1
|
95 size_t size; |
|
96 }; |
|
97 |
|
98 saved_variable::saved_variable (void) |
|
99 { |
529
|
100 gen_ptr = 0; |
|
101 gen_ptr_value = 0; |
1
|
102 type_tag = generic; |
|
103 size = 0; |
|
104 } |
|
105 |
2985
|
106 saved_variable::saved_variable (bool *p, bool v) |
|
107 { |
3018
|
108 type_tag = boolean; |
2985
|
109 ptr_to_bool = p; |
|
110 bool_value = v; |
|
111 size = sizeof (bool); // Is this necessary? |
|
112 } |
|
113 |
1
|
114 saved_variable::saved_variable (int *p, int v) |
|
115 { |
|
116 type_tag = integer; |
|
117 ptr_to_int = p; |
|
118 int_value = v; |
1755
|
119 size = sizeof (int); // Is this necessary? |
|
120 } |
|
121 |
3523
|
122 saved_variable::saved_variable (std::string *p, const std::string& v) |
1755
|
123 { |
|
124 type_tag = string_type; |
|
125 gen_ptr = p; |
3523
|
126 str_value = new std::string (v); |
3549
|
127 size = sizeof (std::string); // Is this necessary? |
1
|
128 } |
|
129 |
|
130 saved_variable::saved_variable (void **p, void *v) |
|
131 { |
|
132 type_tag = generic_ptr; |
|
133 ptr_to_gen_ptr = p; |
|
134 gen_ptr_value = v; |
|
135 size = sizeof (void *); |
|
136 } |
|
137 |
|
138 saved_variable::~saved_variable (void) |
|
139 { |
1755
|
140 switch (type_tag) |
|
141 { |
|
142 case string_type: |
|
143 delete str_value; |
|
144 break; |
|
145 |
|
146 case generic: |
3263
|
147 // XXX FIXME XXX |
|
148 // delete [] gen_ptr_value; |
1755
|
149 break; |
|
150 |
|
151 default: |
|
152 break; |
|
153 } |
1
|
154 } |
|
155 |
|
156 void |
|
157 saved_variable::restore_value (void) |
|
158 { |
|
159 switch (type_tag) |
|
160 { |
2985
|
161 case boolean: |
|
162 *ptr_to_bool = bool_value; |
|
163 break; |
|
164 |
1
|
165 case integer: |
|
166 *ptr_to_int = int_value; |
|
167 break; |
777
|
168 |
1755
|
169 case string_type: |
3523
|
170 (static_cast<std::string *> (gen_ptr)) -> assign (*str_value); |
1755
|
171 break; |
|
172 |
1
|
173 case generic_ptr: |
|
174 *ptr_to_gen_ptr = gen_ptr_value; |
|
175 break; |
777
|
176 |
1
|
177 case generic: |
|
178 memcpy (gen_ptr, gen_ptr_value, size); |
|
179 break; |
777
|
180 |
1
|
181 default: |
|
182 panic_impossible (); |
777
|
183 break; |
1
|
184 } |
|
185 } |
|
186 |
2985
|
187 void |
|
188 saved_variable::restore (void *s) |
1
|
189 { |
2800
|
190 saved_variable *sv = static_cast<saved_variable *> (s); |
1
|
191 sv->restore_value (); |
|
192 delete sv; |
|
193 } |
|
194 |
|
195 void |
2985
|
196 unwind_protect::add (unwind_elem::cleanup_func fptr, void *ptr) |
|
197 { |
|
198 unwind_elem el (fptr, ptr); |
|
199 list.push (el); |
|
200 } |
|
201 |
|
202 void |
|
203 unwind_protect::run (void) |
|
204 { |
|
205 unwind_elem el = list.pop (); |
|
206 |
|
207 unwind_elem::cleanup_func f = el.fptr (); |
|
208 |
|
209 if (f) |
|
210 f (el.ptr ()); |
|
211 } |
|
212 |
|
213 void |
|
214 unwind_protect::discard (void) |
1
|
215 { |
2985
|
216 list.pop (); |
|
217 } |
|
218 |
|
219 void |
3523
|
220 unwind_protect::begin_frame (const std::string& tag) |
2985
|
221 { |
|
222 unwind_elem elem (tag); |
|
223 list.push (elem); |
|
224 } |
|
225 |
|
226 void |
3523
|
227 unwind_protect::run_frame (const std::string& tag) |
2985
|
228 { |
|
229 while (! list.empty ()) |
|
230 { |
|
231 unwind_elem el = list.pop (); |
|
232 |
|
233 unwind_elem::cleanup_func f = el.fptr (); |
|
234 |
|
235 if (f) |
|
236 f (el.ptr ()); |
|
237 |
|
238 if (tag == el.tag ()) |
|
239 break; |
|
240 } |
1
|
241 } |
|
242 |
|
243 void |
3523
|
244 unwind_protect::discard_frame (const std::string& tag) |
1755
|
245 { |
2985
|
246 while (! list.empty ()) |
|
247 { |
|
248 unwind_elem el = list.pop (); |
|
249 |
|
250 if (tag == el.tag ()) |
|
251 break; |
|
252 } |
|
253 } |
|
254 |
|
255 void |
|
256 unwind_protect::run_all (void) |
|
257 { |
|
258 while (! list.empty ()) |
|
259 { |
|
260 unwind_elem el = list.pop (); |
|
261 |
|
262 unwind_elem::cleanup_func f = el.fptr (); |
|
263 |
|
264 if (f) |
|
265 f (el.ptr ()); |
|
266 } |
1755
|
267 } |
|
268 |
|
269 void |
2985
|
270 unwind_protect::discard_all (void) |
|
271 { |
|
272 list.clear (); |
|
273 } |
|
274 |
|
275 void |
|
276 unwind_protect::save_bool (bool *ptr, bool value) |
|
277 { |
|
278 saved_variable *s = new saved_variable (ptr, value); |
|
279 add (saved_variable::restore, s); |
|
280 } |
|
281 |
|
282 void |
|
283 unwind_protect::save_int (int *ptr, int value) |
1
|
284 { |
|
285 saved_variable *s = new saved_variable (ptr, value); |
2985
|
286 add (saved_variable::restore, s); |
|
287 } |
|
288 |
|
289 void |
3523
|
290 unwind_protect::save_str (std::string *ptr, const std::string& value) |
2985
|
291 { |
|
292 saved_variable *s = new saved_variable (ptr, value); |
|
293 add (saved_variable::restore, s); |
|
294 } |
|
295 |
|
296 void |
|
297 unwind_protect::save_ptr (void **ptr, void *value) |
|
298 { |
|
299 saved_variable *s = new saved_variable (ptr, value); |
|
300 add (saved_variable::restore, s); |
1
|
301 } |
|
302 |
|
303 /* |
|
304 ;;; Local Variables: *** |
|
305 ;;; mode: C++ *** |
|
306 ;;; End: *** |
|
307 */ |