summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 4ca1312)
raw | patch | inline | side by side (parent: 4ca1312)
author | Shawn O. Pearce <spearce@spearce.org> | |
Sat, 30 Jun 2007 08:34:59 +0000 (04:34 -0400) | ||
committer | Shawn O. Pearce <spearce@spearce.org> | |
Mon, 9 Jul 2007 01:12:45 +0000 (21:12 -0400) |
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>
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>
lib/class.tcl | patch | blob | history |
diff --git a/lib/class.tcl b/lib/class.tcl
index 9d298d0dcc7d305eded58911c3c0758e94bb7ab6..24e8cecea46d3da6d94b04917a2776e541c234f3 100644 (file)
--- a/lib/class.tcl
+++ b/lib/class.tcl
if {[namespace exists $class]} {
error "class $class already declared"
}
- namespace eval $class {
+ namespace eval $class "
variable __nextid 0
variable __sealed 0
variable __field_list {}
proc cb {name args} {
upvar this this
- set args [linsert $args 0 $name $this]
- return [uplevel [list namespace code $args]]
+ concat \[list ${class}::\$name \$this\] \$args
}
- }
+ "
namespace eval $class $body
}
set mbodyc {}
append mbodyc {set this } $class
- append mbodyc {::__o[incr } $class {::__nextid]} \;
- append mbodyc {namespace eval $this {}} \;
+ append mbodyc {::__o[incr } $class {::__nextid]::__d} \;
+ append mbodyc {create_this } $class \;
+ append mbodyc {set __this [namespace qualifiers $this]} \;
if {$__field_list ne {}} {
append mbodyc {upvar #0}
foreach n $__field_list {
set n [lindex $n 0]
- append mbodyc { ${this}::} $n { } $n
- regsub -all @$n\\M $body "\${this}::$n" body
+ append mbodyc { ${__this}::} $n { } $n
+ regsub -all @$n\\M $body "\${__this}::$n" body
}
append mbodyc \;
foreach n $__field_list {
set params [linsert $params 0 this]
set mbodyc {}
+ append mbodyc {set __this [namespace qualifiers $this]} \;
+
switch $deleted {
{} {}
ifdeleted {
- append mbodyc {if {![namespace exists $this]} }
+ append mbodyc {if {![namespace exists $__this]} }
append mbodyc \{ $del_body \; return \} \;
}
default {
if { [regexp -all -- $n\\M $body] == 1
&& [regexp -all -- \\\$$n\\M $body] == 1
&& [regexp -all -- \\\$$n\\( $body] == 0} {
- regsub -all \\\$$n\\M $body "\[set \${this}::$n\]" body
+ regsub -all \
+ \\\$$n\\M $body \
+ "\[set \${__this}::$n\]" body
} else {
- append decl { ${this}::} $n { } $n
- regsub -all @$n\\M $body "\${this}::$n" body
+ append decl { ${__this}::} $n { } $n
+ regsub -all @$n\\M $body "\${__this}::$n" body
}
}
}
namespace eval $class [list proc $name $params $mbodyc]
}
+proc create_this {class} {
+ upvar this this
+ namespace eval [namespace qualifiers $this] [list proc \
+ [namespace tail $this] \
+ [list name args] \
+ "eval \[list ${class}::\$name $this\] \$args" \
+ ]
+}
+
proc delete_this {{t {}}} {
if {$t eq {}} {
upvar this this
set t $this
}
+ set t [namespace qualifiers $t]
if {[namespace exists $t]} {namespace delete $t}
}