changeset 10075:84b0725f4b09

return class types by reference in const Array element access functions
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 08 Jan 2010 11:25:08 +0100
parents 5e2b4b7967cc
children 4b270d1540f7
files liboctave/Array.cc liboctave/Array.h liboctave/ChangeLog src/ChangeLog src/DLD-FUNCTIONS/cellfun.cc src/oct-obj.h src/ov-base.cc src/ov-base.h src/ov-builtin.h src/ov-fcn-handle.h src/ov-usr-fcn.h src/ov.cc src/ov.h
diffstat 13 files changed, 82 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/Array.cc	Fri Jan 08 08:27:45 2010 +0100
+++ b/liboctave/Array.cc	Fri Jan 08 11:25:08 2010 +0100
@@ -188,62 +188,36 @@
 }
 
 template <class T>
-T
+T&
 Array<T>::range_error (const char *fcn, octave_idx_type n) const
 {
   (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n);
-  return T ();
-}
-
-template <class T>
-T&
-Array<T>::range_error (const char *fcn, octave_idx_type n)
-{
-  (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n);
   static T foo;
   return foo;
 }
 
 template <class T>
-T
+T&
 Array<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const
 {
   (*current_liboctave_error_handler)
     ("%s (%d, %d): range error", fcn, i, j);
-  return T ();
-}
-
-template <class T>
-T&
-Array<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j)
-{
-  (*current_liboctave_error_handler)
-    ("%s (%d, %d): range error", fcn, i, j);
   static T foo;
   return foo;
 }
 
 template <class T>
-T
+T&
 Array<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j, octave_idx_type k) const
 {
   (*current_liboctave_error_handler)
     ("%s (%d, %d, %d): range error", fcn, i, j, k);
-  return T ();
-}
-
-template <class T>
-T&
-Array<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j, octave_idx_type k)
-{
-  (*current_liboctave_error_handler)
-    ("%s (%d, %d, %d): range error", fcn, i, j, k);
   static T foo;
   return foo;
 }
 
 template <class T>
-T
+T&
 Array<T>::range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const
 {
   std::ostringstream buf;
@@ -264,31 +238,6 @@
 
   (*current_liboctave_error_handler) (buf_str.c_str ());
 
-  return T ();
-}
-
-template <class T>
-T&
-Array<T>::range_error (const char *fcn, const Array<octave_idx_type>& ra_idx)
-{
-  std::ostringstream buf;
-
-  buf << fcn << " (";
-
-  octave_idx_type n = ra_idx.length ();
-
-  if (n > 0)
-    buf << ra_idx(0);
-
-  for (octave_idx_type i = 1; i < n; i++)
-    buf << ", " << ra_idx(i);
-
-  buf << "): range error";
-
-  std::string buf_str = buf.str ();
-
-  (*current_liboctave_error_handler) (buf_str.c_str ());
-
   static T foo;
   return foo;
 }
--- a/liboctave/Array.h	Fri Jan 08 08:27:45 2010 +0100
+++ b/liboctave/Array.h	Fri Jan 08 11:25:08 2010 +0100
@@ -111,6 +111,8 @@
 
   typedef T element_type;
 
+  typedef typename ref_param<T>::type crefT;
+
   typedef bool (*compare_fcn_type) (typename ref_param<T>::type,
 				    typename ref_param<T>::type);
 
@@ -303,33 +305,28 @@
   
   octave_idx_type compute_index (const Array<octave_idx_type>& ra_idx) const;
 
