changeset 23511:232c8d69d934

manage interpreter instance in interpreter object Currently it is only possible to have one application or interpreter object. Enforce that restriction. * octave.h, octave.cc (application::the_interpreter): Delete. (application::application): Ensure only one application is created at once. * interpreter.cc, interpreter.h (interpreter::instance): New static variable. (interpreter::interpreter): Ensure only one is created at once. (interpreter::~interpreter): Set instance to nullptr. (interpreter::the_interpreter): New function. * interpreter-private.cc, interpreter-private.cc (__get_interpreter__): Delete. Replace all uses with octave::interpreter::the_interpreter.
author John W. Eaton <jwe@octave.org>
date Thu, 18 May 2017 10:53:24 -0400
parents b0e23bfa9966
children 4396af814c6a
files libinterp/corefcn/interpreter-private.cc libinterp/corefcn/interpreter-private.h libinterp/corefcn/interpreter.cc libinterp/corefcn/interpreter.h libinterp/octave.cc libinterp/octave.h
diffstat 6 files changed, 26 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/interpreter-private.cc	Wed May 17 22:13:28 2017 -0700
+++ b/libinterp/corefcn/interpreter-private.cc	Thu May 18 10:53:24 2017 -0400
@@ -27,25 +27,19 @@
 #include <string>
 
 #include "error.h"
+#include "load-path.h"
 #include "interpreter-private.h"
-#include "octave.h"
+#include "interpreter.h"
 
 namespace octave
 {
-  interpreter& __get_interpreter__ (const std::string& who)
+  load_path& __get_load_path__ (const std::string& who)
   {
-    interpreter *interp = octave::application::the_interpreter ();
+    interpreter *interp = interpreter::the_interpreter ();
 
     if (! interp)
       error ("%s: interpreter context missing", who.c_str ());
 
-    return *interp;
-  }
-
-  load_path& __get_load_path__ (const std::string& who)
-  {
-    interpreter& interp = __get_interpreter__ (who);
-
-    return interp.get_load_path ();
+    return interp->get_load_path ();
   }
 }
--- a/libinterp/corefcn/interpreter-private.h	Wed May 17 22:13:28 2017 -0700
+++ b/libinterp/corefcn/interpreter-private.h	Thu May 18 10:53:24 2017 -0400
@@ -27,12 +27,9 @@
 
 #include <string>
 
-#include "interpreter.h"
-
 namespace octave
 {
-  // Expect these functions to change without warning.
-  extern interpreter& __get_interpreter__ (const std::string& who);
+  class load_path;
 
   extern load_path& __get_load_path__ (const std::string& who);
 }
--- a/libinterp/corefcn/interpreter.cc	Wed May 17 22:13:28 2017 -0700
+++ b/libinterp/corefcn/interpreter.cc	Thu May 18 10:53:24 2017 -0400
@@ -374,6 +374,12 @@
       m_inhibit_startup_message (false), m_load_path_initialized (false),
       m_history_initialized (false), m_initialized (false)
   {
+    if (instance)
+      throw std::runtime_error
+        ("only one Octave interpreter object may be active");
+
+    instance = this;
+
     current_evaluator = m_evaluator;
 
     // Matlab uses "C" locale for LC_NUMERIC class regardless of local setting
@@ -498,10 +504,13 @@
     octave_interpreter_ready = true;
   }
 
+  interpreter *interpreter::instance = nullptr;
+
   interpreter::~interpreter (void)
   {
     cleanup ();
 
+    instance = 0;
     current_evaluator = 0;
 
     delete m_evaluator;
@@ -934,7 +943,7 @@
               {
                 if (parser.stmt_list)
                   {
-                    parser.stmt_list->accept (*current_evaluator);
+                    parser.stmt_list->accept (*m_evaluator);
 
                     octave_quit ();
 
--- a/libinterp/corefcn/interpreter.h	Wed May 17 22:13:28 2017 -0700
+++ b/libinterp/corefcn/interpreter.h	Thu May 18 10:53:24 2017 -0400
@@ -143,8 +143,14 @@
 
     static bool remove_atexit_function (const std::string& fname);
 
+    static interpreter * the_interpreter (void) { return instance; }
+
   private:
 
+    // The interpreter instance;  Currently it is only possible to
+    // have one.
+    static interpreter *instance;
+
     static std::list<std::string> atexit_functions;
 
     void display_startup_message (void) const;
--- a/libinterp/octave.cc	Wed May 17 22:13:28 2017 -0700
+++ b/libinterp/octave.cc	Thu May 18 10:53:24 2017 -0400
@@ -404,9 +404,10 @@
   void application::init (void)
   {
     if (instance)
-      warning ("octave::application: application already initialized");
-    else
-      instance = this;
+      throw std::runtime_error
+        ("only one Octave application object may be active");
+
+    instance = this;
 
     string_vector all_args = m_options.all_args ();
 
--- a/libinterp/octave.h	Wed May 17 22:13:28 2017 -0700
+++ b/libinterp/octave.h	Thu May 18 10:53:24 2017 -0400
@@ -253,11 +253,6 @@
       return instance ? instance->gui_running () : false;
     }
 
-    static interpreter * the_interpreter (void)
-    {
-      return instance ? instance->m_interpreter : 0;
-    }
-
     // Convenience functions.
 
     static bool forced_interactive (void);