author | Claus Gittinger <cg@exept.de> |
Tue, 01 Jul 1997 15:42:02 +0200 | |
changeset 2739 | 6e9c55027219 |
parent 2307 | 54503a6dbb71 |
child 2910 | 1a3e44b10f9b |
permissions | -rw-r--r-- |
1341 | 1 |
" |
2 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
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 |
||
13 |
Dictionary subclass:#WeakValueDictionary |
|
14 |
instanceVariableNames:'' |
|
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Unordered' |
|
18 |
! |
|
19 |
||
20 |
!WeakValueDictionary class methodsFor:'documentation'! |
|
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
WeakValueDictionaries behave like Dictionaries, |
|
39 |
as long as the values are still referenced by some |
|
40 |
other (non-weak) object. |
|
41 |
However, once the last non-weak reference ceases to exist, |
|
42 |
the Dictionary will return nil for the value at position key. |
|
43 |
(with some delay: it will be removed after the next garbage collect). |
|
44 |
||
2307 | 45 |
[Warning:] |
46 |
If you use this, be very careful since the collections size changes |
|
47 |
'magically' - for example, testing for being nonEmpty and then |
|
48 |
removing the first element may fail, since the element may vanish inbetween. |
|
49 |
In general, never trust the value as returned by the size/isEmpty messages. |
|
2260 | 50 |
|
51 |
||
1341 | 52 |
[author:] |
53 |
Stefan Vogel |
|
54 |
||
55 |
[See also:] |
|
56 |
WeakArray WeakIdentityDictionary WeakIdentitySet |
|
57 |
" |
|
58 |
! ! |
|
59 |
||
60 |
!WeakValueDictionary methodsFor:'adding & removing'! |
|
61 |
||
62 |
at:key put:anObject |
|
63 |
"add the argument anObject under key, aKey to the receiver. |
|
64 |
Return anObject (sigh). |
|
65 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
66 |
are added within interrupting high prio processes." |
|
67 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
68 |
|ret| |
1341 | 69 |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
70 |
(OperatingSystem blockInterrupts) ifTrue:[ |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
71 |
"/ already blocked |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
72 |
^ super at:key put:anObject |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
73 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
74 |
|
1790 | 75 |
[ |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
76 |
ret := super at:key put:anObject. |
1790 | 77 |
] valueNowOrOnUnwindDo:[ |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
78 |
OperatingSystem unblockInterrupts |
1790 | 79 |
]. |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
80 |
^ ret |
1341 | 81 |
|
82 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
83 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1790 | 84 |
"Modified: 20.10.1996 / 14:05:04 / cg" |
1341 | 85 |
! |
86 |
||
87 |
removeKey:aKey ifAbsent:aBlock |
|
88 |
"remove the association under aKey from the collection, |
|
89 |
return the value previously stored there.. |
|
90 |
If it was not in the collection return the result |
|
91 |
from evaluating aBlock. |
|
92 |
||
93 |
Redefined to avoid synchronization problems, in case |
|
94 |
of interrupts (otherwise, there could be some other operation |
|
95 |
on the receiver done by another process, which garbles my contents)." |
|
96 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
97 |
|ret| |
1341 | 98 |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
99 |
(OperatingSystem blockInterrupts) ifTrue:[ |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
100 |
"/ already blocked |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
101 |
^ super removeKey:aKey ifAbsent:aBlock |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
102 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
103 |
|
1341 | 104 |
[ |
105 |
ret := super removeKey:aKey ifAbsent:aBlock |
|
106 |
] valueNowOrOnUnwindDo:[ |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
107 |
OperatingSystem unblockInterrupts |
1341 | 108 |
]. |
109 |
^ ret |
|
110 |
||
111 |
"Modified: 6.5.1996 / 12:44:07 / stefan" |
|
112 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
113 |
! |
|
114 |
||
115 |
removeValue:aKey ifAbsent:aBlock |
|
116 |
"remove the association under aValue from the collection, |
|
1351 | 117 |
return the key previously stored there.. |
1341 | 118 |
If it was not in the collection return the result |
119 |
from evaluating aBlock. |
|
120 |
||
121 |
Redefined to avoid synchronization problems, in case |
|
122 |
of interrupts (otherwise, there could be some other operation |
|
123 |
on the receiver done by another process, which garbles my contents)." |
|
124 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
125 |
|ret| |
1341 | 126 |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
127 |
(OperatingSystem blockInterrupts) ifTrue:[ |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
128 |
"/ already blocked |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
129 |
^ super removeValue:aKey ifAbsent:aBlock |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
130 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
131 |
|
1341 | 132 |
[ |
133 |
ret := super removeValue:aKey ifAbsent:aBlock |
|
134 |
] valueNowOrOnUnwindDo:[ |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
135 |
OperatingSystem unblockInterrupts |
1341 | 136 |
]. |
137 |
^ ret. |
|
138 |
||
139 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1351 | 140 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
1341 | 141 |
! ! |
142 |
||
143 |
!WeakValueDictionary methodsFor:'private'! |
|
144 |
||
145 |
valueContainerOfSize:n |
|
146 |
"return a container for values of size n. |
|
147 |
use WeakArrays here." |
|
148 |
||
149 |
^ WeakArray new:n. |
|
150 |
||
151 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
152 |
! ! |
|
153 |
||
2739 | 154 |
!WeakValueDictionary methodsFor:'testing'! |
155 |
||
156 |
includes:anObject |
|
157 |
"redefined to block interrupts |
|
158 |
(avoid change of the dictionary while accessing)" |
|
159 |
||
160 |
|val| |
|
161 |
||
162 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
163 |
"/ already blocked |
|
164 |
^ super includes:anObject. |
|
165 |
]. |
|
166 |
||
167 |
[ |
|
168 |
val := super includes:anObject. |
|
169 |
] valueNowOrOnUnwindDo:[ |
|
170 |
OperatingSystem unblockInterrupts. |
|
171 |
]. |
|
172 |
^ val |
|
173 |
||
174 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
175 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
176 |
"Created: 1.7.1997 / 15:41:14 / cg" |
|
177 |
! |
|
178 |
||
179 |
includesKey:key |
|
180 |
"redefined to block interrupts |
|
181 |
(avoid change of the dictionary while accessing)" |
|
182 |
||
183 |
|val| |
|
184 |
||
185 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
186 |
"/ already blocked |
|
187 |
^ super includesKey:key. |
|
188 |
]. |
|
189 |
||
190 |
[ |
|
191 |
val := super includesKey:key. |
|
192 |
] valueNowOrOnUnwindDo:[ |
|
193 |
OperatingSystem unblockInterrupts. |
|
194 |
]. |
|
195 |
^ val |
|
196 |
||
197 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
198 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
199 |
"Created: 1.7.1997 / 15:41:32 / cg" |
|
200 |
! ! |
|
201 |
||
1341 | 202 |
!WeakValueDictionary class methodsFor:'documentation'! |
203 |
||
204 |
version |
|
2739 | 205 |
^ '$Header: /cvs/stx/stx/libbasic/WeakValueDictionary.st,v 1.8 1997-07-01 13:42:02 cg Exp $' |
1341 | 206 |
! ! |