GNU Octave  9.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-2024 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 <atomic>
34 # include <csignal>
35 # include <iosfwd>
36 # include <list>
37 # include <stdexcept>
38 # include <string>
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  OCTAVE_DEFAULT_COPY_MOVE_DELETE (exit_exception)
171 
172  const char * what (void) const noexcept { return "exit exception"; }
173 
174  int exit_status (void) const { return m_exit_status; }
175 
176  bool safe_to_return (void) const { return m_safe_to_return; }
177 
178 private:
179 
180  int m_exit_status;
181 
182  bool m_safe_to_return;
183 };
184 
185 class interrupt_exception : public std::exception
186 {
187 public:
188 
189  interrupt_exception (void) = default;
190 
191  interrupt_exception (const interrupt_exception&) = default;
192 
193  interrupt_exception& operator = (const interrupt_exception&) = default;
194 
195  ~interrupt_exception (void) = default;
196 
197  const char * what (void) const noexcept { return "interrupt exception"; }
198 };
199 
200 OCTAVE_END_NAMESPACE(octave)
201 
202 extern "C" {
203 
204 #endif
205 
206 // The following enum values are deprecated and will eventually be
207 // removed from Octave, but there seems to be no universally good way
208 // to tag them with an attribute that will generate a warning.
209 
210 enum
212 {
217 };
218 
219 /*
220  > 0: interrupt pending
221  0: no interrupt pending
222  < 0: handling interrupt
223 */
224 
225 #if defined (__cplusplus)
226 
227 extern OCTAVE_API std::atomic<int> octave_interrupt_state;
228 
229 extern OCTAVE_API volatile std::atomic<bool> octave_signal_caught;
230 
231 #endif
232 
233 extern OCTAVE_API void octave_handle_signal (void);
234 
235 #if defined (__cplusplus)
236 
237 inline void octave_quit (void)
238 {
239  bool expected = true;
240 
241  if (octave_signal_caught.compare_exchange_strong (expected, false))
243 }
244 
245 #define OCTAVE_QUIT octave_quit ()
246 
247 #else
248 
249 extern OCTAVE_API void octave_quit_c (void);
250 #define OCTAVE_QUIT octave_quit_c ()
251 
252 #endif
253 
254 /* The following macros are obsolete. Interrupting immediately by
255  calling siglongjmp or similar from a signal handler is asking for
256  trouble. Rather than remove them, however, please leave them in
257  place so that old code that uses them will continue to compile. They
258  are defined to create a dummy do-while block to match the previous
259  definitions. */
260 
261 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
262  do \
263  {
264 
265 #define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
266  } \
267  while (0)
268 
269 #if defined (__cplusplus)
270 
271 /* Likewise, these are obsolete. They are defined to create a
272  dummy scope to match the previous versions that created a try-catch
273  block. */
274 
275 #define BEGIN_INTERRUPT_WITH_EXCEPTIONS \
276  {
277 
278 #define END_INTERRUPT_WITH_EXCEPTIONS \
279  }
280 
281 #endif
282 
283 #if defined (__cplusplus)
284 }
285 
286 /* These should only be declared for C++ code, and should also be
287  outside of any extern "C" block. */
288 
289 extern OCTAVE_API void (*octave_signal_hook) (void);
290 extern OCTAVE_API void (*octave_interrupt_hook) (void);
291 
292 #endif
293 
294 #endif
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:956
#define OCTAVE_API
Definition: main.cc:55
std::atomic< sig_atomic_t > octave_interrupt_state
Definition: quit.cc:39
void(* octave_interrupt_hook)()
Definition: quit.cc:44
void(* octave_signal_hook)()
Definition: quit.cc:43
volatile std::atomic< bool > octave_signal_caught
Definition: quit.cc:41
octave_exception
Definition: quit.h:212
@ octave_quit_exception
Definition: quit.h:216
@ octave_exec_exception
Definition: quit.h:214
@ octave_no_exception
Definition: quit.h:213
@ octave_alloc_exception
Definition: quit.h:215
void octave_quit_c(void)
Definition: quit.cc:103
void octave_handle_signal(void)
Definition: quit.cc:109