changeset 27266:596312d4f25d

don't use singleton pattern for display_info class * display.h, display.cc (class display_info): Eliminate singleton pattern. Change all uses. * interpreter.h, interpreter.cc (interpreter::m_display_info): New data member. (interpreter::interpreter): Initialize m_display_info depending on application context. (interpreter::get_display_info): New function. * interpreter-private.h, interpreter-private.cc (__get_display_info__): New function.
author John W. Eaton <jwe@octave.org>
date Fri, 19 Jul 2019 10:02:20 -0400
parents 5f65319c7835
children d75c7b4f7645
files libinterp/corefcn/display.cc libinterp/corefcn/display.h libinterp/corefcn/graphics.cc libinterp/corefcn/interpreter-private.cc libinterp/corefcn/interpreter-private.h libinterp/corefcn/interpreter.cc libinterp/corefcn/interpreter.h libinterp/dldfcn/__init_fltk__.cc
diffstat 8 files changed, 80 insertions(+), 137 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/display.cc	Fri Jul 19 19:07:13 2019 +0200
+++ b/libinterp/corefcn/display.cc	Fri Jul 19 10:02:20 2019 -0400
@@ -30,56 +30,36 @@
 #include "defun.h"
 #include "display.h"
 #include "error.h"
+#include "interpreter.h"
 #include "ov.h"
 #include "ovl.h"
 
 namespace octave
 {
-  display_info *display_info::instance = nullptr;
-
-  void
-  display_info::init (const std::string& dpy_name, bool query)
+  void display_info::initialize (void)
   {
-    if (query)
-      {
-        int avail = 0;
-
-        const char *display_name
-          = dpy_name.empty () ? nullptr : dpy_name.c_str ();
-
-        const char *msg
-          = octave_get_display_info (display_name, &m_ht, &m_wd, &m_dp,
-                                     &m_rx, &m_ry, &avail);
-
-        m_dpy_avail = avail;
+    int avail = 0;
 
-        if (msg)
-          m_err_msg = msg;
-      }
-  }
+    const char *msg
+      = octave_get_display_info (nullptr, &m_ht, &m_wd, &m_dp,
+                                 &m_rx, &m_ry, &avail);
 
-  bool
-  display_info::instance_ok (bool query)
-  {
-    bool retval = true;
+    m_dpy_avail = avail;
 
-    if (! instance)
-      {
-        instance = new display_info (query);
-        singleton_cleanup_list::add (cleanup_instance);
-      }
-
-    return retval;
+    if (msg)
+      warning ("%s", msg);
   }
 }
 
-DEFUN (have_window_system, , ,
-       doc: /* -*- texinfo -*-
+DEFMETHOD (have_window_system, interp, , ,
+           doc: /* -*- texinfo -*-
 @deftypefn {} {} have_window_system ()
 Return true if a window system is available (X11, Windows, or Apple OS X)
 and false otherwise.
 @seealso{isguirunning}
 @end deftypefn */)
 {
-  return ovl (octave::display_info::display_available ());
+  octave::display_info& dpy_info = interp.get_display_info ();
+
+  return ovl (dpy_info.display_available ());
 }
