changeset 19137:56bc1464ec59

Implement "isa" for classdef objects. * ov.h (octave_value::is_instance_of): New method. * ov-base.h (octave_base_value::is_instance_of): Likewise. * ov-class.h (octave_class::is_instance_of): New method declaration. * ov-class.cc (octave_class::is_instance_of): Implement it with find_parent_class. * ov-classdef.h (octave_classdef::is_instance_of): New method declaration. * ov-classdef.cc (octave_classdef::is_instance_of): Implement it with lookup_class and is_superclass.
author Michael Goffioul <michael.goffioul@gmail.com>
date Sat, 20 Sep 2014 12:43:25 -0400
parents bb20384acf7b
children fbe5a2dd64ae
files libinterp/octave-value/ov-base.h libinterp/octave-value/ov-class.cc libinterp/octave-value/ov-class.h libinterp/octave-value/ov-classdef.cc libinterp/octave-value/ov-classdef.h libinterp/octave-value/ov.h
diffstat 6 files changed, 51 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-base.h	Sat Sep 20 06:31:27 2014 -0700
+++ b/libinterp/octave-value/ov-base.h	Sat Sep 20 12:43:25 2014 -0400
@@ -583,6 +583,9 @@
   virtual octave_base_value *unique_parent_class (const std::string&)
   { return 0; }
 
+  virtual bool is_instance_of (const std::string&) const
+  { return false; }
+
   virtual octave_function *function_value (bool silent = false);
 
   virtual octave_user_function *user_function_value (bool silent = false);
--- a/libinterp/octave-value/ov-class.cc	Sat Sep 20 06:31:27 2014 -0700
+++ b/libinterp/octave-value/ov-class.cc	Sat Sep 20 12:43:25 2014 -0400
@@ -1015,6 +1015,35 @@
   return retval;
 }
 
+bool
+octave_class::is_instance_of (const std::string& cls_name) const
+{
+  bool retval = false;
+
+  if (cls_name == class_name ())
+    retval = true;
+  else
+    {
+      for (std::list<std::string>::const_iterator pit = parent_list.begin ();
+           pit != parent_list.end ();
+           pit++)
+        {
+          octave_map::const_iterator smap = map.seek (*pit);
+
+          const Cell& tmp = map.contents (smap);
+
+          const octave_value& vtmp = tmp(0);
+
+          retval = vtmp.is_instance_of (cls_name);
+
+          if (retval)
+            break;
+        }
+    }
+
+  return retval;
+}
+
 string_vector
 octave_class::all_strings (bool pad) const
 {
@@ -1962,7 +1991,7 @@
       if ((cl == "float"   && obj.is_float_type   ()) ||
           (cl == "integer" && obj.is_integer_type ()) ||
           (cl == "numeric" && obj.is_numeric_type ()) ||
-          obj.class_name () == cl || obj.find_parent_class (cl))
+          obj.class_name () == cl || obj.is_instance_of (cl))
         matches(idx) = true;
     }
   return octave_value (matches);
--- a/libinterp/octave-value/ov-class.h	Sat Sep 20 06:31:27 2014 -0700
+++ b/libinterp/octave-value/ov-class.h	Sat Sep 20 12:43:25 2014 -0400
@@ -167,6 +167,8 @@
 
   octave_base_value *unique_parent_class (const std::string&);
 
+  bool is_instance_of (const std::string&) const;
+
   string_vector all_strings (bool pad) const;
 
   void print (std::ostream& os, bool pr_as_read_syntax = false);
--- a/libinterp/octave-value/ov-classdef.cc	Sat Sep 20 06:31:27 2014 -0700
+++ b/libinterp/octave-value/ov-classdef.cc	Sat Sep 20 12:43:25 2014 -0400
@@ -1088,6 +1088,17 @@
     octave_base_value::print_with_name (os, name, print_padding);
 }
 
+bool
+octave_classdef::is_instance_of (const std::string& cls_name) const
+{
+  cdef_class cls = lookup_class (cls_name, false, false);
+
+  if (cls.ok ())
+    return is_superclass (cls, object.get_class ());
+
+  return false;
+}
+
 //----------------------------------------------------------------------------
 
 class octave_classdef_meta : public octave_function
--- a/libinterp/octave-value/ov-classdef.h	Sat Sep 20 06:31:27 2014 -0700
+++ b/libinterp/octave-value/ov-classdef.h	Sat Sep 20 12:43:25 2014 -0400
@@ -1422,6 +1422,8 @@
   void print_with_name (std::ostream& os, const std::string& name,
                         bool print_padding = true);
 
+  bool is_instance_of (const std::string& cls_name) const;
+
   octave_value_list subsref (const std::string& type,
                              const std::list<octave_value_list>& idx,
                              int nargout);
--- a/libinterp/octave-value/ov.h	Sat Sep 20 06:31:27 2014 -0700
+++ b/libinterp/octave-value/ov.h	Sat Sep 20 12:43:25 2014 -0400
@@ -915,6 +915,9 @@
   find_parent_class (const std::string& parent_class_name)
   { return rep->find_parent_class (parent_class_name); }
 
+  bool is_instance_of (const std::string& cls_name) const
+  { return rep->is_instance_of (cls_name); }
+
   octave_function *function_value (bool silent = false) const;
 
   octave_user_function *user_function_value (bool silent = false) const;