@matheusbsbs / matheus-bagliano / commits / 6233ab1729
Fork 0

git-gui: Teach class system to support [$this cmd] syntax

Its handy to be able to ask an object to do something for you by handing it a subcommand. For example if we want to get the value of an object's private field the object could expose a method that would return that value. Application level code can then invoke "$inst get" to perform the method call. Tk uses this pattern for all of its widgets, so we'd certainly like to use it for our own mega-widgets that we might develop. Up until now we haven't needed such functionality, but I'm working on a new revision picker mega-widget that would benefit from it. To make this work we have to change the definition of $this to actually be a procedure within the namespace. By making $this a procedure any caller that has $this can call subcommands by passing them as the first argument to $this. That subcommand then needs to call the proper subroutine. Placing the dispatch procedure into the object's variable namespace ensures that it will always be deleted when the object is deleted. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jun 30, 2007 at 04:34 UTC 6233ab17297684c0049923cb8492393276672b01
1 file changed +26 -12
lib/class.tcl
+26 -12
@@ -5,7 +5,7 @@ proc class {class body} {
5 if {[namespace exists $class]} {
6 error "class $class already declared"
7 }
8 - namespace eval $class {
8 + namespace eval $class "
9 variable __nextid 0
10 variable __sealed 0
11 variable __field_list {}
@@ -13,10 +13,9 @@ proc class {class body} {
13
14 proc cb {name args} {
15 upvar this this
16 - set args [linsert $args 0 $name $this]
17 - return [uplevel [list namespace code $args]]
16 + concat \[list ${class}::\$name \$this\] \$args
17 }
19 - }
18 + "
19 namespace eval $class $body
20 }
21
@@ -51,15 +50,16 @@ proc constructor {name params body} {
50 set mbodyc {}
51
52 append mbodyc {set this } $class
54 - append mbodyc {::__o[incr } $class {::__nextid]} \;
55 - append mbodyc {namespace eval $this {}} \;
53 + append mbodyc {::__o[incr } $class {::__nextid]::__d} \;
54 + append mbodyc {create_this } $class \;
55 + append mbodyc {set __this [namespace qualifiers $this]} \;
56
57 if {$__field_list ne {}} {
58 append mbodyc {upvar #0}
59 foreach n $__field_list {
60 set n [lindex $n 0]
61 - append mbodyc { ${this}::} $n { } $n
62 - regsub -all @$n\\M $body "\${this}::$n" body
61 + append mbodyc { ${__this}::} $n { } $n
62 + regsub -all @$n\\M $body "\${__this}::$n" body
63 }
64 append mbodyc \;
65 foreach n $__field_list {
@@ -80,10 +80,12 @@ proc method {name params body {deleted {}} {del_body {}}} {
80 set params [linsert $params 0 this]
81 set mbodyc {}
82
83 + append mbodyc {set __this [namespace qualifiers $this]} \;
84 +
85 switch $deleted {
86 {} {}
87 ifdeleted {
86 - append mbodyc {if {![namespace exists $this]} }
88 + append mbodyc {if {![namespace exists $__this]} }
89 append mbodyc \{ $del_body \; return \} \;
90 }
91 default {
@@ -98,10 +100,12 @@ proc method {name params body {deleted {}} {del_body {}}} {
100 if { [regexp -all -- $n\\M $body] == 1
101 && [regexp -all -- \\\$$n\\M $body] == 1
102 && [regexp -all -- \\\$$n\\( $body] == 0} {
101 - regsub -all \\\$$n\\M $body "\[set \${this}::$n\]" body
103 + regsub -all \
104 + \\\$$n\\M $body \
105 + "\[set \${__this}::$n\]" body
106 } else {
103 - append decl { ${this}::} $n { } $n
104 - regsub -all @$n\\M $body "\${this}::$n" body
107 + append decl { ${__this}::} $n { } $n
108 + regsub -all @$n\\M $body "\${__this}::$n" body
109 }
110 }
111 }
@@ -112,11 +116,21 @@ proc method {name params body {deleted {}} {del_body {}}} {
116 namespace eval $class [list proc $name $params $mbodyc]
117 }
118
119 +proc create_this {class} {
120 + upvar this this
121 + namespace eval [namespace qualifiers $this] [list proc \
122 + [namespace tail $this] \
123 + [list name args] \
124 + "eval \[list ${class}::\$name $this\] \$args" \
125 + ]
126 +}
127 +
128 proc delete_this {{t {}}} {
129 if {$t eq {}} {
130 upvar this this
131 set t $this
132 }
133 + set t [namespace qualifiers $t]
134 if {[namespace exists $t]} {namespace delete $t}
135 }
136