GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
quit.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2002-2026 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#if defined (__cplusplus)
32# include <atomic>
33# include <iosfwd>
34# include <list>
35# include <stdexcept>
36# include <string>
37
39
40class frame_info
41{
42public:
43
44 frame_info () = default;
45
46 frame_info (const std::string& file_name, const std::string& fcn_name,
47 int line, int column)
48 : m_file_name (file_name), m_fcn_name (fcn_name), m_line (line),
49 m_column (column)
50 { }
51
52 frame_info (const frame_info&) = default;
53
54 frame_info& operator = (const frame_info&) = default;
55
56 ~frame_info () = default;
57
58 std::string file_name () const { return m_file_name; }
59
60 std::string fcn_name () const { return m_fcn_name; }
61
62 int line () const { return m_line; }
63
64 int column () const { return m_column; }
65
66private:
67
68 std::string m_file_name;
69
70 std::string m_fcn_name;
71
72 int m_line;
73
74 int m_column;
75};
76
77inline bool operator == (const frame_info& a, const frame_info& b)
78{
79 return (a.file_name () == b.file_name ()
80 && a.fcn_name () == b.fcn_name ()
81 && a.line () == b.line ()
82 && a.column () == b.column ());
83}
84
85class OCTAVE_EXCEPTION_API execution_exception : public std::runtime_error
86{
87public:
88
89 typedef std::list<frame_info> stack_info_type;
90
91 execution_exception (const std::string& err_type = "error",
92 const std::string& id = "",
93 const std::string& message = "unspecified error",
94 const stack_info_type& stack_info = stack_info_type ())
95 : runtime_error (message), m_err_type (err_type), m_id (id),
96 m_message (message), m_stack_info (stack_info)
97 { }
98
100
101 execution_exception& operator = (const execution_exception&) = default;
102
103 ~execution_exception () = default;
104
105 void set_err_type (const std::string& et)
106 {
107 m_err_type = et;
108 }
109
110 std::string err_type () const { return m_err_type; }
111
112 virtual std::string stack_trace () const;
113
114 void set_identifier (const std::string& id)
115 {
116 m_id = id;
117 }
118
119 virtual std::string identifier () const { return m_id; }
120
121 void set_message (const std::string& msg)
122 {
123 m_message = msg;
124 }
125
126 std::string message () const { return m_message; }
127
128 // Provided for std::exception interface.
129 const char * what () const noexcept { return m_message.c_str (); }
130
131 virtual stack_info_type stack_info () const
132 {
133 return m_stack_info;
134 }
135
136 void set_stack_info (const stack_info_type& stack_info)
137 {
138 m_stack_info = stack_info;
139 }
140
141 virtual void display (std::ostream& os) const;
142
143private:
144
145 std::string m_err_type;
146
147 std::string m_id;
148
149 std::string m_message;
150
151 stack_info_type m_stack_info;
152};
153
154class OCTAVE_EXCEPTION_API exit_exception : public std::exception
155{
156public:
157
158 exit_exception (int exit_status = 0, bool safe_to_return = false)
159 : std::exception (), m_exit_status (exit_status),
160 m_safe_to_return (safe_to_return)
161 { }
162
163 OCTAVE_DEFAULT_COPY_MOVE_DELETE (exit_exception)
164
165 const char * what () const noexcept { return "exit exception"; }
166
167 int exit_status () const { return m_exit_status; }
168
169 bool safe_to_return () const { return m_safe_to_return; }
170
171private:
172
173 int m_exit_status;
174
175 bool m_safe_to_return;
176};
177
178class OCTAVE_EXCEPTION_API interrupt_exception : public std::exception
179{
180public:
181
182 interrupt_exception () = default;
183
184 interrupt_exception (const interrupt_exception&) = default;
185
186 interrupt_exception& operator = (const interrupt_exception&) = default;
187
188 ~interrupt_exception () = default;
189
190 const char * what () const noexcept { return "interrupt exception"; }
191};
192
193OCTAVE_END_NAMESPACE(octave)
194
195extern "C" {
196
197#endif
198
199// The following enum values are deprecated and will eventually be
200// removed from Octave, but there seems to be no universally good way
201// to tag them with an attribute that will generate a warning.
202
211
212 /*
213 > 0: interrupt pending
214 0: no interrupt pending
215 < 0: handling interrupt
216 */
217
218#if defined (__cplusplus)
219
220 extern OCTAVE_API std::atomic<int> octave_interrupt_state;
221
222 extern OCTAVE_API std::atomic<bool> octave_signal_caught;
223
224#endif
225
226 extern OCTAVE_API void octave_handle_signal (void);
227
228#if defined (__cplusplus)
229
230 inline void octave_quit ()
231 {
232 bool expected = true;
233
234 if (octave_signal_caught.compare_exchange_strong (expected, false))
236 }
237
238#define OCTAVE_QUIT octave_quit ()
239
240#else
241
242 extern OCTAVE_API void octave_quit_c (void);
243#define OCTAVE_QUIT octave_quit_c ()
244
245#endif
246
247 /* The following macros are obsolete. Interrupting immediately by
248 calling siglongjmp or similar from a signal handler is asking for
249 trouble. Rather than remove them, however, please leave them in
250 place so that old code that uses them will continue to compile. They
251 are defined to create a dummy do-while block to match the previous
252 definitions. */
253
254#define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
255 do \
256 {
257
258#define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
259 } \
260 while (0)
261
262#if defined (__cplusplus)
263
264 /* Likewise, these are obsolete. They are defined to create a
265 dummy scope to match the previous versions that created a try-catch
266 block. */
267
268#define BEGIN_INTERRUPT_WITH_EXCEPTIONS \
269 {
270
271#define END_INTERRUPT_WITH_EXCEPTIONS \
272 }
273
274#endif
275
276#if defined (__cplusplus)
277}
278
279/* These should only be declared for C++ code, and should also be
280 outside of any extern "C" block. */
281
282extern OCTAVE_API void (*octave_signal_hook) ();
283extern OCTAVE_API void (*octave_interrupt_hook) ();
284
285#endif
286
287#endif
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
bool operator==(const dim_vector &a, const dim_vector &b)
Definition dim-vector.h:557
void message(const char *name, const char *fmt,...)
Definition error.cc:976
#define OCTAVE_API
Definition main.in.cc:55
std::atomic< bool > octave_signal_caught
Definition quit.cc:41
void(* octave_interrupt_hook)()
Definition quit.cc:44
void(* octave_signal_hook)()
Definition quit.cc:43
std::atomic< int > octave_interrupt_state
Definition quit.cc:39
octave_exception
Definition quit.h:205
@ octave_quit_exception
Definition quit.h:209
@ octave_exec_exception
Definition quit.h:207
@ octave_no_exception
Definition quit.h:206
@ octave_alloc_exception
Definition quit.h:208
void octave_quit_c(void)
Definition quit.cc:103
void octave_handle_signal(void)
Definition quit.cc:109