Oolite
Loading...
Searching...
No Matches
OOWeakReference.m
Go to the documentation of this file.
1/*
2
3OOWeakReference.m
4
5Written by Jens Ayton in 2007-2013 for Oolite.
6This code is hereby placed in the public domain.
7
8*/
9
10#import "OOWeakReference.h"
11
12@interface OOWeakReferenceTemplates : NSObject
13
14+ (void)weakRefDrop;
16+ (id)nilMethod;
17
18@end
19
20@implementation OOWeakReference
21
22// *** Core functionality.
23
24+ (id)weakRefWithObject:(id<OOWeakReferenceSupport>)object
25{
26 if (object == nil)
27 return nil;
28
29 OOWeakReference* result = [OOWeakReference alloc];
30 // No init for proxies.
31 result->_object = object;
32 return [result autorelease];
33}
34
35- (void)dealloc
36{
37 [_object weakRefDied:self];
38
39 [super dealloc];
40}
41
42- (NSString*)description
43{
44 if (_object != nil)
45 return [_object description];
46 else
47 return [NSString stringWithFormat:@"<Dead %@ %p>", [self class], self];
48}
49
51{
52 return _object;
53}
54
56{
57 return [self retain];
58}
59
61{
62 _object = nil;
63}
64
65// *** Proxy evilness beyond this point.
66
67- (Class)class
68{
69 return [_object class];
70}
71
72- (BOOL)isProxy
73{
74 return YES;
75}
76
77- (void)forwardInvocation:(NSInvocation*)invocation
78{
79 // Does the right thing even with nil _object.
80 [invocation invokeWithTarget:_object];
81}
82
83- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector
84{
85 NSMethodSignature* result = nil;
86
87 if (__builtin_expect(
88 selector != @selector(weakRefDrop) && selector != @selector(weakRefUnderlyingObject), 1)) {
89 // Not a proxy method; get signature from _object if it exists, otherwise generic signature for nil calls.
90 if (__builtin_expect(_object != nil, 1))
91 result = [(id)_object methodSignatureForSelector:selector];
92 else
93 result = [OOWeakReferenceTemplates methodSignatureForSelector:@selector(nilMethod)];
94 } else {
95 // One of OOWeakReference's own methods.
96 result = [OOWeakReferenceTemplates methodSignatureForSelector:selector];
97 }
98
99 return result;
100}
101
102- (BOOL)respondsToSelector:(SEL)selector
103{
104 if (__builtin_expect(_object != nil && selector != @selector(weakRefDrop) && selector != @selector(weakRefUnderlyingObject), 1)) {
105 // _object exists and it's not one of our methods, ask _object.
106 return [_object respondsToSelector:selector];
107 } else {
108 // Selector we responds to, or _object is nil and therefore responds to everything.
109 return YES;
110 }
111}
112
113// New fast forwarding mechanism introduced in Mac OS X 10.5.
114// Note that -forwardInvocation: is still called if _object is nil.
115- (id)forwardingTargetForSelector:(SEL)sel
116{
117 return _object;
118}
119
120@end
121
122@implementation NSObject (OOWeakReference)
123
125{
126 return self;
127}
128
129@end
130
131@implementation OOWeakRefObject
132
134{
135 if (weakSelf == nil)
137 return [weakSelf retain]; // Each caller releases this, as -weakRetain must be balanced with -release.
138}
139
140- (void)weakRefDied:(OOWeakReference*)weakRef
141{
142 if (weakRef == weakSelf)
143 weakSelf = nil;
144}
145
146- (void)dealloc
147{
148 [weakSelf weakRefDrop]; // Very important!
149 [super dealloc];
150}
151
153{
154 return [[self weakRetain] autorelease];
155}
156
157@end
158
159@implementation OOWeakReferenceTemplates
160
161// These are never called, but an implementation must exist so that -methodSignatureForSelector: works.
163{
164}
166{
167 return nil;
168}
170{
171 return nil;
172}
173
174@end
return nil
OOWeakReference * weakSelf
id< OOWeakReferenceSupport > _object
NSString * description()
id weakRefWithObject:(id< OOWeakReferenceSupport > object)