diff --git a/ChangeLog b/ChangeLog index d206e065e..ecc1bfc31 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2009-07-13 14:15-EDT Gregory John Casamento + + * Headers/Foundation/Foundation.h + * Headers/Foundation/NSOperation.h + * Source/GNUmakefile + * Source/NSOperation.m: Initial implementation of NSOperation + class. + 2009-07-11 Richard Frith-Macdonald * Source/NSConcreteMapTable.m: Add some comments. diff --git a/Headers/Foundation/Foundation.h b/Headers/Foundation/Foundation.h index 064f252a7..482f54960 100644 --- a/Headers/Foundation/Foundation.h +++ b/Headers/Foundation/Foundation.h @@ -84,6 +84,7 @@ #import #import #import +#import #import #import #import diff --git a/Headers/Foundation/NSOperation.h b/Headers/Foundation/NSOperation.h new file mode 100644 index 000000000..3f8afc8f7 --- /dev/null +++ b/Headers/Foundation/NSOperation.h @@ -0,0 +1,82 @@ +/**Interface for NSOperation for GNUStep + Copyright (C) 2009 Free Software Foundation, Inc. + + Written by: Gregory Casamento + Date: 2009 + + 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., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + + AutogsdocSource: NSOperation.m + */ + +#import + +@class NSMutableArray; + +enum { + NSOperationQueuePriorityVeryLow = -8, + NSOperationQueuePriorityLow = -4, + NSOperationQueuePriorityNormal = 0, + NSOperationQueuePriorityHigh = 4, + NSOperationQueuePriorityVeryHigh = 8 +}; + +typedef NSInteger NSOperationQueuePriority; + +@interface NSOperation : NSObject +{ + // Priority... + NSOperationQueuePriority priority; + + // Status... + BOOL cancelled; + BOOL concurrent; + BOOL executing; + BOOL finished; + BOOL ready; + + // Dependencies + NSMutableArray *dependencies; +} + +// Initialization +- (id) init; + +// Executing the operation +- (void) start; +- (void) main; + +// Cancelling the operation +- (void) cancel; + +// Getting the operation status +- (BOOL) isCancelled; +- (BOOL) isExecuting; +- (BOOL) isFinished; +- (BOOL) isConcurrent; +- (BOOL) isReady; + +// Managing dependencies +- (void) addDependency: (NSOperation *)op; +- (void) removeDependency: (NSOperation *)op; +- (NSArray *)dependencies; + +// Prioritization +- (NSOperationQueuePriority) queuePriority; +- (void) setQueuePriority: (NSOperationQueuePriority)priority; +@end diff --git a/Source/GNUmakefile b/Source/GNUmakefile index be648dc77..0e4e07b07 100644 --- a/Source/GNUmakefile +++ b/Source/GNUmakefile @@ -231,6 +231,7 @@ NSNumberFormatter.m \ NSObjCRuntime.m \ NSObject.m \ NSObject+NSComparisonMethods.m \ +NSOperation.m \ NSPage.m \ NSPathUtilities.m \ NSPipe.m \ @@ -378,6 +379,7 @@ NSNull.h \ NSNumberFormatter.h \ NSObjCRuntime.h \ NSObject.h \ +NSOperation.h \ NSPathUtilities.h \ NSPointerArray.h \ NSPointerFunctions.h \ diff --git a/Source/NSOperation.m b/Source/NSOperation.m new file mode 100644 index 000000000..c99609668 --- /dev/null +++ b/Source/NSOperation.m @@ -0,0 +1,147 @@ +/**Interface for NSOperation for GNUStep + Copyright (C) 2009 Free Software Foundation, Inc. + + Written by: Gregory Casamento + Date: 2009 + + 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., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + + AutogsdocSource: NSOperation.m + */ + +#import +#import +#import + +@implementation NSOperation : NSObject +// Initialization +- (id) init +{ + if((self = [super init]) != nil) + { + priority = NSOperationQueuePriorityNormal; + + cancelled = NO; + concurrent = NO; + executing = NO; + finished = NO; + ready = NO; + + dependencies = [[NSMutableArray alloc] initWithCapacity: 5]; + } + return self; +} + +- (void) dealloc +{ + RELEASE(dependencies); + [super dealloc]; +} + +// Executing the operation +- (void) start +{ + executing = YES; + finished = NO; + + if([self isConcurrent]) + { + [self main]; + } + else + { + NS_DURING + { + [self main]; + } + NS_HANDLER + { + NSLog(@"%@",[localException reason]); + } + NS_ENDHANDLER; + } + + executing = NO; + finished = YES; +} + +- (void) main; +{ + // subclass responsibility... + [self subclassResponsibility: _cmd]; +} + +// Cancelling the operation +- (void) cancel +{ + [self subclassResponsibility: _cmd]; +} + +// Getting the operation status +- (BOOL) isCancelled +{ + return NO; +} + +- (BOOL) isExecuting +{ + return NO; +} + +- (BOOL) isFinished +{ + return NO; +} + +- (BOOL) isConcurrent +{ + return NO; +} + +- (BOOL) isReady +{ + return NO; +} + +// Managing dependencies +- (void) addDependency: (NSOperation *)op +{ + [dependencies addObject: op]; +} + +- (void) removeDependency: (NSOperation *)op +{ + [dependencies removeObject: op]; +} + +- (NSArray *)dependencies +{ + return [[NSArray alloc] initWithArray: dependencies]; +} + +// Prioritization +- (NSOperationQueuePriority) queuePriority +{ + return priority; +} + +- (void) setQueuePriority: (NSOperationQueuePriority)pri +{ + priority = pri; +} +@end