-  T range_error (const char *fcn, octave_idx_type n) const;
-  T& range_error (const char *fcn, octave_idx_type n);
-
-  T range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const;
-  T& range_error (const char *fcn, octave_idx_type i, octave_idx_type j);
-
-  T range_error (const char *fcn, octave_idx_type i, octave_idx_type j, octave_idx_type k) const;
-  T& range_error (const char *fcn, octave_idx_type i, octave_idx_type j, octave_idx_type k);
-
-  T range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const;
-  T& range_error (const char *fcn, const Array<octave_idx_type>& ra_idx);
+  T& range_error (const char *fcn, octave_idx_type n) const;
+  T& range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const;
+  T& range_error (const char *fcn, octave_idx_type i, octave_idx_type j, octave_idx_type k) const;
+  T& range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const;
 
   // No checking, even for multiple references, ever.
 
   T& xelem (octave_idx_type n) { return slice_data [n]; }
-  T xelem (octave_idx_type n) const { return slice_data [n]; }
+  crefT xelem (octave_idx_type n) const { return slice_data [n]; }
 
   T& xelem (octave_idx_type i, octave_idx_type j) { return xelem (dim1()*j+i); }
-  T xelem (octave_idx_type i, octave_idx_type j) const { return xelem (dim1()*j+i); }
+  crefT xelem (octave_idx_type i, octave_idx_type j) const { return xelem (dim1()*j+i); }
 
-  T& xelem (octave_idx_type i, octave_idx_type j, octave_idx_type k) { return xelem (i, dim2()*k+j); }
-  T xelem (octave_idx_type i, octave_idx_type j, octave_idx_type k) const { return xelem (i, dim2()*k+j); }
+  T& xelem (octave_idx_type i, octave_idx_type j, octave_idx_type k) 
+    { return xelem (i, dim2()*k+j); }
+  crefT xelem (octave_idx_type i, octave_idx_type j, octave_idx_type k) const 
+    { return xelem (i, dim2()*k+j); }
 
   T& xelem (const Array<octave_idx_type>& ra_idx)
     { return xelem (compute_index (ra_idx)); }
 
-  T xelem (const Array<octave_idx_type>& ra_idx) const
+  crefT xelem (const Array<octave_idx_type>& ra_idx) const
     { return xelem (compute_index (ra_idx)); }
 
   // FIXME -- would be nice to fix this so that we don't
@@ -398,7 +395,7 @@
   T& operator () (const Array<octave_idx_type>& ra_idx) { return elem (ra_idx); }
 #endif
 
-  T checkelem (octave_idx_type n) const
+  crefT checkelem (octave_idx_type n) const
     {
       if (n < 0 || n >= slice_len)
 	return range_error ("T Array<T>::checkelem", n);
@@ -406,7 +403,7 @@
 	return xelem (n);
     }
 
-  T checkelem (octave_idx_type i, octave_idx_type j) const
+  crefT checkelem (octave_idx_type i, octave_idx_type j) const
     {
       if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
 	return range_error ("T Array<T>::checkelem", i, j);
@@ -414,7 +411,7 @@
 	return elem (dim1()*j+i);
     }
 
-  T checkelem (octave_idx_type i, octave_idx_type j, octave_idx_type k) const
+  crefT checkelem (octave_idx_type i, octave_idx_type j, octave_idx_type k) const
     {
       if (i < 0 || j < 0 || k < 0 || i >= dim1 () || j >= dim2 () || k >= dim3 ())
 	return range_error ("T Array<T>::checkelem", i, j, k);
@@ -422,7 +419,7 @@
 	return Array<T>::elem (i, Array<T>::dim1()*k+j);
     }
 
-  T checkelem (const Array<octave_idx_type>& ra_idx) const
+  crefT checkelem (const Array<octave_idx_type>& ra_idx) const
     {
       octave_idx_type i = compute_index (ra_idx);
 
@@ -432,25 +429,25 @@
 	return Array<T>::elem (i);
     }
 
-  T elem (octave_idx_type n) const { return xelem (n); }
+  crefT elem (octave_idx_type n) const { return xelem (n); }
 
-  T elem (octave_idx_type i, octave_idx_type j) const { return elem (dim1()*j+i); }
+  crefT elem (octave_idx_type i, octave_idx_type j) const { return elem (dim1()*j+i); }
 
