-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
all: tests | ||
|
||
tests: | ||
gcc -framework Foundation --std=c99 main.m MARTNSObject.m RTMethod.m RTIvar.m RTProperty.m RTUnregisteredClass.m | ||
gcc -framework Foundation --std=c99 main.m MARTNSObject.m RTProtocol.m RTMethod.m RTIvar.m RTProperty.m RTUnregisteredClass.m | ||
|
||
clean: | ||
rm -f a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <objc/runtime.h> | ||
|
||
|
||
@interface RTProtocol : NSObject | ||
{ | ||
} | ||
|
||
+ (NSArray *)allProtocols; | ||
|
||
+ (id)protocolWithObjCProtocol: (Protocol *)protocol; | ||
+ (id)protocolWithName: (NSString *)name; | ||
|
||
- (id)initWithObjCProtocol: (Protocol *)protocol; | ||
- (id)initWithName: (NSString *)name; | ||
|
||
- (Protocol *)objCProtocol; | ||
- (NSString *)name; | ||
- (NSArray *)incorporatedProtocols; | ||
- (NSArray *)methodsRequired: (BOOL)isRequiredMethod instance: (BOOL)isInstanceMethod; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
|
||
#import "RTProtocol.h" | ||
#import "RTMethod.h" | ||
|
||
|
||
@interface _RTObjCProtocol : RTProtocol | ||
{ | ||
Protocol *_protocol; | ||
} | ||
@end | ||
|
||
@implementation _RTObjCProtocol | ||
|
||
- (id)initWithObjCProtocol: (Protocol *)protocol | ||
{ | ||
if((self = [self init])) | ||
{ | ||
_protocol = protocol; | ||
} | ||
return self; | ||
} | ||
|
||
- (Protocol *)objCProtocol | ||
{ | ||
return _protocol; | ||
} | ||
|
||
@end | ||
|
||
@implementation RTProtocol | ||
|
||
+ (NSArray *)allProtocols | ||
{ | ||
unsigned int count; | ||
Protocol **protocols = objc_copyProtocolList(&count); | ||
|
||
NSMutableArray *array = [NSMutableArray array]; | ||
for(unsigned i = 0; i < count; i++) | ||
[array addObject: [[[self alloc] initWithObjCProtocol: protocols[i]] autorelease]]; | ||
|
||
free(protocols); | ||
return array; | ||
} | ||
|
||
+ (id)protocolWithObjCProtocol: (Protocol *)protocol | ||
{ | ||
return [[[self alloc] initWithObjCProtocol: protocol] autorelease]; | ||
} | ||
|
||
+ (id)protocolWithName: (NSString *)name | ||
{ | ||
return [[[self alloc] initWithName: name] autorelease]; | ||
} | ||
|
||
- (id)initWithObjCProtocol: (Protocol *)protocol | ||
{ | ||
[self release]; | ||
return [[_RTObjCProtocol alloc] initWithObjCProtocol: protocol]; | ||
} | ||
|
||
- (id)initWithName: (NSString *)name | ||
{ | ||
return [self initWithObjCProtocol:objc_getProtocol([name cStringUsingEncoding:[NSString defaultCStringEncoding]])]; | ||
} | ||
|
||
- (NSString *)description | ||
{ | ||
return [NSString stringWithFormat: @"<%@ %p: %@>", [self class], self, [self name]]; | ||
} | ||
|
||
- (BOOL)isEqual: (id)other | ||
{ | ||
return [other isKindOfClass: [RTProtocol class]] && | ||
[[self objCProtocol] isEqual: [other objCProtocol]]; | ||
} | ||
|
||
- (NSUInteger)hash | ||
{ | ||
return [[self objCProtocol] hash]; | ||
} | ||
|
||
- (Protocol *)objCProtocol | ||
{ | ||
[self doesNotRecognizeSelector: _cmd]; | ||
return nil; | ||
} | ||
|
||
- (NSString *)name | ||
{ | ||
return [NSString stringWithUTF8String: protocol_getName([self objCProtocol])]; | ||
} | ||
|
||
- (NSArray *)incorporatedProtocols | ||
{ | ||
unsigned int count; | ||
Protocol **protocols = protocol_copyProtocolList([self objCProtocol], &count); | ||
|
||
NSMutableArray *array = [NSMutableArray array]; | ||
for(unsigned i = 0; i < count; i++) | ||
[array addObject: [RTProtocol protocolWithObjCProtocol: protocols[i]]]; | ||
|
||
free(protocols); | ||
return array; | ||
} | ||
|
||
- (NSArray *)methodsRequired: (BOOL)isRequiredMethod instance: (BOOL)isInstanceMethod | ||
{ | ||
unsigned int count; | ||
struct objc_method_description *methods = protocol_copyMethodDescriptionList([self objCProtocol], isRequiredMethod, isInstanceMethod, &count); | ||
|
||
NSMutableArray *array = [NSMutableArray array]; | ||
for(unsigned i = 0; i < count; i++) | ||
{ | ||
NSString *signature = [NSString stringWithCString: methods[i].types encoding: [NSString defaultCStringEncoding]]; | ||
[array addObject: [RTMethod methodWithSelector: methods[i].name implementation: NULL signature: signature]]; | ||
} | ||
|
||
free(methods); | ||
return array; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters