Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 125 lines (104 sloc) 4.847 kb
ec735be MAFuture: hello world!
mikeash authored
1 // gcc -framework Foundation -W -Wall -Wno-unused-parameter --std=c99 -g *.m
2 #import <Foundation/Foundation.h>
3
644edd3 MAFuture: separate compound futures into separate file
mikeash authored
4 #import <objc/runtime.h>
5 #import "MACompoundFuture.h"
ec735be MAFuture: hello world!
mikeash authored
6 #import "MAFuture.h"
7
f90e3a2 MAFuture: refactor common code into base class
mikeash authored
8 // make NSLog properly reentrant
9 #define NSLog(...) NSLog(@"%@", [NSString stringWithFormat: __VA_ARGS__])
10
365bcbb MAFuture: add support for return-by-reference parameters
mikeash authored
11
12 @implementation NSObject (ObjectReturnAndPrimitiveByReference)
13
14 + (NSString *)objectReturnAndPrimitiveByReference: (int *)outInt
15 {
16 if(outInt)
17 *outInt = 42;
18 return @"object";
19 }
20
21 @end
22
23 static void TestOutParameters(void)
24 {
25 NSLog(@"Testing out parameters");
26
c49e1cc MAFuture: rename plain futures to background futures
mikeash authored
27 Class nsstring = MACompoundBackgroundFuture(^{ usleep(100000); return [NSString class]; });
365bcbb MAFuture: add support for return-by-reference parameters
mikeash authored
28 NSLog(@"string: %p", [nsstring string]);
29
30 NSError *baderr = nil;
31 NSString *badstr = [nsstring stringWithContentsOfFile: @"/this/file/does/not/exist" encoding: NSUTF8StringEncoding error: &baderr];
32 NSError *gooderr = nil;
33 NSString *goodstr = [nsstring stringWithContentsOfFile: @"/etc/passwd" encoding: NSUTF8StringEncoding error: &gooderr];
34 NSLog(@"stringWithContentsOfFile, pointers: %p error: %p", badstr, baderr);
35 NSLog(@"stringWithContentsOfFile, descriptions: %@ error: %@", badstr, baderr);
36 NSLog(@"stringWithContentsOfFile, pointers: %p error: %p", goodstr, gooderr);
37 NSLog(@"stringWithContentsOfFile, descriptions: %@ error: %@", [goodstr substringToIndex: 2], gooderr);
38
39
c49e1cc MAFuture: rename plain futures to background futures
mikeash authored
40 Class nsobject = MACompoundBackgroundFuture(^{ usleep(100000); return [NSObject class]; });
365bcbb MAFuture: add support for return-by-reference parameters
mikeash authored
41 int x = 0;
42 NSString *str = [nsobject objectReturnAndPrimitiveByReference: &x];
43 NSLog(@"objectReturnAndPrimitiveByReference pointer: %p int: %d", str, x);
44 NSLog(@"objectReturnAndPrimitiveByReference description: %@ int: %d", str, x);
e54e15a MAFuture: preserve out-parameter storage until invocation is invoked
mikeash authored
45
46 NSLog(@"Testing out parameters whose futures are not retained");
47 nsstring = MACompoundLazyFuture(^{ NSLog(@"Fetching NSString class"); return [NSString class]; });
48 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
49 badstr = [nsstring stringWithContentsOfFile: @"/this/file/does/not/exist" encoding: NSUTF8StringEncoding error: &baderr];
50 [badstr retain];
51 [pool release];
52 NSLog(@"nonretained out future destroyed now");
53 NSLog(@"nonretained out future result: %@", badstr);
54 [badstr release];
365bcbb MAFuture: add support for return-by-reference parameters
mikeash authored
55 }
56
ec735be MAFuture: hello world!
mikeash authored
57 int main(int argc, char **argv)
58 {
59 [NSAutoreleasePool new];
60
61 @try
62 {
e5aaf7a MAFuture: add lazy futures
mikeash authored
63 NSLog(@"start");
c49e1cc MAFuture: rename plain futures to background futures
mikeash authored
64 NSString *future = MABackgroundFuture(^{
1a4d87e MAFuture: re-base future hierarchy on custom MAProxy class
mikeash authored
65 NSLog(@"Computing future\n");
e5aaf7a MAFuture: add lazy futures
mikeash authored
66 usleep(100000);
36d3601 MAFuture: more minor refactorings
mikeash authored
67 return @"future result";
644edd3 MAFuture: separate compound futures into separate file
mikeash authored
68 });
e5aaf7a MAFuture: add lazy futures
mikeash authored
69 NSLog(@"future created");
70 NSString *lazyFuture = MALazyFuture(^{
1a4d87e MAFuture: re-base future hierarchy on custom MAProxy class
mikeash authored
71 NSLog(@"Computing lazy future\n");
e5aaf7a MAFuture: add lazy futures
mikeash authored
72 usleep(100000);
73 return @"lazy future result";
74 });
75 NSLog(@"lazy future created");
c49e1cc MAFuture: rename plain futures to background futures
mikeash authored
76 NSString *compoundFuture = MACompoundBackgroundFuture(^{
1a4d87e MAFuture: re-base future hierarchy on custom MAProxy class
mikeash authored
77 NSLog(@"Computing compound future\n");
e5aaf7a MAFuture: add lazy futures
mikeash authored
78 usleep(100000);
79 return @"compound future result";
80 });
81 NSLog(@"compound future created");
82 NSString *compoundLazyFuture = MACompoundLazyFuture(^{
1a4d87e MAFuture: re-base future hierarchy on custom MAProxy class
mikeash authored
83 NSLog(@"Computing compound lazy future\n");
e5aaf7a MAFuture: add lazy futures
mikeash authored
84 usleep(100000);
85 return @"compound future result";
86 });
87 NSLog(@"compound lazy future created");
6b9352e MAFuture: fix nil-return non-compound futures
mikeash authored
88
c49e1cc MAFuture: rename plain futures to background futures
mikeash authored
89 id nilFuture = MABackgroundFuture(^{ return nil; });
90 id nilCompoundFuture = MACompoundBackgroundFuture(^{ return nil; });
e5aaf7a MAFuture: add lazy futures
mikeash authored
91
92 NSLog(@"future: %@", future);
93 NSLog(@"lazy future: %@", lazyFuture);
94 NSLog(@"compound future: %@", [compoundFuture stringByAppendingString: @" suffix"]);
95 NSLog(@"compound lazy future: %@", [compoundLazyFuture stringByAppendingString: @" suffix"]);
36d3601 MAFuture: more minor refactorings
mikeash authored
96
c49e1cc MAFuture: rename plain futures to background futures
mikeash authored
97 NSString *future1 = MABackgroundFuture(^{
36d3601 MAFuture: more minor refactorings
mikeash authored
98 NSLog(@"Computing future\n");
99 usleep(100000);
100 return @"future result";
101 });
c49e1cc MAFuture: rename plain futures to background futures
mikeash authored
102 NSString *future2 = MABackgroundFuture(^{
36d3601 MAFuture: more minor refactorings
mikeash authored
103 NSLog(@"Computing future\n");
104 usleep(100000);
105 return @"future result";
106 });
107 NSLog(@"%p == %p? %llx %llx %s", future1, future2, (long long)[future1 hash], (long long)[future2 hash], [future1 isEqual: future2] ? "YES" : "NO");
ed7b1fc MAFuture: fix a bug with futures that return nil
mikeash authored
108 NSLog(@"nil future: %@", nilFuture);
6b9352e MAFuture: fix nil-return non-compound futures
mikeash authored
109 NSLog(@"nil compound future: %@", nilCompoundFuture);
1ad9b44 MAFuture: remove useless respondsToSelector: implementation
mikeash authored
110 NSLog(@"future responds to description method: %d", [future respondsToSelector: @selector(description)]);
365bcbb MAFuture: add support for return-by-reference parameters
mikeash authored
111
112 TestOutParameters();
ec735be MAFuture: hello world!
mikeash authored
113 }
114 @catch(id exception)
115 {
116 fprintf(stderr, "Exception: %s\n", [[exception description] UTF8String]);
117 }
644edd3 MAFuture: separate compound futures into separate file
mikeash authored
118
119 // unsigned int count;
120 // Method *list = class_copyMethodList([NSProxy class], &count);
121 // for(unsigned i = 0; i < count; i++)
122 // NSLog(@"%@", NSStringFromSelector(method_getName(list[i])));
ec735be MAFuture: hello world!
mikeash authored
123 }
124
Something went wrong with that request. Please try again.