GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
unwind-prot.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2013 John W. Eaton
4 Copyright (C) 2009-2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if !defined (octave_unwind_prot_h)
25 #define octave_unwind_prot_h 1
26 
27 #include <cstddef>
28 
29 #include <stack>
30 #include <memory>
31 
32 #include "action-container.h"
33 
34 class
35 OCTAVE_API
37 {
38 public:
39 
40  unwind_protect (void) : lifo () { }
41 
42  // Destructor should not raise an exception, so all actions
43  // registered should be exception-safe (but setting error_state is
44  // allowed). If you're not sure, see unwind_protect_safe.
45 
46  ~unwind_protect (void) { run (); }
47 
48  virtual void add (elem *new_elem)
49  {
50  lifo.push (new_elem);
51  }
52 
53  void add (void (*fcn) (void *), void *ptr = 0) GCC_ATTR_DEPRECATED
54  {
55  add (new fcn_arg_elem<void *> (fcn, ptr));
56  }
57 
58  operator bool (void) const { return ! empty (); }
59 
60  void run_top (void) GCC_ATTR_DEPRECATED { run_first (); }
61 
62  void run_first (void)
63  {
64  if (! empty ())
65  {
66  // No leak on exception!
67  std::auto_ptr<elem> ptr (lifo.top ());
68  lifo.pop ();
69  ptr->run ();
70  }
71  }
72 
73  void run_top (int num) GCC_ATTR_DEPRECATED { run (num); }
74 
75  void discard_top (void) GCC_ATTR_DEPRECATED { discard_first (); }
76 
77  void discard_first (void)
78  {
79  if (! empty ())
80  {
81  elem *ptr = lifo.top ();
82  lifo.pop ();
83  delete ptr;
84  }
85  }
86 
87  void discard_top (int num) GCC_ATTR_DEPRECATED { discard (num); }
88 
89  size_t size (void) const { return lifo.size (); }
90 
91 protected:
92 
93  std::stack<elem *> lifo;
94 
95 private:
96 
97  // No copying!
98 
100 
101  unwind_protect& operator = (const unwind_protect&);
102 };
103 
104 // Like unwind_protect, but this one will guard against the
105 // possibility of seeing an exception (or interrupt) in the cleanup
106 // actions. Not that we can do much about it, but at least we won't
107 // crash.
108 
109 class
110 OCTAVE_API
112 {
113 private:
114 
115  static void gripe_exception (void);
116 
117 public:
118 
120 
122  {
123  while (! empty ())
124  {
125  try
126  {
127  run_first ();
128  }
129  catch (...) // Yes, the black hole. Remember we're in a dtor.
130  {
131  gripe_exception ();
132  }
133  }
134  }
135 
136 private:
137 
138  // No copying!
139 
141 
142  unwind_protect_safe& operator = (const unwind_protect_safe&);
143 };
144 
145 #endif