changeset 10767:2b041d3995a3

modernize some map usage on toplev.cc and error.cc
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 02 Jul 2010 15:07:58 +0200
parents f0304c545588
children 8a868004a437
files src/ChangeLog src/error.cc src/toplev.cc src/toplev.h
diffstat 4 files changed, 42 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Fri Jul 02 14:10:57 2010 +0200
+++ b/src/ChangeLog	Fri Jul 02 15:07:58 2010 +0200
@@ -1,3 +1,12 @@
+2010-07-02  Jaroslav Hajek  <highegg@gmail.com>
+
+	* toplev.cc (octave_call_stack::do_backtrace): Use static
+	octave_fields struct.
+	(octave_call_stack::empty_backtrace): New static method.
+	* toplev.h: Declare it.
+	* error.cc (Vlast_error_stack, initialize_last_error_stack,
+	Frethrow): Use octave_map or octave_scalar_map where applicable.
+
 2010-07-02  Jaroslav Hajek  <highegg@gmail.com>
 
 	* pt-mat.cc (tm_row_const::tm_row_const_rep::all_1x1,
--- a/src/error.cc	Fri Jul 02 14:10:57 2010 +0200
+++ b/src/error.cc	Fri Jul 02 15:07:58 2010 +0200
@@ -87,7 +87,7 @@
 static std::string Vlast_error_id;
 
 // The last file in which an error occured
-static Octave_map Vlast_error_stack;
+static octave_map Vlast_error_stack;
 
 // Current error state.
 //
@@ -141,24 +141,10 @@
   warning_options.assign ("state", state);
 }
 
-static Octave_map
+static octave_map
 initialize_last_error_stack (void)
 {
-  static bool initialized = false;
-
-  static string_vector sv (4);
-
-  if (! initialized)
-    {
-      sv[0] = "file";
-      sv[1] = "name";
-      sv[2] = "line";
-      sv[3] = "column";
-
-      initialized = true;
-    }
-
-  return Octave_map (dim_vector (0, 1), sv);
+  return octave_call_stack::empty_backtrace ();
 }
 
 // Warning messages are never buffered.
@@ -867,14 +853,14 @@
     print_usage ();
   else
     {
-      Octave_map err = args(0).map_value ();
+      const octave_scalar_map err = args(0).scalar_map_value ();
 
       if (! error_state)
         {
           if (err.contains ("message") && err.contains ("identifier"))
             {
-              std::string msg = err.contents("message")(0).string_value ();
-              std::string id = err.contents("identifier")(0).string_value ();
+              std::string msg = err.contents("message").string_value ();
+              std::string id = err.contents("identifier").string_value ();
               int len = msg.length();
 
               std::string file;
@@ -882,11 +868,11 @@
               int l = -1;
               int c = -1;
 
-              Octave_map err_stack = initialize_last_error_stack ();
+              octave_map err_stack = initialize_last_error_stack ();
 
               if (err.contains ("stack"))
                 {
-                  err_stack = err.contents("stack")(0).map_value ();
+                  err_stack = err.contents("stack").map_value ();
 
                   if (err_stack.numel () > 0)
                     {
--- a/src/toplev.cc	Fri Jul 02 14:10:57 2010 +0200
+++ b/src/toplev.cc	Fri Jul 02 15:07:58 2010 +0200
@@ -234,12 +234,22 @@
   return retval;
 }
 
-Octave_map
+// Use static fields for the best efficiency.
+// NOTE: C++0x will allow these two to be merged into one.
+static const char *bt_fieldnames[] = { "file", "name", "line",
+    "column", "scope", "context", 0 };
+static const octave_fields bt_fields (bt_fieldnames);
+
+octave_map
+octave_call_stack::empty_backtrace (void)
+{
+  return octave_map (dim_vector (0, 1), bt_fields);
+}
+
+octave_map
 octave_call_stack::do_backtrace (size_t nskip,
                                  octave_idx_type& curr_user_frame) const
 {
-  Octave_map retval;
-
   size_t user_code_frames = do_num_user_code_frames (curr_user_frame);
 
   size_t nframes = nskip <= user_code_frames ? user_code_frames - nskip : 0;
@@ -247,21 +257,14 @@
   // Our list is reversed.
   curr_user_frame = nframes - curr_user_frame - 1;
 
-  Cell keys (6, 1);
+  octave_map retval (dim_vector (nframes, 1), bt_fields);
 
-  keys(0) = "file";
-  keys(1) = "name";
-  keys(2) = "line";
-  keys(3) = "column";
-  keys(4) = "scope";
-  keys(5) = "context";
-
-  Cell file (nframes, 1);
-  Cell name (nframes, 1);
-  Cell line (nframes, 1);
-  Cell column (nframes, 1);
-  Cell scope (nframes, 1);
-  Cell context (nframes, 1);
+  Cell& file = retval.contents (0);
+  Cell& name = retval.contents (1);
+  Cell& line = retval.contents (2);
+  Cell& column = retval.contents (3);
+  Cell& scope = retval.contents (4);
+  Cell& context = retval.contents (5);
 
   if (nframes > 0)
     {
@@ -306,13 +309,6 @@
                 }
             }
         }
-
-      retval.assign ("file", file);
-      retval.assign ("name", name);
-      retval.assign ("line", line);
-      retval.assign ("column", column);
-      retval.assign ("scope", scope);
-      retval.assign ("context", context);
     }
 
   return retval;
--- a/src/toplev.h	Fri Jul 02 14:10:57 2010 +0200
+++ b/src/toplev.h	Fri Jul 02 15:07:58 2010 +0200
@@ -266,12 +266,14 @@
       instance->do_goto_base_frame ();
   }
 
-  static Octave_map backtrace (size_t nskip, octave_idx_type& curr_user_frame)
+  static octave_map backtrace (size_t nskip, octave_idx_type& curr_user_frame)
   {
     return instance_ok ()
-      ? instance->do_backtrace (nskip, curr_user_frame) : Octave_map ();
+      ? instance->do_backtrace (nskip, curr_user_frame) : octave_map ();
   }
 
+  static octave_map empty_backtrace (void);
+
   static void pop (void)
   {
     if (instance_ok ())
@@ -389,7 +391,7 @@
       }
   }
 
-  Octave_map do_backtrace (size_t nskip,
+  octave_map do_backtrace (size_t nskip,
                            octave_idx_type& curr_user_frame) const;
 
   bool do_goto_frame (size_t n, bool verbose);