tie_array.tcl

Go to the documentation of this file.
00001 /*  tie_array.tcl --*/
00002 /* */
00003 /*  Data source: Tcl array.*/
00004 /* */
00005 /*  Copyright (c) 2004 Andreas Kupries <andreas_kupries@users.sourceforge.net>*/
00006 /* */
00007 /*  See the file "license.terms" for information on usage and redistribution*/
00008 /*  of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00009 /*  */
00010 /*  RCS: @(#) $Id: tie_array.tcl,v 1.3 2005/09/28 04:51:24 andreas_kupries Exp $*/
00011 
00012 /*  ### ### ### ######### ######### #########*/
00013 /*  Requisites*/
00014 
00015 package require snit
00016 package require tie
00017 
00018 /*  ### ### ### ######### ######### #########*/
00019 /*  Implementation*/
00020 
00021 snit::type ::tie::std::array {
00022 
00023     /*  ### ### ### ######### ######### #########*/
00024     /*  Specials*/
00025 
00026     pragma -hastyperet s no
00027     pragma -hasinfo        no
00028     pragma -simpledispatch yes
00029 
00030     # ### ### ### ######### ######### #########
00031     ## API : Construction & Destruction
00032 
00033     constructor (type rvar) {
00034     # Bring reference to the array into the object scope,
00035     # i.e. namespace of the object. This will fail for proc local
00036     # variables. This latter is enforced by the core, to prevent
00037     # the existence of dangling references to the variable when
00038     # the procedure goes away.
00039 
00040     # upvar 3, because we have to skip 3 snit internal levels to
00041     # access the callers level.
00042 
00043     if {[catch {
00044         upvar 3 $rvar ${selfns}::thesource
00045     }]} {
00046         return -code error "Illegal use of proc local array variable \"$rvar\""
00047     }
00048 
00049     # Now bring the variable into method scope as well, to check
00050     # for its existence.
00051 
00052     variable ${selfns}::thesource
00053 
00054     if {![array exists thesource]} {
00055         return -code error "Undefined source array variable \"$rvar\""
00056     }
00057     return
00058     }
00059 
00060     /*  ### ### ### ######### ######### #########*/
00061     /*  API : Data source methods*/
00062 
00063     ret  get () {
00064     variable ${selfns}::thesource
00065     return [array get thesource]
00066     }
00067 
00068     ret  set (type dict) {
00069     variable ${selfns}::thesource
00070     return [array set thesource $dict]
00071     }
00072 
00073     ret  unset (optional pattern =*) {
00074     variable ${selfns}::thesource
00075     array unset thesource $pattern
00076     return
00077     }
00078 
00079     ret  names () {
00080     variable ${selfns}::thesource
00081     return [array names thesource]
00082     }
00083 
00084     ret  size () {
00085     variable ${selfns}::thesource
00086     return [array size thesource]
00087     }
00088 
00089     ret  getv (type index) {
00090     variable ${selfns}::thesource
00091     return $thesource($index)
00092     }
00093 
00094     ret  setv (type index , type value) {
00095     variable ${selfns}::thesource
00096     set thesource($index) $value
00097     return
00098     }
00099 
00100     ret  unsetv (type index) {
00101     variable ${selfns}::thesource
00102     unset thesource($index)
00103     return
00104     }
00105 
00106     /*  ### ### ### ######### ######### #########*/
00107     /*  Internal : Instance data*/
00108 
00109     /*  During construction the source array variable is imported into*/
00110     /*  the namespace of the object, for direct access through a*/
00111     /*  constant name. This also allows a direct reference without*/
00112     /*  having to deal with changing stack scopes. This is possible if*/
00113     /*  and only if the imported array is a namespaced variable. Proc*/
00114     /*  local variables cannot be imported into a namespace in this*/
00115     /*  manner. Trying to do so results in an error.*/
00116 
00117     /*  ### ### ### ######### ######### #########*/
00118 }
00119 
00120 /*  ### ### ### ######### ######### #########*/
00121 /*  Ready to go*/
00122 
00123 ::tie::register ::tie::std::array as array
00124 package provide   tie::std::array 1.0
00125 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1