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.h
Go to the documentation of this file.
1
/*
2
3
OOWeakReference.h
4
5
Weak reference class for Cocoa/GNUstep/OpenStep. As it stands, this will not
6
work as a weak reference in a garbage-collected environment.
7
8
A weak reference allows code to maintain a reference to an object while
9
allowing the object to reach a retain count of zero and deallocate itself.
10
To function, the referenced object must implement the OOWeakReferenceSupport
11
protocol.
12
13
Client use is extremely simple: to get a weak reference to the object, call
14
-weakRetain and use the returned proxy instead of the actual object. When
15
finished, release the proxy. Messages sent to the proxy will be forwarded as
16
long as the underlying object exists; beyond that, they will act exactly like
17
messages to nil. (IMPORTANT: this means messages returning floating-point or
18
struct values have undefined return values, so use -weakRefUnderlyingObject in
19
such cases.) Example:
20
21
@interface ThingWatcher: NSObject
22
{
23
@private
24
Thing *thing;
25
}
26
@end
27
28
@implementation ThingWatcher
29
- (void)setThing:(Thing *)aThing
30
{
31
[thing release];
32
thing = [aThing weakRetain];
33
}
34
35
- (void)frobThing
36
{
37
[thing frob];
38
}
39
40
- (void)dealloc
41
{
42
[thing release];
43
[super dealloc];
44
}
45
@end
46
47
48
Note that the only reference to OOWeakReference being involved is the call to
49
weakRetain instead of retain. However, the following would not work:
50
thing = aThing;
51
[thing weakRetain];
52
53
Additionally, it is not possible to access instance variables directly -- but
54
then, that's a filthy habit.
55
56
OOWeakReferenceSupport implementation is also simple:
57
58
@interface Thing: NSObject <OOWeakReferenceSupport>
59
{
60
@private
61
OOWeakReference *weakSelf;
62
}
63
@end
64
65
@implementation Thing
66
- (id)weakRetain
67
{
68
if (weakSelf == nil) weakSelf = [OOWeakReference weakRefWithObject:self];
69
return [weakSelf retain];
70
}
71
72
- (void)weakRefDied:(OOWeakReference *)weakRef
73
{
74
if (weakRef == weakSelf) weakSelf = nil;
75
}
76
77
- (void)dealloc
78
{
79
[weakSelf weakRefDrop]; // Very important!
80
[super dealloc];
81
}
82
83
- (void)frob
84
{
85
NSBeep();
86
}
87
@end
88
89
90
Copyright (C) 2007-2013 Jens Ayton
91
This code is hereby placed in the public domain.
92
93
*/
94
95
#import <Foundation/Foundation.h>
96
#import "
OOFunctionAttributes.h
"
97
98
@class
OOWeakReference
;
99
100
101
@protocol
OOWeakReferenceSupport <NSObject>
102
103
- (id)weakRetain
OO_RETURNS_RETAINED
;
// Returns a retained OOWeakReference, which should be released when finished with.
104
- (void)weakRefDied:(
OOWeakReference
*)weakRef;
105
106
@end
107
108
109
@interface
OOWeakReference
: NSProxy
110
{
111
id<OOWeakReferenceSupport>
_object
;
112
}
113
114
- (id)weakRefUnderlyingObject;
115
116
- (id)
weakRetain
OO_RETURNS_RETAINED
;
// Returns [self retain] for weakrefs.
117
118
// For referred object only:
119
+ (id)weakRefWithObject:(
id
<OOWeakReferenceSupport>)object;
120
- (void)
weakRefDrop
;
121
122
@end
123
124
125
@interface
NSObject (OOWeakReference)
126
127
- (id)
weakRefUnderlyingObject
;
// Always self for non-weakrefs (and of course nil for nil).
128
129
@end
130
131
132
/* OOWeakRefObject
133
Simple object implementing OOWeakReferenceSupport, to subclass. This
134
provides a full implementation for simplicity, but keep in mind that the
135
protocol can be implemented by any class.
136
*/
137
@interface
OOWeakRefObject
: NSObject <
OOWeakReferenceSupport
>
138
{
139
OOWeakReference
*
weakSelf
;
140
}
141
142
- (id)weakSelf;
// Equivalent to [[self weakRetain] autorelease]
143
144
@end
OOFunctionAttributes.h
-[NSObject(OOWeakReference) weakRefUnderlyingObject]
id weakRefUnderlyingObject()
Definition
OOWeakReference.m:142
OOWeakRefObject
Definition
OOWeakReference.h:139
OOWeakRefObject::weakSelf
OOWeakReference * weakSelf
Definition
OOWeakReference.m:172
OOWeakReference
Definition
OOWeakReference.h:110
-[OOWeakReference weakRetain]
id weakRetain()
Definition
OOWeakReference.m:58
OOWeakReference::_object
id< OOWeakReferenceSupport > _object
Definition
OOWeakReference.h:111
-[OOWeakReference OO_RETURNS_RETAINED]
id OO_RETURNS_RETAINED()
-[OOWeakReference weakRefDrop]
void weakRefDrop()
Definition
OOWeakReference.m:64
OOWeakReferenceSupport-p
Definition
OOWeakReference.h:101
-[OOWeakReferenceSupport-p OO_RETURNS_RETAINED]
id OO_RETURNS_RETAINED()
Generated by
1.13.2