-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMDMulticastDelegate.m
executable file
·406 lines (336 loc) · 13.1 KB
/
MDMulticastDelegate.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#import "MDMulticastDelegate.h"
#import <libkern/OSAtomic.h>
#if __has_feature(objc_arc_weak) && !TARGET_OS_IPHONE
#import <AppKit/AppKit.h>
#endif
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
/**
* How does this class work?
*
* In theory, this class is very straight-forward.
* It provides a way for multiple delegates to be called, each on its own delegate queue.
*
* In other words, any delegate method call to this class
* will get forwarded (dispatch_async'd) to each added delegate.
*
* Important note concerning thread-safety:
*
* This class is designed to be used from within a single dispatch queue.
* In other words, it is NOT thread-safe, and should only be used from within the external dedicated dispatch_queue.
**/
@interface MDMulticastDelegate () {
NSRecursiveLock *_lock;
NSMapTable<id, NSOrderedSet<dispatch_queue_t> *> *_delegates;
}
@end
@implementation MDMulticastDelegate
- (instancetype)init {
if (self = [super init]) {
_lock = [[NSRecursiveLock alloc] init];
_delegates = [NSMapTable<id, NSOrderedSet<dispatch_queue_t> *> weakToStrongObjectsMapTable];
}
return self;
}
#pragma mark - private
- (void)_addDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue {
NSOrderedSet<dispatch_queue_t> *queues = [_delegates objectForKey:delegate];
NSMutableOrderedSet<dispatch_queue_t> *mutableQueues = queues ? [queues mutableCopy] : [NSMutableOrderedSet orderedSet];
[mutableQueues addObject:delegateQueue];
[_delegates setObject:mutableQueues.copy forKey:delegate];
}
- (void)_removeDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue {
if (delegateQueue) {
NSOrderedSet<dispatch_queue_t> *queues = [_delegates objectForKey:delegate];
if (![queues containsObject:delegateQueue]) return;
NSMutableOrderedSet<dispatch_queue_t> *mutableQueues = queues ? [queues mutableCopy] : [NSMutableOrderedSet<dispatch_queue_t> orderedSet];
[mutableQueues removeObject:delegateQueue];
if (mutableQueues.count) [_delegates setObject:mutableQueues.copy forKey:delegate];
else [_delegates removeObjectForKey:delegate];
} else {
[_delegates removeObjectForKey:delegate];
}
}
- (NSUInteger)_count {
NSMapTable<id, NSOrderedSet<dispatch_queue_t> *> *delegates = [_delegates copy];
NSEnumerator<NSOrderedSet<dispatch_queue_t> *> *enumerator = [delegates objectEnumerator];
NSUInteger count = 0;
NSOrderedSet<dispatch_queue_t> *queues = nil;
while ((queues = enumerator.nextObject)) {
count += queues.count;
}
return count;
}
- (NSUInteger)_countOfDelegateBlock:(BOOL (^)(id delegate))block {
if (!block) return 0;
__block NSUInteger count = 0;
[self _enumerateDelegatesUsingBlock:^(id delegate, NSOrderedSet<dispatch_queue_t> *queues, BOOL *stopPtr) {
if (block(delegate)) count += queues.count;
}];
return count;
}
- (BOOL)_hasDelegateThatRespondsToSelector:(SEL)aSelector {
__block BOOL contained = NO;
[self _enumerateDelegatesByRespondingSelector:aSelector block:^(id delegate, NSOrderedSet<dispatch_queue_t> *queues, BOOL *stopPtr) {
contained = YES;
*stopPtr = YES;
}];
return contained;
}
- (void)_enumerateDelegatesUsingBlock:(void (^)(id delegate, NSOrderedSet<dispatch_queue_t> *queues, BOOL *stop))block {
[self _enumerateDelegatesByRespondingSelector:nil block:block];
}
- (void)_enumerateDelegatesByRespondingSelector:(SEL)selector block:(void (^)(id delegate, NSOrderedSet<dispatch_queue_t> *queues, BOOL *stop))block {
if (!_delegates.count) return;
NSMapTable<id, NSOrderedSet<dispatch_queue_t> *> *delegates = [_delegates copy];
NSMapEnumerator enumerator = NSEnumerateMapTable(delegates);
void *delegatePtr = nil;
void *queuesPtr = nil;
BOOL stop = NO;
while (NSNextMapEnumeratorPair(&enumerator, &delegatePtr, &queuesPtr)) {
id delegate = (__bridge id)delegatePtr;
if (selector && ![delegate respondsToSelector:selector]) continue;
NSOrderedSet<dispatch_queue_t> *queues = (__bridge NSOrderedSet<dispatch_queue_t> *)queuesPtr;
block(delegate, queues, &stop);
if (stop) break;
}
NSEndMapTableEnumeration(&enumerator);
}
- (void)_enumerateDelegatesAndQueuesUsingBlock:(void (^)(id delegate, dispatch_queue_t delegateQueue, BOOL *stop))block {
[self _enumerateDelegatesAndQueuesByRespondingSelector:nil block:block];
}
- (void)_enumerateDelegatesAndQueuesByRespondingSelector:(SEL)selector block:(void (^)(id delegate, dispatch_queue_t delegateQueue, BOOL *stop))block {
__block BOOL stop = NO;
[self _enumerateDelegatesByRespondingSelector:selector block:^(id delegate, NSOrderedSet<dispatch_queue_t> *queues, BOOL *stopPtr) {
for (dispatch_queue_t queue in queues) {
block(delegate, queue, &stop);
*stopPtr = stop;
if (stop) break;
}
}];
}
- (void)_invokeWithDelegate:(id)delegate queue:(dispatch_queue_t)queue invocation:(NSInvocation *)invocation {
// All delegates MUST be invoked ASYNCHRONOUSLY.
NSInvocation *dupInvocation = [self _duplicateInvocation:invocation];
dispatch_async(queue, ^{ @autoreleasepool {
[dupInvocation invokeWithTarget:delegate];
}});
}
- (void)_throwExceptionAtIndex:(NSUInteger)index type:(const char *)type selector:(SEL)selector {
NSString *selectorStr = NSStringFromSelector(selector);
NSString *format = @"Argument %lu to method %@ - Type(%c) not supported";
NSString *reason = [NSString stringWithFormat:format, (unsigned long)(index - 2), selectorStr, *type];
[[NSException exceptionWithName:NSInvalidArgumentException reason:reason userInfo:nil] raise];
}
- (void)_copyStructValueAtIndex:(NSUInteger)index type:(const char *)type fromInvocation:(NSInvocation *)fromInvocation toInvocation:(NSInvocation *)toInvocation {
NSUInteger size = 0;
NSUInteger align = 0;
NSGetSizeAndAlignment(type, &size, &align);
void *buffer = malloc(size);
[fromInvocation getArgument:buffer atIndex:index];
[toInvocation setArgument:buffer atIndex:index];
free(buffer);
}
- (void)_doNothing {}
- (NSInvocation *)_duplicateInvocation:(NSInvocation *)origInvocation {
NSMethodSignature *methodSignature = [origInvocation methodSignature];
NSInvocation *dupInvocation = [NSInvocation invocationWithMethodSignature:methodSignature];
dupInvocation.selector = [origInvocation selector];
NSUInteger i, count = [methodSignature numberOfArguments];
for (i = 2; i < count; i++) {
const char *type = [methodSignature getArgumentTypeAtIndex:i];
switch (*type) {
// void
case 'v': break;
// char
case 'c':
// unsigned char
case 'C': {
char value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// short
case 's':
// unsigned short
case 'S': {
short value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// int
case 'i':
// unsigned int
case 'I': {
int value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// long
case 'l':
// long
case 'L': {
long value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// long long
case 'q':
// unsigned long long
case 'Q': {
long long value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// float
case 'f': {
float value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// double
case 'd': {
double value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// long double
case 'D': {
long double value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// bool
case 'B': {
BOOL value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// selector
case ':': {
SEL value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// c string char *
case '*':
// OC object
case '@':
// pointer
case '^': {
void *value;
[origInvocation getArgument:&value atIndex:i];
[dupInvocation setArgument:&value atIndex:i];
} break;
// struct
case '{': [self _copyStructValueAtIndex:i type:type fromInvocation:origInvocation toInvocation:dupInvocation]; break;
// c array
case '[':
// c union
case '(':
// bitfield
case 'b':
// no type
case 0:
default: [self _throwExceptionAtIndex:i type:type selector:[origInvocation selector]]; break;
}
}
[dupInvocation retainArguments];
return dupInvocation;
}
#pragma mark - public
- (void)addDelegate:(id)delegate {
[self addDelegate:delegate delegateQueue:dispatch_get_main_queue()];
}
- (void)addDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue {
if (delegate == nil) return;
if (delegateQueue == nil) return;
[_lock lock];
[self _addDelegate:delegate delegateQueue:delegateQueue];
[_lock unlock];
}
- (void)removeDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue {
if (delegate == nil) return;
[_lock lock];
[self _removeDelegate:delegate delegateQueue:delegateQueue];
[_lock unlock];
}
- (void)removeDelegate:(id)delegate {
[self removeDelegate:delegate delegateQueue:NULL];
}
- (void)removeAllDelegates {
[_lock lock];
[_delegates removeAllObjects];
[_lock unlock];
}
- (NSUInteger)count {
[_lock lock];
NSUInteger count = [self _count];
[_lock unlock];
return count;
}
- (NSUInteger)countOfDelegates {
[_lock lock];
NSUInteger count = [_delegates count];
[_lock unlock];
return count;
}
- (NSUInteger)countOfClass:(Class)aClass {
[_lock lock];
NSUInteger count = [self _countOfDelegateBlock:^BOOL(id delegate) {
return [delegate isKindOfClass:aClass];
}];
[_lock unlock];
return count;
}
- (NSUInteger)countForSelector:(SEL)aSelector {
[_lock lock];
NSUInteger count = [self _countOfDelegateBlock:^BOOL(id delegate) {
return [delegate respondsToSelector:aSelector];
}];
[_lock unlock];
return count;
}
- (BOOL)hasDelegateThatRespondsToSelector:(SEL)aSelector {
[_lock lock];
BOOL responds = [self _hasDelegateThatRespondsToSelector:aSelector];
[_lock unlock];
return responds;
}
- (void)enumerateDelegatesAndQueuesUsingBlock:(void (^)(id delegate, dispatch_queue_t delegateQueue, BOOL *stop))block {
[_lock lock];
[self _enumerateDelegatesAndQueuesUsingBlock:block];
[_lock unlock];
}
#pragma mark - protected
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
__block NSMethodSignature *result = nil;
[_lock lock];
[self _enumerateDelegatesUsingBlock:^(id delegate, NSOrderedSet<dispatch_queue_t> *queues, BOOL *stop) {
result = [delegate methodSignatureForSelector:aSelector];
if (result) *stop = YES;
}];
[_lock unlock];
if (result) 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 *)invocation {
[_lock lock];
SEL selector = [invocation selector];
[self _enumerateDelegatesAndQueuesByRespondingSelector:selector block:^(id delegate, dispatch_queue_t queue, BOOL *stop) {
[self _invokeWithDelegate:delegate queue:queue invocation:invocation];
}];
[_lock unlock];
}
- (void)doesNotRecognizeSelector:(SEL)aSelector {
// Prevent NSInvalidArgumentException
}
- (void)dealloc {
[self removeAllDelegates];
}
@end