diff m-toolbox/classes/+utils/@helper/isSubclassOf.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m-toolbox/classes/+utils/@helper/isSubclassOf.m	Wed Nov 23 19:22:13 2011 +0100
@@ -0,0 +1,58 @@
+% ISSUBCLASSOF determines if the one class is a subclass of another
+%
+% CALL:
+%             res = isSubclassOf('class1', 'class2')
+%             res = isSubclassOf(metaObject, 'class2');
+%             res = isSubclassOf(metaObject1, metaObject2);
+%
+% $Id: isSubclassOf.m,v 1.3 2011/04/29 15:02:17 ingo Exp $
+%
+function res = isSubclassOf(varargin)
+  
+  if nargin ~= 2
+    help(mfilename);
+    error('Incorrect inputs');
+  end
+  
+  mo1 = getMetaObject(varargin{1});
+  mo2 = getMetaObject(varargin{2});
+  
+  if isempty(mo1) || isempty(mo2)
+    error('### One of the inputs is not a meta class object/name');
+  end
+  
+  res = lt(mo1, mo2);
+  
+end
+
+function obj = getMetaObject(in)
+  if ischar(in)
+    obj = meta.class.fromName(in);
+  elseif isa(in, 'meta.class')
+    obj = in;
+  else
+    error('Unknown input format for object of class %s', class(in));
+  end
+end
+
+function res = checkIsSubclass(mo1, mo2)
+  
+  res = false;
+  
+  % is the super class a user object subclass?
+  if strcmp(mo1.Name, mo2.Name)
+    res = true;
+  else
+    for kk=1:numel(mo1.SuperclassList)
+      mo = mo1.SuperclassList(kk);
+      
+      res = checkIsSubclass(mo, mo2);
+      if res
+        return;
+      else
+        % keep searching...
+      end
+      
+    end
+  end
+end