diff liboctave/util/lo-array-errwarn.cc @ 28012:9a965fec21c1

refactor index_exception classes to accommodate std::exception::what method Since the std::exception::what method returns a pointer to a character buffer, make sure that we will return a pointer that is valid at least until the next non-const method is called for the exception object. * quit.h (execution_exception::message): No longer virtual. * lo-array-errwarn.h, lo-array-errwarn.cc (index_exception::message, index_exception::what, index_exception::idx, index_exception::details): Delete. (index_exception::update_message): New virtual method. Call it in constructors and non-const methods. (invalid_index::details, out_of_range::details): Delete. (invalid_index::update_message, out_of_range::update_message): New functions. * data.cc (index_error): Delete static function. Change all uses to call error directly and to use index_exception::what instead of idx and details. * sub2ind.cc (Find2sub): Use use index_exception::what instead of idx and details. Update test. * utils.cc (dims_to_numel): Use use index_exception::what instead of idx and details. * ov-base-mat.cc (octave_base_matrix<MT>::assign): Consistent with other functions in the same file, use index_exception::set_pos_if_unset and rethrow exception instead of calling err_invalid_index. * ov-complex.cc (complex_index_exception::details): Delete. (complex_index_exception::update_message): New function.
author John W. Eaton <jwe@octave.org>
date Sun, 26 Jan 2020 23:07:45 -0500
parents 403df0b32204
children 0a5b15007766
line wrap: on
line diff
--- a/liboctave/util/lo-array-errwarn.cc	Sun Jan 26 23:16:24 2020 -0500
+++ b/liboctave/util/lo-array-errwarn.cc	Sun Jan 26 23:07:45 2020 -0500
@@ -118,23 +118,6 @@
        is1d ? "I" : "..,I,..", idx, ext);
   }
 
-  // Common procedures of base class index_exception, thrown whenever an
-  // object is indexed incorrectly, such as by an index that is out of
-  // range, negative, fractional, complex, or of a non-numeric type.
-
-  std::string
-  index_exception::message (void) const
-  {
-    return expression () + ": " + details ();
-  }
-
-  const char * index_exception::what (void) const noexcept
-  {
-    std::string tmp = message ();
-
-    return tmp.c_str ();
-   }
-
   // Show the expression that caused the error, e.g.,  "A(-1,_)",
   // "A(0+1i)", "A(_,3)".  Show how many indices come before/after the
   // offending one, e.g., (<error>), (<error>,_), or (_,<error>,...[x5]...)
@@ -164,7 +147,7 @@
           buf << "(...[x" << m_dim - 1 << "]...";
       }
 
-    buf << idx ();
+    buf << m_index;
 
     if (show_parens)
       {
@@ -190,15 +173,19 @@
     invalid_index (const std::string& value, octave_idx_type ndim,
                    octave_idx_type dimen)
       : index_exception (value, ndim, dimen)
-    { }
+    {
+      // Virtual, but the one we want to call is defined in this class.
+      update_message ();
+    }
 
-    std::string details (void) const
+    void update_message (void)
     {
-#if defined (OCTAVE_ENABLE_64)
-      return "subscripts must be either integers 1 to (2^63)-1 or logicals";
-#else
-      return "subscripts must be either integers 1 to (2^31)-1 or logicals";
-#endif
+      static std::string exp
+        = std::to_string (std::numeric_limits<octave_idx_type>::digits);
+
+      set_message (expression ()
+                   + ": subscripts must be either integers 1 to (2^" + exp
+                   + ")-1 or logicals");
     }
 
     // ID of error to throw
@@ -254,14 +241,16 @@
                   octave_idx_type dim, octave_idx_type ext,
                   const dim_vector& size)
       : index_exception (value, nd, dim), m_size (size), m_extent (ext)
-    { }
-
-    std::string details (void) const
     {
-      std::string expl;
+      // Virtual, but the one we want to call is defined in this class.
+      update_message ();
+    }
 
-      return "out of bound " + std::to_string (m_extent)
-        + " (dimensions are " + m_size.str ('x') + ")";
+    void update_message (void)
+    {
+      set_message (expression () + ": out of bound "
+                   + std::to_string (m_extent)
+                   + " (dimensions are " + m_size.str ('x') + ")");
     }
 
     // ID of error to throw.