annotate src/unwind-prot.h @ 10781:f7584d0ba5d3

allow unwind_protect register actions with const T& args
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 12 Jul 2010 10:52:15 +0200
parents 2ef5144ccb0d
children fd0a3ac60b0e
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
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
5 Copyright (C) 2009, 2010 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>
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
31 #include <memory>
240
a99f28f5e351 [project @ 1993-11-30 20:24:36 by jwe]
jwe
parents: 164
diff changeset
32
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
33 // This class allows registering cleanup actions.
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.
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
41 // Also, contains a pointer to the next element.
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
42 class elem
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
43 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
44 elem *next;
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
45
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
46 public:
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
47 virtual void run (void) { }
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
48 virtual ~elem (void) { }
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
49
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
50 friend class unwind_protect;
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
51 };
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
52
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
53 // An element that merely runs a void (*)(void) function.
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
54
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
55 class fcn_elem : public elem
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
56 {
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
57 public:
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
58 fcn_elem (void (*fptr) (void))
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
59 : e_fptr (fptr) { }
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
60
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
61 void run (void) { e_fptr (); }
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
62
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
63 private:
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
64 void (*e_fptr) (void);
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
65 };
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
66
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
67 // 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
68 // function pointer, and calls the function with the parameter.
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
69
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
70 template <class T>
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
71 class fcn_arg_elem : public elem
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
72 {
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
73 public:
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
74 fcn_arg_elem (void (*fcn) (T), T arg)
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
75 : e_fcn (fcn), e_arg (arg) { }
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
76
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
77 void run (void) { e_fcn (e_arg); }
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 private:
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
80 void (*e_fcn) (T);
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
81 T e_arg;
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
82 };
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
83
10781
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
84 // An element that stores a variable of type T along with a void (*) (const T&)
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
85 // function pointer, and calls the function with the parameter.
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
86
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
87 template <class T>
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
88 class fcn_crefarg_elem : public elem
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
89 {
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
90 public:
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
91 fcn_crefarg_elem (void (*fcn) (const T&), T arg)
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
92 : e_fcn (fcn), e_arg (arg) { }
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
93
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
94 void run (void) { e_fcn (e_arg); }
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
95
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
96 private:
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
97 void (*e_fcn) (const T&);
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
98 T e_arg;
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
99 };
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
100
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
101 // An element for calling a member function.
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
102
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
103 template <class T>
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
104 class method_elem : public elem
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
105 {
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
106 public:
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
107 method_elem (T *obj, void (T::*method) (void))
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
108 : e_obj (obj), e_method (method) { }
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
109
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
110 void run (void) { (e_obj->*e_method) (); }
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
111
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
112 private:
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
113 T *e_obj;
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
114 void (T::*e_method) (void);
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
115 };
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
116
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
117 // 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
118
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
119 template <class T>
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
120 class restore_var_elem : public elem
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
121 {
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
122 public:
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
123 restore_var_elem (T& ref, const T& val)
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
124 : e_ptr (&ref), e_val (val) { }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
125
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
126 void run (void) { *e_ptr = e_val; }
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
127
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
128 private:
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
129 T *e_ptr, e_val;
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
130 };
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
131
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
132 // Deletes a class allocated using new.
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
133
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
134 template <class T>
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
135 class delete_ptr_elem : public elem
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
136 {
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
137 public:
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
138 delete_ptr_elem (T *ptr)
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
139 : e_ptr (ptr) { }
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
140
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
141 void run (void) { delete e_ptr; }
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
142
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
143 private:
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
144 T *e_ptr;
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
145 };
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
146
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
147 unwind_protect (void) : head () { }
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
148
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
149 void add (elem *new_elem)
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
150 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
151 new_elem->next = head;
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
152 head = new_elem;
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
153 }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
154
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
155 // For backward compatibility.
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
156 void add (void (*fcn) (void *), void *ptr = 0)
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
157 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
158 add (new fcn_arg_elem<void *> (fcn, ptr));
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
159 }
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
160
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
161 // Call to void func (void).
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
162 void add_fcn (void (*fcn) (void))
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
163 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
164 add (new fcn_elem (fcn));
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
165 }
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
166
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
167 // Call to void func (T).
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
168 template <class T>
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
169 void add_fcn (void (*action) (T), T val)
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
170 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
171 add (new fcn_arg_elem<T> (action, val));
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
172 }
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
173
10781
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
174 // Call to void func (const T&).
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
175 template <class T>
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
176 void add_fcn (void (*action) (const T&), T val)
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
177 {
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
178 add (new fcn_crefarg_elem<T> (action, val));
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
179 }
f7584d0ba5d3 allow unwind_protect register actions with const T& args
Jaroslav Hajek <highegg@gmail.com>
parents: 10343
diff changeset
180
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
181 // Call to T::method (void).
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
182 template <class T>
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
183 void add_method (T *obj, void (T::*method) (void))
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
184 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
185 add (new method_elem<T> (obj, method));
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
186 }
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
187
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
188 // Call to delete (T*).
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
189
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
190 template <class T>
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
191 void add_delete (T *obj)
9396
17af7cce7d1b yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9394
diff changeset
192 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
193 add (new delete_ptr_elem<T> (obj));
9394
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
194 }
b3ab22ee8544 further improve unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 9384
diff changeset
195
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
196 // Protect any variable.
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
197 template <class T>
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
198 void protect_var (T& var)
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
199 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
200 add (new restore_var_elem<T> (var, var));
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
201 }
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
202
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
203 // Protect any variable, value given.
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
204 template <class T>
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
205 void protect_var (T& var, const T& val)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
206 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
207 add (new restore_var_elem<T> (var, val));
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
208 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
209
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
210 operator bool (void) const
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
211 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
212 return head != 0;
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
213 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
214
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
215 void run_top (void)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
216 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
217 if (head)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
218 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
219 // No leak on exception!
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
220 std::auto_ptr<elem> ptr (head);
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
221 head = ptr->next;
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
222 ptr->run ();
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
223 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
224 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
225
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
226 void run_top (int num)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
227 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
228 while (num-- > 0)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
229 run_top ();
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
230 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
231
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
232 void discard_top (void)
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
233 {
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
234 if (head)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
235 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
236 elem *ptr = head;
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
237 head = ptr->next;
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
238 delete ptr;
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
239 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
240 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
241
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
242 void discard_top (int num)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
243 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
244 while (num-- > 0)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
245 discard_top ();
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
246 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
247
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
248 void run (void)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
249 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
250 while (head)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
251 run_top ();
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
252 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
253
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
254 void discard (void)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
255 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
256 while (head)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
257 discard_top ();
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
258 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
259
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
260 // Destructor should not raise an exception, so all actions registered should
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
261 // be exception-safe (but setting error_state is allowed). If you're not sure,
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
262 // see unwind_protect_safe.
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
263 ~unwind_protect (void)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
264 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
265 run ();
9376
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
266 }
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
267
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
268 private:
d58086453171 refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
269
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
270 elem *head;
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
271 };
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
272
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
273 // Like unwind_protect, but this one will guard against the possibility of seeing
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
274 // an exception (or interrupt) in the cleanup actions. Not that we can do much about
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
275 // it, but at least we won't crash.
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
276
10343
2ef5144ccb0d Add few OCTINTERP_API tags.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10160
diff changeset
277 class
2ef5144ccb0d Add few OCTINTERP_API tags.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10160
diff changeset
278 OCTINTERP_API
2ef5144ccb0d Add few OCTINTERP_API tags.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10160
diff changeset
279 unwind_protect_safe : public unwind_protect
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
280 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
281 static void gripe_exception (void);
7752
40c428ea3408 initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents: 7336
diff changeset
282
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
283 public:
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
284 ~unwind_protect_safe (void)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
285 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
286 while (*this)
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
287 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
288 try
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
289 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
290 run_top ();
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
291 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
292 catch (...) // Yes, the black hole. Remember we're in a dtor.
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
293 {
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
294 gripe_exception ();
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
295 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
296 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
297 }
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 9396
diff changeset
298 };
2985
aa9d0c0e0458 [project @ 1997-05-16 06:54:18 by jwe]
jwe
parents: 2847
diff changeset
299
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
300 #endif