From: Joseph Spiros Date: Tue, 26 Oct 2004 23:18:13 +0000 (+0000) Subject: Adding a category to NSObject which can dynamically provide an array X-Git-Tag: v1.0~9 X-Git-Url: http://git.ithinksw.org/ITFoundation.git/commitdiff_plain/e1c9d15e410c3ce93c566a64e6514bc339bd526d Adding a category to NSObject which can dynamically provide an array of any class' subclasses (and direct subclasses, meaning only those classes whose parent is the class to which the message was sent) via two new class methods, +subclasses, and +directSubclasses. --- diff --git a/ITCategory-NSObject.h b/ITCategory-NSObject.h new file mode 100644 index 0000000..96c7e31 --- /dev/null +++ b/ITCategory-NSObject.h @@ -0,0 +1,24 @@ +/* + * ITKit + * ITCategory-NSObject.h + * Category which extends NSObject + * + * Original Author : Joseph Spiros + * Responsibility : Joseph Spiros + * + * Copyright (c) 2002 - 2004 iThink Software. + * All Rights Reserved + * + */ + + +#import + + +@interface NSObject (ITCategory) + ++ (NSArray *)subclasses; ++ (NSArray *)directSubclasses; + + +@end diff --git a/ITCategory-NSObject.m b/ITCategory-NSObject.m new file mode 100644 index 0000000..09281e8 --- /dev/null +++ b/ITCategory-NSObject.m @@ -0,0 +1,129 @@ +#import "ITCategory-NSObject.h" +#import + +@implementation NSObject (ITCategory) + ++ (NSArray *)subclasses +{ + NSMutableArray *tempArray; + NSArray *resultArray; + Class *classes; + struct objc_class *superClass; + Class *current; + int count, newCount, index; + + tempArray = [[NSMutableArray allocWithZone:nil] initWithCapacity:12]; + resultArray = nil; + + if (tempArray) + { + classes = NULL; + count = objc_getClassList(NULL, 0); + if (count) + { + classes = malloc(sizeof(Class) * count); + if (classes) + { + newCount = objc_getClassList(classes, count); + while (count < newCount) + { + count = newCount; + free(classes); + classes = malloc(sizeof(Class) * count); + if (classes) + newCount = objc_getClassList(classes, count); + } + count = newCount; + } + } + + if (classes) + { + const Class thisClass = [self class]; + current = classes; + + for (index = 0; index < count; ++index) + { + superClass = (*current)->super_class; + if (superClass) + { + do + { + if (superClass == thisClass) + { + [tempArray addObject:*current]; + break; + } + superClass = superClass->super_class; + } while (superClass); + } + + ++current; + } + + free(classes); + } + + resultArray = [NSArray arrayWithArray:tempArray]; + [tempArray release]; + } + + return resultArray; +} + ++ (NSArray *)directSubclasses +{ + NSMutableArray *tempArray; + NSArray *resultArray; + Class *classes; + Class *current; + int count, newCount, index; + + tempArray = [[NSMutableArray allocWithZone:nil] initWithCapacity:12]; + resultArray = nil; + + if (tempArray) + { + classes = NULL; + count = objc_getClassList(NULL, 0); + if (count) + { + classes = malloc(sizeof(Class) * count); + if (classes) + { + newCount = objc_getClassList(classes, count); + while (count < newCount) + { + count = newCount; + free(classes); + classes = malloc(sizeof(Class) * count); + if (classes) + newCount = objc_getClassList(classes, count); + } + count = newCount; + } + } + + if (classes) + { + const Class thisClass = [self class]; + current = classes; + + for (index = 0; index < count; ++index) + { + if ((*current)->super_class == thisClass) + [tempArray addObject:*current]; + ++current; + } + + free(classes); + } + + resultArray = [NSArray arrayWithArray:tempArray]; + [tempArray release]; + } + + return resultArray; +} + +@end \ No newline at end of file diff --git a/ITFoundation.xcode/project.pbxproj b/ITFoundation.xcode/project.pbxproj index 0ea001c..e7e51ca 100755 --- a/ITFoundation.xcode/project.pbxproj +++ b/ITFoundation.xcode/project.pbxproj @@ -210,6 +210,8 @@ //2A4 2AB93A2C057059DC007E748F = { children = ( + 7C058DF7072F10530082E1E9, + 7C058DF8072F10530082E1E9, 2AB93A3005705A0C007E748F, 2AB93A3105705A0C007E748F, ); @@ -595,6 +597,37 @@ //7C2 //7C3 //7C4 + 7C058DF7072F10530082E1E9 = { + fileEncoding = 4; + isa = PBXFileReference; + lastKnownFileType = sourcecode.c.h; + path = "ITCategory-NSObject.h"; + refType = 4; + sourceTree = ""; + }; + 7C058DF8072F10530082E1E9 = { + fileEncoding = 4; + isa = PBXFileReference; + lastKnownFileType = sourcecode.c.objc; + path = "ITCategory-NSObject.m"; + refType = 4; + sourceTree = ""; + }; + 7C058DF9072F10530082E1E9 = { + fileRef = 7C058DF7072F10530082E1E9; + isa = PBXBuildFile; + settings = { + ATTRIBUTES = ( + Public, + ); + }; + }; + 7C058DFA072F10530082E1E9 = { + fileRef = 7C058DF8072F10530082E1E9; + isa = PBXBuildFile; + settings = { + }; + }; 7C97DC2C05B614300013E85F = { fileEncoding = 4; isa = PBXFileReference; @@ -876,6 +909,7 @@ 3D97137F05D9FBF40033607F, 3D97138A05D9FD6B0033607F, 376AF4DF06597CA900F0979E, + 7C058DF9072F10530082E1E9, ); isa = PBXHeadersBuildPhase; runOnlyForDeploymentPostprocessing = 0; @@ -914,6 +948,7 @@ 3D97138105D9FBFA0033607F, 3D97138B05D9FD6B0033607F, 376AF4E006597CA900F0979E, + 7C058DFA072F10530082E1E9, ); isa = PBXSourcesBuildPhase; runOnlyForDeploymentPostprocessing = 0;