1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
1
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
1
|
20 |
|
21 */ |
|
22 |
383
|
23 #if !defined (octave_unwind_prot_h) |
|
24 #define octave_unwind_prot_h 1 |
1
|
25 |
1342
|
26 #include <cstddef> |
529
|
27 |
1755
|
28 #include <string> |
4214
|
29 #include <stack> |
1
|
30 |
453
|
31 class |
6109
|
32 OCTINTERP_API |
453
|
33 unwind_elem |
240
|
34 { |
2985
|
35 public: |
|
36 |
|
37 typedef void (*cleanup_func) (void *ptr); |
|
38 |
1755
|
39 unwind_elem (void) |
|
40 : ue_tag (), ue_fptr (0), ue_ptr (0) { } |
|
41 |
3523
|
42 unwind_elem (const std::string &t) |
1755
|
43 : ue_tag (t), ue_fptr (0), ue_ptr (0) { } |
|
44 |
|
45 unwind_elem (cleanup_func f, void *p) |
|
46 : ue_tag (), ue_fptr (f), ue_ptr (p) { } |
|
47 |
|
48 unwind_elem (const unwind_elem& el) |
|
49 : ue_tag (el.ue_tag), ue_fptr (el.ue_fptr), ue_ptr (el.ue_ptr) { } |
|
50 |
|
51 ~unwind_elem (void) { } |
240
|
52 |
1755
|
53 unwind_elem& operator = (const unwind_elem& el) |
|
54 { |
|
55 ue_tag = el.ue_tag; |
|
56 ue_fptr = el.ue_fptr; |
|
57 ue_ptr = el.ue_ptr; |
240
|
58 |
1755
|
59 return *this; |
|
60 } |
|
61 |
3523
|
62 std::string tag (void) { return ue_tag; } |
1755
|
63 |
|
64 cleanup_func fptr (void) { return ue_fptr; } |
|
65 |
|
66 void *ptr (void) { return ue_ptr; } |
240
|
67 |
2985
|
68 private: |
|
69 |
3523
|
70 std::string ue_tag; |
2985
|
71 |
1755
|
72 cleanup_func ue_fptr; |
2985
|
73 |
1755
|
74 void *ue_ptr; |
240
|
75 }; |
|
76 |
2985
|
77 class |
6109
|
78 OCTINTERP_API |
2985
|
79 unwind_protect |
|
80 { |
|
81 public: |
|
82 |
|
83 static void add (unwind_elem::cleanup_func fptr, void *ptr); |
|
84 |
|
85 static void run (void); |
|
86 |
|
87 static void discard (void); |
|
88 |
3523
|
89 static void begin_frame (const std::string& tag); |
2985
|
90 |
3523
|
91 static void run_frame (const std::string& tag); |
2985
|
92 |
3523
|
93 static void discard_frame (const std::string& tag); |
2985
|
94 |
|
95 static void run_all (void); |
|
96 |
|
97 static void discard_all (void); |
|
98 |
|
99 // Ways to save variables. |
|
100 |
|
101 static void save_bool (bool *ptr, bool value); |
|
102 |
|
103 static void save_int (int *ptr, int value); |
|
104 |
3523
|
105 static void save_str (std::string *ptr, const std::string& value); |
2985
|
106 |
|
107 static void save_ptr (void **ptr, void *value); |
|
108 |
|
109 static void save_var (void *ptr, void *value, size_t size); |
|
110 |
4214
|
111 static std::stack<unwind_elem> elt_list; |
2985
|
112 }; |
|
113 |
|
114 // We could get by without these macros, but they are nice to have... |
|
115 |
|
116 #define unwind_protect_bool(b) \ |
|
117 unwind_protect::save_bool (&(b), (b)) |
|
118 |
|
119 #define unwind_protect_int(i) \ |
|
120 unwind_protect::save_int (&(i), (i)) |
|
121 |
|
122 #define unwind_protect_str(s) \ |
|
123 unwind_protect::save_str (&(s), (s)) |
|
124 |
|
125 #define unwind_protect_ptr(p) \ |
5760
|
126 unwind_protect::save_ptr (reinterpret_cast<void **> (&(p)), \ |
|
127 reinterpret_cast<void *> (p)) |
|
128 |
5854
|
129 #define unwind_protect_fptr(p) \ |
|
130 unwind_protect::save_ptr (reinterpret_cast<void **> (&(p)), \ |
|
131 FCN_PTR_CAST (void *, p)) |
|
132 |
5760
|
133 #define unwind_protect_const_ptr(p) \ |
|
134 unwind_protect::save_ptr (const_cast<void **> (reinterpret_cast<const void **> (&(p))), \ |
|
135 const_cast<void *> (reinterpret_cast<const void *> (p))) |
2985
|
136 |
1
|
137 #endif |
|
138 |
|
139 /* |
|
140 ;;; Local Variables: *** |
|
141 ;;; mode: C++ *** |
|
142 ;;; End: *** |
|
143 */ |