forked from tinycode/xmppframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MulticastDelegate.m
227 lines (179 loc) · 4.35 KB
/
MulticastDelegate.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
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
#import "MulticastDelegate.h"
@interface MulticastDelegateListNode (PrivateAPI)
- (void)clearDelegate;
@end
@implementation MulticastDelegate
- (id)init
{
if(self = [super init])
{
currentInvocationIndex = 0;
}
return self;
}
- (void)addDelegate:(id)delegate
{
MulticastDelegateListNode *node = [[MulticastDelegateListNode alloc] initWithDelegate:delegate];
if(delegateList != nil)
{
[node setNext:delegateList];
[[node next] setPrev:node];
}
delegateList = node;
}
- (void)removeDelegate:(id)delegate
{
MulticastDelegateListNode *node = delegateList;
NSUInteger index = 0;
while(node != nil)
{
if(delegate == [node delegate])
{
if([node prev] == nil)
delegateList = [node next];
else
[[node prev] setNext:[node next]];
[[node next] setPrev:[node prev]];
// We do NOT change the prev/next pointers of the node.
// If it's in use within forwardInvocation, these pointers are still needed.
// However, if multiple delegates are removed in the middle of a delegate callback,
// we still need to be sure not to invoke any delegates that were removed.
[node clearDelegate];
[node release];
if(index < currentInvocationIndex)
{
currentInvocationIndex--;
}
break;
}
index++;
node = [node next];
}
}
- (void)removeAllDelegates
{
MulticastDelegateListNode *node = delegateList;
while(node != nil)
{
MulticastDelegateListNode *next = [node next];
[node setPrev:nil];
[node setNext:nil];
[node release];
node = next;
}
currentInvocationIndex = 0;
delegateList = nil;
}
- (NSUInteger)count
{
NSUInteger count = 0;
MulticastDelegateListNode *node;
for(node = delegateList; node != nil; node = [node next])
{
count++;
}
return count;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
MulticastDelegateListNode *node;
for(node = delegateList; node != nil; node = [node next])
{
NSMethodSignature *result = [[node delegate] methodSignatureForSelector:aSelector];
if(result != nil)
{
return result;
}
}
// This causes a crash...
// return [super methodSignatureForSelector:aSelector];
// This also causes a crash...
// return nil;
return [[self class] instanceMethodSignatureForSelector:@selector(doNothing)];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Here are the rules:
// If a delegate is added during this method, it should NOT be invoked.
// If a delegate is removed during this method that has not already been invoked, it should not be invoked.
//
// The first rule is the reason for the currentInvocationIndex variable.
MulticastDelegateListNode *node = delegateList;
currentInvocationIndex = 0;
while([node next] != nil)
{
node = [node next];
currentInvocationIndex++;
}
while(node != nil)
{
// Retain the node before we invoke the delegate.
// We do this because the delegate might remove itself from the delegate list within the invoked method.
// And we don't want to get the previous node now, because it may also be
// removed from the list within the invoked method.
[[node retain] autorelease];
if([[node delegate] respondsToSelector:[anInvocation selector]])
{
[anInvocation invokeWithTarget:[node delegate]];
}
node = [node prev];
if(currentInvocationIndex > 0)
currentInvocationIndex--;
else
break;
}
[pool release];
}
- (void)doesNotRecognizeSelector:(SEL)aSelector
{
// Prevent NSInvalidArgumentException
}
- (void)doNothing {}
- (void)dealloc
{
[self removeAllDelegates];
[super dealloc];
}
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation MulticastDelegateListNode
- (id)initWithDelegate:(id)aDelegate
{
if(self = [super init])
{
delegate = aDelegate;
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (id)delegate
{
return delegate;
}
- (void)clearDelegate
{
delegate = nil;
}
- (MulticastDelegateListNode *)prev
{
return prev;
}
- (void)setPrev:(MulticastDelegateListNode *)newPrev
{
prev = newPrev;
}
- (MulticastDelegateListNode *)next
{
return next;
}
- (void)setNext:(MulticastDelegateListNode *)newNext
{
next = newNext;
}
@end