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 |
|
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 |
|
78 unwind_protect |
|
79 { |
|
80 public: |
|
81 |
|
82 static void add (unwind_elem::cleanup_func fptr, void *ptr); |
|
83 |
|
84 static void run (void); |
|
85 |
|
86 static void discard (void); |
|
87 |
3523
|
88 static void begin_frame (const std::string& tag); |
2985
|
89 |
3523
|
90 static void run_frame (const std::string& tag); |
2985
|
91 |
3523
|
92 static void discard_frame (const std::string& tag); |
2985
|
93 |
|
94 static void run_all (void); |
|
95 |
|
96 static void discard_all (void); |
|
97 |
|
98 // Ways to save variables. |
|
99 |
|
100 static void save_bool (bool *ptr, bool value); |
|
101 |
|
102 static void save_int (int *ptr, int value); |
|
103 |
3523
|
104 static void save_str (std::string *ptr, const std::string& value); |
2985
|
105 |
|
106 static void save_ptr (void **ptr, void *value); |
|
107 |
|
108 static void save_var (void *ptr, void *value, size_t size); |
|
109 |
4214
|
110 static std::stack<unwind_elem> elt_list; |
2985
|
111 }; |
|
112 |
|
113 // We could get by without these macros, but they are nice to have... |
|
114 |
|
115 #define unwind_protect_bool(b) \ |
|
116 unwind_protect::save_bool (&(b), (b)) |
|
117 |
|
118 #define unwind_protect_int(i) \ |
|
119 unwind_protect::save_int (&(i), (i)) |
|
120 |
|
121 #define unwind_protect_str(s) \ |
|
122 unwind_protect::save_str (&(s), (s)) |
|
123 |
|
124 #define unwind_protect_ptr(p) \ |
3145
|
125 unwind_protect::save_ptr (X_CAST (void **, &(p)), X_CAST (void *, (p))) |
2985
|
126 |
1
|
127 #endif |
|
128 |
|
129 /* |
|
130 ;;; Local Variables: *** |
|
131 ;;; mode: C++ *** |
|
132 ;;; End: *** |
|
133 */ |