forked from jerrykrinock/ClassesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAKVONotificationCenter.m
executable file
·262 lines (219 loc) · 7.23 KB
/
MAKVONotificationCenter.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
//
// MAKVONotificationCenter.m
// MAKVONotificationCenter
//
// Created by Michael Ash on 10/15/08.
//
#import "MAKVONotificationCenter.h"
#import <libkern/OSAtomic.h>
#import <objc/message.h>
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
// For some reason, this was not needed when I was using the 10.5 SDK
#import <objc/objc-auto.h>
#endif
@interface MAKVObservation : NSObject
{
id _observer;
SEL _selector;
id _userInfo;
id _target;
NSString* _keyPath;
}
- (id)initWithObserver:(id)observer object:(id)target keyPath:(NSString *)keyPath selector:(SEL)selector userInfo: (id)userInfo options: (NSKeyValueObservingOptions)options;
- (void)deregister;
@end
@implementation MAKVObservation
static char MAKVONotificationMagicContext;
- (id)initWithObserver:(id)observer object:(id)target keyPath:(NSString *)keyPath selector:(SEL)selector userInfo: (id)userInfo options: (NSKeyValueObservingOptions)options
{
if((self = [self init]))
{
_observer = observer;
_selector = selector;
_userInfo = [userInfo retain];
_target = target;
_keyPath = [keyPath retain];
[target addObserver:self
forKeyPath:keyPath
options:options
context:&MAKVONotificationMagicContext];
}
return self;
}
- (id)observer {
return _observer;
}
- (id)target {
return _target;
}
- (NSString*)keyPath {
return _keyPath;
}
- (SEL)selector {
return _selector;
}
- (void)dealloc
{
[_userInfo release];
[_keyPath release];
[super dealloc];
}
- (NSString*)description {
return [NSString stringWithFormat:
@"<%@ %p ivars:\n keyPath: %@\n observer: %@ %p\n selector: %@\n target: %@ %p\n userInfo: %@",
[self class],
self,
_keyPath,
[_observer class],
_observer,
NSStringFromSelector(_selector),
[_target class],
_target,
_userInfo] ;
}
#pragma mark -
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(context == &MAKVONotificationMagicContext)
{
// we only ever sign up for one notification per object, so if we got here
// then we *know* that the key path and object are what we want
((void (*)(id, SEL, NSString *, id, NSDictionary *, id))objc_msgSend)(_observer, _selector, keyPath, object, change, _userInfo);
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)deregister
{
[_target removeObserver:self forKeyPath:_keyPath];
}
@end
@implementation MAKVONotificationCenter
+ (id)defaultCenter
{
static MAKVONotificationCenter *center = nil;
if(!center)
{
// Do a bit of clever atomic setting to make this thread safe.
// If two threads try to set simultaneously, one will fail
// and the other will set things up so that the failing thread
// gets the shared center...
MAKVONotificationCenter *newCenter = [[self alloc] init];
// objc_atomicCompareAndSwapGlobalBarrier() is not documented. We
// assume it behaves as OSAtomicCompareAndSwapPtrBarrier(), with the
// same signature. The first argument, 'predicate', is interpreted
// to the the "pre-existing" value or '__oldValue' of
// OSAtomicCompareAndSwapPtrBarrier().
// In our usage, either method supposedly compares center to nil,
// if center is nil, sets center to newCenter and returns true
// Note that this whole code block is if(!center).
// This is thus the normal behavior when no thread race occurs
// if center is not nil, does nothing and returns false
// This will happen if another thread snuck in and set it.
// Mike's original line, does not work with garbage collection.
// if(!OSAtomicCompareAndSwapPtrBarrier(nil, newCenter, (void *)¢er)) {
// See Brian Webster's first comment in http://www.mikeash.com/?page=pyblog/key-value-observing-done-right.html
// Patched line:
if(!objc_atomicCompareAndSwapGlobalBarrier(nil, newCenter, ¢er)) {
// Thread race has occurred and we lost
// center has previously been set to newCenter by another thread
[newCenter release];
}
}
return center;
}
- (id)init
{
if((self = [super init]))
{
observations = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc
{
[observations release];
[super dealloc];
}
#pragma mark -
- (id)_dictionaryKeyForObserver:(id)observer object:(id)target keyPath:(NSString *)keyPath selector:(SEL)selector
{
return [NSString stringWithFormat:@"%p:%p:%@:%p", observer, target, keyPath, selector];
}
- (id)_dictionaryKeyForObservation:(MAKVObservation*)observation
{
return [self _dictionaryKeyForObserver:[observation observer] object:[observation target] keyPath:[observation keyPath] selector:[observation selector]];
}
- (id)addObserver:(id)observer object:(id)target keyPath:(NSString *)keyPath selector:(SEL)selector userInfo: (id)userInfo options: (NSKeyValueObservingOptions)options
{
MAKVObservation *observation = nil;
id key = [self _dictionaryKeyForObserver:observer object:target keyPath:keyPath selector:selector];
@synchronized(self)
{
id oldObservation = [observations objectForKey:key];
if (!oldObservation) {
observation = [[MAKVObservation alloc] initWithObserver:observer object:target keyPath:keyPath selector:selector userInfo:userInfo options:options];
[observations setObject:observation forKey:key];
}
}
return [observation autorelease];
}
- (void)removeObservation:(id)observation
{
@synchronized(self)
{
[observation retain];
[observations removeObjectForKey:[self _dictionaryKeyForObservation:observation]];
}
[(MAKVObservation*)observation deregister];
[observation release];
}
- (void)removeObserver:(id)observer {
{
NSString* prefix = [NSString stringWithFormat:@"%p:", observer] ;
NSMutableArray* keysToRemove = [[NSMutableArray alloc] init] ;
@synchronized(self)
{
// I guess we're still supporting Mac OS X 10.4?...
// Geez, I forgot how to do this...
NSEnumerator* e = [observations keyEnumerator];
NSString* key ;
while ((key = [e nextObject])) {
if ([key hasPrefix:prefix]) {
[keysToRemove addObject:key];
}
}
e = [keysToRemove objectEnumerator] ;
while ((key = [e nextObject])) {
MAKVObservation* observation = [observations objectForKey:key];
[observation retain];
[observations removeObjectForKey:[self _dictionaryKeyForObservation:observation]];
[observation deregister];
[observation release];
}
}
[keysToRemove release] ;
}
}
- (void)removeObserver:(id)observer object:(id)target keyPath:(NSString *)keyPath selector:(SEL)selector
{
id key = [self _dictionaryKeyForObserver:observer object:target keyPath:keyPath selector:selector];
@synchronized(self)
{
MAKVObservation *observation = [observations objectForKey:key];
[self removeObservation:observation];
}
}
@end
@implementation NSObject (MAKVONotification)
- (id)addObserver:(id)observer forKeyPath:(NSString *)keyPath selector:(SEL)selector userInfo:(id)userInfo options:(NSKeyValueObservingOptions)options
{
return [[MAKVONotificationCenter defaultCenter] addObserver:observer object:self keyPath:keyPath selector:selector userInfo:userInfo options:options];
}
- (void)removeObserver:(id)observer keyPath:(NSString *)keyPath selector:(SEL)selector
{
[[MAKVONotificationCenter defaultCenter] removeObserver:observer object:self keyPath:keyPath selector:selector];
}
@end