GNU Octave  4.2.1
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-2017 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 "octave-config.h"
28 
29 #include <cstddef>
30 
31 #include <stack>
32 #include <memory>
33 
34 #include "action-container.h"
35 
36 namespace octave
37 {
38  class
39  OCTAVE_API
41  {
42  public:
43 
44  unwind_protect (void) : lifo () { }
45 
46  // Destructor should not raise an exception, so all actions
47  // registered should be exception-safe. If you're not sure, see
48  // unwind_protect_safe.
49 
50  ~unwind_protect (void) { run (); }
51 
52  virtual void add (elem *new_elem)
53  {
54  lifo.push (new_elem);
55  }
56 
57  OCTAVE_DEPRECATED ("use 'add (new fcn_arg_elem<void *> (fcn, ptr))' instead")
58  void add (void (*fcn) (void *), void *ptr = 0)
59  {
60  add (new fcn_arg_elem<void *> (fcn, ptr));
61  }
62 
63  operator bool (void) const { return ! empty (); }
64 
65  OCTAVE_DEPRECATED ("use 'run_first' instead")
66  void run_top (void) { run_first (); }
67 
68  void run_first (void)
69  {
70  if (! empty ())
71  {
72  // No leak on exception!
73  std::unique_ptr<elem> ptr (lifo.top ());
74  lifo.pop ();
75  ptr->run ();
76  }
77  }
78 
79  OCTAVE_DEPRECATED ("use 'run' instead")
80  void run_top (int num) { run (num); }
81 
82  OCTAVE_DEPRECATED ("use 'discard_first' instead")
83  void discard_top (void) { discard_first (); }
84 
85  void discard_first (void)
86  {
87  if (! empty ())
88  {
89  elem *ptr = lifo.top ();
90  lifo.pop ();
91  delete ptr;
92  }
93  }
94 
95  OCTAVE_DEPRECATED ("use 'discard' instead")
96  void discard_top (int num) { discard (num); }
97 
98  size_t size (void) const { return lifo.size (); }
99 
100  protected:
101 
102  std::stack<elem *> lifo;
103 
104  private:
105 
106  // No copying!
107 
108  unwind_protect (const unwind_protect&);
109 
110  unwind_protect& operator = (const unwind_protect&);
111  };
112 
113  // Like unwind_protect, but this one will guard against the possibility
114  // of seeing an exception (or interrupt) in the cleanup actions.
115  // Not that we can do much about it, but at least we won't crash.
116 
117  class
118  OCTAVE_API
120  {
121  private:
122 
123  void warn_unhandled_exception (void) const;
124 
125  public:
126 
128 
130  {
131  while (! empty ())
132  {
133  try
134  {
135  run_first ();
136  }
137  catch (...) // Yes, the black hole. Remember we're in a destructor.
138  {
139  warn_unhandled_exception ();
140  }
141  }
142  }
143 
144  private:
145 
146  // No copying!
147 
149 
150  unwind_protect_safe& operator = (const unwind_protect_safe&);
151  };
152 }
153 
154 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
155 
156 OCTAVE_DEPRECATED ("use 'octave::unwind_protect' instead")
157 typedef octave::unwind_protect unwind_protect;
158 
159 OCTAVE_DEPRECATED ("use 'octave::unwind_protect_safe' instead")
160 typedef octave::unwind_protect_safe unwind_protect_safe;
161 
162 #endif
163 
164 #endif
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
virtual void add(elem *new_elem)
Definition: unwind-prot.h:52
octave_function * fcn
Definition: ov-class.cc:1743
create a structure array and initialize its values The dimensions of each cell array of values must match Singleton cells and non cell values are repeated so that they fill the entire array If the cells are empty
Definition: ov-struct.cc:1688
static int elem
Definition: __contourc__.cc:50
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable or any other valid Octave code The number of return their size
Definition: input.cc:871