1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 // Original - Christopher Lloyd <cjwl@objc.net>
10 #import <Foundation/NSNotification.h>
11 #import <Foundation/NSArray.h>
12 #import <Foundation/NSDictionary.h>
13 #import <Foundation/NSEnumerator.h>
14 #import <Foundation/NSThread.h>
15 #import <Foundation/NSString.h>
17 #import <Foundation/NSObjectToObservers.h>
18 #import <Foundation/NSThread-Private.h>
19 #import <Foundation/NSNotification_concrete.h>
20 #import <Foundation/NSAutoreleasePool-private.h>
22 @implementation NSNotificationCenter
24 static NSNotificationCenter *defaultCenter = nil;
26 +(NSNotificationCenter *)defaultCenter {
28 if (defaultCenter == nil) {
29 defaultCenter = [[[self class] alloc] init];
36 _nameToRegistry=[[NSMutableDictionary allocWithZone:[self zone]] init];
37 _noNameRegistry=[[NSObjectToObservers allocWithZone:[self zone]] init];
42 [_nameToRegistry release];
43 [_noNameRegistry release];
47 -(void)addObserver:anObserver selector:(SEL)selector name:(NSString *)name
50 NSNotificationObserver *observer=[[[NSNotificationObserver allocWithZone:[self zone]]
51 initWithObserver:anObserver selector:selector] autorelease];
52 NSObjectToObservers *registry;
55 registry=_noNameRegistry;
57 registry=[_nameToRegistry objectForKey:name];
60 registry=[[[NSObjectToObservers allocWithZone:[self zone]] init]
62 [_nameToRegistry setObject:registry forKey:name];
66 [registry addObserver:observer object:object];
70 -(void)removeObserver:anObserver {
72 NSMutableArray *removeRegistries=[NSMutableArray array];
73 NSEnumerator *keyEnumerator=[_nameToRegistry keyEnumerator];
75 NSObjectToObservers *registry;
78 while((key=[keyEnumerator nextObject])!=nil){
79 registry=[_nameToRegistry objectForKey:key];
80 [registry removeObserver:anObserver object:nil];
81 if([registry count]==0)
82 [removeRegistries addObject:key];
85 [_noNameRegistry removeObserver:anObserver object:nil];
87 count=[removeRegistries count];
89 [_nameToRegistry removeObjectForKey:[removeRegistries objectAtIndex:count]];
93 -(void)removeObserver:anObserver name:(NSString *)name object:object {
95 NSMutableArray *removeRegistries=[NSMutableArray array];
96 NSObjectToObservers *registry;
100 registry=[_nameToRegistry objectForKey:name];
102 [registry removeObserver:anObserver object:object];
104 if([registry count]==0)
105 [removeRegistries addObject:name];
108 NSEnumerator *keyEnumerator=[_nameToRegistry keyEnumerator];
111 while((key=[keyEnumerator nextObject])!=nil){
112 registry=[_nameToRegistry objectForKey:key];
114 [registry removeObserver:anObserver object:object];
116 if([registry count]==0)
117 [removeRegistries addObject:key];
120 [_noNameRegistry removeObserver:anObserver object:object];
123 count=[removeRegistries count];
125 NSString *key=[removeRegistries objectAtIndex:count];
126 NSObjectToObservers *oto=[_nameToRegistry objectForKey:key];
130 [_nameToRegistry removeObjectForKey:key];
135 static inline void postNotification(NSNotificationCenter *self,NSNotification *note){
136 NSAutoreleasePool *pool=[NSAutoreleasePool new];
138 @synchronized(self) {
139 NSObjectToObservers *registry=self->_noNameRegistry;
141 [registry postNotification:note];
142 registry=[self->_nameToRegistry objectForKey:[note name]];
143 [[registry retain] postNotification:note];
150 -(void)postNotification:(NSNotification *)note {
151 postNotification(self,note);
154 -(void)postNotificationName:(NSString *)name object:object userInfo:(NSDictionary *)userInfo {
155 NSNotification *note = NSNotification_concreteNew(NULL,name,object,userInfo);
156 postNotification(self,note);
160 -(void)postNotificationName:(NSString *)name object:object {
161 NSNotification *note = NSNotification_concreteNew(NULL,name,object,nil);
162 postNotification(self,note);