Skip to content

Commit

Permalink
MAFuture: re-base future hierarchy on custom MAProxy class
Browse files Browse the repository at this point in the history
git-svn-id: http://mikeash.com/svn/MAFuture@114 1ea6ab17-982b-0410-b0ab-91172c90a6e5
  • Loading branch information
mikeash committed Feb 6, 2010
1 parent f90e3a2 commit 1a4d87e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 20 deletions.
4 changes: 3 additions & 1 deletion MABaseFuture.h
@@ -1,7 +1,9 @@
#import <Foundation/Foundation.h>

#import "MAProxy.h"

@interface MABaseFuture : NSProxy

@interface MABaseFuture : MAProxy
{
id _value;
NSCondition *_lock;
Expand Down
19 changes: 9 additions & 10 deletions MABaseFuture.m
Expand Up @@ -5,16 +5,6 @@

@implementation MABaseFuture

+ (void)initialize
{
// NSProxy implements -description for some boneheaded reason
// this effectively removes that implementation by re-pointing
// our IMP to point to the implementation of a non-existent method
Method m = class_getInstanceMethod(self, @selector(description));
IMP forwarder = class_getMethodImplementation(self, @selector(thisMethodDoesNotExist));
class_replaceMethod(self, @selector(description), forwarder, method_getTypeEncoding(m));
}

- (id)init
{
_lock = [[NSCondition alloc] init];
Expand Down Expand Up @@ -83,4 +73,13 @@ - (id)resolveFuture
return nil;
}

- (Class)class
{
Class c = [[self resolveFuture] class];
if([NSStringFromClass(c) hasPrefix: @"NSCF"])
return [c superclass];
else
return c;
}

@end
3 changes: 2 additions & 1 deletion MAFuture.m
Expand Up @@ -5,7 +5,7 @@
#import "MAFuture.h"


#define ENABLE_LOGGING 0
#define ENABLE_LOGGING 1

#if ENABLE_LOGGING
#define LOG(...) NSLog(__VA_ARGS__)
Expand Down Expand Up @@ -66,6 +66,7 @@ - (id)resolveFuture

- (id)forwardingTargetForSelector: (SEL)sel
{
LOG(@"%p forwardingTargetForSelector: %@, resolving future", self, NSStringFromSelector(sel));
return [self resolveFuture];
}

Expand Down
17 changes: 17 additions & 0 deletions MAProxy.h
@@ -0,0 +1,17 @@
@interface MAProxy
{
Class isa;
int32_t _refcountMinusOne;
}

+ (id)alloc;

- (void)dealloc;
- (BOOL)isProxy;
- (id)retain;
- (void)release;
- (id)autorelease;
- (NSUInteger)retainCount;

@end

56 changes: 56 additions & 0 deletions MAProxy.m
@@ -0,0 +1,56 @@
#import <Foundation/Foundation.h>
#import <libkern/OSAtomic.h>

#import "MAProxy.h"


@implementation MAProxy

+ (void)initialize
{
// runtime requires an implementation
}

+ (id)alloc
{
return NSAllocateObject(self, 0, NULL);
}

- (void)dealloc
{
NSDeallocateObject(self);
}

- (void)finalize
{
}

- (BOOL)isProxy
{
return YES;
}

- (id)retain
{
OSAtomicIncrement32(&_refcountMinusOne);
return self;
}

- (void)release
{
if(OSAtomicDecrement32(&_refcountMinusOne) == -1)
[self dealloc];
}

- (id)autorelease
{
[NSAutoreleasePool addObject: self];
return self;
}

- (NSUInteger)retainCount
{
return _refcountMinusOne + 1;
}

@end
16 changes: 8 additions & 8 deletions tester.m
Expand Up @@ -17,36 +17,36 @@ int main(int argc, char **argv)

NSLog(@"start");
NSString *future = MAFuture(^{
fprintf(stderr, "Computing future\n");
NSLog(@"Computing future\n");
usleep(100000);
return @"future result";
return [NSString stringWithFormat:@"%d", 42];
});
NSString *future2 = MAFuture(^{
fprintf(stderr, "Computing future\n");
NSLog(@"Computing future\n");
usleep(100000);
return @"future result";
return [NSString stringWithFormat:@"%d", 42];
});
NSLog(@"future created");
NSString *lazyFuture = MALazyFuture(^{
fprintf(stderr, "Computing lazy future\n");
NSLog(@"Computing lazy future\n");
usleep(100000);
return @"lazy future result";
});
NSLog(@"lazy future created");
NSString *compoundFuture = MACompoundFuture(^{
fprintf(stderr, "Computing compound future\n");
NSLog(@"Computing compound future\n");
usleep(100000);
return @"compound future result";
});
NSLog(@"compound future created");
NSString *compoundLazyFuture = MACompoundLazyFuture(^{
fprintf(stderr, "Computing compound lazy future\n");
NSLog(@"Computing compound lazy future\n");
usleep(100000);
return @"compound future result";
});
NSLog(@"compound lazy future created");

NSLog(@"%d", [future isEqual: future2]);
NSLog(@"%p == %p? %llx %llx %s %s", future, future2, (long long)[future hash], (long long)[future2 hash], [future isEqual: future2] ? "YES" : "NO", [future isEqual: future2] ? "YES" : "NO");
NSLog(@"future: %@", future);
NSLog(@"lazy future: %@", lazyFuture);
NSLog(@"compound future: %@", [compoundFuture stringByAppendingString: @" suffix"]);
Expand Down

0 comments on commit 1a4d87e

Please sign in to comment.