|
|
|
ACCESSING ARRAYS AND STRUCTURES With version 2.0 of SoftWish one gets and sets elements of arrays and structures using the access procedure. For example we set the variable, var, to the value of the first element of a 1 dimensional array, myArray, like this: set var [access myArray 0] and we set the 3rd element of myArray to the value of var like this: access myArray 2 $var The access procedure works similarly with structures and unions, the major difference being that keywords are used to specify fields.For structures the keyword takes the form: -fieldname, for unions it takes the form: -unionname.fieldname. Suppose we have a SAA_AttributeValue structure, AttributeValueID. The C definition of an SAA_AttributeValue structure is: typedef struct Then: set valueIndex [ access $AttributeValueID -value_type ]sets the variable valueIndex to the value of the value_type field of the SAA_AttributeValue structure AttributeValueId. Likewise set coordinate [ access $AttributeValueID -u.coord_val ]sets the value of the variable coordinate to the value of the coord_val field of the union u, which is an element of the AttributeValue structure AttributeValueId. The coord_val field is itself a structure, defined in C by: typedef struct{ SAA_CoordSys type; float fval; } SAA_CoordVal; We can get an element of this SAA_CoordVal structure from the union contained in SAA_AttributeValue structure within a single access call, like this: access $AttributeValueID -u.coord_val -fval This is rather like: AttributeValueID.u.coord.fval is in C; and is equivalent to first getting a SAA_CoordVal structure (as we did above when we set the coordinate variable) and then getting a field of that element, like this: access $coordinate -fval The access procedure has a few built-in keywords, names -alloc, -size, -string and --, which are used when dealing with arrays, and -clone which is to duplicate both arrays and structures. Note that this precludes the accessing of structures or unions with fields named alloc, clone, size or string. Earlier version of SoftWish did not have an access function. They used the tag returned by the various SoftWish allocation procedures as procedures which functioned much as the access function does.. For example in earlier versions of SoftWish we would have set var to the value of the first element of the array, myArray, like this: set var [ $myArray 0 ] Similarly set valueIndex [ $AttributeValueID -value_type ] would have been used to get the value of the value_type field of AttributeValueId. This earlier style of getting and setting array elements and structures fields is still supported for compatibility. However this earlier style should no longer be used for two reasons. The first reason is that the new style, using the access procedure, is MUCH faster.The second reason is that we do not guarantee to support the older style indefinitely. Future releases may not support it at all. |
|
copyright Video-Collage Inc. |