~ubuntu-branches/ubuntu/wily/oolite/wily-proposed

« back to all changes in this revision

Viewing changes to tests/groups/OOWeakReference.h

  • Committer: Package Import Robot
  • Author(s): Nicolas Boulenguez
  • Date: 2011-12-22 00:22:39 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20111222002239-pr3upeupp4jw1psp
Tags: 1.76-1
* New upstream.
* watch: scan upstream stable releases instead of dev snapshots.
* control: use default gobjc instead of explicit 4.6.
* rules: use dpkg-dev build flags.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 -weakRefObjectStillExists
 
19
or -weakRefUnderlyingObject in such cases.) Example:
 
20
 
 
21
@interface ThingWatcher: NSObject
 
22
{
 
23
        Thing                   *thing;
 
24
}
 
25
@end
 
26
 
 
27
@implementation ThingWatcher
 
28
- (void)setThing:(Thing *)aThing
 
29
{
 
30
        [thing release];
 
31
        thing = [aThing weakRetain];
 
32
}
 
33
 
 
34
- (void)frobThing
 
35
{
 
36
        [thing frob];
 
37
}
 
38
 
 
39
- (void)dealloc
 
40
{
 
41
        [thing release];
 
42
        [super dealloc];
 
43
}
 
44
@end
 
45
 
 
46
 
 
47
Note that the only reference to OOWeakReference being involved is the call to
 
48
weakRetain instead of retain. However, the following would not work:
 
49
        thing = aThing;
 
50
        [thing weakRetain];
 
51
 
 
52
Additionally, it is not possible to access instance variables directly -- but
 
53
then, that's a filthy habit.
 
54
 
 
55
OOWeakReferenceSupport implementation is also simple:
 
56
 
 
57
@interface Thing: NSObject <OOWeakReferenceSupport>
 
58
{
 
59
        OOWeakReference         *weakSelf;
 
60
}
 
61
@end
 
62
 
 
63
@implementation Thing
 
64
- (id)weakRetain
 
65
{
 
66
        if (weakSelf == nil)  weakSelf = [OOWeakReference weakRefWithObject:self];
 
67
        return [weakSelf retain];
 
68
}
 
69
 
 
70
- (void)weakRefDied:(OOWeakReference *)weakRef
 
71
{
 
72
        if (weakRef == weakSelf)  weakSelf = nil;
 
73
}
 
74
 
 
75
- (void)dealloc
 
76
{
 
77
        [weakSelf weakRefDrop]; // Very important!
 
78
        [super dealloc];
 
79
}
 
80
 
 
81
- (void)frob
 
82
{
 
83
        NSBeep();
 
84
}
 
85
@end
 
86
 
 
87
 
 
88
Written by Jens Ayton in 2007 for Oolite.
 
89
This code is hereby placed in the public domain.
 
90
 
 
91
*/
 
92
 
 
93
#import <Foundation/Foundation.h>
 
94
 
 
95
@class OOWeakReference;
 
96
 
 
97
 
 
98
@protocol OOWeakReferenceSupport <NSObject>
 
99
 
 
100
- (id)weakRetain;               // Returns a retained OOWeakReference, which should be released when finished with.
 
101
- (void)weakRefDied:(OOWeakReference *)weakRef;
 
102
 
 
103
@end
 
104
 
 
105
 
 
106
@interface OOWeakReference: NSProxy
 
107
{
 
108
        id<OOWeakReferenceSupport>      _object;
 
109
}
 
110
 
 
111
- (BOOL)weakRefObjectStillExists;
 
112
- (id)weakRefUnderlyingObject;
 
113
 
 
114
- (id)weakRetain;       // Returns self for weakrefs.
 
115
 
 
116
// For referred object only:
 
117
+ (id)weakRefWithObject:(id<OOWeakReferenceSupport>)object;
 
118
- (void)weakRefDrop;
 
119
 
 
120
@end
 
121
 
 
122
 
 
123
@interface NSObject (OOWeakReference)
 
124
 
 
125
- (BOOL)weakRefObjectStillExists;       // Always YES for non-weakrefs. ObjC semantics causes it to return NO for nil, so it acts as an existence check.
 
126
- (id)weakRefUnderlyingObject;          // Always self for non-weakrefs (and of course nil for nil).
 
127
 
 
128
@end
 
129
 
 
130
 
 
131
/*      OOWeakRefObject
 
132
        Simple object implementing OOWeakReferenceSupport, to subclass. This
 
133
        provides a full implementation for simplicity, but keep in mind that the
 
134
        protocol can be implemented by any class.
 
135
*/
 
136
@interface OOWeakRefObject: NSObject <OOWeakReferenceSupport>
 
137
{
 
138
        OOWeakReference         *weakSelf;
 
139
}
 
140
@end