-  T elem (octave_idx_type i, octave_idx_type j, octave_idx_type k) const { return elem (i, dim2()*k+j); }
+  crefT elem (octave_idx_type i, octave_idx_type j, octave_idx_type k) const { return elem (i, dim2()*k+j); }
 
-  T elem (const Array<octave_idx_type>& ra_idx) const
+  crefT elem (const Array<octave_idx_type>& ra_idx) const
     { return Array<T>::elem (compute_index (ra_idx)); }
 
 #if defined (BOUNDS_CHECKING)
-  T operator () (octave_idx_type n) const { return checkelem (n); }
-  T operator () (octave_idx_type i, octave_idx_type j) const { return checkelem (i, j); }
-  T operator () (octave_idx_type i, octave_idx_type j, octave_idx_type k) const { return checkelem (i, j, k); }
-  T operator () (const Array<octave_idx_type>& ra_idx) const { return checkelem (ra_idx); }
+  crefT operator () (octave_idx_type n) const { return checkelem (n); }
+  crefT operator () (octave_idx_type i, octave_idx_type j) const { return checkelem (i, j); }
+  crefT operator () (octave_idx_type i, octave_idx_type j, octave_idx_type k) const { return checkelem (i, j, k); }
+  crefT operator () (const Array<octave_idx_type>& ra_idx) const { return checkelem (ra_idx); }
 #else
-  T operator () (octave_idx_type n) const { return elem (n); }
-  T operator () (octave_idx_type i, octave_idx_type j) const { return elem (i, j); }
-  T operator () (octave_idx_type i, octave_idx_type j, octave_idx_type k) const { return elem (i, j, k); }
-  T operator () (const Array<octave_idx_type>& ra_idx) const { return elem (ra_idx); }
+  crefT operator () (octave_idx_type n) const { return elem (n); }
+  crefT operator () (octave_idx_type i, octave_idx_type j) const { return elem (i, j); }
+  crefT operator () (octave_idx_type i, octave_idx_type j, octave_idx_type k) const { return elem (i, j, k); }
+  crefT operator () (const Array<octave_idx_type>& ra_idx) const { return elem (ra_idx); }
 #endif
 
   // Fast extractors. All of these produce shallow copies.
--- a/liboctave/ChangeLog	Fri Jan 08 08:27:45 2010 +0100
+++ b/liboctave/ChangeLog	Fri Jan 08 11:25:08 2010 +0100
@@ -1,3 +1,11 @@
+2010-01-08  Jaroslav Hajek  <highegg@gmail.com>
+
+	* Array.h (Array<T>::crefT): New helper typedef.
+	(Array<T>::elem const, Array<T>::xelem const, 
+	Array<T>::checkelem const, Array<T>::operator () const): Return crefT.
+	(Array<T>::range_error const): Remove overloads.
+	* Array.cc (Array<T>::range_error const): Remove overloads.
+
 2010-01-07  John W. Eaton  <jwe@octave.org>
 
 	* oct-time.cc (octave_time::stamp): Assume gettimeofday is
--- a/src/ChangeLog	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/ChangeLog	Fri Jan 08 11:25:08 2010 +0100
@@ -1,3 +1,21 @@
+2010-01-08  Jaroslav Hajek  <highegg@gmail.com>
+
+	* oct-obj.h (octave_value_list::operator () const,
+	octave_value_list::elem const): Return const octave_value&.
+	* ov.cc (octave_value::function_value,
+	octave_value::user_function_value, octave_value::user_script_value,
+	octave_value::user_code_value, octave_value::fcn_handle_value,
+	octave_value::fcn_inline_value): Declare as const.
+	* ov.h: Likewise.
+	* ov-base.h (octave_value::function_value const): Remove overload.
+	* ov-usr-fcn.h (octave_user_function::function_value const): Likewise.
+	* ov-builtin.h (octave_builtin::function_value const): Likewise.
+	* ov-builtin.h (octave_builtin::function_value const): Likewise.
+
+	* DLD-FUNCTIONS/cellfun.cc (Fcellfun): Take a non-const copy prior to
+	calling non-const functions.
+
+
 2010-01-08  Jaroslav Hajek  <highegg@gmail.com>
 
 	* DLD-FUNCTIONS/strfind.cc (qs_replace): New function.
