GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
quit.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2002-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_quit_h)
27 #define octave_quit_h 1
28 
29 #include "octave-config.h"
30 
31 /* The signal header is just needed for the sig_atomic_t type. */
32 #if defined (__cplusplus)
33 # include <csignal>
34 # include <iosfwd>
35 # include <list>
36 # include <stdexcept>
37 # include <string>
38 extern "C" {
39 #else
40 # include <signal.h>
41 #endif
42 
43 #if defined (__cplusplus)
44 
46 
47 class frame_info
48 {
49 public:
50 
51  frame_info (void) = default;
52 
53  frame_info (const std::string& file_name, const std::string& fcn_name,
54  int line, int column)
55  : m_file_name (file_name), m_fcn_name (fcn_name), m_line (line),
56  m_column (column)
57  { }
58 
59  frame_info (const frame_info&) = default;
60 
61  frame_info& operator = (const frame_info&) = default;
62 
63  ~frame_info (void) = default;
64 
65  std::string file_name (void) const { return m_file_name; }
66 
67  std::string fcn_name (void) const { return m_fcn_name; }
68 
69  int line (void) const { return m_line; }
70 
71  int column (void) const { return m_column; }
72 
73 private:
74 
75  std::string m_file_name;
76 
77  std::string m_fcn_name;
78 
79  int m_line;
80 
81  int m_column;
82 };
83 
84 inline bool operator == (const frame_info& a, const frame_info& b)
85 {
86  return (a.file_name () == b.file_name ()
87  && a.fcn_name () == b.fcn_name ()
88  && a.line () == b.line ()
89  && a.column () == b.column ());
90 }
91 
92 class OCTAVE_EXCEPTION_API execution_exception : public std::runtime_error
93 {
94 public:
95 
96  typedef std::list<frame_info> stack_info_type;
97 
98  execution_exception (const std::string& err_type = "error",
99  const std::string& id = "",
100  const std::string& message = "unspecified error",
101  const stack_info_type& stack_info = stack_info_type ())
102  : runtime_error (message), m_err_type (err_type), m_id (id),
103  m_message (message), m_stack_info (stack_info)
104  { }
105 
106  execution_exception (const execution_exception&) = default;
107 
108  execution_exception& operator = (const execution_exception&) = default;
109 
110  ~execution_exception (void) = default;
111 
112  void set_err_type (const std::string& et)
113  {
114  m_err_type = et;
115  }
116 
117  std::string err_type (void) const { return m_err_type; }
118 
119  virtual std::string stack_trace (void) const;
120 
121  void set_identifier (const std::string& id)
122  {
123  m_id = id;
124  }
125 
126  virtual std::string identifier (void) const { return m_id; }
127 
128  void set_message (const std::string& msg)
129  {
130  m_message = msg;
131  }
132 
133  std::string message (void) const { return m_message; }
134 
135  // Provided for std::exception interface.
136  const char * what (void) const noexcept { return m_message.c_str (); }
137 
138  virtual stack_info_type stack_info (void) const
139  {
140  return m_stack_info;
141  }
142 
143  void set_stack_info (const stack_info_type& stack_info)
144  {
145  m_stack_info = stack_info;
146  }
147 
148  virtual void display (std::ostream& os) const;
149 
150 private:
151 
152  std::string m_err_type;
153 
154  std::string m_id;
155 
156  std::string m_message;
157 
158  stack_info_type m_stack_info;
159 };
160 
161 class OCTAVE_EXCEPTION_API exit_exception : public std::exception
162 {
163 public:
164 
165  exit_exception (int exit_status = 0, bool safe_to_return = false)
166  : std::exception (), m_exit_status (exit_status),
167  m_safe_to_return (safe_to_return)
168  { }
169 
170  exit_exception (const exit_exception&) = default;
171 
172  exit_exception& operator = (exit_exception&) = default;
173 
174  ~exit_exception (void) = default;
175 
176  const char * what (void) const noexcept { return "exit exception"; }
177 
178  int exit_status (void) const { return m_exit_status; }
179 
180  bool safe_to_return (void) const { return m_safe_to_return; }
181 
182 private:
183 
184  int m_exit_status;
185 
186  bool m_safe_to_return;
187 };
188 
189 class interrupt_exception : public std::exception
190 {
191 public:
192 
193  interrupt_exception (void) = default;
194 
195  interrupt_exception (const interrupt_exception&) = default;
196 
197  interrupt_exception& operator = (const interrupt_exception&) = default;
198 
199  ~interrupt_exception (void) = default;
200 
201  const char * what (void) const noexcept { return "interrupt exception"; }
202 };
203 
205 
206 #endif
207 
208 // The following enum values are deprecated and will eventually be
209 // removed from Octave, but there seems to be no universally good way
210 // to tag them with an attribute that will generate a warning.
211 
212 enum
214 {
219 };
220 
221 /*
222  > 0: interrupt pending
223  0: no interrupt pending
224  < 0: handling interrupt
225 */
226 extern OCTAVE_API sig_atomic_t octave_interrupt_state;
227 
228 extern OCTAVE_API volatile sig_atomic_t octave_signal_caught;
229 
230 extern OCTAVE_API void octave_handle_signal (void);
231 
232 #if defined (__cplusplus)
233 
234 inline void octave_quit (void)
235 {
237  {
240  }
241 }
242 
243 #define OCTAVE_QUIT octave_quit ()
244 
245 #else
246 
247 #define OCTAVE_QUIT \
248  do \
249  { \
250  if (octave_signal_caught) \
251  { \
252  octave_signal_caught = 0; \
253  octave_handle_signal (); \
254  } \
255  } \
256  while (0)
257 #endif
258 
259 /* The following macros are obsolete. Interrupting immediately by
260  calling siglongjmp or similar from a signal handler is asking for
261  trouble. Rather than remove them, however, please leave them in
262  place so that old code that uses them will continue to compile. They
263  are defined to create a dummy do-while block to match the previous
264  definitions. */
265 
266 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
267  do \
268  {
269 
270 #define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
271  } \
272  while (0)
273 
274 #if defined (__cplusplus)
275 
276 /* Likewise, these are obsolete. They are defined to create a
277  dummy scope to match the previous versions that created a try-catch
278  block. */
279 
280 #define BEGIN_INTERRUPT_WITH_EXCEPTIONS \
281  {
282 
283 #define END_INTERRUPT_WITH_EXCEPTIONS \
284  }
285 
286 #endif
287 
288 #if defined (__cplusplus)
289 }
290 
291 /* These should only be declared for C++ code, and should also be
292  outside of any extern "C" block. */
293 
294 extern OCTAVE_API void (*octave_signal_hook) (void);
295 extern OCTAVE_API void (*octave_interrupt_hook) (void);
296 
297 #endif
298 
299 #endif
OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:520
void message(const char *name, const char *fmt,...)
Definition: error.cc:947
#define OCTAVE_API
Definition: main.in.cc:55
void(* octave_interrupt_hook)(void)
Definition: quit.cc:43
void(* octave_signal_hook)(void)
Definition: quit.cc:42
octave_exception
Definition: quit.h:214
@ octave_quit_exception
Definition: quit.h:218
@ octave_exec_exception
Definition: quit.h:216
@ octave_no_exception
Definition: quit.h:215
@ octave_alloc_exception
Definition: quit.h:217
OCTAVE_API sig_atomic_t octave_interrupt_state
Definition: quit.cc:38
OCTAVE_API void octave_handle_signal(void)
Definition: quit.cc:101
OCTAVE_API volatile sig_atomic_t octave_signal_caught
Definition: quit.cc:40