2005-07-01 21:00:04 +00:00
|
|
|
/* Test/example program for the base library
|
|
|
|
|
|
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
|
|
|
|
2005-07-15 22:51:23 +00:00
|
|
|
Copying and distribution of this file, with or without modification,
|
|
|
|
are permitted in any medium without royalty provided the copyright
|
|
|
|
notice and this notice are preserved.
|
|
|
|
|
2005-07-01 21:00:04 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
*/
|
1996-03-03 02:18:46 +00:00
|
|
|
/* The simplest of tests for the NSNotification and NSNotificationCenter
|
2005-02-22 11:22:44 +00:00
|
|
|
classes. These tests should be expanded.
|
1996-03-03 02:18:46 +00:00
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
(The Tcp*Port classes, however, do test the notification mechanism
|
1996-03-03 02:18:46 +00:00
|
|
|
further.) */
|
|
|
|
|
2002-03-10 08:05:25 +00:00
|
|
|
#include <Foundation/Foundation.h>
|
1996-03-03 02:18:46 +00:00
|
|
|
|
|
|
|
@interface Observer : NSObject
|
|
|
|
- (void) gotNotificationFoo: not;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Observer
|
|
|
|
|
|
|
|
- (void) gotNotificationFoo: (NSNotification*)not
|
|
|
|
{
|
1999-02-16 16:29:07 +00:00
|
|
|
printf ("Got %s\n", [[not name] cString]);
|
1996-03-03 02:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) gotNotificationFooNoObject: (NSNotification*)not
|
|
|
|
{
|
1999-02-16 16:29:07 +00:00
|
|
|
printf ("Got %s without object\n", [[not name] cString]);
|
1996-03-03 02:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
id foo = @"NotificationTestFoo";
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
2002-04-18 06:02:22 +00:00
|
|
|
id o1;
|
|
|
|
id observer1;
|
1996-03-07 00:39:51 +00:00
|
|
|
id arp;
|
|
|
|
|
|
|
|
arp = [NSAutoreleasePool new];
|
2002-04-18 06:02:22 +00:00
|
|
|
NSLog(@"Make string object");
|
|
|
|
o1 = [NSString new];
|
|
|
|
NSLog(@"Make Observer object");
|
|
|
|
observer1 = [Observer new];
|
|
|
|
|
|
|
|
NSLog(@"Add observer to process centre");
|
1996-03-03 02:18:46 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver: observer1
|
|
|
|
selector: @selector(gotNotificationFoo:)
|
|
|
|
name: foo
|
|
|
|
object: o1];
|
|
|
|
|
2002-04-18 06:02:22 +00:00
|
|
|
NSLog(@"Add observer to distributed centre");
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: observer1
|
|
|
|
selector: @selector(gotNotificationFoo:)
|
|
|
|
name: foo
|
|
|
|
object: o1];
|
|
|
|
|
|
|
|
NSLog(@"Add observer to process centre");
|
1996-03-03 02:18:46 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver: observer1
|
|
|
|
selector: @selector(gotNotificationFooNoObject:)
|
|
|
|
name: foo
|
|
|
|
object: nil];
|
|
|
|
|
2002-04-18 06:02:22 +00:00
|
|
|
NSLog(@"Add observer to distributed centre");
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: observer1
|
|
|
|
selector: @selector(gotNotificationFooNoObject:)
|
|
|
|
name: foo
|
|
|
|
object: nil];
|
1996-03-07 00:39:51 +00:00
|
|
|
|
2002-04-18 06:02:22 +00:00
|
|
|
|
|
|
|
NSLog(@"Post to process centre");
|
1996-03-03 02:18:46 +00:00
|
|
|
/* This will cause two messages to be printed, one for each request above. */
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: o1];
|
|
|
|
|
2002-04-18 06:02:22 +00:00
|
|
|
NSLog(@"Post to distributed centre");
|
|
|
|
/* This will cause two messages to be printed, one for each request above. */
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: o1];
|
|
|
|
|
|
|
|
NSLog(@"Post to process centre");
|
1996-03-03 02:18:46 +00:00
|
|
|
/* This will cause one message to be printed. */
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: nil];
|
|
|
|
|
2002-04-18 06:02:22 +00:00
|
|
|
NSLog(@"Post to distributed centre");
|
|
|
|
/* This will cause one message to be printed. */
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: nil];
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2002-04-18 06:02:22 +00:00
|
|
|
NSLog(@"Remove observer from process centre");
|
1996-03-07 00:39:51 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
removeObserver: observer1
|
|
|
|
name: nil
|
|
|
|
object: o1];
|
|
|
|
|
|
|
|
/* This will cause message to be printed. */
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: o1];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
removeObserver: observer1];
|
|
|
|
|
|
|
|
/* This will cause no messages to be printed. */
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: o1];
|
|
|
|
|
2002-03-10 08:05:25 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: observer1
|
|
|
|
selector: @selector(gotNotificationFooNoObject:)
|
|
|
|
name: foo
|
|
|
|
object: nil];
|
|
|
|
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: @"hello"];
|
|
|
|
|
1996-03-07 00:39:51 +00:00
|
|
|
[arp release];
|
|
|
|
|
1996-03-03 02:18:46 +00:00
|
|
|
exit (0);
|
|
|
|
}
|