--- a/libinterp/corefcn/display.h	Fri Jul 19 19:07:13 2019 +0200
+++ b/libinterp/corefcn/display.h	Fri Jul 19 10:02:20 2019 -0400
@@ -31,121 +31,50 @@
 
 namespace octave
 {
-  class
-  OCTINTERP_API
-  display_info
+  class display_info
   {
-  protected:
-
-    display_info (void)
-      : m_ht (1), m_wd (1), m_dp (0),
-        m_rx (72), m_ry (72), m_dpy_avail (false), m_err_msg ()
-    {
-      init ();
-    }
-
-    explicit display_info (const std::string& dpy_name)
-      : m_ht (1), m_wd (1), m_dp (0),
-        m_rx (72), m_ry (72), m_dpy_avail (false), m_err_msg ()
-    {
-      init (dpy_name);
-    }
-
-    explicit display_info (bool query)
-      : m_ht (1), m_wd (1), m_dp (0),
-        m_rx (72), m_ry (72), m_dpy_avail (false), m_err_msg ()
-    {
-      init ("", query);
-    }
-
-    explicit display_info (const std::string& dpy_name, bool query)
-      : m_ht (1), m_wd (1), m_dp (0),
-        m_rx (72), m_ry (72), m_dpy_avail (false), m_err_msg ()
-    {
-      init (dpy_name, query);
-    }
-
   public:
 
-    static int height (void)
-    {
-      return instance_ok () ? instance->do_height () : 0;
-    }
-
-    static int width (void)
-    {
-      return instance_ok () ? instance->do_width () : 0;
-    }
+    // Create object with default values.  To be useful, you must call
+    // initialize to find the actual system parameters for the given
+    // display.
 
-    static int depth (void)
-    {
-      return instance_ok () ? instance->do_depth () : 0;
-    }
+    display_info (void)
+      : m_rx (72), m_ry (72), m_ht (1), m_wd (1), m_dp (0), m_dpy_avail (false)
+    { }
 
-    static double x_dpi (void)
-    {
-      return instance_ok () ? instance->do_x_dpi () : 0;
-    }
+    ~display_info (void) = default;
+
+    display_info (const display_info&) = default;
+
+    display_info& operator = (const display_info&) = default;
 
-    static double y_dpi (void)
-    {
-      return instance_ok () ? instance->do_y_dpi () : 0;
-    }
+    void initialize (void);
 
-    static bool display_available (void)
-    {
-      std::string msg;
-      return instance_ok () ? instance->do_display_available (msg) : false;
-    }
+    double x_dpi (void) const { return m_rx; }
+
+    double y_dpi (void) const { return m_ry; }
 
-    static bool display_available (std::string& msg)
-    {
-      return instance_ok () ? instance->do_display_available (msg) : false;
-    }
+    int height (void) const { return m_ht; }
+
+    int width (void) const { return m_wd; }
 
-    // To disable querying the window system for defaults, this function
-    // must be called before any other display_info function.
-    static void no_window_system (void)
-    {
-      instance_ok (false);
-    }
+    int depth (void) const { return m_dp; }
+
+    bool display_available (void) const { return m_dpy_avail; }
 
   private:
 
-    static display_info *instance;
-
-    static void cleanup_instance (void) { delete instance; instance = nullptr; }
+    // X- and Y- Resolution of the display in dots (pixels) per inch.
+    double m_rx;
+    double m_ry;
 
     // Height, width, and depth of the display.
     int m_ht;
     int m_wd;
     int m_dp;
 
-    // X- and Y- Resolution of the display in dots (pixels) per inch.
-    double m_rx;
-    double m_ry;
-
     bool m_dpy_avail;
-
-    std::string m_err_msg;
-
-    int do_height (void) const { return m_ht; }
-    int do_width (void) const { return m_wd; }
-    int do_depth (void) const { return m_dp; }
-
-    double do_x_dpi (void) const { return m_rx; }
-    double do_y_dpi (void) const { return m_ry; }
-
-    bool do_display_available (std::string& msg) const
-    {
-      msg = m_err_msg;
-
-      return m_dpy_avail;
-    }
-
-    void init (const std::string& dpy_name = "", bool query = true);
-
-    static bool instance_ok (bool query = true);
   };
 }
 
--- a/libinterp/corefcn/graphics.cc	Fri Jul 19 19:07:13 2019 +0200
+++ b/libinterp/corefcn/graphics.cc	Fri Jul 19 10:02:20 2019 -0400
@@ -220,7 +220,10 @@
 static double
 default_screendepth (void)
 {
-  return octave::display_info::depth ();
+  octave::display_info& dpy_info
+    = octave::__get_display_info__ ("default_screendepth");
+
+  return dpy_info.depth ();
 }
 
 static Matrix
@@ -228,10 +231,13 @@
 {
   Matrix retval (1, 4);
 
+  octave::display_info& dpy_info
+    = octave::__get_display_info__ ("default_screensize");
+
   retval(0) = 1.0;
   retval(1) = 1.0;
-  retval(2) = octave::display_info::width ();
-  retval(3) = octave::display_info::height ();
+  retval(2) = dpy_info.width ();
+  retval(3) = dpy_info.height ();
 
   return retval;
 }
