Complete code for NSBlockOperation, need to write tests

This commit is contained in:
Gregory John Casamento 2019-08-02 05:20:59 -04:00
parent dcfbe9e0f8
commit 88dd60a8fc
2 changed files with 74 additions and 0 deletions

View file

@ -199,6 +199,24 @@ typedef NSInteger NSOperationQueuePriority;
@end
@interface NSBlockOperation : NSOperation
{
#if GS_NONFRAGILE
# if defined(GS_NSBlockOperation_IVARS)
@public GS_NSBlockOperation_IVARS
# endif
#else
@private id _internal;
#endif
}
// Managing the blocks in the Operation
DEFINE_BLOCK_TYPE(GSBlockOperationBlock, void, void);
+ (instancetype)blockOperationWithBlock: (GSBlockOperationBlock)block;
- (void)addExecutionBlock: (GSBlockOperationBlock)block;
- (NSArray *) executionBlocks;
@end
/**
* NSOperationQueue
@ -295,6 +313,8 @@ enum {
- (void) waitUntilAllOperationsAreFinished;
@end
#if defined(__cplusplus)
}
#endif

View file

@ -44,6 +44,9 @@
NSMutableArray *dependencies; \
GSOperationCompletionBlock completionBlock;
#define GS_NSBlockOperation_IVARS \
NSMutableArray *executionBlocks;
#define GS_NSOperationQueue_IVARS \
NSRecursiveLock *lock; \
NSConditionLock *cond; \
@ -533,6 +536,57 @@ static NSArray *empty = nil;
@end
@implementation NSBlockOperation
// Initialize
- (id) init
{
self = [super init];
if(self != nil)
{
GS_CREATE_INTERNAL(NSBlockOperation);
internal->executionBlocks = [[NSMutableArray alloc] initWithCapacity: 10];
}
return self;
}
- (void) dealloc
{
RELEASE(internal->executionBlocks);
[super dealloc];
}
// Managing the blocks in the Operation
+ (instancetype)blockOperationWithBlock: (GSBlockOperationBlock)block
{
NSBlockOperation *op = [[NSBlockOperation alloc] init];
[op addExecutionBlock: block];
return op;
}
- (void)addExecutionBlock: (GSBlockOperationBlock)block
{
[internal->executionBlocks addObject: block];
}
- (NSArray *) executionBlocks
{
return internal->executionBlocks;
}
- (void) main
{
NSEnumerator *en = [[self executionBlocks] objectEnumerator];
GSBlockOperationBlock theBlock;
while((theBlock = [en nextObject]) != nil)
{
CALL_BLOCK_NO_ARGS(theBlock);
}
}
@end
#undef GSInternal
#define GSInternal NSOperationQueueInternal
#include "GSInternal.h"