from
issubclass
by Jesse Hopkins
Determines if a class is a subclass of another class.
|
| issubclass(SubClassName,SuperClassName)
|
% tf = issubclass(SubClassName,SuperClassName)
% Returns true if the specified "SubClassName" is a subclass of
% the class specified by "SuperClassName", otherwise returns false.
%
% Inputs:
% SubClassName: string. Name of the "Sub Class" that you are testing
% to see if it was inherited from the specified
% "SuperClass"
%
% SuperClassName: string. Name of the "Super Class" that you are
% testing to see if the sub-class was inherited from
%
% Output:
% tf: logical, true if "SubClassName" is in fact a subclass of "SuperClassName"
%
% Examples:
% >> tf = issubclass('MySubClass','MySuperClass')
%
% >> x = MySubClass;
% >> y = MySuperClass;
% >> tf = issubclass(class(x),class(y);
%
% Author: Jesse Hopkins
% Date: Jan 20, 2011
function tf = issubclass(SubClassName,SuperClassName)
%input check
assert(nargin == 2 && ischar(SubClassName) && ischar(SuperClassName),'Invalid inputs!');
%initialize output
tf = false;
subClassMetaData = meta.class.fromName(SubClassName);
if isempty(subClassMetaData)
tf = issubclass_recurse(subClassMetaData,SuperClassName);
else
warning([ mfilename ':NonexistentClass'],'Could not get metadata for class: %s (does it exist?)',SubClassName);
end
end
function tf = issubclass_recurse(subClassMetaData,SuperClassName)
tf = false;
if ~isempty(subClassMetaData)
for i = 1:length(subClassMetaData.SuperClasses)
if strcmp(subClassMetaData.SuperClasses{i}.Name,SuperClassName)
tf = true;
break;
else
tf = issubclass_recurse(subClassMetaData.SuperClasses{i},SuperClassName);
if tf == true
break;
end
end
end
end
end
|
|
Contact us