Fix too many ]s in try
[tcl-tlc-base.git] / scripts / refcounted.itcl
blobfcb0081c3c826cf9ff5e5a994a036bce87f99f07
1 # vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
3 class tlc::Refcounted {
4 constructor {} {}
5 destructor {}
7 public {
8 method object_registry {varname}
9 method incref {args}
10 method decref {args}
11 method refcount {}
14 protected {
15 variable log_cmd ""
17 method autoscoperef {}
20 private {
21 variable refcount 1
22 variable registry_var
24 method clear_registry {}
25 method reflog {lvl msg args}
30 body tlc::Refcounted::constructor {} { #<<<
33 #>>>
34 body tlc::Refcounted::destructor {} { #<<<
35 clear_registry
38 #>>>
39 body tlc::Refcounted::autoscoperef {} { #<<<
40 reflog debug "Refcounted::constructor callstack: [tlc::stackdump]"
41 upvar 2 _tlc_refcounted_scoperef_[string map {:: //} $this] scopevar
42 set scopevar $this
43 trace variable scopevar u [code $this decref "scopevar unset"]
46 #>>>
47 body tlc::Refcounted::incref {args} { #<<<
48 set old $refcount
49 incr refcount
50 reflog debug "$this: refcount $old -> $refcount ($args)"
53 #>>>
54 body tlc::Refcounted::decref {args} { #<<<
55 set old $refcount
56 incr refcount -1
57 reflog debug "$this: refcount $old -> $refcount ($args)"
58 if {$refcount <= 0} {
59 reflog debug "$this: our time has come"
60 delete object $this
61 return
65 #>>>
66 body tlc::Refcounted::refcount {} { #<<<
67 return $refcount
70 #>>>
71 body tlc::Refcounted::object_registry {varname} { #<<<
72 clear_registry
74 set registry_var $varname
75 upvar $registry_var var_ref
77 set var_ref $this
80 #>>>
81 body tlc::Refcounted::clear_registry {} { #<<<
82 if {[info exists registry_var]} {
83 upvar $registry_var old_registry
84 if {[info exists old_registry]} {
85 unset old_registry
90 #>>>
91 body tlc::Refcounted::reflog {lvl msg args} { #<<<
92 if {$log_cmd ne ""} {
93 uplevel 1 $log_cmd [list $lvl $msg {*}$args]
97 #>>>