GNU Octave  4.0.0
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-2015 John W. Eaton
4 Copyright (C) 2011-2015 Jacob Dawid
5 Copyright (C) 2011-2015 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 <string>
29 
30 #include "event-queue.h"
31 
32 class octave_mutex;
33 class string_vector;
34 class workspace_element;
35 
36 // \class OctaveLink
37 // \brief Provides threadsafe access to octave.
38 // \author Jacob Dawid
39 //
40 // This class is a wrapper around octave and provides thread safety by
41 // buffering access operations to octave and executing them in the
42 // readline event hook, which lives in the octave thread.
43 
44 class
47 {
48 protected:
49 
50  octave_link (void);
51 
52 public:
53 
54  virtual ~octave_link (void) { }
55 
56  static void generate_events (void)
57  {
58  if (enabled ())
59  instance->do_generate_events ();
60  }
61 
62  // If disable is TRUE, then no additional events will be processed
63  // other than exit.
64 
65  static void process_events (bool disable = false)
66  {
67  if (enabled ())
68  {
69  if (disable)
70  instance->link_enabled = false;
71 
72  instance->do_process_events ();
73  }
74  }
75 
76  static void discard_events (void)
77  {
78  if (enabled ())
79  instance->do_discard_events ();
80  }
81 
82  static bool confirm_shutdown (void)
83  {
84  bool retval = true;
85 
86  if (instance_ok ())
87  retval = instance->do_confirm_shutdown ();
88 
89  return retval;
90  }
91 
92  static bool exit (int status)
93  {
94  bool retval = false;
95 
96  if (instance_ok ())
97  retval = instance->do_exit (status);
98 
99  return retval;
100  }
101 
102  template <class T>
103  static void post_event (T *obj, void (T::*method) (void))
104  {
105  if (enabled ())
106  instance->do_post_event (obj, method);
107  }
108 
109  template <class T, class A>
110  static void post_event (T *obj, void (T::*method) (A), A arg)
111  {
112  if (enabled ())
113  instance->do_post_event (obj, method, arg);
114  }
115 
116  template <class T, class A>
117  static void post_event (T *obj, void (T::*method) (const A&), const A& arg)
118  {
119  if (enabled ())
120  instance->do_post_event (obj, method, arg);
121  }
122 
123  static void entered_readline_hook (void)
124  {
125  if (enabled ())
126  instance->do_entered_readline_hook ();
127  }
128 
129  static void finished_readline_hook (void)
130  {
131  if (enabled ())
132  instance->do_finished_readline_hook ();
133  }
134 
135  static bool
136  copy_image_to_clipboard (const std::string& file)
137  {
138  return enabled () ? instance->do_copy_image_to_clipboard (file) : false;
139  }
140 
141  static bool
142  edit_file (const std::string& file)
143  {
144  return enabled () ? instance->do_edit_file (file) : false;
145  }
146 
147  static bool
148  prompt_new_edit_file (const std::string& file)
149  {
150  return enabled () ? instance->do_prompt_new_edit_file (file) : false;
151  }
152 
153  static int
154  message_dialog (const std::string& dlg, const std::string& msg,
155  const std::string& title)
156  {
157  return enabled () ? instance->do_message_dialog (dlg, msg, title) : 0;
158  }
159 
160  static std::string
161  question_dialog (const std::string& msg, const std::string& title,
162  const std::string& btn1, const std::string& btn2,
163  const std::string& btn3, const std::string& btndef)
164  {
165  return enabled () ? instance->do_question_dialog (msg, title, btn1,
166  btn2, btn3, btndef) : 0;
167  }
168 
169  static std::pair<std::list<int>, int>
170  list_dialog (const std::list<std::string>& list,
171  const std::string& mode,
172  int width, int height,
173  const std::list<int>& initial_value,
174  const std::string& name,
175  const std::list<std::string>& prompt,
176  const std::string& ok_string,
177  const std::string& cancel_string)
178  {
179  return enabled ()
180  ? instance->do_list_dialog (list, mode, width, height,
181  initial_value, name, prompt,
182  ok_string, cancel_string)
183  : std::pair<std::list<int>, int> ();
184  }
185 
186  static std::list<std::string>
187  input_dialog (const std::list<std::string>& prompt,
188  const std::string& title,
189  const std::list<float>& nr,
190  const std::list<float>& nc,
191  const std::list<std::string>& defaults)
192  {
193  return enabled ()
194  ? instance->do_input_dialog (prompt, title, nr, nc, defaults)
195  : std::list<std::string> ();
196  }
197 
198  typedef std::list<std::pair<std::string, std::string> > filter_list;
199 
200  static std::list<std::string>
201  file_dialog (const filter_list& filter, const std::string& title,
202  const std::string& filename, const std::string& dirname,
203  const std::string& multimode)
204  {
205  return enabled ()
206  ? instance->do_file_dialog (filter, title, filename, dirname,
207  multimode)
208  : std::list<std::string> ();
209  }
210 
211 
212  static int debug_cd_or_addpath_error (const std::string& file,
213  const std::string& dir,
214  bool addpath_option)
215  {
216  return enabled ()
217  ? instance->do_debug_cd_or_addpath_error (file, dir, addpath_option)
218  : 0;
219  }
220 
221  static void change_directory (const std::string& dir)
222  {
223  if (enabled ())
224  instance->do_change_directory (dir);
225  }
226 
227  // Preserves pending input.
228  static void execute_command_in_terminal (const std::string& command)
229  {
230  if (enabled ())
231  instance->do_execute_command_in_terminal (command);
232  }
233 
234  static void set_workspace (void);
235 
236  static void set_workspace (bool top_level,
237  const std::list<workspace_element>& ws)
238  {
239  if (enabled ())
240  instance->do_set_workspace (top_level, instance->debugging, ws);
241  }
242 
243  static void clear_workspace (void)
244  {
245  if (enabled ())
246  instance->do_clear_workspace ();
247  }
248 
249  static void set_history (const string_vector& hist)
250  {
251  if (enabled ())
252  instance->do_set_history (hist);
253  }
254 
255  static void append_history (const std::string& hist_entry)
256  {
257  if (enabled ())
258  instance->do_append_history (hist_entry);
259  }
260 
261  static void clear_history (void)
262  {
263  if (enabled ())
264  instance->do_clear_history ();
265  }
266 
267  static void pre_input_event (void)
268  {
269  if (enabled ())
270  instance->do_pre_input_event ();
271  }
272 
273  static void post_input_event (void)
274  {
275  if (enabled ())
276  instance->do_post_input_event ();
277  }
278 
279  static void enter_debugger_event (const std::string& file, int line)
280  {
281  if (enabled ())
282  {
283  instance->debugging = true;
284 
285  instance->do_enter_debugger_event (file, line);
286  }
287  }
288 
289  static void execute_in_debugger_event (const std::string& file, int line)
290  {
291  if (enabled ())
292  instance->do_execute_in_debugger_event (file, line);
293  }
294 
295  static void exit_debugger_event (void)
296  {
297  if (enabled () && instance->debugging)
298  {
299  instance->debugging = false;
300 
301  instance->do_exit_debugger_event ();
302  }
303  }
304 
305  static void
306  update_breakpoint (bool insert, const std::string& file, int line)
307  {
308  if (enabled ())
309  instance->do_update_breakpoint (insert, file, line);
310  }
311 
312  static void connect_link (octave_link *);
313 
314  static void set_default_prompts (std::string& ps1, std::string& ps2,
315  std::string& ps4)
316  {
317  if (enabled ())
318  instance->do_set_default_prompts (ps1, ps2, ps4);
319  }
320 
321  static bool enabled (void)
322  {
323  return instance_ok () ? instance->link_enabled : false;
324  }
325 
326  static bool
328  {
329  if (enabled ())
330  {
331  instance->do_show_preferences ();
332  return true;
333  }
334  else
335  return false;
336  }
337 
338  static bool
339  show_doc (const std::string & file)
340  {
341  if (enabled ())
342  {
343  instance->do_show_doc (file);
344  return true;
345  }
346  else
347  return false;
348 
349  }
350 
351 private:
352 
354 
355  // No copying!
356 
357  octave_link (const octave_link&);
358 
360 
361  static bool instance_ok (void) { return instance != 0; }
362 
363 protected:
364 
365  // Semaphore to lock access to the event queue.
367 
368  // Event Queue.
370 
371  bool debugging;
373 
374  void do_generate_events (void);
375  void do_process_events (void);
376  void do_discard_events (void);
377 
378  template <class T>
379  void do_post_event (T *obj, void (T::*method) (void))
380  {
381  gui_event_queue.add_method (obj, method);
382  }
383 
384  template <class T, class A>
385  void do_post_event (T *obj, void (T::*method) (A), A arg)
386  {
387  gui_event_queue.add_method (obj, method, arg);
388  }
389 
390  template <class T, class A>
391  void do_post_event (T *obj, void (T::*method) (const A&), const A& arg)
392  {
393  gui_event_queue.add_method (obj, method, arg);
394  }
395 
396  void do_entered_readline_hook (void) { }
398 
399  virtual bool do_confirm_shutdown (void) = 0;
400  virtual bool do_exit (int status) = 0;
401 
402  virtual bool do_copy_image_to_clipboard (const std::string& file) = 0;
403 
404  virtual bool do_edit_file (const std::string& file) = 0;
405  virtual bool do_prompt_new_edit_file (const std::string& file) = 0;
406 
407  virtual int
408  do_message_dialog (const std::string& dlg, const std::string& msg,
409  const std::string& title) = 0;
410 
411  virtual std::string
412  do_question_dialog (const std::string& msg, const std::string& title,
413  const std::string& btn1, const std::string& btn2,
414  const std::string& btn3, const std::string& btndef) = 0;
415 
416  virtual std::pair<std::list<int>, int>
417  do_list_dialog (const std::list<std::string>& list,
418  const std::string& mode,
419  int width, int height,
420  const std::list<int>& initial_value,
421  const std::string& name,
422  const std::list<std::string>& prompt,
423  const std::string& ok_string,
424  const std::string& cancel_string) = 0;
425 
426  virtual std::list<std::string>
427  do_input_dialog (const std::list<std::string>& prompt,
428  const std::string& title,
429  const std::list<float>& nr,
430  const std::list<float>& nc,
431  const std::list<std::string>& defaults) = 0;
432 
433  virtual std::list<std::string>
434  do_file_dialog (const filter_list& filter, const std::string& title,
435  const std::string& filename, const std::string& dirname,
436  const std::string& multimode) = 0;
437 
438  virtual int
439  do_debug_cd_or_addpath_error (const std::string& file,
440  const std::string& dir,
441  bool addpath_option) = 0;
442 
443  virtual void do_change_directory (const std::string& dir) = 0;
444 
445  virtual void do_execute_command_in_terminal (const std::string& command) = 0;
446 
447  virtual void
448  do_set_workspace (bool top_level, bool debug,
449  const std::list<workspace_element>& ws) = 0;
450 
451  virtual void do_clear_workspace (void) = 0;
452 
453  virtual void do_set_history (const string_vector& hist) = 0;
454  virtual void do_append_history (const std::string& hist_entry) = 0;
455  virtual void do_clear_history (void) = 0;
456 
457  virtual void do_pre_input_event (void) = 0;
458  virtual void do_post_input_event (void) = 0;
459 
460  virtual void
461  do_enter_debugger_event (const std::string& file, int line) = 0;
462 
463  virtual void
464  do_execute_in_debugger_event (const std::string& file, int line) = 0;
465 
466  virtual void do_exit_debugger_event (void) = 0;
467 
468  virtual void do_update_breakpoint (bool insert,
469  const std::string& file, int line) = 0;
470 
471  virtual void do_set_default_prompts (std::string& ps1, std::string& ps2,
472  std::string& ps4) = 0;
473 
474  virtual void do_show_preferences (void) = 0;
475 
476  virtual void do_show_doc (const std::string &file) = 0;
477 };
478 
479 #endif // OCTAVELINK_H
F77_RET_T const octave_idx_type Complex * A
Definition: CmplxGEPBAL.cc:39
workspace_element operator=(const workspace_element &ws_elt)
int octave_link(const std::string &old_name, const std::string &new_name)
Definition: file-ops.cc:440
void add_method(T *obj, void(T::*method)(void))
#define OCTINTERP_API
Definition: mexproto.h:66
MArray< double > filter(MArray< double > &, MArray< double > &, MArray< double > &, int dim)
double arg(double x)
Definition: lo-mappers.h:37
static bool debug
Definition: mkoctfile.cc:227