changeset 15344:cd499f25f7be

make symbol_information a proper class * symbol-information.h (class symbol_information): Declare as class, not struct. Make data members private. Provide accessor functions. Change all uses. Provide copy constructor, assignment operator, and destructor. Cache hash value. (friend operator ==): Adapt from equals member function. (symbol_information::symbol_information): Adapt from symbol_information::from_symbol_record. Improve handling of dimensions. Don't display dimensions in value column.
author John W. Eaton <jwe@octave.org>
date Mon, 10 Sep 2012 17:09:27 -0400
parents b565850bdce7
children bd906ac220b6
files libgui/src/symbol-information.cc libgui/src/symbol-information.h libgui/src/workspace-model.cc
diffstat 3 files changed, 90 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/symbol-information.cc	Mon Sep 10 15:56:17 2012 -0400
+++ b/libgui/src/symbol-information.cc	Mon Sep 10 17:09:27 2012 -0400
@@ -31,35 +31,49 @@
 
 #include "symbol-information.h"
 
-bool
-symbol_information::from_symbol_record
-  (const symbol_table::symbol_record& symbol_record)
+symbol_information::symbol_information (const symbol_table::symbol_record& sr)
 {
-  if (symbol_record.is_local () && !symbol_record.is_global () && !symbol_record.is_hidden ())
+  if (sr.is_local () && !sr.is_global () && !sr.is_hidden ())
     _scope = local;
-  else if (symbol_record.is_global ())
+  else if (sr.is_global ())
     _scope = global;
-  else if (symbol_record.is_persistent ())
+  else if (sr.is_persistent ())
     _scope = persistent;
   else
     _scope = unknown;
 
-  _symbol = QString (symbol_record.name ().c_str ());
-  _class  = QString (symbol_record.varval ().class_name ().c_str ());
-  octave_value ov = symbol_record.varval ();
+  _symbol = QString (sr.name ().c_str ());
+  _class_name = QString (sr.varval ().class_name ().c_str ());
+  octave_value ov = sr.varval ();
+  dim_vector dv = ov.dims ();
 
   // In case we have really large matrices or strings, cut them down
   // for performance reasons.
   QString short_value_string;
   bool use_short_value_string = false;
-  if (ov.is_matrix_type () || ov.is_cell ())
+  if (ov.is_range ())
     {
-      if (ov.rows () * ov.columns () > 10)
-        {
-          use_short_value_string = true;
-          short_value_string
-            = QString ("%1x%2 items").arg (ov.rows ()).arg (ov.columns ());
-        }
+      use_short_value_string = true;
+
+      Range r = ov.range_value ();
+
+      double base = r.base ();
+      double increment = r.base ();
+      double limit = r.base ();
+
+      std::stringstream buffer;
+
+      buffer << base << ":";
+      if (increment != 1 )
+        buffer << increment << ":";
+      buffer << limit;
+      
+      short_value_string = QString::fromStdString (buffer.str ());
+    }
+  else if (ov.is_matrix_type () || ov.is_cell ())
+    {
+      if (ov.numel () > 10)
+        use_short_value_string = true;
     }
   else if (ov.is_string ())
     {
@@ -83,23 +97,8 @@
     }
   _value.replace("\n", " ");
 
-  if (ov.is_string ())
-    _dimension = QString ("%1").arg (ov.string_value ().length ());
-  else if (ov.is_range ())
-    _dimension =  QString ("%1 : %2 : %3").arg (ov.range_value ().base ())
-      .arg (ov.range_value ().inc ())
-      .arg (ov.range_value ().limit ());
-  else if (ov.is_matrix_type () || ov.is_cell ())
-    _dimension = QString ("%1x%2").arg (ov.rows ())
-      .arg (ov.columns ());
-  else if (ov.is_function_handle ())
-    // See code for func2str for a possible solution
-    _dimension = QString ("func handle");
-  else if (ov.is_inline_function ())
-    // See code for formula for a possible solution
-    _dimension = QString ("inline func");
-  else
-    _dimension = "1";
+  _dimension = QString::fromStdString (dv.str ());
 
-  return true;
+  _hash = _scope + qHash (_symbol) + qHash (_class_name) + qHash (_value)
+    + qHash (_dimension);
 }
