discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Selectors and categories in GNUstep (porting from Cocoa)


From: Tom Hageman
Subject: Re: Selectors and categories in GNUstep (porting from Cocoa)
Date: Thu, 9 May 2002 20:03:34 +0200

On Thu, 9 May 2002 00:25:20 +0200, Andreas Hoeschler wrote:
> if (sel == FBQualifierOperatorNotEqual)
> return [leftValue isNotEqualTo:rightValue];
> else if (sel == FBQualifierOperatorLessThan)
> return [leftValue isLessThan:rightValue];
> else if (sel == FBQualifierOperatorGreaterThan)
> return [leftValue isGreaterThan:rightValue];
> else if (sel == FBQualifierOperatorLessThanOrEqualTo)
> return [leftValue isLessThanOrEqualTo:rightValue];
> else if (sel == FBQualifierOperatorGreaterThanOrEqualTo)
> return [leftValue isGreaterThanOrEqualTo:rightValue];
> else if (sel == FBQualifierOperatorContains)
> return [leftValue doesContain:rightValue];
> else if (sel == FBQualifierOperatorLike)

Ouch, that's a very clumsy way to do things.  The very idea to pass down  
a selector instead of some random enum value is so you can perform it  
directly on a target object instead of having to hardwire a lot of  
decisions like above.

So conceptually, you'd like to do:

        return (BOOL)[leftValue performSelector:sel withValue:rightValue];

There's a slight complication, in that performSelector returns an (id),  
and the invoked selector returns a (BOOL).  It may not always be safe to  
do the (BOOL) -> (id) -> (BOOL) cast above (eg when doing DO), so a safer  
approach is:

        typedef BOOL (*BOOL_IMP)(id, SEL, ...);
        BOOL_IMP methodIMP;

        methodIMP = (BOOL_IMP)[leftValue methodForSelector:sel];
        NSParameterAssert(methodIMP != NULL);
        return methodIMP(leftValue, sel, rightValue);

(NB. compiled in mail only!)

Hope this helps,
Tom.

--
__/__/__/__/  Tom Hageman  <trh@xs4all.nl> (home)  [NeXTmail/Mime OK]
  __/ __/_/         <t.hageman@proteon.nl> (work)  [Mime OK]
 __/__/__/
__/  _/_/                                              [ObjC retain];
objc: FREED(id): message retain sent to freed object=0xdeadcafe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]