diff src/ov-class.cc @ 8154:265a821f6555

Add subsindex and ismethod functions
author David Bateman <dbateman@free.fr>
date Fri, 26 Sep 2008 13:29:29 -0400
parents 5bf4e2c13ed8
children ebf6f6a0f9a7
line wrap: on
line diff
--- a/src/ov-class.cc	Fri Sep 26 13:23:02 2008 -0400
+++ b/src/ov-class.cc	Fri Sep 26 13:29:29 2008 -0400
@@ -561,6 +561,40 @@
   return retval;
 }
 
+idx_vector
+octave_class::index_vector (void) const
+{
+  idx_vector retval;
+
+  octave_value meth = symbol_table::find_method ("subsindex", class_name ());
+
+  if (meth.is_defined ())
+    {
+      octave_value_list args;
+      args(0) = octave_value (new octave_class (map, c_name));
+
+      octave_value_list tmp = feval (meth.function_value (), args, 1);
+
+      if (!error_state && tmp.length () >= 1)
+	{
+	  if (tmp(0).is_object())
+	    error ("subsindex function must return a valid index vector");
+	  else
+	    // Index vector returned by subsindex is zero based 
+	    // (why this inconsistency Mathworks?), and so we must
+	    // add one to the value returned as the index_vector method
+	    // expects it to be one based.
+	    retval = do_binary_op (octave_value::op_add, tmp (0), 
+				   octave_value (1.0)).index_vector ();
+	}
+    }
+  else
+    error ("no subsindex method defined for class %s",
+	   class_name().c_str ());
+
+  return retval;
+}
+
 size_t
 octave_class::byte_size (void) const
 {
@@ -1025,6 +1059,47 @@
   return retval;
 }
 
+DEFUN (ismethod, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} ismethod (@var{x}, @var{method})\n\
+Return true if @var{x} is a class object and the string @var{method}\n\
+is a method of this class.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  if (args.length () == 2)
+    {
+      octave_value arg = args(0);
+
+      std::string class_name;
+
+      if (arg.is_object ())
+	class_name = arg.class_name ();
+      else if (arg.is_string ())
+	class_name = arg.string_value ();
+      else
+	error ("ismethod: expecting object or class name as first argument");
+
+      if (! error_state)
+	{
+	  std::string method = args(1).string_value ();
+
+	  if (! error_state)
+	    {
+	      if (load_path::find_method (class_name, method) != std::string ())
+		retval = true;
+	      else
+		retval = false;
+	    }
+	}
+    }
+  else
+    print_usage ();
+
+  return retval;
+}
+
 DEFCMD (methods, args, nargout,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} methods (@var{x})\n\