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