Add convenience methods to perform a method as an operation

This commit is contained in:
rfm 2025-01-06 21:40:39 +00:00
parent 1cdc60d748
commit 9395e20db9
6 changed files with 223 additions and 0 deletions

View file

@ -50,6 +50,7 @@
#import <GNUstepBase/NSNetServices+GNUstepBase.h>
#import <GNUstepBase/NSNumber+GNUstepBase.h>
#import <GNUstepBase/NSObject+GNUstepBase.h>
#import <GNUstepBase/NSOperationQueue+GNUstepBase.h>
#import <GNUstepBase/NSProcessInfo+GNUstepBase.h>
#import <GNUstepBase/NSStream+GNUstepBase.h>
#import <GNUstepBase/NSString+GNUstepBase.h>

View file

@ -0,0 +1,60 @@
/** Declaration of extension methods for base additions
Copyright (C) 2025 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#ifndef INCLUDED_NSOperationQueue_GNUstepBase_h
#define INCLUDED_NSOperationQueue_GNUstepBase_h
#import <GNUstepBase/GSVersionMacros.h>
#import <Foundation/NSOperation.h>
#if defined(__cplusplus)
extern "C" {
#endif
#if OS_API_VERSION(GS_API_NONE,GS_API_LATEST)
@interface NSOperationQueue (GNUstepBase)
- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2;
- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1;
- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector;
@end
#endif /* OS_API_VERSION */
#if defined(__cplusplus)
}
#endif
#endif /* INCLUDED_NSOperationQueue_GNUstepBase_h */

View file

@ -54,6 +54,7 @@ Additions_OBJC_FILES =\
NSMutableString+GNUstepBase.m \
NSNumber+GNUstepBase.m \
NSObject+GNUstepBase.m \
NSOperationQueue+GNUstepBase.m \
NSPropertyList+GNUstepBase.m \
NSProcessInfo+GNUstepBase.m \
NSStream+GNUstepBase.m \

View file

@ -0,0 +1,159 @@
/* Implementation of extension methods to base additions
Copyright (C) 2025 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#import "../common.h"
#import "Foundation/NSObject.h"
#import "Foundation/NSOperation.h"
#import "GNUstepBase/NSOperationQueue+GNUstepBase.h"
@interface GSTargetOperation : NSOperation
{
id<NSObject> target;
SEL selector;
id<NSObject> o1;
id<NSObject> o2;
enum {
argc_zero = 0,
argc_one = 1,
argc_two = 2
} argc;
}
+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2;
+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1;
+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector;
@end
/**
* Extension methods for the NSObjectQueue class
*/
@implementation NSOperationQueue (GNUstepBase)
- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
{
GSTargetOperation *top = [GSTargetOperation operationWithTarget: aTarget
performSelector: aSelector
withObject: object1
withObject: object2];
[self addOperation: top];
}
- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
{
GSTargetOperation *top = [GSTargetOperation operationWithTarget: aTarget
performSelector: aSelector
withObject: object1];
[self addOperation: top];
}
- (void) addOperationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
{
GSTargetOperation *top = [GSTargetOperation operationWithTarget: aTarget
performSelector: aSelector];
[self addOperation: top];
}
@end
@implementation GSTargetOperation
+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
withObject: (id<NSObject>)object2
{
GSTargetOperation *op = [[self alloc] init];
op->target = RETAIN(aTarget);
op->selector = aSelector;
op->o1 = RETAIN(object1);
op->o2 = RETAIN(object2);
op->argc = argc_two;
return AUTORELEASE(op);
}
+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
withObject: (id<NSObject>)object1
{
GSTargetOperation *op = [[self alloc] init];
op->target = RETAIN(aTarget);
op->selector = aSelector;
op->o1 = RETAIN(object1);
op->o2 = nil;
op->argc = argc_one;
return AUTORELEASE(op);
}
+ (instancetype) operationWithTarget: (id<NSObject>)aTarget
performSelector: (SEL)aSelector
{
GSTargetOperation *op = [[self alloc] init];
op->target = RETAIN(aTarget);
op->selector = aSelector;
op->o1 = nil;
op->o2 = nil;
op->argc = argc_zero;
return AUTORELEASE(op);
}
- (void) dealloc
{
RELEASE(target);
if (argc > 0) RELEASE(o1);
if (argc > 1) RELEASE(o2);
DEALLOC
}
- (void) main
{
switch (argc)
{
case argc_two:
[target performSelector: selector withObject: o1 withObject: o2];
return;
case argc_one:
[target performSelector: selector withObject: o1];
return;
case argc_zero:
[target performSelector: selector];
return;
}
}
@end

View file

@ -228,6 +228,7 @@ NSFileHandle+GNUstepBase.h \
NSMutableString+GNUstepBase.h \
NSNumber+GNUstepBase.h \
NSObject+GNUstepBase.h \
NSOperationQueue+GNUstepBase.h \
NSProcessInfo+GNUstepBase.h \
NSString+GNUstepBase.h \
NSTask+GNUstepBase.h \

View file

@ -144,6 +144,7 @@ NSMutableString+GNUstepBase.h \
NSNetServices+GNUstepBase.h \
NSNumber+GNUstepBase.h \
NSObject+GNUstepBase.h \
NSOperationQueue+GNUstepBase.h \
NSProcessInfo+GNUstepBase.h \
NSStream+GNUstepBase.h \
NSString+GNUstepBase.h \