forked from S2Ler/MAObjCRuntime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTProtocol.m
122 lines (95 loc) · 2.77 KB
/
RTProtocol.m
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
#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]] &&
protocol_isEqual([self objCProtocol], [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