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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
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 |
|
32 unwind_elem |
240
|
33 { |
2985
|
34 public: |
|
35 |
|
36 typedef void (*cleanup_func) (void *ptr); |
|
37 |
1755
|
38 unwind_elem (void) |
|
39 : ue_tag (), ue_fptr (0), ue_ptr (0) { } |
|
40 |
3523
|
41 unwind_elem (const std::string &t) |
1755
|
42 : ue_tag (t), ue_fptr (0), ue_ptr (0) { } |
|
43 |
|
44 unwind_elem (cleanup_func f, void *p) |
|
45 : ue_tag (), ue_fptr (f), ue_ptr (p) { } |
|
46 |
|
47 unwind_elem (const unwind_elem& el) |
|
48 : ue_tag (el.ue_tag), ue_fptr (el.ue_fptr), ue_ptr (el.ue_ptr) { } |
|
49 |
|
50 ~unwind_elem (void) { } |
240
|
51 |
1755
|
52 unwind_elem& operator = (const unwind_elem& el) |
|
53 { |
|
54 ue_tag = el.ue_tag; |
|
55 ue_fptr = el.ue_fptr; |
|
56 ue_ptr = el.ue_ptr; |
240
|
57 |
1755
|
58 return *this; |
|
59 } |
|
60 |
3523
|
61 std::string tag (void) { return ue_tag; } |
1755
|
62 |
|
63 cleanup_func fptr (void) { return ue_fptr; } |
|
64 |
|
65 void *ptr (void) { return ue_ptr; } |
240
|
66 |
2985
|
67 private: |
|
68 |
3523
|
69 std::string ue_tag; |
2985
|
70 |
1755
|
71 cleanup_func ue_fptr; |
2985
|
72 |
1755
|
73 void *ue_ptr; |
240
|
74 }; |
|
75 |
2985
|
76 class |
|
77 unwind_protect |
|
78 { |
|
79 public: |
|
80 |
|
81 static void add (unwind_elem::cleanup_func fptr, void *ptr); |
|
82 |
|
83 static void run (void); |
|
84 |
|
85 static void discard (void); |
|
86 |
3523
|
87 static void begin_frame (const std::string& tag); |
2985
|
88 |
3523
|
89 static void run_frame (const std::string& tag); |
2985
|
90 |
3523
|
91 static void discard_frame (const std::string& tag); |
2985
|
92 |
|
93 static void run_all (void); |
|
94 |
|
95 static void discard_all (void); |
|
96 |
|
97 // Ways to save variables. |
|
98 |
|
99 static void save_bool (bool *ptr, bool value); |
|
100 |
|
101 static void save_int (int *ptr, int value); |
|
102 |
3523
|
103 static void save_str (std::string *ptr, const std::string& value); |
2985
|
104 |
|
105 static void save_ptr (void **ptr, void *value); |
|
106 |
|
107 static void save_var (void *ptr, void *value, size_t size); |
|
108 |
4214
|
109 static std::stack<unwind_elem> elt_list; |
2985
|
110 }; |
|
111 |
|
112 // We could get by without these macros, but they are nice to have... |
|
113 |
|
114 #define unwind_protect_bool(b) \ |
|
115 unwind_protect::save_bool (&(b), (b)) |
|
116 |
|
117 #define unwind_protect_int(i) \ |
|
118 unwind_protect::save_int (&(i), (i)) |
|
119 |
|
120 #define unwind_protect_str(s) \ |
|
121 unwind_protect::save_str (&(s), (s)) |
|
122 |
|
123 #define unwind_protect_ptr(p) \ |
3145
|
124 unwind_protect::save_ptr (X_CAST (void **, &(p)), X_CAST (void *, (p))) |
2985
|
125 |
1
|
126 #endif |
|
127 |
|
128 /* |
|
129 ;;; Local Variables: *** |
|
130 ;;; mode: C++ *** |
|
131 ;;; End: *** |
|
132 */ |