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