changeset 27619:0053b8e0dc56

allow only one base_qobject instance and provide gobal access to it * octave-qobject.h, octave-qobject.cc (base_qobject::s_instance): New static data member. (base_qobject::base_qobject): Set s_instance if not already set. Prevent creation of more than one base_qobject object. (base_qobject::the_octave_qobject): New static function. Provide global access to the one true octave_qobject instance.
author John W. Eaton <jwe@octave.org>
date Sat, 02 Nov 2019 15:24:14 -0400
parents dd9803e83b9f
children 45bb5bbaf291
files libgui/src/octave-qobject.cc libgui/src/octave-qobject.h
diffstat 2 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/octave-qobject.cc	Sat Nov 02 10:54:48 2019 -0400
+++ b/libgui/src/octave-qobject.cc	Sat Nov 02 15:24:14 2019 -0400
@@ -99,6 +99,8 @@
   // we can handle forward Octave interpreter exceptions from the GUI
   // thread to the interpreter thread.
 
+  base_qobject *base_qobject::s_instance = nullptr;
+
   base_qobject::base_qobject (qt_application& app_context)
     : QObject (), m_app_context (app_context),
       m_argc (m_app_context.sys_argc ()),
@@ -110,6 +112,12 @@
       m_interpreter_qobj (new interpreter_qobject (*this)),
       m_main_thread (new QThread ())
   {
+    if (s_instance)
+      throw std::runtime_error
+        ("only one octave_qobject object may be active");
+
+    s_instance = this;
+
     std::string show_gui_msgs =
       sys::env::getenv ("OCTAVE_SHOW_GUI_MESSAGES");
 
--- a/libgui/src/octave-qobject.h	Sat Nov 02 10:54:48 2019 -0400
+++ b/libgui/src/octave-qobject.h	Sat Nov 02 15:24:14 2019 -0400
@@ -123,6 +123,8 @@
 
     void copy_image_to_clipboard (const QString& file, bool remove_file);
 
+    static base_qobject * the_octave_qobject (void) { return s_instance; }
+
   protected:
 
     qt_application& m_app_context;
@@ -146,6 +148,8 @@
     interpreter_qobject *m_interpreter_qobj;
 
     QThread *m_main_thread;
+
+    static base_qobject *s_instance;
   };
 
   //! This object provides a command-line interface to Octave that may