Oolite
Loading...
Searching...
No Matches
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 "
OOFunctionAttributes.h
"
96
#import <Foundation/Foundation.h>
97
98
@class
OOWeakReference
;
99
100
@protocol
OOWeakReferenceSupport <NSObject>
101
102
- (id)weakRetain
OO_RETURNS_RETAINED
;
// Returns a retained OOWeakReference, which should be released when finished with.
103
- (void)weakRefDied:(
OOWeakReference
*)weakRef;
104
105
@end
106
107
@interface
OOWeakReference
: NSProxy {
108
id<OOWeakReferenceSupport>
_object
;
109
}
110
111
- (id)weakRefUnderlyingObject;
112
113
- (id)
weakRetain
OO_RETURNS_RETAINED
;
// Returns [self retain] for weakrefs.
114
115
// For referred object only:
116
+ (id)weakRefWithObject:(
id
<OOWeakReferenceSupport>)object;
117
- (void)
weakRefDrop
;
118
119
@end
120
121
@interface
NSObject (OOWeakReference)
122
123
- (id)
weakRefUnderlyingObject
;
// Always self for non-weakrefs (and of course nil for nil).
124
125
@end
126
127
/* OOWeakRefObject
128
Simple object implementing OOWeakReferenceSupport, to subclass. This
129
provides a full implementation for simplicity, but keep in mind that the
130
protocol can be implemented by any class.
131
*/
132
@interface
OOWeakRefObject
: NSObject <
OOWeakReferenceSupport
> {
133
OOWeakReference
*
weakSelf
;
134
}
135
136
- (id)weakSelf;
// Equivalent to [[self weakRetain] autorelease]
137
138
@end
OOFunctionAttributes.h
-[NSObject(OOWeakReference) weakRefUnderlyingObject]
id weakRefUnderlyingObject()
Definition
OOWeakReference.m:124
OOWeakRefObject
Definition
OOWeakReference.h:132
OOWeakRefObject::weakSelf
OOWeakReference * weakSelf
Definition
OOWeakReference.m:152
OOWeakReference
Definition
OOWeakReference.h:107
-[OOWeakReference weakRetain]
id weakRetain()
Definition
OOWeakReference.m:55
OOWeakReference::_object
id< OOWeakReferenceSupport > _object
Definition
OOWeakReference.h:108
-[OOWeakReference OO_RETURNS_RETAINED]
id OO_RETURNS_RETAINED()
-[OOWeakReference weakRefDrop]
void weakRefDrop()
Definition
OOWeakReference.m:60
OOWeakReferenceSupport-p
Definition
OOWeakReference.h:100
-[OOWeakReferenceSupport-p OO_RETURNS_RETAINED]
id OO_RETURNS_RETAINED()
src
Core
OOWeakReference.h
Generated by
1.15.0