Version 7 of megapkg structure extension

Updated 2005-03-21 15:58:33

George Peter Staplin: Mar 20, 2005 - The megapkg structure extension provides rich structures, and capabilities beyond dict, and arrays. It's available under a BSD-like license.

Tarballs are available from: http://www.xmission.com/~georgeps/implementation/software/megapkg/


Structure creation:

 set s [structure]

Alternatively named structures can be created. This is useful for widgets, and object systems.

 structure name

Structure keys:'

Any characters can be used for keys in a structure (even binary). A custom hash table is used, so the NUL character is valid in keys.

Setting keys in a Structure:

Here are some examples of setting keys:

 $s -foo bar
 $s -key value -anotherkey value

Usage of a named structure is like this:

 structure foo
 foo -key value

Getting key values from a structure:

 set s [structure]
 $s -key value
 # This prints value
 puts [$s -key]

Locking:

The structure extension supports locking of keys, key creation, and structures as a whole.

Global locking

This makes it so that no keys can be created or set, but key values may be retrieved as stated above.

 lock.structure $s

 unlock.structure $s

Locking keys to prevent modification

 lock.structure.keys $s -key1 -key2 ...

 unlock.structure.keys $s -key1 -key2 ...

Locking key creation

It can be useful for the construction of structures that are made available in a public API, to only allow certain keys to be set. By locking key creation new keys will not be created. This helps avoid errors. For example if the user ran the following:

 $s -backgrnd 

instead of:

 $s -background

The unfortunate result would be that -backgrnd is set instead of -background. Locking key creation prevents this error from occuring.

The usage is:

 lock.structure.creation $s

 unlock.structure.creation $s

Key traces

It's possible to setup the evalution of a callback script, whenever a key is modified. If an error occurs in the callback it will be thrown as a background error.

 set s [structure]

 $s -text Hello

 proc text.modified s { puts "You changed -text." }

 trace.structure.key $s -text [list text.modified $s]

It's allowable to free a structure while in a callback. The reference count of a structure is increased before the execution of any callback, and it will be freed upon the return of a callback, if free.structure was called in the callback.

Freeing a structure

After some experiments it was decided that having reference-counted collection of a structure would be difficult to achieve with a custom Tcl_ObjType, and the performance might suffer. It's expected that structures will generally be retained for longer than the scope of a procedure. In cases of a procedure that requires a structure to have limited scope, an array can be used.

The patterns to free structures are:

 set s [structure]
 free.structure $s 

 structure foo
 free.structure foo

By George Peter Staplin


Comments:

So, the purpose of this structure data structure is to have variables which refer to groups of information - similar in to the purpose of Tclx's keyed lists, albeit with a differing syntax? Seems like an interesting idea. One thing I've always thought about was some sort of structure which could be used to map to C structs. I first saw an idea like that in Apple's Dylan language - one could, in their application, just reference a C header and a part of the dylan build environment would dynamically create the appropriate glue connections so that your program could access the data structures (and functions!) appropriately.

George Peter Staplin: TclX's keyed lists sound similar, though I doubt they have locking (I'll check out the code though :). I envision this as useful for creating widgets from the Tcl-level, that could be extended for megawidgets. It's also generic enough for almost anything. I view Tk_ConfigureWidget, and read-only widget structures in C as a limitation. I've implemented a few widgets in the past using gui.so (for exposing drawing, and the Tk C API) with a Tcl object system and multiple inheritance. While it worked, I found it to be overly complex, and then one day this "structure" idea occured to me.


Category Package Category Data Structure