GNU Octave  4.0.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
ov-oncleanup.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2010-2015 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "defun.h"
28 #include "ov-oncleanup.h"
29 #include "ov-fcn.h"
30 #include "ov-usr-fcn.h"
31 #include "pt-misc.h"
32 #include "toplev.h"
33 
35  "onCleanup");
36 
38  : fcn (f)
39 {
40  if (f.is_function_handle ())
41  {
43  if (fptr)
44  {
46  = dynamic_cast<octave_user_function *> (fptr);
47 
48  if (uptr != 0)
49  {
50  tree_parameter_list *pl = uptr->parameter_list ();
51 
52  if (pl != 0 && pl->length () > 0)
53  warning ("onCleanup: cleanup action takes parameters");
54  }
55  }
56  else
57  error ("onCleanup: no default dispatch for function handle");
58  }
59  else
60  {
61  fcn = octave_value ();
62  error ("onCleanup: argument must be a function handle");
63  }
64 }
65 
67 {
68  if (fcn.is_undefined ())
69  return;
70 
71  unwind_protect frame;
72 
73  // Clear interrupts.
76 
77  // Disallow quit().
78  frame.protect_var (quit_allowed);
79  quit_allowed = false;
80 
81  // Clear errors.
82  frame.protect_var (error_state);
83  error_state = 0;
84 
85  try
86  {
87  // Run the actual code.
89  }
90  catch (octave_interrupt_exception)
91  {
92  // Swallow the interrupt.
93  warning ("onCleanup: interrupt occured in cleanup action");
94  }
95  catch (...) // Yes, the black hole. We're in a d-tor.
96  {
97  // This shouldn't happen, in theory.
98  error ("onCleanup: internal error: unhandled exception in cleanup action");
99  }
100 
101  // FIXME: can this happen now?
102  if (error_state)
103  frame.discard_first ();
104 }
105 
108 {
109  octave_scalar_map retval;
110  retval.setfield ("task", fcn);
111  return retval;
112 }
113 
114 static void
116 {
117  warning ("onCleanup: load and save not supported");
118 }
119 
120 bool
121 octave_oncleanup::save_ascii (std::ostream& /* os */)
122 {
123  warn_save_load ();
124  return true;
125 }
126 
127 bool
128 octave_oncleanup::load_ascii (std::istream& /* is */)
129 {
130  warn_save_load ();
131  return true;
132 }
133 
134 bool
135 octave_oncleanup::save_binary (std::ostream& /* os */,
136  bool& /* save_as_floats */)
137 {
138  warn_save_load ();
139  return true;
140 }
141 
142 bool
143 octave_oncleanup::load_binary (std::istream& /* is */, bool /* swap */,
144  oct_mach_info::float_format /* fmt */)
145 {
146  warn_save_load ();
147  return true;
148 }
149 
150 bool
152  const char * /* name */,
153  bool /* save_as_floats */)
154 {
155  warn_save_load ();
156  return true;
157 }
158 
159 bool
161  const char * /* name */)
162 {
163  warn_save_load ();
164  return true;
165 }
166 
167 void
168 octave_oncleanup::print (std::ostream& os, bool pr_as_read_syntax)
169 {
170  print_raw (os, pr_as_read_syntax);
171  newline (os);
172 }
173 
174 void
175 octave_oncleanup::print_raw (std::ostream& os, bool pr_as_read_syntax) const
176 {
177  os << "onCleanup (";
178  if (fcn.is_defined ())
179  fcn.print_raw (os, pr_as_read_syntax);
180  os << ")";
181 }
182 
183 DEFUN (onCleanup, args, ,
184  "-*- texinfo -*-\n\
185 @deftypefn {Built-in Function} {@var{obj} =} onCleanup (@var{function})\n\
186 Create a special object that executes a given function upon destruction.\n\
187 \n\
188 If the object is copied to multiple variables (or cell or struct array\n\
189 elements) or returned from a function, @var{function} will be executed after\n\
190 clearing the last copy of the object. Note that if multiple local onCleanup\n\
191 variables are created, the order in which they are called is unspecified.\n\
192 For similar functionality @xref{The unwind_protect Statement}.\n\
193 @end deftypefn")
194 {
195  octave_value retval;
196 
197  if (args.length () == 1)
198  retval = octave_value (new octave_oncleanup (args(0)));
199  else
200  print_usage ();
201 
202  return retval;
203 }
204 
205 /*
206 %!test
207 %! old_wstate = warning ("query");
208 %! unwind_protect
209 %! trigger = onCleanup (@() warning ("on", "__MY_WARNING__"));
210 %! warning ("off", "__MY_WARNING__");
211 %! assert ((warning ("query", "__MY_WARNING__")).state, "off");
212 %! clear trigger;
213 %! assert ((warning ("query", "__MY_WARNING__")).state, "on");
214 %! unwind_protect_cleanup
215 %! warning (old_wstate);
216 %! end_unwind_protect
217 */
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
size_t length(void) const
Definition: base-list.h:45
bool quit_allowed
Definition: toplev.cc:91
tree_parameter_list * parameter_list(void)
Definition: ov-usr-fcn.h:376
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
octave_value fcn
Definition: ov-oncleanup.h:98
static void warn_save_load(void)
bool is_defined(void) const
Definition: ov.h:520
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
Definition: lo-specfun.cc:1732
void protect_var(T &var)
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
void error(const char *fmt,...)
Definition: error.cc:476
void setfield(const std::string &key, const octave_value &val)
Definition: oct-map.cc:171
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
void newline(std::ostream &os) const
Definition: ov-base.cc:1500
bool is_function_handle(void) const
Definition: ov.h:686
octave_value_list do_multi_index_op(int nargout, const octave_value_list &idx)
Definition: ov.cc:1382
void discard_first(void)
Definition: unwind-prot.h:77
F77_RET_T const double const double * f
int error_state
Definition: error.cc:101
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov.h:1034
sig_atomic_t octave_interrupt_state
Definition: cquit.c:80
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
bool save_binary(std::ostream &os, bool &save_as_floats)
octave_oncleanup(void)
Definition: ov-oncleanup.h:42
friend class octave_value
Definition: ov-base.h:206
octave_function * function_value(bool silent=false) const
Definition: ov.cc:1597
bool save_ascii(std::ostream &os)
void warning(const char *fmt,...)
Definition: error.cc:681
octave_scalar_map scalar_map_value(void) const
bool is_undefined(void) const
Definition: ov.h:523
void print(std::ostream &os, bool pr_as_read_syntax=false)
bool load_ascii(std::istream &is)
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)