Oolite
Loading...
Searching...
No Matches
OOWeakReference.h
Go to the documentation of this file.
1/*
2
3OOWeakReference.h
4
5Weak reference class for Cocoa/GNUstep/OpenStep. As it stands, this will not
6work as a weak reference in a garbage-collected environment.
7
8A weak reference allows code to maintain a reference to an object while
9allowing the object to reach a retain count of zero and deallocate itself.
10To function, the referenced object must implement the OOWeakReferenceSupport
11protocol.
12
13Client 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
15finished, release the proxy. Messages sent to the proxy will be forwarded as
16long as the underlying object exists; beyond that, they will act exactly like
17messages to nil. (IMPORTANT: this means messages returning floating-point or
18struct values have undefined return values, so use -weakRefUnderlyingObject in
19such 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
48Note that the only reference to OOWeakReference being involved is the call to
49weakRetain instead of retain. However, the following would not work:
50 thing = aThing;
51 [thing weakRetain];
52
53Additionally, it is not possible to access instance variables directly -- but
54then, that's a filthy habit.
55
56OOWeakReferenceSupport 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
90Copyright (C) 2007-2013 Jens Ayton
91This code is hereby placed in the public domain.
92
93*/
94
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*/
134}
135
136- (id)weakSelf; // Equivalent to [[self weakRetain] autorelease]
137
138@end
OOWeakReference * weakSelf
id< OOWeakReferenceSupport > _object