# HG changeset patch # User Michael Goffioul # Date 1411231405 14400 # Node ID 56bc1464ec597d7a658d88ce7955b829f1d24a01 # Parent bb20384acf7b8ec554a26ba01cb9d111ceca05cc 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. diff -r bb20384acf7b -r 56bc1464ec59 libinterp/octave-value/ov-base.h --- 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); diff -r bb20384acf7b -r 56bc1464ec59 libinterp/octave-value/ov-class.cc --- 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::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); diff -r bb20384acf7b -r 56bc1464ec59 libinterp/octave-value/ov-class.h --- 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); diff -r bb20384acf7b -r 56bc1464ec59 libinterp/octave-value/ov-classdef.cc --- 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 diff -r bb20384acf7b -r 56bc1464ec59 libinterp/octave-value/ov-classdef.h --- 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& idx, int nargout); diff -r bb20384acf7b -r 56bc1464ec59 libinterp/octave-value/ov.h --- 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;