ValueHolderWithWeakDependents.st
author Claus Gittinger <cg@exept.de>
Wed, 03 Dec 2003 12:48:56 +0100
changeset 1869 c6800c75b7d0
parent 1803 d69cb54a3b56
child 2950 4527bf8b1d19
permissions -rw-r--r--
resource file handling cleaned up
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1803
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
     1
"
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
     2
 COPYRIGHT (c) 1999 by eXept Software AG
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
     3
              All Rights Reserved
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
     4
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
     5
 This software is furnished under a license and may be used
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
     6
 only in accordance with the terms of that license and with the
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
     8
 be provided or otherwise made available to, or used by, any
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
     9
 other person.  No title to or ownership of the software is
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    10
 hereby transferred.
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    11
"
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    12
1557
e5e78ee63670 #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 1257
diff changeset
    13
"{ Package: 'stx:libview2' }"
e5e78ee63670 #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 1257
diff changeset
    14
1257
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
ValueHolder subclass:#ValueHolderWithWeakDependents
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	instanceVariableNames:''
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	classVariableNames:''
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	poolDictionaries:''
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	category:'Interface-Support-Models'
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
!
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
1803
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    22
!ValueHolderWithWeakDependents class methodsFor:'documentation'!
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    23
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    24
copyright
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    25
"
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    26
 COPYRIGHT (c) 1999 by eXept Software AG
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    27
              All Rights Reserved
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    28
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    29
 This software is furnished under a license and may be used
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    30
 only in accordance with the terms of that license and with the
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    32
 be provided or otherwise made available to, or used by, any
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    33
 other person.  No title to or ownership of the software is
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    34
 hereby transferred.
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    35
"
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
    36
! !
1257
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
!ValueHolderWithWeakDependents methodsFor:'dependents access'!
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
addDependent:anObject
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    "make the argument, anObject be a weak dependent of the receiver"
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    |wasBlocked|
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
    wasBlocked := OperatingSystem blockInterrupts.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
    [
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
        |deps dep|
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
        deps := dependents.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
        "/
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
        "/ store the very first dependent directly in
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
        "/ the dependents instVar
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
        "/
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
        (deps isNil and:[anObject isCollection not]) ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
            dependents := WeakArray with:anObject
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
        ] ifFalse:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
            deps class == WeakArray ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
                dep := deps at:1.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
                dep ~~ anObject ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
                    (dep isNil or:[dep == 0]) ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
                        deps at:1 put:anObject
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
                    ] ifFalse:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
                        self dependents:(WeakIdentitySet with:dep with:anObject)
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
                    ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
                ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
            ] ifFalse:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
                deps add:anObject
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
            ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
        ]
1557
e5e78ee63670 #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 1257
diff changeset
    70
    ] ensure:[
1257
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
        wasBlocked ifFalse:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
            OperatingSystem unblockInterrupts
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
        ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
    ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    "Modified: 8.1.1997 / 23:40:30 / cg"
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
!
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
dependentsDo:aBlock
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
    "evaluate aBlock for all of my dependents"
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    |deps|
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    deps := dependents.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
    deps size ~~ 0 ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
        deps do:[:d | 
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
                    (d notNil and:[d ~~ 0]) ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
                        aBlock value:d
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
                    ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
                ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    ].
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
!
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
removeDependent:anObject
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
    "make the argument, anObject be independent of the receiver"
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
    |wasBlocked|
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    "/ must do this save from interrupts, since the dependents collection
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
    "/ is possibly accessed from multiple threads.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
    "/ Used to use #valueUninterruptably here; inlined that code for slightly
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
    "/ faster execution.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
    wasBlocked := OperatingSystem blockInterrupts.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
    [
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
        |deps n d|
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
        deps := dependents.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
        deps size ~~ 0 ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
            "/ to save a fair amount of memory in case of
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
            "/ many dependencies, we store a single dependent in
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
            "/ a WeakArray, and switch to a WeakSet if more dependents are
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
            "/ added. Here we have to do the inverse ...
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
            deps class == WeakArray ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
                ((d := deps at:1) == anObject 
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
                or:[d isNil
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
                or:[d == 0]]) ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
                    dependents := nil
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
                ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
            ] ifFalse:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
                deps remove:anObject ifAbsent:[].
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
                (n := deps size) == 0 ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
                    dependents := nil
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
                ] ifFalse:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
                    n == 1 ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
                        d := deps firstIfEmpty:nil.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
                        d notNil ifTrue:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
                            deps := WeakArray with:d
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
                        ] ifFalse:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
                            deps := nil
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
                        ].
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
                        dependents := deps.
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
                    ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
                ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
            ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
        ]
1557
e5e78ee63670 #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 1257
diff changeset
   140
    ] ensure:[
1257
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
        wasBlocked ifFalse:[
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
            OperatingSystem unblockInterrupts
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
        ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
    ]
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
    "Modified: / 26.1.1998 / 19:51:50 / cg"
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
! !
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
!ValueHolderWithWeakDependents class methodsFor:'documentation'!
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
version
1803
d69cb54a3b56 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1557
diff changeset
   152
    ^ '$Header: /cvs/stx/stx/libview2/ValueHolderWithWeakDependents.st,v 1.3 2003-08-30 12:37:19 cg Exp $'
1257
6c1eab277273 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
! !