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
ov-oncleanup.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2010-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any 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, see
19 <http://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 "pt-misc.h"
33 
35  "onCleanup");
36 
38  : fcn (f)
39 {
40  if (f.is_function_handle ())
41  {
43  if (! fptr)
44  error ("onCleanup: no default dispatch for function handle");
45 
47  = dynamic_cast<octave_user_function *> (fptr);
48 
49  if (uptr != 0)
50  {
51  tree_parameter_list *pl = uptr->parameter_list ();
52 
53  if (pl != 0 && pl->length () > 0)
54  warning ("onCleanup: cleanup action takes parameters");
55  }
56  }
57  else
58  {
59  fcn = octave_value ();
60  error ("onCleanup: argument must be a function handle");
61  }
62 }
63 
65 {
66  if (fcn.is_undefined ())
67  return;
68 
70 
71  // Clear interrupts.
74 
75  // Disallow quit().
76  frame.protect_var (quit_allowed);
77  quit_allowed = false;
78 
79  interpreter_try (frame);
80 
81  try
82  {
83  // Run the actual code.
85  }
86  catch (const octave::interrupt_exception&)
87  {
89 
90  warning ("onCleanup: interrupt occurred in cleanup action");
91  }
92  catch (const octave::execution_exception&)
93  {
95  warning ("onCleanup: error caught while executing cleanup function:\n%s\n",
96  msg.c_str ());
97 
98  }
99  catch (const octave::exit_exception&)
100  {
101  // This shouldn't happen since we disabled quit above.
102  warning ("onCleanup: exit disabled while executing cleanup function");
103  }
104  catch (...) // Yes, the black hole. We're in a d-tor.
105  {
106  // This shouldn't happen, in theory.
107  warning ("onCleanup: internal error: unhandled exception in cleanup action");
108  }
109 }
110 
113 {
115  retval.setfield ("task", fcn);
116  return retval;
117 }
118 
119 bool
120 octave_oncleanup::save_ascii (std::ostream& /* os */)
121 {
122  warning ("save: unable to save onCleanup variables, skipping");
123 
124  return true;
125 }
126 
127 bool
128 octave_oncleanup::load_ascii (std::istream& /* is */)
129 {
130  // Silently skip object that was not saved
131  return true;
132 }
133 
134 bool
135 octave_oncleanup::save_binary (std::ostream& /* os */,
136  bool& /* save_as_floats */)
137 {
138  warning ("save: unable to save onCleanup variables, skipping");
139 
140  return true;
141 }
142 
143 bool
144 octave_oncleanup::load_binary (std::istream& /* is */, bool /* swap */,
146 {
147  // Silently skip object that was not saved
148  return true;
149 }
150 
151 bool
153  const char * /* name */,
154  bool /* save_as_floats */)
155 {
156  warning ("save: unable to save onCleanup variables, skipping");
157 
158  return true;
159 }
160 
161 bool
163  const char * /* name */)
164 {
165  // Silently skip object that was not saved
166  return true;
167 }
168 
169 void
170 octave_oncleanup::print (std::ostream& os, bool pr_as_read_syntax)
171 {
172  print_raw (os, pr_as_read_syntax);
173  newline (os);
174 }
175 
176 void
177 octave_oncleanup::print_raw (std::ostream& os, bool pr_as_read_syntax) const
178 {
179  os << "onCleanup (";
180  if (fcn.is_defined ())
181  fcn.print_raw (os, pr_as_read_syntax);
182  os << ")";
183 }
184 
185 DEFUN (onCleanup, args, ,
186  doc: /* -*- texinfo -*-
187 @deftypefn {} {@var{obj} =} onCleanup (@var{function})
188 Create a special object that executes a given function upon destruction.
189 
190 If the object is copied to multiple variables (or cell or struct array
191 elements) or returned from a function, @var{function} will be executed after
192 clearing the last copy of the object. Note that if multiple local onCleanup
193 variables are created, the order in which they are called is unspecified.
194 For similar functionality @xref{The unwind_protect Statement}.
195 @end deftypefn */)
196 {
197  if (args.length () != 1)
198  print_usage ();
199 
200  return ovl (new octave_oncleanup (args(0)));
201 }
202 
203 /*
204 %!test
205 %! old_wstate = warning ("query");
206 %! unwind_protect
207 %! trigger = onCleanup (@() warning ("on", "__MY_WARNING__"));
208 %! warning ("off", "__MY_WARNING__");
209 %! assert ((warning ("query", "__MY_WARNING__")).state, "off");
210 %! clear trigger;
211 %! assert ((warning ("query", "__MY_WARNING__")).state, "on");
212 %! unwind_protect_cleanup
213 %! warning (old_wstate);
214 %! end_unwind_protect
215 */
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
tree_parameter_list * parameter_list(void)
Definition: ov-usr-fcn.h:377
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
OCTINTERP_API void print_usage(void)
Definition: defun.cc:52
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE * f
octave_value fcn
Definition: ov-oncleanup.h:94
bool is_defined(void) const
Definition: ov.h:536
void protect_var(T &var)
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
void error(const char *fmt,...)
Definition: error.cc:570
void setfield(const std::string &key, const octave_value &val)
Definition: oct-map.cc:178
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:169
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
void newline(std::ostream &os) const
Definition: ov-base.cc:1381
bool is_function_handle(void) const
Definition: ov.h:702
octave_function * fcn
Definition: ov-class.cc:1743
JNIEnv void * args
Definition: ov-java.cc:67
octave_value_list do_multi_index_op(int nargout, const octave_value_list &idx)
Definition: ov.cc:1527
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov.h:1219
octave_value retval
Definition: data.cc:6294
int64_t octave_hdf5_id
sig_atomic_t octave_interrupt_state
Definition: cquit.c:58
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:211
octave_function * function_value(bool silent=false) const
Definition: ov.cc:1705
bool save_ascii(std::ostream &os)
void warning(const char *fmt,...)
Definition: error.cc:788
octave::unwind_protect frame
Definition: graphics.cc:11584
void recover_from_exception(void)
Definition: interpreter.cc:200
OCTINTERP_API std::string last_error_message(void)
octave_scalar_map scalar_map_value(void) const
size_t length(void) const
Definition: base-list.h:50
bool is_undefined(void) const
Definition: ov.h:539
void print(std::ostream &os, bool pr_as_read_syntax=false)
bool load_ascii(std::istream &is)
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
Definition: lo-specfun.cc:1724
bool quit_allowed
Definition: interpreter.cc:75
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:854
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
OCTINTERP_API void interpreter_try(octave::unwind_protect &)