Re: Encapsulating methods in ObjC
Re: Encapsulating methods in ObjC
- Subject: Re: Encapsulating methods in ObjC
- From: Marcel Weiher <email@hidden>
- Date: Wed, 21 May 2003 16:27:11 +0200
On Wednesday, May 21, 2003, at 04:07 Uhr, Lindsey Spratt wrote:
On Wednesday, May 21, 2003, at 08:25 AM,
email@hidden wrote:
Furthermore, if this sort of thing were to be needed with any
frequency, I might very well wrap it up in a higher order message, so
it looks like this:
[[self inProtectedGstate] drawMyObject];
This is something I've wondered about the best way to do in ObjC. In
Lisp or Prolog, I'd just use a metacall:
And in Smalltalk, you'd just use a block. Higher Order Messaging
provides a similar mechanism for Objective-C, based on messages.
But it seemed to me that I'd need to use selectors and/or NSInvocation
to get a similar facility in ObjC.
Yup.
What kind of object does '[self inProtectedGstate] ' return? Could
you give more code for your example?
It would return a "trampoline", which would catch the message, turn it
into an NSInvocation and then forward it on.
A HOM implementation can be found in MPWFoundation, where it is used
primarily for collection operations. MPWFoundation is available from
the metaobject web-site.
Below is the implementation of the "safely" HOM. It is a bit more
verbose than necessary because it tries to clean up all temporary
objects except for the exception (if it happens):
-exceptionPerformingInvocation:(NSInvocation*)invocation
{
id pool=[[NSAutoreleasePool alloc] init];
id exception=nil;
const char *returnType=[[invocation methodSignature]
methodReturnType];
NS_DURING
[invocation invokeWithTarget:self];
NS_HANDLER
exception = [localException retain];
[pool release];
[exception autorelease];
pool=[[NSAutoreleasePool alloc] init];
NS_ENDHANDLER
if ( returnType && *returnType !='v') {
[invocation setReturnValue:&exception];
}
[pool release];
return exception;
}
-safely
{
return [self
trampolineWithSelector:@selector(exceptionPerformingInvocation:)];
}
The -trampolineWithSelector method is generic:
-trampolineWithSelector:(SEL)selector
{
id trampoline=[MPWTrampoline quickTrampoline];
[trampoline setXxxTarget:self];
[trampoline setXxxSelector:selector];
return trampoline;
}
Cheers,
Marcel
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.