Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / Foundation / NSNotificationCenter / NSNotificationCenter.m
blob030ed01c82616bcdfa93f6f4cd4cec045a43ae5c
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 {
27         @synchronized(self) {
28                 if (defaultCenter == nil) {
29                         defaultCenter = [[[self class] alloc] init];
30                 }
31         }
32    return defaultCenter;
35 -init {
36    _nameToRegistry=[[NSMutableDictionary allocWithZone:[self zone]] init];
37    _noNameRegistry=[[NSObjectToObservers allocWithZone:[self zone]] init];
38    return self;
41 -(void)dealloc {
42    [_nameToRegistry release];
43    [_noNameRegistry release];
44    [super dealloc];
47 -(void)addObserver:anObserver selector:(SEL)selector name:(NSString *)name
48    object:object {
49         @synchronized(self) {
50    NSNotificationObserver *observer=[[[NSNotificationObserver allocWithZone:[self zone]] 
51         initWithObserver:anObserver selector:selector] autorelease];
52    NSObjectToObservers *registry;
54    if(name==nil)
55     registry=_noNameRegistry;
56    else{
57     registry=[_nameToRegistry objectForKey:name];
59     if(registry==nil){
60      registry=[[[NSObjectToObservers allocWithZone:[self zone]] init]
61        autorelease];
62      [_nameToRegistry setObject:registry forKey:name];
63     }
64    }
66    [registry addObserver:observer object:object];
67         }
70 -(void)removeObserver:anObserver {
71         @synchronized(self) {
72    NSMutableArray          *removeRegistries=[NSMutableArray array];
73    NSEnumerator            *keyEnumerator=[_nameToRegistry keyEnumerator];
74    NSString                *key;
75    NSObjectToObservers *registry;
76    NSInteger                count;
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];
83    }
85    [_noNameRegistry removeObserver:anObserver object:nil];
87    count=[removeRegistries count];
88    while(--count>=0)
89     [_nameToRegistry removeObjectForKey:[removeRegistries objectAtIndex:count]];
90         }
93 -(void)removeObserver:anObserver name:(NSString *)name object:object {
94         @synchronized(self) {
95    NSMutableArray          *removeRegistries=[NSMutableArray array];
96    NSObjectToObservers *registry;
97    NSInteger               count;
99    if(name!=nil){
100     registry=[_nameToRegistry objectForKey:name];
102     [registry removeObserver:anObserver object:object];
104     if([registry count]==0)
105      [removeRegistries addObject:name];
106    }
107    else {
108     NSEnumerator *keyEnumerator=[_nameToRegistry keyEnumerator];
109     NSString     *key;
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];
118     }
120     [_noNameRegistry removeObserver:anObserver object:object];
121    }
123    count=[removeRegistries count];
124    while(--count>=0){
125     NSString            *key=[removeRegistries objectAtIndex:count];
126     NSObjectToObservers *oto=[_nameToRegistry objectForKey:key];
128     [oto invalidate];
130     [_nameToRegistry removeObjectForKey:key];
131    }
132         }
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];
144    [registry release];
145         }
147    [pool release];
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);
157         [note release];
160 -(void)postNotificationName:(NSString *)name object:object {
161         NSNotification *note = NSNotification_concreteNew(NULL,name,object,nil);
162         postNotification(self,note);
163         [note release];
166 @end