changeset 27262:d67f369b3074

use shared_ptr to manage octave_link_events object * octave-link.h, octave-link.cc (octave_link::instance): Now a shared_ptr<octave_link_events> object. Change all uses. (octave_link::enable): (octave_link::connect_link): (octave_link::disconnect_link): Delete. * interpreter-qobject.h, interpreter-qobject.cc (interpreter_qobject::m_qt_link): Now a shared_ptr<octave_qt_link_events> object. Change all uses. (interpreter_qobject::execute): Also enable link after connecting. * interpreter.cc (interpreter::cleanup): Disable m_octave_link instead of disconnecting.
author John W. Eaton <jwe@octave.org>
date Thu, 18 Jul 2019 09:58:41 -0400
parents dccdc3b001a2
children 99aa1bcb8848
files libgui/src/interpreter-qobject.cc libgui/src/interpreter-qobject.h libinterp/corefcn/interpreter.cc libinterp/corefcn/octave-link.cc libinterp/corefcn/octave-link.h
diffstat 5 files changed, 37 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/interpreter-qobject.cc	Wed Jul 17 14:09:20 2019 -0400
+++ b/libgui/src/interpreter-qobject.cc	Thu Jul 18 09:58:41 2019 -0400
@@ -51,11 +51,12 @@
     octave_link& olnk = interp.get_octave_link ();
 
     olnk.connect_link (m_qt_link);
+    olnk.enable ();
 
-    connect (m_qt_link, SIGNAL (confirm_shutdown_signal (void)),
+    connect (qt_link (), SIGNAL (confirm_shutdown_signal (void)),
              m_octave_qobject, SLOT (confirm_shutdown_octave (void)));
 
-    connect (m_qt_link,
+    connect (qt_link (),
              SIGNAL (copy_image_to_clipboard_signal (const QString&, bool)),
              m_octave_qobject,
              SLOT (copy_image_to_clipboard (const QString&, bool)));
--- a/libgui/src/interpreter-qobject.h	Wed Jul 17 14:09:20 2019 -0400
+++ b/libgui/src/interpreter-qobject.h	Thu Jul 18 09:58:41 2019 -0400
@@ -24,6 +24,8 @@
 #if ! defined (octave_interpreter_qobject_h)
 #define octave_interpreter_qobject_h 1
 
+#include <memory>
+
 #include <QObject>
 
 namespace octave
@@ -41,7 +43,7 @@
 
     ~interpreter_qobject (void) = default;
 
-    octave_qt_link_events * qt_link (void) { return m_qt_link; }
+    octave_qt_link_events * qt_link (void) { return m_qt_link.get (); }
 
     void confirm_shutdown (bool closenow);
 
@@ -60,7 +62,7 @@
 
     base_qobject *m_octave_qobject;
 
-    octave_qt_link_events *m_qt_link;
+    std::shared_ptr<octave_qt_link_events> m_qt_link;
   };
 }
 
--- a/libinterp/corefcn/interpreter.cc	Wed Jul 17 14:09:20 2019 -0400
+++ b/libinterp/corefcn/interpreter.cc	Thu Jul 18 09:58:41 2019 -0400
@@ -1019,10 +1019,10 @@
   void interpreter::cleanup (void)
   {
     // If we are attached to a GUI, process pending events and
-    // disconnect the link.
+    // disable the link.
 
     m_octave_link.process_events (true);
-    m_octave_link.disconnect_link ();
+    m_octave_link.disable ();
 
     OCTAVE_SAFE_CALL (m_input_system.clear_input_event_hooks, ());
 
--- a/libinterp/corefcn/octave-link.cc	Wed Jul 17 14:09:20 2019 -0400
+++ b/libinterp/corefcn/octave-link.cc	Thu Jul 18 09:58:41 2019 -0400
@@ -50,7 +50,7 @@
 
 octave_link::octave_link (void)
   : instance (nullptr), event_queue_mutex (new octave::mutex ()),
-    gui_event_queue (), debugging (false), link_enabled (true)
+    gui_event_queue (), debugging (false), link_enabled (false)
 {
   octave::command_editor::add_event_hook (octave_readline_hook);
 }
@@ -60,34 +60,32 @@
   delete event_queue_mutex;
 }
 
-// OBJ should be an object of a class that is derived from the base
-// class octave_link, or 0 to disconnect the link.  It is the
-// responsibility of the caller to delete obj.
+// Programming Note: It is possible to disable the link without deleting
+// the connection.  This allows it to be temporarily disabled.  But if
+// the link is removed, we also set the link_enabled flag to false
+// because if there is no link, it can't be enabled.  Also, access to
+// instance is only protected by a check on the link_enabled flag.
 
 void
-octave_link::connect_link (octave_link_events *obj)
+octave_link::connect_link (const std::shared_ptr<octave_link_events>& obj)
 {
-  if (obj && instance)
-    error ("octave_link is already linked!");
+  if (! obj)
+    disable ();
 
   instance = obj;
 }
 
-octave_link_events *
-octave_link::disconnect_link (bool delete_instance)
+bool
+octave_link::enable (void)
 {
-  if (delete_instance)
-    {
-      delete instance;
-      instance = nullptr;
-      return nullptr;
-    }
+  bool retval = link_enabled;
+
+  if (instance)
+    link_enabled = true;
   else
-    {
-      octave_link_events *retval = instance;
-      instance = nullptr;
-      return retval;
-    }
+    warning ("octave_link: must have connected link to enable");
+
+  return retval;
 }
 
 void
--- a/libinterp/corefcn/octave-link.h	Wed Jul 17 14:09:20 2019 -0400
+++ b/libinterp/corefcn/octave-link.h	Thu Jul 18 09:58:41 2019 -0400
@@ -28,6 +28,7 @@
 #include "octave-config.h"
 
 #include <list>
+#include <memory>
 #include <string>
 
 #include "oct-mutex.h"
@@ -212,16 +213,13 @@
 
   virtual ~octave_link (void);
 
-  void connect_link (octave_link_events *obj);
-
-  octave_link_events * disconnect_link (bool delete_instance = true);
+  // OBJ should be an object of a class that is derived from the base
+  // class octave_link_events, or nullptr to disconnect and delete the
+  // previous link.
 
-  bool enable (void)
-  {
-    bool retval = link_enabled;
-    link_enabled = true;
-    return retval;
-  }
+  void connect_link (const std::shared_ptr<octave_link_events>& obj);
+
+  bool enable (void);
 
   bool disable (void)
   {
@@ -525,9 +523,10 @@
 
 private:
 
-  octave_link_events *instance;
+  // Using a shared_ptr to manage the link_events object ensures that it
+  // will be valid until it is no longer needed.
 
-  bool instance_ok (void) { return instance != nullptr; }
+  std::shared_ptr<octave_link_events> instance;
 
 protected: