[Xotcl] "tie" command

Uwe Zdun uwe.zdun at wu-wien.ac.at
Fri Jan 24 15:49:47 CET 2003


On Friday 24 January 2003 15:09, Kristoffer Lawson wrote:
> On Fri, 24 Jan 2003, Uwe Zdun wrote:
> > nice idea, I've played a bit with var traces to make some tie
> > functionality work; is this what you were thinking of?
>
> Yeah, nice going. Looks like exactly what I had in mind (I reckoned you
> could probably do it with Tcl traces). I thinking that I might almost
> prefer if [tie] was not an object command as it's not really related to
> any specific object (f.ex. using it from outside an object would be
> funny). Or what do you think?

its no big deal to do that: for instance see the code below; the problem with 
this code sample is you have to be sure that the variable is defined one 
level above the trace; if you are not, you'll have to use global variables 
only for ties; or you store all tie variables in a special namespace; such as 
the xotcl namespace.


proc tie {varname objname} {
    upvar $varname v
    set v $objname
    trace variable v wu "tie_cb $objname"    
}
proc tie_cb {tieobj n1 n2 mode} {
    upvar $n1 name
    ## delete the tieobj
    if {[xotcl::Object isobject $tieobj]} {
	$tieobj destroy
    }
    ## delete the trace for this tieobj
    if {$mode == "w"} {
	foreach t [trace vinfo name] {
	    set mode [lindex $t 0]
	    set cmd [lindex $t 1]
	    if {[string equal $cmd "tie_cb $tieobj"]} {
		#puts "delete:  $mode $cmd"
		trace vdelete name $mode $cmd
		#puts [trace vinfo name]
	    }
	}
    }
}

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

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

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

### test unset
unset myVar

puts [Object info instances]


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




More information about the Xotcl mailing list