4347
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
6233
|
13 |
"{ Package: 'stx:libbasic' }"
|
|
14 |
|
4347
|
15 |
IdentityDictionary subclass:#WeakValueIdentityDictionary
|
|
16 |
instanceVariableNames:''
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
4350
|
19 |
category:'Collections-Weak'
|
4347
|
20 |
!
|
|
21 |
|
|
22 |
!WeakValueIdentityDictionary class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
27 |
All Rights Reserved
|
|
28 |
|
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
|
37 |
|
|
38 |
documentation
|
|
39 |
"
|
|
40 |
WeakValueIdentityDictionaries behave like IdentityDictionaries,
|
|
41 |
as long as the values are still referenced by some
|
|
42 |
other (non-weak) object.
|
|
43 |
However, once the last non-weak reference ceases to exist,
|
|
44 |
the Dictionary will return nil for the value at position key.
|
|
45 |
(with some delay: it will be removed after the next garbage collect).
|
|
46 |
|
|
47 |
[Warning:]
|
|
48 |
If you use this, be very careful since the collections size changes
|
|
49 |
'magically' - for example, testing for being nonEmpty and then
|
|
50 |
removing the first element may fail, since the element may vanish inbetween.
|
|
51 |
In general, never trust the value as returned by the size/isEmpty messages.
|
|
52 |
|
|
53 |
|
|
54 |
[author:]
|
|
55 |
Claus (stolen from Stefans WeakValueDictionary)
|
|
56 |
|
|
57 |
[See also:]
|
|
58 |
WeakArray WeakIdentityDictionary WeakIdentitySet WeakValueDictionary
|
|
59 |
"
|
|
60 |
! !
|
|
61 |
|
|
62 |
!WeakValueIdentityDictionary methodsFor:'adding & removing'!
|
|
63 |
|
|
64 |
at:key put:anObject
|
|
65 |
"add the argument anObject under key, aKey to the receiver.
|
|
66 |
Return anObject (sigh).
|
|
67 |
Redefined to block interrupts, to avoid trouble when dependencies
|
|
68 |
are added within interrupting high prio processes."
|
|
69 |
|
|
70 |
|ret|
|
|
71 |
|
|
72 |
(OperatingSystem blockInterrupts) ifTrue:[
|
|
73 |
"/ already blocked
|
|
74 |
^ super at:key put:anObject
|
|
75 |
].
|
|
76 |
|
|
77 |
[
|
|
78 |
ret := super at:key put:anObject.
|
|
79 |
] valueNowOrOnUnwindDo:[
|
|
80 |
OperatingSystem unblockInterrupts
|
|
81 |
].
|
|
82 |
^ ret
|
|
83 |
|
|
84 |
"Modified: 6.5.1996 / 12:22:26 / stefan"
|
|
85 |
"Created: 6.5.1996 / 14:47:37 / stefan"
|
|
86 |
"Modified: 20.10.1996 / 14:05:04 / cg"
|
|
87 |
!
|
|
88 |
|
|
89 |
removeKey:aKey ifAbsent:aBlock
|
|
90 |
"remove the association under aKey from the collection,
|
|
91 |
return the value previously stored there.
|
|
92 |
If it was not in the collection return the result
|
|
93 |
from evaluating aBlock.
|
|
94 |
|
|
95 |
Redefined to avoid synchronization problems, in case
|
|
96 |
of interrupts (otherwise, there could be some other operation
|
|
97 |
on the receiver done by another process, which garbles my contents)."
|
|
98 |
|
|
99 |
|ret|
|
|
100 |
|
|
101 |
(OperatingSystem blockInterrupts) ifTrue:[
|
|
102 |
"/ already blocked
|
|
103 |
^ super removeKey:aKey ifAbsent:aBlock
|
|
104 |
].
|
|
105 |
|
|
106 |
[
|
|
107 |
ret := super removeKey:aKey ifAbsent:aBlock
|
|
108 |
] valueNowOrOnUnwindDo:[
|
|
109 |
OperatingSystem unblockInterrupts
|
|
110 |
].
|
|
111 |
^ ret
|
|
112 |
|
|
113 |
"Modified: 6.5.1996 / 12:44:07 / stefan"
|
|
114 |
"Created: 6.5.1996 / 14:47:37 / stefan"
|
|
115 |
!
|
|
116 |
|
|
117 |
removeValue:aKey ifAbsent:aBlock
|
|
118 |
"remove the association under aValue from the collection,
|
|
119 |
return the key previously stored there.
|
|
120 |
If it was not in the collection return the result
|
|
121 |
from evaluating aBlock.
|
|
122 |
|
|
123 |
Redefined to avoid synchronization problems, in case
|
|
124 |
of interrupts (otherwise, there could be some other operation
|
|
125 |
on the receiver done by another process, which garbles my contents)."
|
|
126 |
|
|
127 |
|ret|
|
|
128 |
|
|
129 |
(OperatingSystem blockInterrupts) ifTrue:[
|
|
130 |
"/ already blocked
|
|
131 |
^ super removeValue:aKey ifAbsent:aBlock
|
|
132 |
].
|
|
133 |
|
|
134 |
[
|
|
135 |
ret := super removeValue:aKey ifAbsent:aBlock
|
|
136 |
] valueNowOrOnUnwindDo:[
|
|
137 |
OperatingSystem unblockInterrupts
|
|
138 |
].
|
|
139 |
^ ret.
|
|
140 |
|
|
141 |
"Created: 6.5.1996 / 14:47:37 / stefan"
|
|
142 |
"Modified: 8.5.1996 / 14:54:09 / stefan"
|
|
143 |
! !
|
|
144 |
|
|
145 |
!WeakValueIdentityDictionary methodsFor:'element disposal'!
|
|
146 |
|
|
147 |
update:something with:aParameter from:changedObject
|
|
148 |
"an element (either key or value) died - clear out slots for
|
|
149 |
disposed keys."
|
|
150 |
|
|
151 |
|wasBlocked|
|
|
152 |
|
|
153 |
something == #ElementExpired ifTrue:[
|
|
154 |
"
|
|
155 |
have to block here - dispose may be done at a low priority
|
|
156 |
from the background finalizer. If new items are added by a
|
|
157 |
higher prio process, the dictionary might get corrupted otherwise
|
|
158 |
"
|
|
159 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
160 |
|
|
161 |
keyArray
|
|
162 |
forAllDeadIndicesDo:[:idx |
|
|
163 |
valueArray basicAt:idx put:nil.
|
|
164 |
tally := tally - 1.
|
|
165 |
]
|
|
166 |
replacingCorpsesWith:DeletedEntry.
|
|
167 |
|
|
168 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
169 |
]
|
|
170 |
|
|
171 |
"Created: 7.1.1997 / 16:59:30 / stefan"
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
! !
|
|
177 |
|
|
178 |
!WeakValueIdentityDictionary methodsFor:'private'!
|
|
179 |
|
6233
|
180 |
slotIsEmpty:probe
|
|
181 |
^ probe isNil or:[probe == 0 "collected"]
|
|
182 |
!
|
|
183 |
|
4347
|
184 |
valueContainerOfSize:n
|
|
185 |
"return a container for values of size n.
|
|
186 |
use WeakArrays here."
|
|
187 |
|
|
188 |
|a|
|
|
189 |
|
|
190 |
a := WeakArray new:n.
|
|
191 |
a addDependent:self.
|
|
192 |
^ a
|
|
193 |
|
|
194 |
"Created: 6.5.1996 / 14:47:37 / stefan"
|
|
195 |
! !
|
|
196 |
|
|
197 |
!WeakValueIdentityDictionary methodsFor:'testing'!
|
|
198 |
|
|
199 |
includes:anObject
|
|
200 |
"redefined to block interrupts
|
|
201 |
(avoid change of the dictionary while accessing)"
|
|
202 |
|
|
203 |
|val|
|
|
204 |
|
|
205 |
(OperatingSystem blockInterrupts) ifTrue:[
|
|
206 |
"/ already blocked
|
|
207 |
^ super includes:anObject.
|
|
208 |
].
|
|
209 |
|
|
210 |
[
|
|
211 |
val := super includes:anObject.
|
|
212 |
] valueNowOrOnUnwindDo:[
|
|
213 |
OperatingSystem unblockInterrupts.
|
|
214 |
].
|
|
215 |
^ val
|
|
216 |
|
|
217 |
"Modified: 6.5.1996 / 12:22:26 / stefan"
|
|
218 |
"Modified: 1.7.1997 / 10:45:52 / cg"
|
|
219 |
"Created: 1.7.1997 / 15:41:14 / cg"
|
|
220 |
!
|
|
221 |
|
|
222 |
includesKey:key
|
|
223 |
"redefined to block interrupts
|
|
224 |
(avoid change of the dictionary while accessing)"
|
|
225 |
|
|
226 |
|val|
|
|
227 |
|
|
228 |
(OperatingSystem blockInterrupts) ifTrue:[
|
|
229 |
"/ already blocked
|
|
230 |
^ super includesKey:key.
|
|
231 |
].
|
|
232 |
|
|
233 |
[
|
|
234 |
val := super includesKey:key.
|
|
235 |
] valueNowOrOnUnwindDo:[
|
|
236 |
OperatingSystem unblockInterrupts.
|
|
237 |
].
|
|
238 |
^ val
|
|
239 |
|
|
240 |
"Modified: 6.5.1996 / 12:22:26 / stefan"
|
|
241 |
"Modified: 1.7.1997 / 10:45:52 / cg"
|
|
242 |
"Created: 1.7.1997 / 15:41:32 / cg"
|
9242
|
243 |
!
|
|
244 |
|
|
245 |
isWeakCollection
|
|
246 |
"return true, if the receiver has weak references to its elements."
|
|
247 |
|
|
248 |
^ true
|
4347
|
249 |
! !
|
|
250 |
|
|
251 |
!WeakValueIdentityDictionary class methodsFor:'documentation'!
|
|
252 |
|
|
253 |
version
|
9242
|
254 |
^ '$Header: /cvs/stx/stx/libbasic/WeakValueIdentityDictionary.st,v 1.4 2006-03-06 10:04:38 cg Exp $'
|
4347
|
255 |
! !
|