annotate src/unwind-prot.h @ 9394:b3ab22ee8544

further improve unwind_protect
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 25 Jun 2009 08:47:24 +0200
parents 8bec23396924
children 17af7cce7d1b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
1 /*
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
2
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2002, 2004,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 7752
diff changeset
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
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
6
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
7 This file is part of Octave.
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
8
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
9 Octave is free software; you can redistribute it and/or modify it
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
10 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6109
diff changeset
11 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6109
diff changeset
12 option) any later version.
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
13
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
14 Octave is distributed in the hope that it will be useful, but WITHOUT
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
17 for more details.
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
18
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6109
diff changeset
20 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6109
diff changeset
21 <http://www.gnu.org/licenses/>.
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
22
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
23 */
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
24
383
0b52c68ec81f [project @ 1994-03-09 21:19:44 by jwe]
jwe
parents: 240
diff changeset
25 #if !defined (octave_unwind_prot_h)
0b52c68ec81f [project @ 1994-03-09 21:19:44 by jwe]
jwe
parents: 240
diff changeset
26 #define octave_unwind_prot_h 1
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
27
1342
61bb2bdee11e [project @ 1995-09-04 00:19:22 by jwe]
jwe
parents: 1315
diff changeset
28 #include <cstddef>
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 453
diff changeset
29
1755
3a9462b655f1 [project @ 1996-01-22 04:47:22 by jwe]
jwe
parents: 1488
diff changeset
30 #include <string>
4214
b9317f3973ec [project @ 2002-12-04 04:57:01 by jwe]
jwe
parents: 4192
diff changeset
31 #include <stack>
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
32 #include <memory>
240
a99f28f5e351 [project @ 1993-11-30 20:24:36 by jwe]
jwe
parents: 164
diff changeset
33
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
34 class
6109
c68896f193e3 [project @ 2006-10-27 02:16:18 by jwe]
jwe
parents: 5854
diff changeset
35 OCTINTERP_API
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
36 unwind_protect
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
37 {
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
38 public:
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
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
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
47
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
48 // An element that merely runs a void (*)(void *) function.
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:
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
53 fcn_elem (void (*fptr) (void *), void *ptr)
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
54 : e_fptr (fptr), e_ptr (ptr) { }
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
55
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
56 void run (void) { e_fptr (e_ptr); }
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:
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
59 void (*e_fptr) (void *);
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
60 void *e_ptr;
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
61 };
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
62
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
63 // An element that stores arbitrary variable, and restores it.
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
64
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
65 template <class T>
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
66 class restore_var_elem : public elem
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
67 {
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
68 public:
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
69 restore_var_elem (T& ref, const T& val)
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
70 : e_ptr (&ref), e_val (val) { }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
71
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
72 void run (void) { *e_ptr = e_val; }
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
73
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
74 private:
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
75 T *e_ptr, e_val;
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
76 };
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
77
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
78 // An element that stores a variable of type T along with a void (*) (T)
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
79 // function pointer, and calls the function with the parameter.
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
80
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
81 template <class T>
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
82 class action_var_elem : public elem
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
83 {
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
84 public:
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
85 action_var_elem (void (*fcn) (T), T val)
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
86 : e_fcn (fcn), e_val (val) { }
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
87
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
88 void run (void) { e_fcn (e_val); }
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
89
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
90 private:
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
91 void (*e_fcn) (T);
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
92 T e_val;
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
93 };
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
94
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
95 typedef size_t frame_id_t;
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
96
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
97 // Generic. Users may subclass elem to provide their own cleanup.
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
98 static void add (elem *el)
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
99 {
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
100 elt_list.push (el);
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
101 }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
102
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
103 static bool empty (void)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
104 { return elt_list.empty (); }
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 static void run (void)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
107 {
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
108 // 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
109 // exception, we still clean up the element.
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
110 std::auto_ptr<elem> elt (elt_list.top ());
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
111 elt_list.pop ();
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
112
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
113 elt->run ();
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
114 }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
115
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
116 static void discard (void)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
117 {
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
118 // No need to use ato_ptr here.
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
119 elem *elt = elt_list.top ();
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
120 elt_list.pop ();
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
121
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
122 delete elt;
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
123 }
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 static frame_id_t begin_frame ()
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
126 {
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
127 return elt_list.size ();
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
128 }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
129
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
130 static void run_frame (frame_id_t frame_id)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
131 {
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
132 while (elt_list.size () > frame_id)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
133 run ();
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
134 }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
135
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
136 static void discard_frame (frame_id_t frame_id)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
137 {
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
138 while (elt_list.size () > frame_id)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
139 discard ();
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
140 }
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
141
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
142 // String tags are deprecated. Use the above trio.
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
143
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
144 static void begin_frame (const std::string& tag) GCC_ATTR_DEPRECATED;
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
145
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
146 static void run_frame (const std::string& tag) GCC_ATTR_DEPRECATED;
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
147
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
148 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
149
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
150 static void run_all (void)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
151 {
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
152 run_frame (0);
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
153 while (! tag_list.empty ())
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
154 tag_list.pop ();
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
155 }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
156
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
157 static void discard_all (void)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
158 {
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
159 discard_frame (0);
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
160 while (! tag_list.empty ())
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
161 tag_list.pop ();
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
162 }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
163
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
164 static void add (void (*fcn) (void *), void *ptr = 0)
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
165 {
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
166 elt_list.push (new fcn_elem (fcn, ptr));
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
167 }
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
168
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
169 // Protect any variable.
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
170 template <class T>
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
171 static void protect_var (T& var)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
172 {
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
173 elt_list.push (new restore_var_elem<T> (var, var));
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
174 }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
175
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
176 // Protect any variable, value given.
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
177 template <class T>
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
178 static void protect_var (T& var, const T& val)
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
179 {
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
180 elt_list.push (new restore_var_elem<T> (var, val));
9376
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
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
183 // Store a function pointer together with a single argument (passed by value).
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
184 template <class T>
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
185 static void add_action_var (void (*action) (T), T val)
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
186 {
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
187 elt_list.push (new action_var_elem<T> (action, val));
9376
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
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
190 private:
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
191
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
192 static std::stack<elem *> elt_list;
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
193
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
194 static std::stack<std::pair <std::string, frame_id_t> > tag_list;
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
195 };
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
196
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
197 // Backward compatibility macros. Avoid them; use protect_var directly.
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
198
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
199 #define unwind_protect_bool(b) \
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
200 unwind_protect::protect_var (b)
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
201
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
202 #define unwind_protect_int(i) \
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
203 unwind_protect::protect_var (i)
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
204
7752
40c428ea3408 initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents: 7336
diff changeset
205 #define unwind_protect_size_t(i) \
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
206 unwind_protect::protect_var (i)
7752
40c428ea3408 initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents: 7336
diff changeset
207
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
208 #define unwind_protect_str(s) \
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
209 unwind_protect::protect_var (s)
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
210
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
211 #define unwind_protect_ptr(p) \
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
212 unwind_protect::protect_var (p)
5760
8d7162924bd3 [project @ 2006-04-14 04:01:37 by jwe]
jwe
parents: 5307
diff changeset
213
5854
68f8017ef077 [project @ 2006-06-12 15:54:27 by jwe]
jwe
parents: 5760
diff changeset
214 #define unwind_protect_fptr(p) \
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
215 unwind_protect::protect_var (p)
5854
68f8017ef077 [project @ 2006-06-12 15:54:27 by jwe]
jwe
parents: 5760
diff changeset
216
5760
8d7162924bd3 [project @ 2006-04-14 04:01:37 by jwe]
jwe
parents: 5307
diff changeset
217 #define unwind_protect_const_ptr(p) \
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
218 unwind_protect::protect_var (p)
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
219
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
220 #endif
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
221
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
222 /*
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
223 ;;; Local Variables: ***
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
224 ;;; mode: C++ ***
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
225 ;;; End: ***
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
226 */