GNU Octave  4.2.1
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
octave-link.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2017 John W. Eaton
4 Copyright (C) 2011-2016 Jacob Dawid
5 Copyright (C) 2011-2016 John P. Swensen
6 
7 This file is part of Octave.
8 
9 Octave is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 Octave is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #if ! defined (octave_octave_link_h)
26 #define octave_octave_link_h 1
27 
28 #include "octave-config.h"
29 
30 #include <string>
31 
32 #include "event-queue.h"
33 
34 class octave_mutex;
35 class string_vector;
36 class workspace_element;
37 
38 // \class OctaveLink
39 // \brief Provides threadsafe access to octave.
40 // \author Jacob Dawid
41 //
42 // This class is a wrapper around octave and provides thread safety by
43 // buffering access operations to octave and executing them in the
44 // readline event hook, which lives in the octave thread.
45 
46 class
49 {
50 protected:
51 
52  octave_link (void);
53 
54 public:
55 
56  virtual ~octave_link (void);
57 
58  static void generate_events (void)
59  {
60  if (enabled ())
61  instance->do_generate_events ();
62  }
63 
64  // If disable is TRUE, then no additional events will be processed
65  // other than exit.
66 
67  static void process_events (bool disable = false)
68  {
69  if (enabled ())
70  {
71  if (disable)
72  instance->link_enabled = false;
73 
74  instance->do_process_events ();
75  }
76  }
77 
78  static void discard_events (void)
79  {
80  if (enabled ())
81  instance->do_discard_events ();
82  }
83 
84  static bool confirm_shutdown (void)
85  {
86  bool retval = true;
87 
88  if (instance_ok ())
89  retval = instance->do_confirm_shutdown ();
90 
91  return retval;
92  }
93 
94  static bool exit (int status)
95  {
96  bool retval = false;
97 
98  if (instance_ok ())
99  retval = instance->do_exit (status);
100 
101  return retval;
102  }
103 
104  template <typename T>
105  static void post_event (T *obj, void (T::*method) (void))
106  {
107  if (enabled ())
108  instance->do_post_event (obj, method);
109  }
110 
111  template <typename T, typename A>
112  static void post_event (T *obj, void (T::*method) (A), A arg)
113  {
114  if (enabled ())
115  instance->do_post_event (obj, method, arg);
116  }
117 
118  template <typename T, typename A>
119  static void post_event (T *obj, void (T::*method) (const A&), const A& arg)
120  {
121  if (enabled ())
122  instance->do_post_event (obj, method, arg);
123  }
124 
125  static void entered_readline_hook (void)
126  {
127  if (enabled ())
128  instance->do_entered_readline_hook ();
129  }
130 
131  static void finished_readline_hook (void)
132  {
133  if (enabled ())
134  instance->do_finished_readline_hook ();
135  }
136 
137  static bool
139  {
140  return enabled () ? instance->do_copy_image_to_clipboard (file) : false;
141  }
142 
143  static bool
145  {
146  return enabled () ? instance->do_edit_file (file) : false;
147  }
148 
149  static bool
151  {
152  return enabled () ? instance->do_prompt_new_edit_file (file) : false;
153  }
154 
155  static int
156  message_dialog (const std::string& dlg, const std::string& msg,
157  const std::string& title)
158  {
159  return enabled () ? instance->do_message_dialog (dlg, msg, title) : 0;
160  }
161 
162  static std::string
163  question_dialog (const std::string& msg, const std::string& title,
164  const std::string& btn1, const std::string& btn2,
165  const std::string& btn3, const std::string& btndef)
166  {
167  return enabled () ? instance->do_question_dialog (msg, title, btn1,
168  btn2, btn3, btndef) : 0;
169  }
170 
171  static std::pair<std::list<int>, int>
172  list_dialog (const std::list<std::string>& list,
173  const std::string& mode,
174  int width, int height,
175  const std::list<int>& initial_value,
176  const std::string& name,
177  const std::list<std::string>& prompt,
178  const std::string& ok_string,
179  const std::string& cancel_string)
180  {
181  return enabled ()
182  ? instance->do_list_dialog (list, mode, width, height,
183  initial_value, name, prompt,
184  ok_string, cancel_string)
185  : std::pair<std::list<int>, int> ();
186  }
187 
188  static std::list<std::string>
189  input_dialog (const std::list<std::string>& prompt,
190  const std::string& title,
191  const std::list<float>& nr,
192  const std::list<float>& nc,
193  const std::list<std::string>& defaults)
194  {
195  return enabled ()
196  ? instance->do_input_dialog (prompt, title, nr, nc, defaults)
197  : std::list<std::string> ();
198  }
199 
200  typedef std::list<std::pair<std::string, std::string> > filter_list;
201 
202  static std::list<std::string>
203  file_dialog (const filter_list& filter, const std::string& title,
204  const std::string& filename, const std::string& dirname,
205  const std::string& multimode)
206  {
207  return enabled ()
208  ? instance->do_file_dialog (filter, title, filename, dirname,
209  multimode)
210  : std::list<std::string> ();
211  }
212 
214  const std::string& dir,
215  bool addpath_option)
216  {
217  return enabled ()
218  ? instance->do_debug_cd_or_addpath_error (file, dir, addpath_option)
219  : 0;
220  }
221 
222  static void change_directory (const std::string& dir)
223  {
224  if (enabled ())
225  instance->do_change_directory (dir);
226  }
227 
228  // Preserves pending input.
230  {
231  if (enabled ())
232  instance->do_execute_command_in_terminal (command);
233  }
234 
235  static void set_workspace (void);
236 
237  static void set_workspace (bool top_level,
238  const std::list<workspace_element>& ws)
239  {
240  if (enabled ())
241  instance->do_set_workspace (top_level, instance->debugging, ws);
242  }
243 
244  static void clear_workspace (void)
245  {
246  if (enabled ())
247  instance->do_clear_workspace ();
248  }
249 
250  static void set_history (const string_vector& hist)
251  {
252  if (enabled ())
253  instance->do_set_history (hist);
254  }
255 
256  static void append_history (const std::string& hist_entry)
257  {
258  if (enabled ())
259  instance->do_append_history (hist_entry);
260  }
261 
262  static void clear_history (void)
263  {
264  if (enabled ())
265  instance->do_clear_history ();
266  }
267 
268  static void pre_input_event (void)
269  {
270  if (enabled ())
271  instance->do_pre_input_event ();
272  }
273 
274  static void post_input_event (void)
275  {
276  if (enabled ())
277  instance->do_post_input_event ();
278  }
279 
280  static void enter_debugger_event (const std::string& file, int line)
281  {
282  if (enabled ())
283  {
284  instance->debugging = true;
285 
286  instance->do_enter_debugger_event (file, line);
287  }
288  }
289 
290  static void execute_in_debugger_event (const std::string& file, int line)
291  {
292  if (enabled ())
293  instance->do_execute_in_debugger_event (file, line);
294  }
295 
296  static void exit_debugger_event (void)
297  {
298  if (enabled () && instance->debugging)
299  {
300  instance->debugging = false;
301 
302  instance->do_exit_debugger_event ();
303  }
304  }
305 
306  static void
307  update_breakpoint (bool insert, const std::string& file, int line,
308  const std::string& cond = "")
309  {
310  if (enabled ())
311  instance->do_update_breakpoint (insert, file, line, cond);
312  }
313 
314  static void connect_link (octave_link *);
315 
316  static void set_default_prompts (std::string& ps1, std::string& ps2,
317  std::string& ps4)
318  {
319  if (enabled ())
320  instance->do_set_default_prompts (ps1, ps2, ps4);
321  }
322 
323  static bool enabled (void)
324  {
325  return instance_ok () ? instance->link_enabled : false;
326  }
327 
328  static bool
330  {
331  if (enabled ())
332  {
333  instance->do_show_preferences ();
334  return true;
335  }
336  else
337  return false;
338  }
339 
340  static bool
342  {
343  if (enabled ())
344  {
345  instance->do_show_doc (file);
346  return true;
347  }
348  else
349  return false;
350 
351  }
352 
353 private:
354 
356 
357  // No copying!
358 
359  octave_link (const octave_link&);
360 
362 
363  static bool instance_ok (void) { return instance != 0; }
364 
365 protected:
366 
367  // Semaphore to lock access to the event queue.
369 
370  // Event Queue.
372 
373  bool debugging;
375 
376  void do_generate_events (void);
377  void do_process_events (void);
378  void do_discard_events (void);
379 
380  template <typename T>
381  void do_post_event (T *obj, void (T::*method) (void))
382  {
383  gui_event_queue.add_method (obj, method);
384  }
385 
386  template <typename T, typename A>
387  void do_post_event (T *obj, void (T::*method) (A), A arg)
388  {
389  gui_event_queue.add_method (obj, method, arg);
390  }
391 
392  template <typename T, typename A>
393  void do_post_event (T *obj, void (T::*method) (const A&), const A& arg)
394  {
395  gui_event_queue.add_method (obj, method, arg);
396  }
397 
398  void do_entered_readline_hook (void) { }
400 
401  virtual bool do_confirm_shutdown (void) = 0;
402  virtual bool do_exit (int status) = 0;
403 
404  virtual bool do_copy_image_to_clipboard (const std::string& file) = 0;
405 
406  virtual bool do_edit_file (const std::string& file) = 0;
407  virtual bool do_prompt_new_edit_file (const std::string& file) = 0;
408 
409  virtual int
410  do_message_dialog (const std::string& dlg, const std::string& msg,
411  const std::string& title) = 0;
412 
413  virtual std::string
414  do_question_dialog (const std::string& msg, const std::string& title,
415  const std::string& btn1, const std::string& btn2,
416  const std::string& btn3, const std::string& btndef) = 0;
417 
418  virtual std::pair<std::list<int>, int>
419  do_list_dialog (const std::list<std::string>& list,
420  const std::string& mode,
421  int width, int height,
422  const std::list<int>& initial_value,
423  const std::string& name,
424  const std::list<std::string>& prompt,
425  const std::string& ok_string,
426  const std::string& cancel_string) = 0;
427 
428  virtual std::list<std::string>
429  do_input_dialog (const std::list<std::string>& prompt,
430  const std::string& title,
431  const std::list<float>& nr,
432  const std::list<float>& nc,
433  const std::list<std::string>& defaults) = 0;
434 
435  virtual std::list<std::string>
436  do_file_dialog (const filter_list& filter, const std::string& title,
437  const std::string& filename, const std::string& dirname,
438  const std::string& multimode) = 0;
439 
440  virtual int
441  do_debug_cd_or_addpath_error (const std::string& file,
442  const std::string& dir,
443  bool addpath_option) = 0;
444 
445  virtual void do_change_directory (const std::string& dir) = 0;
446 
447  virtual void do_execute_command_in_terminal (const std::string& command) = 0;
448 
449  virtual void
450  do_set_workspace (bool top_level, bool debug,
451  const std::list<workspace_element>& ws) = 0;
452 
453  virtual void do_clear_workspace (void) = 0;
454 
455  virtual void do_set_history (const string_vector& hist) = 0;
456  virtual void do_append_history (const std::string& hist_entry) = 0;
457  virtual void do_clear_history (void) = 0;
458 
459  virtual void do_pre_input_event (void) = 0;
460  virtual void do_post_input_event (void) = 0;
461 
462  virtual void
463  do_enter_debugger_event (const std::string& file, int line) = 0;
464 
465  virtual void
466  do_execute_in_debugger_event (const std::string& file, int line) = 0;
467 
468  virtual void do_exit_debugger_event (void) = 0;
469 
470  virtual void do_update_breakpoint (bool insert,
471  const std::string& file, int line,
472  const std::string& cond) = 0;
473 
474  virtual void do_set_default_prompts (std::string& ps1, std::string& ps2,
475  std::string& ps4) = 0;
476 
477  virtual void do_show_preferences (void) = 0;
478 
479  virtual void do_show_doc (const std::string &file) = 0;
480 };
481 
482 #endif
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:120
The value of lines which begin with a space character are not saved in the history list A value of all commands are saved on the history list
Definition: oct-hist.cc:728
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6386
workspace_element operator=(const workspace_element &ws_elt)
std::string filename
Definition: urlwrite.cc:340
to define functions rather than attempting to enter them directly on the command line The block of commands is executed as soon as you exit the editor To avoid executing any simply delete all the lines from the buffer before leaving the editor When invoked with no edit the previously executed command
Definition: oct-hist.cc:587
void add_method(T *obj, void(T::*method)(void))
octave_value arg
Definition: pr-output.cc:3440
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
#define OCTINTERP_API
Definition: mexproto.h:69
octave_value retval
Definition: data.cc:6294
MArray< T > filter(MArray< T > &b, MArray< T > &a, MArray< T > &x, MArray< T > &si, int dim=0)
Definition: filter.cc:43
static bool debug
Definition: mkoctfile.cc:239
std::string method
Definition: urlwrite.cc:342
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
F77_RET_T const F77_INT F77_CMPLX * A