view libgui/src/octave-cmd.h @ 25923:a7511a1489b8

call octave functions with return value from GUI (bug #47585) * file-editor-tab.cc (update_breakpoints): new function using octave_cmd_builtin for calling dbstatus; (update_breakpoints_handler) slot for the signal from the worker thread going through the list of returned breakpoints and update the markers of the breakpoints which are related to the current file; (handle_octave_result): removed this stub * file-editor-tab.h: new method update_breakpoints, new slot update_breakpoints_handler * file-editor.cc (request_open_file): call update_breakpoints for the new editor tab after the file contents has been loaded * main-window.cc (main_window): register octave_value_list as meta type allowing to send this data type via queued signal/slot connections; (handle_load_workspace_request): update usage of octave_cmd_builtin according to its new parameter list * octave-cmd.cc (octave_cmd_builtin::execute): call builtin function with return value list, if nargout is not zero, emit the signal with the returned list; (octave_cmd_builtin::init_cmd_retval): method for connecting the return value signal with the slot given by the caller * octave-cmd.h: (octave_cmd_builtin): add nargout as well as receiver and handler of the return value signals slot to the parameter list, call method for connecting the signal with the given slot; (argout_signal): new signal for passing the return values to the caller thread
author Torsten <mttl@mailbox.org>
date Mon, 08 Oct 2018 20:41:31 +0200
parents 20e294a607af
children 00f796120a6d
line wrap: on
line source

/*

Copyright (C) 2014-2018 Torsten

This file is part of Octave.

Octave is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Octave is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, see
<https://www.gnu.org/licenses/>.

*/

// Author: Torsten <mttl@mailbox.org>

#if ! defined (octave_octave_cmd_h)
#define octave_octave_cmd_h 1

#include <QSemaphore>
#include <QMutex>
#include <QPointer>
#include <QString>
#include <QFileInfo>

#include "octave-qt-link.h"
#include "ovl.h"


namespace octave
{
  class interpreter;

  class octave_cmd : public QObject
  {
    Q_OBJECT;

  public:

    octave_cmd (void) : QObject () {  };

    virtual ~octave_cmd (void) = default;

    virtual void execute (interpreter&) { }
  };

  class octave_cmd_exec : public octave_cmd
  {
  public:

    octave_cmd_exec (const QString& cmd) : octave_cmd () { m_cmd = cmd; }

    void execute (interpreter& interp);

  protected:

    QString m_cmd;
  };

  class octave_cmd_eval : public octave_cmd
  {
  public:

    octave_cmd_eval (const QFileInfo& info) : octave_cmd () { m_info = info; }

    void execute (interpreter& interp);

  protected:

    QFileInfo m_info;
  };

  class octave_cmd_builtin : public octave_cmd
  {
    Q_OBJECT;

  public:

    enum cmd_upd {
      CMD_UPD_NO        = 0,
      CMD_UPD_WORKSPACE = 1
    };

    //! Command using built in functions requiring interpreter, no return values
    /*! Command calling a built in function, which uses an interpreter and does
     *  not have any return values
     * @param Ff Pointer to the builtin function
     * @param argin Input parameters for Ff as octave value list (default empty)
     * @param update A memeber of cmd_upd for possibly required updates after
     *               the executing Ff
     */
    octave_cmd_builtin (
          octave_value_list (*Ff) (octave::interpreter&, const octave_value_list&, int),
          octave_value_list argin = ovl (), cmd_upd update = CMD_UPD_NO)
      : octave_cmd (), m_callback_fi (Ff), m_callback_f (nullptr),
        m_argin (argin), m_nargout (0), m_argout_receiver (nullptr),
        m_argout_handler (nullptr), m_update (update)
    {  }

    //! Command using built in functions not requiring interpreter, no return values
    octave_cmd_builtin (
          octave_value_list (*Ff) (const octave_value_list&, int),
          octave_value_list argin = ovl (), cmd_upd update = CMD_UPD_NO)
      : octave_cmd (), m_callback_fi (nullptr), m_callback_f (Ff),
        m_argin (argin), m_nargout (0), m_argout_receiver (nullptr),
        m_argout_handler (nullptr), m_update (update)
    {  }

    //! Command using built in functions requiring interpreter, with return values
    /*! Command calling a built in function, which uses an interpreter and
     *  has return values
     * @param Ff Pointer to the builtin function
     * @param argin Input parameters for Ff as octave value list
     * @param argout Number of output values
     * @param argout_receiver Receiver of the the signal containing the return values
     * @param argout_handler  Slot for the signal containing the return values
     * @param update A member of cmd_upd for possibly required updates after
     *               the executing Ff
     * argout_receiver and argout_handler live in the GUI thread. Using a thread
     * crossing signal/slot mechanism, the GUI thread is not blocked if the
     * worker thread is busy and can not execute the desired command immediately.
     */
    octave_cmd_builtin (
          octave_value_list (*Ff) (octave::interpreter&, const octave_value_list&, int),
          octave_value_list argin, int nargout, QObject *argout_receiver,
          const char *argout_handler = nullptr, cmd_upd update = CMD_UPD_NO)
      : octave_cmd (), m_callback_fi (Ff), m_callback_f (nullptr),
        m_argin (argin), m_nargout (nargout), m_argout_receiver (argout_receiver),
        m_argout_handler (argout_handler), m_update (update)
    {
      init_cmd_retval ();
    }

    //! Command using built in functions not requiring interpreter, with return values
    octave_cmd_builtin (
          octave_value_list (*Ff) (const octave_value_list&, int),
          octave_value_list argin, int nargout, QObject *argout_receiver,
          const char *argout_handler = nullptr, cmd_upd update = CMD_UPD_NO)
      : octave_cmd (), m_callback_fi (nullptr), m_callback_f (Ff),
        m_argin (argin), m_nargout (nargout), m_argout_receiver (argout_receiver),
        m_argout_handler (argout_handler), m_update (update)
    {
      init_cmd_retval ();
    }

    void execute (interpreter& interp);

  signals:

    //! Signal for sending the return values to the GUI thread
    void argout_signal (const octave_value_list&);

  protected:

    octave_value_list (*m_callback_fi) (octave::interpreter&,
                                        const octave_value_list&, int);
    octave_value_list (*m_callback_f) (const octave_value_list&, int);

    octave_value_list m_argin;

    int m_nargout;
    QObject *m_argout_receiver;
    const char *m_argout_handler;

    cmd_upd m_update;

  private:

    //! Internal method connecting the signal for the return values
    /*! Internal method for connecting the signal for later sending the return
     * values to the caller. The connection has to be queued ensuring that
     * the receiver's slot is actually executed in the GUI thread
     */
    void init_cmd_retval (void);
  };

  class octave_cmd_debug : public octave_cmd_exec
  {
  public:

    octave_cmd_debug (const QString& cmd, bool suppress_location)
      : octave_cmd_exec (cmd), m_suppress_dbg_location (suppress_location) { }

    void execute (interpreter& interp);

  protected:

    bool m_suppress_dbg_location;
  };

  //! Queuing octave commands from the GUI for the worker thread.

  class octave_command_queue : QObject
  {
    Q_OBJECT;

  public:

    octave_command_queue (void)
      : QObject (), m_queue (QList<QPointer<octave_cmd>> ()), m_processing (1),
        m_queue_mutex ()
    { }

    ~octave_command_queue (void) = default;

    //! Adds a new octave command to the command queue.
    //!
    //! @param cmd The octave command to be queued.

    void add_cmd (octave_cmd *cmd);

    //! Callback routine for executing the command by the worker thread.

    void execute_command_callback (void);

  private:

    QList<QPointer<octave_cmd>> m_queue;
    QSemaphore m_processing;
    QMutex m_queue_mutex;
  };
}

#endif