GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
unwind-prot.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2018 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
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License 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 <https://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  // No copying!
47 
48  unwind_protect (const unwind_protect&) = delete;
49 
50  unwind_protect& operator = (const unwind_protect&) = delete;
51 
52  // Destructor should not raise an exception, so all actions
53  // registered should be exception-safe. If you're not sure, see
54  // unwind_protect_safe.
55 
56  ~unwind_protect (void) { run (); }
57 
58  virtual void add (elem *new_elem)
59  {
60  lifo.push (new_elem);
61  }
62 
63  operator bool (void) const { return ! empty (); }
64 
65  void run_first (void)
66  {
67  if (! empty ())
68  {
69  // No leak on exception!
70  std::unique_ptr<elem> ptr (lifo.top ());
71  lifo.pop ();
72  ptr->run ();
73  }
74  }
75 
76  void discard_first (void)
77  {
78  if (! empty ())
79  {
80  elem *ptr = lifo.top ();
81  lifo.pop ();
82  delete ptr;
83  }
84  }
85 
86  size_t size (void) const { return lifo.size (); }
87 
88  protected:
89 
90  std::stack<elem *> lifo;
91  };
92 
93  // Like unwind_protect, but this one will guard against the possibility
94  // of seeing an exception (or interrupt) in the cleanup actions.
95  // Not that we can do much about it, but at least we won't crash.
96 
97  class
98  OCTAVE_API
100  {
101  private:
102 
103  void warn_unhandled_exception (void) const;
104 
105  public:
106 
108 
109  // No copying!
110 
111  unwind_protect_safe (const unwind_protect_safe&) = delete;
112 
113  unwind_protect_safe& operator = (const unwind_protect_safe&) = delete;
114 
116  {
117  while (! empty ())
118  {
119  try
120  {
121  run_first ();
122  }
123  catch (...) // Yes, the black hole. Remember we're in a destructor.
124  {
125  warn_unhandled_exception ();
126  }
127  }
128  }
129  };
130 }
131 
132 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
133 
134 OCTAVE_DEPRECATED (4.2, "use 'octave::unwind_protect' instead")
135 typedef octave::unwind_protect unwind_protect;
136 
137 OCTAVE_DEPRECATED (4.2, "use 'octave::unwind_protect_safe' instead")
138 typedef octave::unwind_protect_safe unwind_protect_safe;
139 
140 #endif
141 
142 #endif
virtual void add(elem *new_elem)
Definition: unwind-prot.h:58
std::stack< elem * > lifo
Definition: unwind-prot.h:90
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:1736
static int elem
Definition: __contourc__.cc:47
void discard_first(void)
Definition: unwind-prot.h:76
size_t size(void) const
Definition: unwind-prot.h:86