1# tie_array.tcl -- 2# 3# Data source: Tcl array. 4# 5# Copyright (c) 2004 Andreas Kupries <andreas_kupries@users.sourceforge.net> 6# 7# See the file "license.terms" for information on usage and redistribution 8# of this file, and for a DISCLAIMER OF ALL WARRANTIES. 9# 10# RCS: @(#) $Id: tie_array.tcl,v 1.3 2005/09/28 04:51:24 andreas_kupries Exp $ 11 12# ### ### ### ######### ######### ######### 13## Requisites 14 15package require snit 16package require tie 17 18# ### ### ### ######### ######### ######### 19## Implementation 20 21snit::type ::tie::std::array { 22 23 # ### ### ### ######### ######### ######### 24 ## Specials 25 26 pragma -hastypemethods no 27 pragma -hasinfo no 28 pragma -simpledispatch yes 29 30 # ### ### ### ######### ######### ######### 31 ## API : Construction & Destruction 32 33 constructor {rvar} { 34 # Bring reference to the array into the object scope, 35 # i.e. namespace of the object. This will fail for proc local 36 # variables. This latter is enforced by the core, to prevent 37 # the existence of dangling references to the variable when 38 # the procedure goes away. 39 40 # upvar 3, because we have to skip 3 snit internal levels to 41 # access the callers level. 42 43 if {[catch { 44 upvar 3 $rvar ${selfns}::thesource 45 }]} { 46 return -code error "Illegal use of proc local array variable \"$rvar\"" 47 } 48 49 # Now bring the variable into method scope as well, to check 50 # for its existence. 51 52 variable ${selfns}::thesource 53 54 if {![array exists thesource]} { 55 return -code error "Undefined source array variable \"$rvar\"" 56 } 57 return 58 } 59 60 # ### ### ### ######### ######### ######### 61 ## API : Data source methods 62 63 method get {} { 64 variable ${selfns}::thesource 65 return [array get thesource] 66 } 67 68 method set {dict} { 69 variable ${selfns}::thesource 70 return [array set thesource $dict] 71 } 72 73 method unset {{pattern *}} { 74 variable ${selfns}::thesource 75 array unset thesource $pattern 76 return 77 } 78 79 method names {} { 80 variable ${selfns}::thesource 81 return [array names thesource] 82 } 83 84 method size {} { 85 variable ${selfns}::thesource 86 return [array size thesource] 87 } 88 89 method getv {index} { 90 variable ${selfns}::thesource 91 return $thesource($index) 92 } 93 94 method setv {index value} { 95 variable ${selfns}::thesource 96 set thesource($index) $value 97 return 98 } 99 100 method unsetv {index} { 101 variable ${selfns}::thesource 102 unset thesource($index) 103 return 104 } 105 106 # ### ### ### ######### ######### ######### 107 ## Internal : Instance data 108 109 ## During construction the source array variable is imported into 110 ## the namespace of the object, for direct access through a 111 ## constant name. This also allows a direct reference without 112 ## having to deal with changing stack scopes. This is possible if 113 ## and only if the imported array is a namespaced variable. Proc 114 ## local variables cannot be imported into a namespace in this 115 ## manner. Trying to do so results in an error. 116 117 # ### ### ### ######### ######### ######### 118} 119 120# ### ### ### ######### ######### ######### 121## Ready to go 122 123::tie::register ::tie::std::array as array 124package provide tie::std::array 1.0 125