1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
//---------------------------------------------------------------------------------------
// NSObject+Extensions.m created by erik on Sun 06-Sep-1998
// @(#)$Id: NSObject+Extensions.m,v 2.2 2003/01/25 22:33:49 erik Exp $
//
// Copyright (c) 1998-2000 by Erik Doernenburg. All rights reserved.
//
// Permission to use, copy, modify and distribute this software and its documentation
// is hereby granted, provided that both the copyright notice and this permission
// notice appear in all copies of the software, derivative works or modified versions,
// and any portions thereof, and that both notices appear in supporting documentation,
// and that credit is given to Erik Doernenburg in all documents and publicity
// pertaining to direct or indirect use of this code or its derivatives.
//
// THIS IS EXPERIMENTAL SOFTWARE AND IT IS KNOWN TO HAVE BUGS, SOME OF WHICH MAY HAVE
// SERIOUS CONSEQUENCES. THE COPYRIGHT HOLDER ALLOWS FREE USE OF THIS SOFTWARE IN ITS
// "AS IS" CONDITION. THE COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY
// DAMAGES WHATSOEVER RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE
// OR OF ANY DERIVATIVE WORK.
//---------------------------------------------------------------------------------------
#import <Foundation/Foundation.h>
#import "NSObject+Extensions.h"
#import "EDObjcRuntime.h"
//---------------------------------------------------------------------------------------
@implementation NSObject(EDExtensions)
//---------------------------------------------------------------------------------------
/*" Various common extensions to #NSObject. "*/
//---------------------------------------------------------------------------------------
// RUNTIME CONVENIENCES
//---------------------------------------------------------------------------------------
/*" Raises an #NSInternalInconsistencyException stating that the method must be overriden. "*/
- (volatile void)methodIsAbstract:(SEL)selector
{
[NSException raise:NSInternalInconsistencyException format:@"*** -[%@ %@]: Abstract definition must be overriden.", NSStringFromClass([self class]), NSStringFromSelector(selector)];
}
/*" Prints a warning that the method is obsolete. This warning is only printed once per method. "*/
- (void)methodIsObsolete:(SEL)selector
{
[self methodIsObsolete:selector hint:nil];
}
/*" Prints a warning that the method is obsolete including the %hint supplied. This warning is only printed once per method. "*/
- (void)methodIsObsolete:(SEL)selector hint:(NSString *)hint
{
static NSMutableSet *methodList = nil;
EDObjcMethodInfo method;
NSValue *methodKey;
if(methodList == nil)
methodList = [[NSMutableSet alloc] init];
if((method = EDObjcClassGetInstanceMethod(isa, selector)) == NULL)
method = EDObjcClassGetClassMethod(isa, selector);
methodKey = [NSValue valueWithBytes:&method objCType:@encode(Method)];
if([methodList containsObject:methodKey] == NO)
{
[methodList addObject:methodKey];
if(hint == nil)
NSLog(@"*** Warning: Compatibility method '%@' in class %@ has been invoked at least once.", NSStringFromSelector(selector), NSStringFromClass([self class]));
else
NSLog(@"*** Warning: Compatibility method '%@' in class %@ has been invoked at least once. %@", NSStringFromSelector(selector), NSStringFromClass([self class]), hint);
}
}
- (NSString *)className
{
return NSStringFromClass([self class]);
}
//---------------------------------------------------------------------------------------
// EXTENDED INTROSPECTION
//---------------------------------------------------------------------------------------
IMP EDGetFirstUnusedIMPForSelector(Class aClass, SEL aSelector, BOOL isClassMethod)
{
#ifndef GNU_RUNTIME
IMP activeIMP;
struct objc_method_list *mlist;
void *iterator;
int i;
if(isClassMethod)
aClass = aClass->isa;
iterator = 0;
activeIMP = [aClass instanceMethodForSelector:aSelector];
while((mlist = class_nextMethodList(aClass, &iterator)) != NULL)
{
for(i = 0; i < mlist->method_count; i++)
{
if((mlist->method_list[i].method_name == aSelector) && (mlist->method_list[i].method_imp != activeIMP))
return mlist->method_list[i].method_imp;
}
}
return NULL;
#else /* GNU_RUNTIME */
#warning ** implementation missing for GNU runtime
return NULL;
#endif
}
BOOL EDClassIsSuperclassOfClass(Class aClass, Class subClass)
{
Class class;
class = class_getSuperclass(subClass);
while(class != nil)
{
if(class == aClass)
return YES;
class = class_getSuperclass(class);
}
return NO;
}
NSArray *EDSubclassesOfClass(Class aClass)
{
#ifndef GNU_RUNTIME
#ifdef EDCOMMON_OSXBUILD
NSMutableArray *subclasses;
Class *classes;
int numClasses, newNumClasses, i;
// cf. /System/Library/Frameworks/System.framework/Headers/objc/objc-runtime.h
numClasses = 0, newNumClasses = objc_getClassList(NULL, 0);
classes = NULL;
while (numClasses < newNumClasses)
{
numClasses = newNumClasses;
classes = realloc(classes, sizeof(Class) * numClasses);
newNumClasses = objc_getClassList(classes, numClasses);
}
subclasses = [NSMutableArray array];
for(i = 0; i < numClasses; i++)
{
if(EDClassIsSuperclassOfClass(aClass, classes[i]) == YES)
[subclasses addObject:classes[i]];
}
free(classes);
return subclasses;
#else /* OSXS_BUILD */
NSMutableArray *subclasses;
NXHashTable *subClasses;
NXHashState subIterator;
Class subClass;
subClasses = objc_getClasses();
subIterator = NXInitHashState(subClasses);
subclasses = [NSMutableArray array];
while(NXNextHashState(subClasses, &subIterator, (void **)&subClass))
{
if(EDClassIsSuperclassOfClass(aClass, subClass) == YES)
[subclasses addObject:subClass];
}
return subclasses;
#endif
#else /* GNU_RUNTIME */
NSMutableArray *subclasses;
Class *classes;
int i, numClasses;
subclasses = [NSMutableArray array];
numClasses = objc_getClassList(NULL, 0);
classes = (Class *)NSZoneMalloc(NULL, numClasses * sizeof(Class));
numClasses = objc_getClassList(classes, numClasses);
for (i = 0; i < numClasses; i++)
[subclasses addObject:classes[i]];
NSZoneFree(NULL, classes);
return subclasses;
#endif
}
/*" Returns all subclasses of the receiving class "*/
+ (NSArray *)subclasses
{
return EDSubclassesOfClass(self);
}
//---------------------------------------------------------------------------------------
// MAPPING
//---------------------------------------------------------------------------------------
/*" Invokes the method described by %selector in the receiver once for each object in %anArray and collects the return values in another array that is returned. Note that the selector is assumed to take one argument, the current object from the array, and return the corresponding object.
Example: Assume you have an array !{a} which contains names and an object !{phoneBook} implementing a method !{lookupPhoneNumber:} which returns a phone number for a name. In this case you can use !{[phoneBook mapArray:a withSelector:@selector(lookupPhoneNumber:)]} to get the corresponding array of phone numbers."*/
- (NSArray *)mapArray:(NSArray *)anArray withSelector:(SEL)selector
{
NSMutableArray *mappedArray;
unsigned int i, n = [anArray count];
mappedArray = [[[NSMutableArray allocWithZone:[self zone]] initWithCapacity:n] autorelease];
for(i = 0; i < n; i++)
[mappedArray addObject:EDObjcMsgSend1(self, selector, [anArray objectAtIndex:i])];
return mappedArray;
}
//---------------------------------------------------------------------------------------
// REPEATED PERFORM
//---------------------------------------------------------------------------------------
/*" Invokes the method described by %selector in the receiver once for each object in %array, passing the respective object as an argument. "*/
- (void)performSelector:(SEL)selector withObjects:(NSArray *)array
{
unsigned int i, n = [array count];
for(i = 0; i < n; i++)
EDObjcMsgSend1(self, selector, [array objectAtIndex:i]);
}
/*" Invokes the method described by %selector in the receiver once for each object enumerated by %enumerator, passing the respective object as an argument. "*/
- (void)performSelector:(SEL)selector withObjectsEnumeratedBy:(NSEnumerator *)enumerator
{
id object;
while((object = [enumerator nextObject]) != nil)
EDObjcMsgSend1(self, selector, object);
}
//---------------------------------------------------------------------------------------
@end
//---------------------------------------------------------------------------------------
|