-
Notifications
You must be signed in to change notification settings - Fork 40
/
GH_MAZeroingWeakRef.h
73 lines (55 loc) · 2.08 KB
/
GH_MAZeroingWeakRef.h
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
//
// GH_MAZeroingWeakRef.h
// ZeroingWeakRef
//
// Created by Michael Ash on 7/5/10.
//
#import <Foundation/Foundation.h>
@interface GH_MAZeroingWeakRef : NSObject
{
id _target;
BOOL _nativeZWR;
#if NS_BLOCKS_AVAILABLE
void (^_cleanupBlock)(id target);
#endif
}
+ (BOOL)canRefCoreFoundationObjects;
+ (id)refWithTarget: (id)target;
- (id)initWithTarget: (id)target;
#if NS_BLOCKS_AVAILABLE
// ON 10.7:
// cleanup block runs while the target's memory is still
// allocated but after all dealloc methods have run
// (it runs at associated object cleanup time)
// you can use the target's pointer value but don't
// manipulate its contents!
// ON 10.6 AND BELOW:
// cleanup block runs while the global ZWR lock is held
// so make it short and sweet!
// use GCD or something to schedule execution later
// if you need to do something that may take a while
//
// it is unsafe to call -target on the weak ref from
// inside the cleanup block, which is why the target
// is passed in as a parameter
// note that you must not resurrect the target at this point!
- (void)setCleanupBlock: (void (^)(id target))block;
#endif
- (id)target;
@end
#ifndef __has_feature
#define __has_feature(feature) 0
#endif
#define GH_MAWeakVar(var) __weak_ ## var
#if __has_feature(objc_arc_weak)
#define GH_MAWeakDeclare(var) __weak __typeof__((var)) GH_MAWeakVar(var) = var
#define GH_MAWeakImport(var) __typeof__((GH_MAWeakVar(var))) var = GH_MAWeakVar(var)
#define GH_MAWeakImportReturn(var) GH_MAWeakImport(var); do { if(var == nil) return; } while(NO)
#else
#define GH_MAWeakDeclare(var) __typeof__((var)) GH_MAWeakVar(var) = (id)[GH_MAZeroingWeakRef refWithTarget:var]
#define GH_MAWeakImport(var) __typeof__((GH_MAWeakVar(var))) var = [(GH_MAZeroingWeakRef *)GH_MAWeakVar(var) target]
#define GH_MAWeakImportReturn(var) GH_MAWeakImport(var); do { if(var == nil) return; } while(NO)
#endif
#define GH_MAWeakSelfDeclare() GH_MAWeakDeclare(self)
#define GH_MAWeakSelfImport() GH_MAWeakImport(self)
#define GH_MAWeakSelfImportReturn() GH_MAWeakImportReturn(self)