View · Search · Index
No registered users in community xowiki
in last 10 minutes

Re: [Xotcl] "tie" command

From: Uwe Zdun <uwe.zdun_at_wu-wien.ac.at>
Date: Fri, 24 Jan 2003 10:32:47 +0100

nice idea, I've played a bit with var traces to make some tie functionality
work; is this what you were thinking of?

--Uwe


####################################################### TIE code

Object instproc tie {varname objname} {
    my set $varname $objname
    my trace variable $varname wu "Object tie_cb $objname [self]"
}
Object proc tie_cb {tieobj obj n1 n2 mode} {
    ## delete the tieobj
    if {[my isobject $tieobj]} {
        $tieobj destroy
    }
    ## delete the trace for this tieobj
    if {$mode == "w" && [my isobject $obj]} {
        foreach t [$obj trace vinfo $n1] {
            set mode [lindex $t 0]
            set cmd [lindex $t 1]
            if {[string equal $cmd "Object tie_cb $tieobj $obj"]} {
                $obj trace vdelete $n1 $mode $cmd
            }
        }
    }
}

####################################################### Test

Object x
x proc test {} {
    set n 0
    while {$n < 10} {
        my tie myVar [Object new]
        incr n
    }
}
x test

### test unset
#x unset myVar
#x set myVar abc

### only one should have survived
puts [Object info instances]

### it gets killed when the obj gets killed
x destroy
puts [Object info instances]

###############################################################



On Friday 24 January 2003 04:32, Kristoffer Lawson wrote:
> Been thinking if this would make sense:
>
> while {$stuff} {
> tie myVar [MyClass new]
> ...
> }
>
> And the instance created from MyClass would be automagically collected on
> each iteration. Ie. [tie] would tie an object to a variable: when the
> variable's value is changed, or when the variable is destroyed, the object
> is too -- or at least the refcount decreased. I often have Message objects
> which are used once (after some data is read from somewhere) and then
> destroyed. Quite often I forget the latter part ;-)
>
> / http://www.fishpool.com/~setok/
>
>
> _______________________________________________
> Xotcl mailing list - Xotcl_at_alice.wu-wien.ac.at
> http://alice.wu-wien.ac.at/mailman/listinfo/xotcl

-- 
Uwe Zdun
Department of Information Systems, Vienna University of Economics
Phone: +43 1 313 36 4796, Fax: +43 1 313 36 746
zdun_at_{xotcl,computer,acm}.org, uwe.zdun_at_wu-wien.ac.at