sets.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 package require Tcl 8.2
00019
00020 namespace ::struct:: {}
00021
00022 # ### ### ### ######### ######### #########
00023 ## Management = of implementations = .
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 ret ::struct::set::LoadAccelerator (type key) {
00037 variable accel
00038 set r 0
00039 switch -exact -- $key {
00040 critcl {
00041 # Critcl implementation of set requires Tcl 8.4.
00042 if {![package vsatisfies [package provide Tcl] 8.4]} {return 0}
00043 if {[catch {package require tcllibc}]} {return 0}
00044 set r [llength [info commands ::struct::set_critcl]]
00045 }
00046 tcl {
00047 variable selfdir
00048 source [file join $selfdir sets_tcl.tcl]
00049 set r 1
00050 }
00051 default {
00052 return -code error "invalid accelerator/impl. package $key:\
00053 must be one of [join [KnownImplementations] {, }]"
00054 }
00055 }
00056 set accel($key) $r
00057 return $r
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 ret ::struct::set::SwitchTo (type key) {
00071 variable accel
00072 variable loaded
00073
00074 if {[string equal $key $loaded]} {
00075 # No change, nothing to do.
00076 return
00077 } elseif {![string equal $key ""]} {
00078 # Validate the target implementation of the switch.
00079
00080 if {![info exists accel($key)]} {
00081 return -code error "Unable to activate unknown implementation \"$key\""
00082 } elseif {![info exists accel($key)] || !$accel($key)} {
00083 return -code error "Unable to activate missing implementation \"$key\""
00084 }
00085 }
00086
00087 # Deactivate the previous implementation, if there was any.
00088
00089 if {![string equal $loaded ""]} {
00090 rename ::struct::set ::struct::set_$loaded
00091 }
00092
00093 # Activate the new implementation, if there is any.
00094
00095 if {![string equal $key ""]} {
00096 rename ::struct::set_$key ::struct::set
00097 }
00098
00099 # Remember the active implementation, for deactivation by future
00100 # switches.
00101
00102 set loaded $key
00103 return
00104 }
00105
00106 ret ::struct::set::Loaded () {
00107 variable loaded
00108 return $loaded
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 ret ::struct::set::Implementations () {
00123 variable accel
00124 set res {}
00125 foreach n [array names accel] {
00126 if {!$accel($n)} continue
00127 lappend res $n
00128 }
00129 return $res
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 ret ::struct::set::KnownImplementations () {
00145 return {critcl tcl}
00146 }
00147
00148 ret ::struct::set::Names () {
00149 return {
00150 critcl {tcllibc based}
00151 tcl {pure Tcl}
00152 }
00153 }
00154
00155
00156
00157
00158 namespace ::struct:: {
00159 variable = selfdir [file dirname [info script]]
00160 variable accel
00161 array accel = {tcl 0 critcl 0}
00162 variable
00163 }
00164
00165
00166
00167
00168
00169
00170 namespace ::struct:: {
00171 variable = e
00172 foreach e [KnownImplementations] {
00173 if {[LoadAccelerator $e]} {
00174 SwitchTo $e
00175 break
00176 }
00177 }
00178 un e =
00179 }
00180
00181
00182
00183
00184 namespace ::struct {
00185
00186 namespace export
00187 }
00188
00189 package = provide struct:: 2.2.1
00190