/usr/share/doc/nam/examples/tcl/observable.tcl is in nam-examples 1.15-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #
# for MCV
#
Class Observable
Observable instproc init {} {
$self instvar observerlist_
set observerlist_ ""
}
# Adds an observer to the set of observers for this object.
# @param o an observer to be added.
Observable instproc addObserver { o } {
$self instvar observerlist_
set cnt 0
set oid [$o id]
foreach ob $observerlist_ {
set obid [$ob id]
if { $oid == $obid } {
set cnt 1
break;
}
}
if { $cnt == 0 } {
lappend observerlist_ $o
}
}
# Deletes an observer from the set of observers of this object.
# @param o the observer to be deleted.
Observable instproc deleteObserver { o } {
$self instvar observerlist_
set backlist_ ""
set oid [$o id]
foreach ob $observerlist_ {
set obid [$ob id]
if { $oid != $obid } {
lappend backlist_ $ob
} else {
# $o destroy
# leave the work to the application
}
}
set observerlist_ $backlist_
}
# If this object has changed, as indicated by the
# hasChanged method, then notify all of its observers
# and then call the clearChanged method to indicate
# that this object has no longer changed.
#
# Each observer has its update method called with two
# arguments: this observable object and the arg argument
Observable instproc notifyObservers { arg } {
$self instvar observerlist_
#??? Synchronization here before updating ???
foreach ob $observerlist_ {
if ![ catch { $ob info class } ] {
$ob update $arg
}
}
}
# Returns the number of observers of this object.
Observable instproc countObservers {} {
$self instvar observerlist_
set size [llength $observerlist_]
return $size
}
|