-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlockTest.m
137 lines (111 loc) · 2.39 KB
/
BlockTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#import <Foundation/Foundation.h>
#include "blocks_runtime.h"
//#import <Cocoa/Cocoa.h>
#if 1
typedef int (^intblock)(int);
@interface PBTest : NSObject
{
intblock onRelease;
int num;
}
@property (copy) intblock onRelease;
@property (assign) int num;
@end
@implementation PBTest
@synthesize onRelease, num;
- (id)init
{
if((self = [super init]))
{
}
return self;
}
- (void)release
{
onRelease(num);
[super release];
}
- (void)dealloc
{
[onRelease release];
[super dealloc];
}
@end
int main (int argc, char const *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
PBTest *a = [[PBTest alloc] init];
a.num = 3;
a.onRelease = ^(int i){ printf("Returning 2 * %i", i); return 2*i; };
[a release];
[pool drain];
return 0;
}
#else
typedef void (^BLK)(void);
typedef void (^BLKI)(int);
void testBlock(BLK *ret1, BLK *ret2)
{
__block int i = 0;
BLK b1 = ^{ i++; };
BLK b2 = ^{ printf("print: %d\n", i); };
*ret1 = (BLK)__Block_copy(b1);
*ret2 = (BLK)__Block_copy(b2);
}
void tryBlock(BLK b1, BLK b2)
{
b1();
b2();
b1();
b2();
}
@interface Test : NSObject
{
BLKI onRelease;
}
@property(copy, nonatomic) BLKI onRelease;
- (BLKI)selfBlock;
- (BLKI)superBlock;
@end
@implementation Test
@synthesize onRelease;
- (BLKI)selfBlock
{
return [[^(int i){ NSLog(@"%@ %d", [self description], i); } copy] autorelease];
}
- (BLKI)superBlock
{
return [[^(int i){ NSLog(@"%@ %d", [super description], i); } copy] autorelease];
}
- (void) dealloc
{
onRelease(42);
[onRelease release];
[super dealloc];
}
@end
BLKI b = ^(int i){ NSLog(@"%d", i); };
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Pure C compatible part
BLK b1, b2;
testBlock(&b1, &b2);
tryBlock(b1, b2);
__Block_release(b1);
__Block_release(b2);
// ObjC part
Test *obj = [[Test new] autorelease];
BLKI blk = [obj selfBlock];
blk(10);
[obj superBlock](42);
BLKI b3 = ^(int i){ NSLog(@"%d", i); };
BLKI b4 = ^(int i){ NSLog(@"%d", i); };
NSLog(@"%p, %p, %p, %p", (void *)b, b3, b4, main);
NSLog(@"%@", ((id) ^(int i){ NSLog(@"%d", i); })->isa);
obj.onRelease = ^(int i){ NSLog(@"test %d", i); };
//[blk release];
//
[pool drain];
return 0;
}
#endif