Re: How To Design A Queue of Messages? [warning: complete source]
Re: How To Design A Queue of Messages? [warning: complete source]
- Subject: Re: How To Design A Queue of Messages? [warning: complete source]
- From: Kins Collins <email@hidden>
- Date: Mon, 12 Sep 2005 16:38:56 -0700
Here is my contribution to the "Queue of Messages" contest. I aim at
simplicity and clarity. I don't mix the "queue" question with the
"send message" question. Also I found no need to use STL or
NSDictionary. In accordance with Krinock's initial request, my code
only accommodates methods having no arguments, though return values
are permitted.
I define two classes:
(1.) Queue (with push: & pop), and
(2.) MessageQueue (with addReceiver:andMessage: & sendNextMsg).
This code has been tested, and seems to work right.
Kins Collins
Silicon Valley, USA
// Queue.h
@interface Queue : NSObject
{
NSMutableArray *theQueue;
}
- (void) push:(id)item;
- (id) pop;
- (int) size;
- (void) print;
@end
// Queue.m
#import "Queue.h"
@implementation Queue
//----------------------------------------------------------------//
- (id)init
{
self = [super init];
if (self != nil)
theQueue = [[NSMutableArray alloc] init];
return self;
}
//----------------------------------------------------------------//
- (void)dealloc
{
[theQueue release];
[super dealloc];
}
//----------------------------------------------------------------//
- (void)push:(id)item
{
[theQueue addObject:item];
}
//----------------------------------------------------------------//
- (id)pop
{
id firstItem = [theQueue objectAtIndex:0];
[theQueue removeObjectAtIndex:0];
return firstItem;
}
//----------------------------------------------------------------//
- (int)size
{
return [theQueue count];
}
//----------------------------------------------------------------//
- (void)print
{
int i;
NSLog(@"\n");
for (i=0; i<[self size]; i++)
NSLog([theQueue objectAtIndex:i]);
NSLog(@"\n");
}
//----------------------------------------------------------------//
@end
// MessageQueue.h
#import "Queue.h"
@interface MessageQueue : NSObject
{
Queue *theQueue;
}
- (void) addReceiver:(id)target andMessage:(SEL)action;
- (id) sendNextMsg;
@end
// MessageQueue.m
#import "MessageQueue.h"
@implementation MessageQueue
//----------------------------------------------------------------//
- (id)init
{
self = [super init];
if (self != nil)
theQueue = [[Queue alloc] init];
return self;
}
//----------------------------------------------------------------//
- (void)dealloc
{
[theQueue release];
[super dealloc];
}
//----------------------------------------------------------------//
- (void)addReceiver:(id)target andMessage:(SEL)action
{
[theQueue push:target];
[theQueue push:NSStringFromSelector(action)];
}
//----------------------------------------------------------------//
- (id)sendNextMsg
{
if ([theQueue size] != 0)
{
id x = [theQueue pop];
SEL y = NSSelectorFromString([theQueue pop]);
return [x performSelector:y];
}
else
return nil;
}
//----------------------------------------------------------------//
@end
#import "Queue.h"
#import "MessageQueue.h"
//----------------------------------------------------------------//
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Queue class
NSLog(@"------ test of Queue class -------\n");
Queue *myQueue = [[Queue alloc] init];
[myQueue push:@"This is item #0."];
[myQueue push:@"This is item #1."];
[myQueue push:@"This is item #2."];
NSLog(@"Queue loaded with 3 items.");
[myQueue print];
[myQueue pop];
[myQueue pop];
NSLog(@"Two items removed.");
[myQueue print];
[myQueue push:@"This is item #3."];
NSLog(@"One item added.");
[myQueue print];
[myQueue release];
// MessageQueue class
NSLog(@"--- test of MessageQueue class ---\n");
MessageQueue *msgQueue = [[MessageQueue alloc] init];
NSNumber *num1 = [NSNumber numberWithFloat:29.55];
NSNumber *num2 = [NSNumber numberWithFloat:59.55];
[msgQueue addReceiver:num1 andMessage:@selector(intValue)];
[msgQueue addReceiver:num2 andMessage:@selector(stringValue)];
NSLog(@"%d\n", [msgQueue sendNextMsg]);
NSLog(@"%@\n", [msgQueue sendNextMsg]);
NSLog(@"%@\n", [msgQueue sendNextMsg]); // there IS no
3rd message
[msgQueue release];
[pool release];
return 0;
}
//----------------------------------------------------------------//
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden