Huge update to ITFoundation. I've gone through every file (except queue.h
[ITFoundation.git] / ITCategory-NSObject.m
1 #import "ITCategory-NSObject.h"
2 #import <objc/objc-runtime.h>
3
4 @implementation NSObject (ITFoundationCategory)
5
6 + (NSArray *)subclasses
7 {
8         NSMutableArray *tempArray;
9         NSArray *resultArray;
10         Class *classes;
11         struct objc_class *superClass;
12         Class *current;
13         int count, newCount, index;
14         tempArray = [[NSMutableArray allocWithZone:nil] initWithCapacity:12];
15         resultArray = nil;
16         if (tempArray) {
17                 classes = NULL;
18                 count = objc_getClassList(NULL, 0);
19                 if (count) {
20                         classes = malloc(sizeof(Class) * count);
21                         if (classes) {
22                                 newCount = objc_getClassList(classes, count);
23                                 while (count < newCount) {
24                                         count = newCount;
25                                         free(classes);
26                                         classes = malloc(sizeof(Class) * count);
27                                         if (classes) {
28                                                 newCount = objc_getClassList(classes, count);
29                                         }
30                                 }
31                                 count = newCount;
32                         }
33                 }
34                 if (classes) {
35                         const Class thisClass = [self class];
36                         current = classes;
37                         for (index = 0; index < count; ++index) {
38                                 superClass = (*current)->super_class;
39                                 if (superClass) {
40                                         do {
41                                                 if (superClass == thisClass) {
42                                                         [tempArray addObject:*current];
43                                                         break;
44                                                 }
45                                                 superClass = superClass->super_class;
46                                         } while (superClass);
47                                 }
48                                 ++current;
49                         }
50                         free(classes);
51                 }
52                 resultArray = [NSArray arrayWithArray:tempArray];
53                 [tempArray release];
54         }
55         return resultArray;
56 }
57
58 + (NSArray *)directSubclasses
59 {
60         NSMutableArray *tempArray;
61         NSArray *resultArray;
62         Class *classes;
63         Class *current;
64         int count, newCount, index;
65         tempArray = [[NSMutableArray allocWithZone:nil] initWithCapacity:12];
66         resultArray = nil;
67         if (tempArray) {
68                 classes = NULL;
69                 count = objc_getClassList(NULL, 0);
70                 if (count) {
71                         classes = malloc(sizeof(Class) * count);
72                         if (classes) {
73                                 newCount = objc_getClassList(classes, count);
74                                 while (count < newCount) {
75                                         count = newCount;
76                                         free(classes);
77                                         classes = malloc(sizeof(Class) * count);
78                                         if (classes) {
79                                                 newCount = objc_getClassList(classes, count);
80                                         }
81                                 }
82                                 count = newCount;
83                         }
84                 }
85                 if (classes) {
86                         const Class thisClass = [self class];
87                         current = classes;
88
89                         for (index = 0; index < count; ++index) {
90                                 if ((*current)->super_class == thisClass) {
91                                         [tempArray addObject:*current];
92                                 }
93                                 ++current;
94                         }
95                         free(classes);
96                 }
97                 resultArray = [NSArray arrayWithArray:tempArray];
98                 [tempArray release];
99         }
100         return resultArray;
101 }
102
103 @end