Re: How To Design A Queue of Messages?
Re: How To Design A Queue of Messages?
- Subject: Re: How To Design A Queue of Messages?
- From: Frode <email@hidden>
- Date: Sat, 10 Sep 2005 19:34:47 +0200
2005-09-10 kl. 16.01 skrev Andreas Mayer:
Am 10.09.2005 um 14:47 Uhr schrieb Jerry Krinock:
My tasks/messages have no arguments and no return values,
In that case, I'd just use strings to store the message names.
See NSStringFromSelector() and NSSelectorFromString().
I would use objc_sendMsg(). Maybe this is what you looking for? I'm
quite newbie to Cocoa, but I learnt using the objc_msgSend()-routine
from Apple Sample Code, so I guess it is kind of "official". And it
would be the quickest method. Note, I haven't tried compile this
myself...
@interface ReceiverSelectorPair: NSObject
{
Class _receiver;
SEL _sel;
}
- (id)initWithObjectAndSelector:(id)object selector:sel;
+ (ReceiverSelectorPair)pair:(id)object selector:sel;
- (void)sendMessage;
- (int)sendMessageWithArgumentAndResult:(id)argument;
@end
@implementation ReceiverSelectorPair
- (id)initWithObjectAndSelector:(id)object selector:sel {
self = [super init];
_receiver = [object class];
if ([object respondsToSelector:sel]) {
// it would be reasonable to check that object implement selector
HERE, // instead of in the loop sendMessages below...
[self dealloc];
return nil;
}
_sel = sel;
return self;
}
+ (ReceiverSelectorPair)pair:(id)object selector:sel {
id rs = [[ReceiverSelectorPair alloc] initWithObjectAndSelector:object
selector:sel];
if (rs) [rs autorelease];
return rs;
}
-(void)sendMessage {
((void (*)(id, SEL))objc_msgSend)(_receiver, _sel)
}
-(int)sendMessageWithArgumentAndResult:(id)argument {
return ((int (*)(id, SEL, id))objc_msgSend)(_receiver, _sel, object)
}
@end
@implementation NSMutableArray (ReceiverSelectorPairExtensions)
- (void)sendMessages {
NSEnumeration *objEnum = [self objectEnumerator];
ReceiverSelectorPair *rs;
while (nil != (rs = [objEnumerator nextObject])) {
[rs sendMessage];
//Alternative: ((void (*)(id, SEL))objc_msgSend)(rs->_receiver,
rs->_sel)
}
}
@end
Stick to Cocoa. However, I think I would use C++ std::queue and a
simple struct for the Class-SEL pair, but don't begin mix language if
you aren't already using STL and Cocoa! :-) Note that things are
encapsulated in ReceiverSelectorPair in this implementation, that's it,
if you need to change HOW the message should be sent, then it is
suffient to do it there. That's my tip! :-)
_______________________________________________
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