1 # utils.tcl --
2 #
3 # FIXME: This file needs a description here.
4 #
5 # Copyright (c) 1993-2002 The Regents of the University of California.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # A. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # B. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 # C. Neither the names of the copyright holders nor the names of its
17 # contributors may be used to endorse or promote products derived from this
18 # software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
24 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31
32 package provide mb_utils
33
34 #
35 # if old is found in list, replace it by new
36 # if new is {}, old is deleted.
37 #
38 proc lsubst {list old new} {
39 set ix [lsearch -exact $list $old]
40 if {$ix >= 0} {
41 if {$new!={}} {
42 return [lreplace $list $ix $ix $new]
43 } else {
44 return [lreplace $list $ix $ix]
45 }
46 }
47 }
48
49 # we abstract this so that someday when can move this to C/C++
50 proc removeFirst {varName} {
51 upvar $varName l
52 set f [lindex $l 0]
53 set l [lrange $l 1 end]
54 return $f
55 }
56
57 # experimental superclass for functions that allows callbacks
58 Class Callback
59
60 # does nothing
61 Callback instproc init {} {
62 }
63
64 # register a callback for event
65 # the callback will be executed like: eval $cmd $args
66 Callback instproc add_callback {event cmd} {
67 $self instvar callbacks_
68 lappend callbacks_($event) $cmd
69 }
70
71 #
72 # call a callback on event,
73 # if there is no such callback, nothing is executed
74 # otherwise a callback is issued for each registered callback
75 #
76 Callback instproc callback {event args} {
77 $self instvar callbacks_
78 if [info exists callbacks_($event)] {
79 foreach cmd $callbacks_($event) {
80 puts "callback: [concat $cmd $args]"
81 uplevel #0 $cmd $args
82 }
83 }
84 }
85
86
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.