@@ -239,7 +245,10 @@
 static double
 default_screenpixelsperinch (void)
 {
-  return (octave::display_info::x_dpi () + octave::display_info::y_dpi ()) / 2;
+  octave::display_info& dpy_info
+    = octave::__get_display_info__ ("default_screenpixelsperinch");
+
+  return (dpy_info.x_dpi () + dpy_info.y_dpi ()) / 2;
 }
 
 static Matrix
--- a/libinterp/corefcn/interpreter-private.cc	Fri Jul 19 19:07:13 2019 +0200
+++ b/libinterp/corefcn/interpreter-private.cc	Fri Jul 19 10:02:20 2019 -0400
@@ -30,6 +30,7 @@
 #include "bp-table.h"
 #include "cdef-manager.h"
 #include "child-list.h"
+#include "display.h"
 #include "error.h"
 #include "event-manager.h"
 #include "gtk-manager.h"
@@ -182,6 +183,13 @@
     return interp.get_cdef_manager ();
   }
 
+  display_info& __get_display_info__ (const std::string& who)
+  {
+    interpreter& interp = __get_interpreter__ (who);
+
+    return interp.get_display_info ();
+  }
+
   gtk_manager& __get_gtk_manager__ (const std::string& who)
   {
     interpreter& interp = __get_interpreter__ (who);
--- a/libinterp/corefcn/interpreter-private.h	Fri Jul 19 19:07:13 2019 +0200
+++ b/libinterp/corefcn/interpreter-private.h	Fri Jul 19 10:02:20 2019 -0400
@@ -35,6 +35,7 @@
   class bp_table;
   class cdef_manager;
   class child_list;
+  class display_info;
   class dynamic_loader;
   class error_system;
   class event_manager;
@@ -85,6 +86,8 @@
 
   extern cdef_manager& __get_cdef_manager__ (const std::string& who);
 
+  extern display_info& __get_display_info__ (const std::string& who);
+
   extern gtk_manager& __get_gtk_manager__ (const std::string& who);
 
 
--- a/libinterp/corefcn/interpreter.cc	Fri Jul 19 19:07:13 2019 +0200
+++ b/libinterp/corefcn/interpreter.cc	Fri Jul 19 10:02:20 2019 -0400
@@ -362,6 +362,7 @@
 
   interpreter::interpreter (application *app_context)
     : m_app_context (app_context),
+      m_display_info (),
       m_environment (),
       m_settings (),
       m_error_system (*this),
@@ -426,6 +427,9 @@
     else
       quit_allowed = false;
 
+    if (! m_app_context)
+      m_display_info.initialize ();
+
     bool line_editing = false;
     bool traditional = false;
 
@@ -457,8 +461,8 @@
         if (! image_path.empty ())
           m_environment.image_path (image_path);
 
-        if (options.no_window_system ())
-          display_info::no_window_system ();
+        if (! options.no_window_system ())
+          m_display_info.initialize ();
 
         // Is input coming from a terminal?  If so, we are probably
         // interactive.
--- a/libinterp/corefcn/interpreter.h	Fri Jul 19 19:07:13 2019 +0200
+++ b/libinterp/corefcn/interpreter.h	Fri Jul 19 10:02:20 2019 -0400
@@ -33,6 +33,7 @@
 #include "str-vec.h"
 
 #include "cdef-manager.h"
+#include "display.h"
 #include "dynamic-ld.h"
 #include "environment.h"
 #include "error.h"
@@ -151,6 +152,11 @@
       return m_initialized;
     }
 
+    display_info& get_display_info (void)
+    {
+      return m_display_info;
+    }
+
     environment& get_environment (void)
     {
       return m_environment;
@@ -419,6 +425,8 @@
 
     application *m_app_context;
 
+    display_info m_display_info;
+
     environment m_environment;
 
     settings m_settings;
--- a/libinterp/dldfcn/__init_fltk__.cc	Fri Jul 19 19:07:13 2019 +0200
+++ b/libinterp/dldfcn/__init_fltk__.cc	Fri Jul 19 10:02:20 2019 -0400
@@ -2460,7 +2460,9 @@
 @end deftypefn */)
 {
 #if defined (HAVE_FLTK)
-  if (! octave::display_info::display_available ())
+  octave::display_info& dpy_info = interp.get_display_info ();
+
+  if (! dpy_info.display_available ())
     error ("__init_fltk__: no graphics DISPLAY available");
   else if (! toolkit_loaded)
     {