Skip to content

Commit

Permalink
allow more arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Jan 6, 2025
1 parent 9395e20 commit 6b04eaa
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 7 deletions.
13 changes: 13 additions & 0 deletions Headers/GNUstepBase/NSOperationQueue+GNUstepBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ extern "C" {

@interface NSOperationQueue (GNUstepBase)

- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
withObject: (id<NSObject>)object3
withObject: (id<NSObject>)object4;

- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
withObject: (id<NSObject>)object3;

- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
Expand Down
156 changes: 149 additions & 7 deletions Source/Additions/NSOperationQueue+GNUstepBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,40 @@
*/
#import "../common.h"
#import "Foundation/NSException.h"
#import "Foundation/NSObject.h"
#import "Foundation/NSOperation.h"
#import "GNUstepBase/NSOperationQueue+GNUstepBase.h"


@interface GSTargetOperation : NSOperation
{
IMP msg;
id<NSObject> target;
SEL selector;
id<NSObject> o1;
id<NSObject> o2;
id<NSObject> o3;
id<NSObject> o4;
enum {
argc_zero = 0,
argc_one = 1,
argc_two = 2
argc_two = 2,
argc_three = 3,
argc_four = 4
} argc;
}
+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
withObject: (id<NSObject>)object3
withObject: (id<NSObject>)object4;
+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
withObject: (id<NSObject>)object3;
+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
Expand All @@ -55,6 +72,36 @@ + (instancetype) operationWithTarget: (id<NSObject>)aTarget
*/
@implementation NSOperationQueue (GNUstepBase)

- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
withObject: (id<NSObject>)object3
withObject: (id<NSObject>)object4
{
GSTargetOperation *top = [GSTargetOperation operationWithTarget: aTarget
performSelector: aSelector
withObject: object1
withObject: object2
withObject: object3
withObject: object4];
[self addOperation: top];
}

- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
withObject: (id<NSObject>)object3
{
GSTargetOperation *top = [GSTargetOperation operationWithTarget: aTarget
performSelector: aSelector
withObject: object1
withObject: object2
withObject: object3];
[self addOperation: top];
}

- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
Expand Down Expand Up @@ -90,17 +137,94 @@ - (void) addOperationWithTarget: (id<NSObject>)aTarget

@implementation GSTargetOperation

static IMP
check(Class c, SEL _cmd, id t, SEL s)
{
IMP msg;

if (nil == t)
[NSException raise: NSInvalidArgumentException
format: @"%@ null target given", NSStringFromSelector(_cmd)];
if (0 == s)
[NSException raise: NSInvalidArgumentException
format: @"%@ null selector given", NSStringFromSelector(_cmd)];

/* The Apple runtime API would do:
* msg = class_getMethodImplementation(object_getClass(self), aSelector);
* but this cannot ask self for information about any method reached by
* forwarding, so the returned forwarding function would ge a generic one
* rather than one aware of hardware issues with returning structures
* and floating points. We therefore prefer the GNU API which is able to
* use forwarding callbacks to get better type information.
*/
msg = objc_msg_lookup(t, s);
if (!msg)
[NSException raise: NSGenericException
format: @"%@ invalid selector '%s' passed to %s",
t, sel_getName(s), sel_getName(_cmd)];

return msg;
}

+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
withObject: (id<NSObject>)object3
withObject: (id<NSObject>)object4
{
GSTargetOperation *op;
IMP msg = check(self, _cmd, aTarget, aSelector);

op = [[self alloc] init];
op->msg = msg;
op->target = RETAIN(aTarget);
op->selector = aSelector;
op->o1 = RETAIN(object1);
op->o2 = RETAIN(object2);
op->o3 = RETAIN(object3);
op->o4 = RETAIN(object4);
op->argc = argc_four;
return AUTORELEASE(op);
}

+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
withObject: (id<NSObject>)object3
{
GSTargetOperation *op;
IMP msg = check(self, _cmd, aTarget, aSelector);

op = [[self alloc] init];
op->msg = msg;
op->target = RETAIN(aTarget);
op->selector = aSelector;
op->o1 = RETAIN(object1);
op->o2 = RETAIN(object2);
op->o3 = RETAIN(object3);
op->o4 = nil;
op->argc = argc_three;
return AUTORELEASE(op);
}

+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
{
GSTargetOperation *op = [[self alloc] init];
GSTargetOperation *op;
IMP msg = check(self, _cmd, aTarget, aSelector);

op = [[self alloc] init];
op->msg = msg;
op->target = RETAIN(aTarget);
op->selector = aSelector;
op->o1 = RETAIN(object1);
op->o2 = RETAIN(object2);
op->o3 = nil;
op->o4 = nil;
op->argc = argc_two;
return AUTORELEASE(op);
}
Expand All @@ -109,25 +233,35 @@ + (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
{
GSTargetOperation *op = [[self alloc] init];
GSTargetOperation *op;
IMP msg = check(self, _cmd, aTarget, aSelector);

op = [[self alloc] init];
op->msg = msg;
op->target = RETAIN(aTarget);
op->selector = aSelector;
op->o1 = RETAIN(object1);
op->o2 = nil;
op->o3 = nil;
op->o4 = nil;
op->argc = argc_one;
return AUTORELEASE(op);
}

+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
{
GSTargetOperation *op = [[self alloc] init];
GSTargetOperation *op;
IMP msg = check(self, _cmd, aTarget, aSelector);

op = [[self alloc] init];
op->msg = msg;
op->target = RETAIN(aTarget);
op->selector = aSelector;
op->o1 = nil;
op->o2 = nil;
op->o3 = nil;
op->o4 = nil;
op->argc = argc_zero;
return AUTORELEASE(op);
}
Expand All @@ -137,21 +271,29 @@ - (void) dealloc
RELEASE(target);
if (argc > 0) RELEASE(o1);
if (argc > 1) RELEASE(o2);
if (argc > 2) RELEASE(o3);
if (argc > 3) RELEASE(o4);
DEALLOC
}

- (void) main
{
switch (argc)
{
case argc_four:
(*msg)(target, selector, o1, o2, o3, o4);
return;
case argc_three:
(*msg)(target, selector, o1, o2, o3);
return;
case argc_two:
[target performSelector: selector withObject: o1 withObject: o2];
(*msg)(target, selector, o1, o2);
return;
case argc_one:
[target performSelector: selector withObject: o1];
(*msg)(target, selector, o1);
return;
case argc_zero:
[target performSelector: selector];
(*msg)(target, selector);
return;
}
}
Expand Down

0 comments on commit 6b04eaa

Please sign in to comment.