GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-oncleanup.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2010-2018 VZLU Prague
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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "defun.h"
28 #include "interpreter.h"
29 #include "ov-oncleanup.h"
30 #include "ov-fcn.h"
31 #include "ov-usr-fcn.h"
32 #include "parse.h"
33 #include "pt-misc.h"
34 
36  "onCleanup");
37 
39  : fcn (f)
40 {
41  if (f.is_function_handle ())
42  {
43  octave_function *fptr = f.function_value (true);
44  if (! fptr)
45  error ("onCleanup: no default dispatch for function handle");
46 
48  = dynamic_cast<octave_user_function *> (fptr);
49 
50  if (uptr != nullptr)
51  {
53 
54  if (pl != nullptr && pl->length () > 0)
55  warning ("onCleanup: cleanup action takes parameters");
56  }
57  }
58  else
59  {
60  fcn = octave_value ();
61  error ("onCleanup: argument must be a function handle");
62  }
63 }
64 
66 {
67  if (fcn.is_undefined ())
68  return;
69 
71 
72  // Clear interrupts.
75 
76  // Disallow quit().
78  quit_allowed = false;
79 
81 
82  try
83  {
84  // Run the actual code.
86  }
87  catch (const octave::interrupt_exception&)
88  {
90 
91  warning ("onCleanup: interrupt occurred in cleanup action");
92  }
93  catch (const octave::execution_exception&)
94  {
96  warning ("onCleanup: error caught while executing cleanup function:\n%s\n",
97  msg.c_str ());
98 
99  }
100  catch (const octave::exit_exception&)
101  {
102  // This shouldn't happen since we disabled quit above.
103  warning ("onCleanup: exit disabled while executing cleanup function");
104  }
105  catch (...) // Yes, the black hole. We're in a d-tor.
106  {
107  // This shouldn't happen, in theory.
108  warning ("onCleanup: internal error: unhandled exception in cleanup action");
109  }
110 }
111 
114 {
116  retval.setfield ("task", fcn);
117  return retval;
118 }
119 
120 bool
121 octave_oncleanup::save_ascii (std::ostream& /* os */)
122 {
123  warning ("save: unable to save onCleanup variables, skipping");
124 
125  return true;
126 }
127 
128 bool
129 octave_oncleanup::load_ascii (std::istream& /* is */)
130 {
131  // Silently skip object that was not saved
132  return true;
133 }
134 
135 bool
136 octave_oncleanup::save_binary (std::ostream& /* os */,
137  bool& /* save_as_floats */)
138 {
139  warning ("save: unable to save onCleanup variables, skipping");
140 
141  return true;
142 }
143 
144 bool
145 octave_oncleanup::load_binary (std::istream& /* is */, bool /* swap */,
147 {
148  // Silently skip object that was not saved
149  return true;
150 }
151 
152 bool
154  const char * /* name */,
155  bool /* save_as_floats */)
156 {
157  warning ("save: unable to save onCleanup variables, skipping");
158 
159  return true;
160 }
161 
162 bool
164  const char * /* name */)
165 {
166  // Silently skip object that was not saved
167  return true;
168 }
169 
170 void
171 octave_oncleanup::print (std::ostream& os, bool pr_as_read_syntax)
172 {
173  print_raw (os, pr_as_read_syntax);
174  newline (os);
175 }
176 
177 void
178 octave_oncleanup::print_raw (std::ostream& os, bool pr_as_read_syntax) const
179 {
180  os << "onCleanup (";
181  if (fcn.is_defined ())
182  fcn.print_raw (os, pr_as_read_syntax);
183  os << ')';
184 }
185 
186 DEFUN (onCleanup, args, ,
187  doc: /* -*- texinfo -*-
188 @deftypefn {} {@var{obj} =} onCleanup (@var{function})
189 Create a special object that executes a given function upon destruction.
190 
191 If the object is copied to multiple variables (or cell or struct array
192 elements) or returned from a function, @var{function} will be executed after
193 clearing the last copy of the object. Note that if multiple local onCleanup
194 variables are created, the order in which they are called is unspecified.
195 For similar functionality @xref{The unwind_protect Statement}.
196 @end deftypefn */)
197 {
198  if (args.length () != 1)
199  print_usage ();
200 
201  return ovl (new octave_oncleanup (args(0)));
202 }
203 
204 /*
205 %!test
206 %! old_wstate = warning ("query");
207 %! unwind_protect
208 %! trigger = onCleanup (@() warning ("on", "__MY_WARNING__"));
209 %! warning ("off", "__MY_WARNING__");
210 %! assert ((warning ("query", "__MY_WARNING__")).state, "off");
211 %! clear trigger;
212 %! assert ((warning ("query", "__MY_WARNING__")).state, "on");
213 %! unwind_protect_cleanup
214 %! warning (old_wstate);
215 %! end_unwind_protect
216 */
OCTINTERP_API octave_value_list feval(const std::string &name, const octave_value_list &args=octave_value_list(), int nargout=0)
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
static void recover_from_exception(void)
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
octave_value fcn
Definition: ov-oncleanup.h:94
void newline(std::ostream &os) const
Definition: ov-base.cc:1328
size_t length(void) const
Definition: base-list.h:50
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
void error(const char *fmt,...)
Definition: error.cc:578
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:180
bool is_defined(void) const
Definition: ov.h:523
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
octave_function * fcn
Definition: ov-class.cc:1754
octave::tree_parameter_list * parameter_list(void)
Definition: ov-usr-fcn.h:384
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov.h:1268
octave_value retval
Definition: data.cc:6246
int64_t octave_hdf5_id
sig_atomic_t octave_interrupt_state
Definition: cquit.c:32
bool save_binary(std::ostream &os, bool &save_as_floats)
octave_oncleanup(void)
Definition: ov-oncleanup.h:38
friend class octave_value
Definition: ov-base.h:228
bool save_ascii(std::ostream &os)
void warning(const char *fmt,...)
Definition: error.cc:801
octave::unwind_protect frame
Definition: graphics.cc:12190
OCTINTERP_API std::string last_error_message(void)
bool is_undefined(void) const
Definition: ov.h:526
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
Definition: lo-specfun.cc:1129
void print(std::ostream &os, bool pr_as_read_syntax=false)
bool load_ascii(std::istream &is)
octave_scalar_map scalar_map_value(void) const
bool quit_allowed
Definition: interpreter.cc:77
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
octave::stream os
Definition: file-io.cc:627
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
OCTINTERP_API void interpreter_try(octave::unwind_protect &)