--- a/src/DLD-FUNCTIONS/cellfun.cc	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/DLD-FUNCTIONS/cellfun.cc	Fri Jan 08 11:25:08 2010 +0100
@@ -445,7 +445,10 @@
             {
               Cell result (f_args.dims ());
               for (octave_idx_type count = 0; count < k && ! error_state; count++)
-                result(count) = f_args.elem (count).subsref (type, idx);
+                {
+                  octave_value tmp = f_args.elem (count);
+                  result(count) = tmp.subsref (type, idx);
+                }
 
               retval(0) = result;
             }
@@ -1325,7 +1328,7 @@
 		      if (j != 0)
 			n /= new_dv(j-1);
 		    }
-		  ret(i) = args(0).do_index_op(lst, 0);
+		  ret(i) = octave_value(args(0)).do_index_op(lst, 0);
 		  if (error_state)
 		    break;
 		}
@@ -1374,7 +1377,7 @@
 		  OCTAVE_QUIT;
 
 		  lst(0) = Range(idx + 1., idx + d(i));
-		  ret(i) = args(0).do_index_op(lst, 0);
+		  ret(i) = octave_value(args(0)).do_index_op(lst, 0);
 		  idx += d(i);
 		  if (error_state)
 		    break;
--- a/src/oct-obj.h	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/oct-obj.h	Fri Jan 08 11:25:08 2010 +0100
@@ -106,7 +106,7 @@
 
   octave_value& operator () (octave_idx_type n) { return elem (n); }
 
-  octave_value operator () (octave_idx_type n) const { return elem (n); }
+  const octave_value& operator () (octave_idx_type n) const { return elem (n); }
 
   octave_idx_type length (void) const { return data.length (); }
 
@@ -165,7 +165,7 @@
       return data(n);
     }
 
-  octave_value elem (octave_idx_type n) const
+  const octave_value& elem (octave_idx_type n) const
     { return data(n); }
 };
 
--- a/src/ov-base.cc	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/ov-base.cc	Fri Jan 08 11:25:08 2010 +0100
@@ -928,17 +928,6 @@
   return retval;
 }
 
-const octave_function *
-octave_base_value::function_value (bool silent) const
-{
-  const octave_function *retval = 0;
-
-  if (! silent)
-    gripe_wrong_type_arg ("octave_base_value::function_value()",
-			  type_name ());
-  return retval;
-}
-
 octave_user_function *
 octave_base_value::user_function_value (bool silent)
 {
--- a/src/ov-base.h	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/ov-base.h	Fri Jan 08 11:25:08 2010 +0100
@@ -542,8 +542,6 @@
 
   virtual octave_function *function_value (bool silent = false);
 
-  virtual const octave_function *function_value (bool silent = false) const;
-
   virtual octave_user_function *user_function_value (bool silent = false);
 
   virtual octave_user_script *user_script_value (bool silent = false);
--- a/src/ov-builtin.h	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/ov-builtin.h	Fri Jan 08 11:25:08 2010 +0100
@@ -63,8 +63,6 @@
 
   octave_function *function_value (bool = false) { return this; }
 
-  const octave_function *function_value (bool = false) const { return this; }
-
   bool is_builtin_function (void) const { return true; }
 
   octave_value_list
--- a/src/ov-fcn-handle.h	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/ov-fcn-handle.h	Fri Jan 08 11:25:08 2010 +0100
@@ -94,9 +94,6 @@
   octave_function *function_value (bool = false)
     { return fcn.function_value (); }
 
-  const octave_function *function_value (bool = false) const
-    { return fcn.function_value (); }
-
   octave_user_function *user_function_value (bool = false)
     { return fcn.user_function_value (); }
 
--- a/src/ov-usr-fcn.h	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/ov-usr-fcn.h	Fri Jan 08 11:25:08 2010 +0100
@@ -91,8 +91,6 @@
 
   octave_function *function_value (bool = false) { return this; }
 
-  const octave_function *function_value (bool = false) const { return this; }
-
   octave_user_script *user_script_value (bool = false) { return this; }
 
   octave_user_code *user_code_value (bool = false) { return this; }
@@ -181,8 +179,6 @@
 
   octave_function *function_value (bool = false) { return this; }
 
-  const octave_function *function_value (bool = false) const { return this; }
-
   octave_user_function *user_function_value (bool = false) { return this; }
 
   octave_user_code *user_code_value (bool = false) { return this; }
--- a/src/ov.cc	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/ov.cc	Fri Jan 08 11:25:08 2010 +0100
@@ -1424,43 +1424,37 @@
 }
 
 octave_function *
