GNU Octave 7.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-2022 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>
38extern "C" {
39#else
40# include <signal.h>
41#endif
42
43#if defined (__cplusplus)
44
45namespace octave
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_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 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}
204
205#endif
206
207// The following enum values are deprecated and will eventually be
208// removed from Octave, but there seems to be no universally good way
209// to tag them with an attribute that will generate a warning.
210
211enum
213{
219
220/*
221 > 0: interrupt pending
222 0: no interrupt pending
223 < 0: handling interrupt
224*/
225extern OCTAVE_API sig_atomic_t octave_interrupt_state;
226
227#if defined (OCTAVE_PROVIDE_DEPRECATED_SYMBOLS)
228OCTAVE_DEPRECATED (6, "'octave_exception_state' is an obsolete internal variable; any uses should be removed")
229extern OCTAVE_API sig_atomic_t octave_exception_state;
230#endif
231
232extern OCTAVE_API volatile sig_atomic_t octave_signal_caught;
233
234extern OCTAVE_API void octave_handle_signal (void);
235
236#if defined (OCTAVE_PROVIDE_DEPRECATED_SYMBOLS)
237OCTAVE_DEPRECATED (6, "use 'throw octave::interrupt_exception' instead")
238OCTAVE_NORETURN extern OCTAVE_API void octave_throw_interrupt_exception (void);
239
240OCTAVE_DEPRECATED (6, "use 'throw octave::execution_exception' instead")
241OCTAVE_NORETURN extern OCTAVE_API void octave_throw_execution_exception (void);
242
243OCTAVE_DEPRECATED (6, "use 'throw std::bad_alloc' instead")
244OCTAVE_NORETURN extern OCTAVE_API void octave_throw_bad_alloc (void);
245
246OCTAVE_DEPRECATED (6, "use 'throw' instead")
247extern OCTAVE_API void octave_rethrow_exception (void);
248#endif
249
250#if defined (__cplusplus)
251
252inline void octave_quit (void)
253{
255 {
258 }
259}
260
261#define OCTAVE_QUIT octave_quit ()
262
263#else
264
265#define OCTAVE_QUIT \
266 do \
267 { \
268 if (octave_signal_caught) \
269 { \
270 octave_signal_caught = 0; \
271 octave_handle_signal (); \
272 } \
273 } \
274 while (0)
275#endif
276
277/* The following macros are obsolete. Interrupting immediately by
278 calling siglongjmp or similar from a signal handler is asking for
279 trouble. Rather than remove them, however, please leave them in
280 place so that old code that uses them will continue to compile. They
281 are defined to create a dummy do-while block to match the previous
282 definitions. */
283
284#define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
285 do \
286 {
287
288#define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
289 } \
290 while (0)
291
292#if defined (__cplusplus)
293
294/* Likewise, these are obsolete. They are defined to create a
295 dummy scope to match the previous versions that created a try-catch
296 block. */
297
298#define BEGIN_INTERRUPT_WITH_EXCEPTIONS \
299 {
300
301#define END_INTERRUPT_WITH_EXCEPTIONS \
302 }
303
304#endif
305
306#if defined (__cplusplus)
307}
308
309/* These should only be declared for C++ code, and should also be
310 outside of any extern "C" block. */
311
312extern OCTAVE_API void (*octave_signal_hook) (void);
313extern OCTAVE_API void (*octave_interrupt_hook) (void);
314
315#if defined (OCTAVE_PROVIDE_DEPRECATED_SYMBOLS)
316OCTAVE_DEPRECATED (6, "'octave_bad_alloc_hook' is obsolete and no longer used")
317extern OCTAVE_API void (*octave_bad_alloc_hook) (void);
318#endif
319
320#endif
321
322#endif
void message(const char *name, const char *fmt,...)
Definition: error.cc:948
#define OCTAVE_API
Definition: main.in.cc:55
bool operator==(const cdef_class &clsa, const cdef_class &clsb)
Definition: cdef-class.h:437
STL namespace.
void(* octave_bad_alloc_hook)(void)
Definition: quit.cc:53
void octave_rethrow_exception(void)
Definition: quit.cc:166
void octave_throw_bad_alloc(void)
Definition: quit.cc:157
void octave_throw_interrupt_exception(void)
Definition: quit.cc:136
void(* octave_interrupt_hook)(void)
Definition: quit.cc:50
sig_atomic_t octave_exception_state
Definition: quit.cc:42
void(* octave_signal_hook)(void)
Definition: quit.cc:49
void octave_throw_execution_exception(void)
Definition: quit.cc:146
octave_exception
Definition: quit.h:213
@ octave_quit_exception
Definition: quit.h:217
@ octave_exec_exception
Definition: quit.h:215
@ octave_no_exception
Definition: quit.h:214
@ octave_alloc_exception
Definition: quit.h:216
OCTAVE_API sig_atomic_t octave_interrupt_state
Definition: quit.cc:38
OCTAVE_API void octave_handle_signal(void)
Definition: quit.cc:121
OCTAVE_API volatile sig_atomic_t octave_signal_caught
Definition: quit.cc:47