Mercurial > hg > octave-nkf
annotate src/unwind-prot.h @ 9445:c5f03874ea2a
simplify symbol_table::find and associated functions
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 22 Jul 2009 11:41:41 +0200 |
parents | 17af7cce7d1b |
children | 2cd940306a06 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2002, 2004, |
8920 | 4 2005, 2006, 2007, 2008 John W. Eaton |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5 Copyright (C) 2009 VZLU Prague |
1 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
1 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
1 | 22 |
23 */ | |
24 | |
383 | 25 #if !defined (octave_unwind_prot_h) |
26 #define octave_unwind_prot_h 1 | |
1 | 27 |
1342 | 28 #include <cstddef> |
529 | 29 |
1755 | 30 #include <string> |
4214 | 31 #include <stack> |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
32 #include <memory> |
240 | 33 |
2985 | 34 class |
6109 | 35 OCTINTERP_API |
2985 | 36 unwind_protect |
37 { | |
38 public: | |
39 | |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
40 // A generic unwind_protect element. Knows how to run itself and discard itself. |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
41 class elem |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
42 { |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
43 public: |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
44 virtual void run (void) { } |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
45 virtual ~elem (void) { } |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
46 }; |
2985 | 47 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
48 // An element that merely runs a void (*)(void) function. |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
49 |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
50 class fcn_elem : public elem |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
51 { |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
52 public: |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
53 fcn_elem (void (*fptr) (void)) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
54 : e_fptr (fptr) { } |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
55 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
56 void run (void) { e_fptr (); } |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
57 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
58 private: |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
59 void (*e_fptr) (void); |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
60 }; |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
61 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
62 // An element that stores a variable of type T along with a void (*) (T) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
63 // function pointer, and calls the function with the parameter. |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
64 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
65 template <class T> |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
66 class fcn_arg_elem : public elem |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
67 { |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
68 public: |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
69 fcn_arg_elem (void (*fcn) (T), T arg) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
70 : e_fcn (fcn), e_arg (arg) { } |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
71 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
72 void run (void) { e_fcn (e_arg); } |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
73 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
74 private: |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
75 void (*e_fcn) (T); |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
76 T e_arg; |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
77 }; |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
78 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
79 // An element for calling a member function. |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
80 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
81 template <class T> |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
82 class method_elem : public elem |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
83 { |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
84 public: |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
85 method_elem (T *obj, void (T::*method) (void)) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
86 : e_obj (obj), e_method (method) { } |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
87 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
88 void run (void) { (e_obj->*e_method) (); } |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
89 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
90 private: |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
91 T *e_obj; |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
92 void (T::*e_method) (void); |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
93 }; |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
94 |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
95 // An element that stores arbitrary variable, and restores it. |
2985 | 96 |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
97 template <class T> |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
98 class restore_var_elem : public elem |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
99 { |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
100 public: |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
101 restore_var_elem (T& ref, const T& val) |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
102 : e_ptr (&ref), e_val (val) { } |
2985 | 103 |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
104 void run (void) { *e_ptr = e_val; } |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
105 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
106 private: |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
107 T *e_ptr, e_val; |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
108 }; |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
109 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
110 // Deletes a class allocated using new. |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
111 |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
112 template <class T> |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
113 class delete_ptr_elem : public elem |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
114 { |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
115 public: |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
116 delete_ptr_elem (T *ptr) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
117 : e_ptr (ptr) { } |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
118 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
119 void run (void) { delete e_ptr; } |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
120 |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
121 private: |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
122 T *e_ptr; |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
123 }; |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
124 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
125 typedef size_t frame_id_t; |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
126 |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
127 // Generic. Users may subclass elem to provide their own cleanup. |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
128 static void add (elem *el) |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
129 { |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
130 elt_list.push (el); |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
131 } |
2985 | 132 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
133 static bool empty (void) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
134 { return elt_list.empty (); } |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
135 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
136 static void run (void) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
137 { |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
138 // Use auto_ptr, so that even if the following run () call throws an |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
139 // exception, we still clean up the element. |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
140 std::auto_ptr<elem> elt (elt_list.top ()); |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
141 elt_list.pop (); |
2985 | 142 |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
143 elt->run (); |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
144 } |
2985 | 145 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
146 static void discard (void) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
147 { |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
148 // No need to use ato_ptr here. |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
149 elem *elt = elt_list.top (); |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
150 elt_list.pop (); |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
151 |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
152 delete elt; |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
153 } |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
154 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
155 static frame_id_t begin_frame () |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
156 { |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
157 return elt_list.size (); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
158 } |
2985 | 159 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
160 static void run_frame (frame_id_t frame_id) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
161 { |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
162 while (elt_list.size () > frame_id) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
163 run (); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
164 } |
2985 | 165 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
166 static void discard_frame (frame_id_t frame_id) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
167 { |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
168 while (elt_list.size () > frame_id) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
169 discard (); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
170 } |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
171 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
172 // String tags are deprecated. Use the above trio. |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
173 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
174 static void begin_frame (const std::string& tag) GCC_ATTR_DEPRECATED; |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
175 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
176 static void run_frame (const std::string& tag) GCC_ATTR_DEPRECATED; |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
177 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
178 static void discard_frame (const std::string& tag) GCC_ATTR_DEPRECATED; |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
179 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
180 static void run_all (void) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
181 { |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
182 run_frame (0); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
183 while (! tag_list.empty ()) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
184 tag_list.pop (); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
185 } |
2985 | 186 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
187 static void discard_all (void) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
188 { |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
189 discard_frame (0); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
190 while (! tag_list.empty ()) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
191 tag_list.pop (); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
192 } |
2985 | 193 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
194 // For backward compatibility. |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
195 static void add (void (*fcn) (void *), void *ptr = 0) |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
196 { |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
197 elt_list.push (new fcn_arg_elem<void *> (fcn, ptr)); |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
198 } |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
199 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
200 // Call to void func (void). |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
201 static void add_fcn (void (*fcn) (void)) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
202 { |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
203 elt_list.push (new fcn_elem (fcn)); |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
204 } |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
205 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
206 // Call to void func (T). |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
207 template <class T> |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
208 static void add_fcn (void (*action) (T), T val) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
209 { |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
210 elt_list.push (new fcn_arg_elem<T> (action, val)); |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
211 } |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
212 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
213 // Call to T::method (void). |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
214 template <class T> |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
215 static void add_method (T *obj, void (T::*method) (void)) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
216 { |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
217 elt_list.push (new method_elem<T> (obj, method)); |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
218 } |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
219 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
220 // Call to delete (T*). |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
221 |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
222 template <class T> |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
223 static void add_delete (T *obj) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
224 { |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9394
diff
changeset
|
225 elt_list.push (new delete_ptr_elem<T> (obj)); |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
226 } |
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
227 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
228 // Protect any variable. |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
229 template <class T> |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
230 static void protect_var (T& var) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
231 { |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
232 elt_list.push (new restore_var_elem<T> (var, var)); |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
233 } |
2985 | 234 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
235 // Protect any variable, value given. |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
236 template <class T> |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
237 static void protect_var (T& var, const T& val) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
238 { |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
239 elt_list.push (new restore_var_elem<T> (var, val)); |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
240 } |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
241 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
242 private: |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
243 |
9394
b3ab22ee8544
further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
9384
diff
changeset
|
244 static std::stack<elem *> elt_list; |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
245 |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
246 static std::stack<std::pair <std::string, frame_id_t> > tag_list; |
2985 | 247 }; |
248 | |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
249 // Backward compatibility macros. Avoid them; use protect_var directly. |
2985 | 250 |
251 #define unwind_protect_bool(b) \ | |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
252 unwind_protect::protect_var (b) |
2985 | 253 |
254 #define unwind_protect_int(i) \ | |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
255 unwind_protect::protect_var (i) |
2985 | 256 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
257 #define unwind_protect_size_t(i) \ |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
258 unwind_protect::protect_var (i) |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
259 |
2985 | 260 #define unwind_protect_str(s) \ |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
261 unwind_protect::protect_var (s) |
2985 | 262 |
263 #define unwind_protect_ptr(p) \ | |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
264 unwind_protect::protect_var (p) |
5760 | 265 |
5854 | 266 #define unwind_protect_fptr(p) \ |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
267 unwind_protect::protect_var (p) |
5854 | 268 |
5760 | 269 #define unwind_protect_const_ptr(p) \ |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
270 unwind_protect::protect_var (p) |
2985 | 271 |
1 | 272 #endif |
273 | |
274 /* | |
275 ;;; Local Variables: *** | |
276 ;;; mode: C++ *** | |
277 ;;; End: *** | |
278 */ |