-octave_value::function_value (bool silent)
-{
-  return rep->function_value (silent);
-}
-
-const octave_function *
 octave_value::function_value (bool silent) const
 {
   return rep->function_value (silent);
 }
 
 octave_user_function *
-octave_value::user_function_value (bool silent)
+octave_value::user_function_value (bool silent) const
 {
   return rep->user_function_value (silent);
 }
 
 octave_user_script *
-octave_value::user_script_value (bool silent)
+octave_value::user_script_value (bool silent) const
 {
   return rep->user_script_value (silent);
 }
 
 octave_user_code *
-octave_value::user_code_value (bool silent)
+octave_value::user_code_value (bool silent) const
 {
   return rep->user_code_value (silent);
 }
 
 octave_fcn_handle *
-octave_value::fcn_handle_value (bool silent)
+octave_value::fcn_handle_value (bool silent) const
 {
   return rep->fcn_handle_value (silent);
 }
 
 octave_fcn_inline *
-octave_value::fcn_inline_value (bool silent)
+octave_value::fcn_inline_value (bool silent) const
 {
   return rep->fcn_inline_value (silent);
 }
@@ -2745,7 +2739,10 @@
       decode_subscripts ("subsref", args(1), type, idx);
 
       if (! error_state)
-	retval = args(0).subsref (type, idx, nargout);
+        {
+          octave_value tmp = args(0);
+          retval = tmp.subsref (type, idx, nargout);
+        }
     }
   else
     print_usage ();
--- a/src/ov.h	Fri Jan 08 08:27:45 2010 +0100
+++ b/src/ov.h	Fri Jan 08 11:25:08 2010 +0100
@@ -860,19 +860,17 @@
   find_parent_class (const std::string& parent_class_name)
     { return rep->find_parent_class (parent_class_name); }
 
-  octave_function *function_value (bool silent = false);
+  octave_function *function_value (bool silent = false) const;
 
-  const octave_function *function_value (bool silent = false) const;
+  octave_user_function *user_function_value (bool silent = false) const;
 
-  octave_user_function *user_function_value (bool silent = false);
+  octave_user_script *user_script_value (bool silent = false) const;
 
-  octave_user_script *user_script_value (bool silent = false);
-
-  octave_user_code *user_code_value (bool silent = false);
+  octave_user_code *user_code_value (bool silent = false) const;
 
-  octave_fcn_handle *fcn_handle_value (bool silent = false);
+  octave_fcn_handle *fcn_handle_value (bool silent = false) const;
 
-  octave_fcn_inline *fcn_inline_value (bool silent = false);
+  octave_fcn_inline *fcn_inline_value (bool silent = false) const;
 
   octave_value_list list_value (void) const;