--- a/libgui/src/symbol-information.h	Mon Sep 10 15:56:17 2012 -0400
+++ b/libgui/src/symbol-information.h	Mon Sep 10 17:09:27 2012 -0400
@@ -32,8 +32,6 @@
 #include <QString>
 #include <QHash>
 
-#include <sstream>
-
 #include "symtab.h"
 
 /**
@@ -45,8 +43,10 @@
   * about a symbol-table entry that will be used in the model for the
   * graphical user interface.
   */
-struct symbol_information
+class symbol_information
 {
+public:
+
   enum Scope
   {
     unknown     = 0,
@@ -55,37 +55,60 @@
     persistent  = 3
   };
 
-  QString _symbol;
-  QString _class;
-  QString _value;
-  QString _dimension;
-  Scope   _scope;
+  symbol_information (const symbol_table::symbol_record& symbol_record);
+
+  symbol_information (const symbol_information& x)
+    : _scope (x._scope), _symbol (x._symbol), _class_name (x._class_name),
+      _value (x._value), _dimension (x._dimension), _hash (x._hash)
+  { }
 
-  /** Hashes the symbol information for quickly comparing it. */
-  int
-  hash () const
+  symbol_information operator = (const symbol_information& x)
   {
-    return qHash (_symbol) + qHash (_class) + qHash (_value)
-      + qHash (_dimension) + (int)_scope;
+    if (this != &x)
+      {
+        _scope = x._scope;
+        _symbol = x._symbol;
+        _class_name = x._class_name;
+        _value = x._value;
+        _dimension = x._dimension;
+        _hash = x._hash;
+      }
+
+    return *this;
   }
 
-  /** Compares two symbol information objects. */
-  bool
-  equals (const symbol_information& other) const
+  ~symbol_information (void) { }
+
+  QString symbol (void) const { return _symbol; }
+  QString class_name (void) const { return _class_name; }
+  QString value (void) const { return _value; }
+  QString dimension (void) const { return _dimension; }
+  Scope scope (void) const { return _scope; }
+
+  friend bool
+  operator == (const symbol_information& a, const symbol_information& b)
   {
-    if (hash () == other.hash ())
-      {
-        return _symbol == other._symbol
-            && _class  == other._class
-            && _value  == other._value
-            && _scope  == other._scope
-            && _dimension == other._dimension;
-      }
+    return (a.hash () == b.hash ()
+            && a.scope () == b.scope ()
+            && a.symbol () == b.symbol ()
+            && a.class_name () == b.class_name ()
+            && a.value () == b.value ()
+            && a.dimension () == b.dimension ());
   }
 
-  /** Extracts meta information from a given symbol record. */
-  bool
-  from_symbol_record (const symbol_table::symbol_record& symbol_record);
+private:
+
+  // FIXME -- this is not really the scope of the symbol.
+  Scope _scope;
+
+  QString _symbol;
+  QString _class_name;
+  QString _value;
+  QString _dimension;
+
+  int _hash;
+
+  int hash (void) const { return _hash; }
 };
 
 #endif // SYMBOLINFORMATION_H
--- a/libgui/src/workspace-model.cc	Mon Sep 10 15:56:17 2012 -0400
+++ b/libgui/src/workspace-model.cc	Mon Sep 10 17:09:27 2012 -0400
@@ -76,12 +76,8 @@
 
       _symbol_information.clear ();
       for (std::list < symbol_table::symbol_record > ::iterator iterator = symbolTable.begin ();
-         iterator != symbolTable.end (); iterator++)
-      {
-        symbol_information symbolInformation;
-        symbolInformation.from_symbol_record (*iterator);
-        _symbol_information.push_back (symbolInformation);
-      }
+           iterator != symbolTable.end (); iterator++)
+        _symbol_information.push_back (symbol_information (*iterator));
 
       beginResetModel();
       top_level_item (0)->delete_child_items ();
@@ -92,12 +88,12 @@
         {
           tree_item *child = new tree_item ();
 
-          child->set_data (0, s._symbol);
-          child->set_data (1, s._class);
-          child->set_data (2, s._dimension);
-          child->set_data (3, s._value);
+          child->set_data (0, s.symbol ());
+          child->set_data (1, s.class_name ());
+          child->set_data (2, s.dimension ());
+          child->set_data (3, s.value ());
 
-          switch (s._scope)
+          switch (s.scope ())
             {
               case symbol_information::local:       top_level_item (0)->add_child (child); break;
               case symbol_information::global:      top_level_item (1)->add_child (child); break;