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