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