author | Claus Gittinger <cg@exept.de> |
Wed, 29 Jan 1997 15:29:04 +0100 | |
changeset 2303 | f19df2d4c238 |
parent 2260 | 96d898c2874d |
child 2307 | 54503a6dbb71 |
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 |
||
2260 | 45 |
If you use this, be very careful since the collections size changes |
46 |
'magically' - for example, testing for being nonEmpty and then |
|
47 |
removing the first element may fail, since the element may vanish inbetween. |
|
48 |
In general, never trust the value as returned by the size/isEmpty messages. |
|
49 |
||
50 |
||
1341 | 51 |
[author:] |
52 |
Stefan Vogel |
|
53 |
||
54 |
[See also:] |
|
55 |
WeakArray WeakIdentityDictionary WeakIdentitySet |
|
56 |
" |
|
57 |
! ! |
|
58 |
||
59 |
!WeakValueDictionary methodsFor:'adding & removing'! |
|
60 |
||
61 |
at:key put:anObject |
|
62 |
"add the argument anObject under key, aKey to the receiver. |
|
63 |
Return anObject (sigh). |
|
64 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
65 |
are added within interrupting high prio processes." |
|
66 |
||
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
|
67 |
|ret| |
1341 | 68 |
|
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
|
69 |
(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
|
70 |
"/ 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
|
71 |
^ 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
|
72 |
]. |
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 |
|
1790 | 74 |
[ |
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
|
75 |
ret := super at:key put:anObject. |
1790 | 76 |
] 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
|
77 |
OperatingSystem unblockInterrupts |
1790 | 78 |
]. |
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
|
79 |
^ ret |
1341 | 80 |
|
81 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
82 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1790 | 83 |
"Modified: 20.10.1996 / 14:05:04 / cg" |
1341 | 84 |
! |
85 |
||
86 |
removeKey:aKey ifAbsent:aBlock |
|
87 |
"remove the association under aKey from the collection, |
|
88 |
return the value previously stored there.. |
|
89 |
If it was not in the collection return the result |
|
90 |
from evaluating aBlock. |
|
91 |
||
92 |
Redefined to avoid synchronization problems, in case |
|
93 |
of interrupts (otherwise, there could be some other operation |
|
94 |
on the receiver done by another process, which garbles my contents)." |
|
95 |
||
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
|
96 |
|ret| |
1341 | 97 |
|
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
|
98 |
(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
|
99 |
"/ 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
|
100 |
^ 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
|
101 |
]. |
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 |
|
1341 | 103 |
[ |
104 |
ret := super removeKey:aKey ifAbsent:aBlock |
|
105 |
] 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
|
106 |
OperatingSystem unblockInterrupts |
1341 | 107 |
]. |
108 |
^ ret |
|
109 |
||
110 |
"Modified: 6.5.1996 / 12:44:07 / stefan" |
|
111 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
112 |
! |
|
113 |
||
114 |
removeValue:aKey ifAbsent:aBlock |
|
115 |
"remove the association under aValue from the collection, |
|
1351 | 116 |
return the key previously stored there.. |
1341 | 117 |
If it was not in the collection return the result |
118 |
from evaluating aBlock. |
|
119 |
||
120 |
Redefined to avoid synchronization problems, in case |
|
121 |
of interrupts (otherwise, there could be some other operation |
|
122 |
on the receiver done by another process, which garbles my contents)." |
|
123 |
||
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
|
124 |
|ret| |
1341 | 125 |
|
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
|
126 |
(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
|
127 |
"/ 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
|
128 |
^ 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
|
129 |
]. |
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 |
|
1341 | 131 |
[ |
132 |
ret := super removeValue:aKey ifAbsent:aBlock |
|
133 |
] 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
|
134 |
OperatingSystem unblockInterrupts |
1341 | 135 |
]. |
136 |
^ ret. |
|
137 |
||
138 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1351 | 139 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
1341 | 140 |
! ! |
141 |
||
142 |
!WeakValueDictionary methodsFor:'private'! |
|
143 |
||
144 |
valueContainerOfSize:n |
|
145 |
"return a container for values of size n. |
|
146 |
use WeakArrays here." |
|
147 |
||
148 |
^ WeakArray new:n. |
|
149 |
||
150 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
151 |
! ! |
|
152 |
||
153 |
!WeakValueDictionary class methodsFor:'documentation'! |
|
154 |
||
155 |
version |
|
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
|
156 |
^ '$Header: /cvs/stx/stx/libbasic/WeakValueDictionary.st,v 1.6 1997-01-29 14:29:04 cg Exp $' |
1341 | 157 |
! ! |