| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 
 | [comment {-*- tcl -*-}]
[manpage_begin struct::record n 1.2.1]
[keywords {data structures}]
[keywords record]
[keywords struct]
[copyright {2002, Brett Schwarz <brett_schwarz@yahoo.com>}]
[moddesc   {Tcl Data Structures}]
[titledesc {Define and create records (similar to 'C' structures)}]
[category  {Data structures}]
[require Tcl 8.2]
[require struct::record [opt 1.2.1]]
[description]
The [cmd ::struct::record] package provides a mechanism to group variables together
as one data structure, similar to a 'C' structure. The members of a
record can be variables or other records. However, a record can not contain circular
record, i.e. records that contain the same record as a
member.
[para]
This package was structured so that it is very similar to how Tk objects work. Each record
definition creates a record object that encompasses that definition. Subsequently, that
record object can create instances of that record. These instances can then
be manipulated with the [method cget] and [method configure] methods.
[para]
The package only contains one top level command, but several sub commands (see below). It also obeys the namespace in which the record was define, hence the objects returned are fully qualified.
[list_begin definitions]
[call [cmd {record define}] [arg recordName] [arg recordMembers] [opt [arg "instanceName1 instanceName2 ..."]]]
Defines a record. [arg recordName] is the name of the record, and is also
used as an object command. This object command is used to create instances of the
record definition. [arg recordMembers] are the members of
the record that make up the record definition. These are variables
and other record. If optional [arg instanceName] args are given, then an instance
is generated after the definition is created for each [arg instanceName].
[call [cmd {record show}] [arg record]]
Returns a list of records that have been defined.
[call [cmd {record show}] [arg instances] [arg recordName]]
Returns the instances that have been instantiated by
[arg recordName].
[call [cmd {record show}] [arg members] [arg recordName]]
Returns the members that are defined for
record [arg recordName]. It returns the same format as how the
records were defined.
[call [cmd {record show}] [arg values] [arg instanceName]]
Returns a list of values that are set for the instance
[arg instanceName]. The output is a list of key/value pairs. If there
are nested records, then the values of the nested records will
itself be a list.
[call [cmd {record exists}] [arg record] [arg recordName]]
Tests for the existence of a [arg record] with the
name [arg recordName].
[call [cmd {record exists}] [arg instance] [arg instanceName]]
Tests for the existence of a [arg instance] with the
name [arg instanceName].
[call [cmd {record delete}] [arg record] [arg recordName]]
Deletes [arg recordName], and all instances of [arg recordName]. It will return
an error if the record does not exist.
[call [cmd {record delete}] [arg instance] [arg instanceName]]
Deletes [arg instance] with the name of [arg instanceName]. It
will return an error if the instance does not exist.
[list_end]
[para]
[section {RECORD MEMBERS}]
Record members can either be variables, or other records, However, the
same record can not be nested witin itself (circular). To define a
nested record, you need to specify the [const record] keyword, along
the with name of the record, and the name of the instance of that
nested record. For example, it would look like this:
[para]
[example_begin]
# this is the nested record
record define mynestedrecord {
    nest1
    nest2
}
# This is the main record
record define myrecord {
    mem1
    mem2
    {record mynestedrecord mem3}
}
[example_end]
You can also assign default or initial values to the members of a record,
by enclosing the member entry in braces:
[para]
[example_begin]
record define myrecord {
    mem1
    {mem2 5}
}
[example_end]
All instances created from this record definition, will initially have 5 as
the value for [arg mem2]. If no default is given, then the value will be the empty string.
[para]
[emph {Getting Values}]
[para]
To get a value of a member, there are several ways to do this.
[list_begin enumerated]
[enum]
To get a member value, then use the instance built-in [method cget] method:
[para]
    [arg instanceName] [method cget] -mem1
[enum]
To get multiple member values, you can specify them all in one command:
[para]
    [arg instanceName] [method cget] -mem1 -mem2
[enum]
To get a list of the key/value of all of the members, there are 3 ways:
[para]
    - [arg instanceName] [method cget]
[para]
	- [arg instanceName] [method configure]
[para]
	- [arg instanceName]
[enum]
To get a value of a nested member, then use the dot notation:
[para]
    [arg instanceName] [method cget] -mem3.nest1
[list_end]
[para]
[emph {Setting Values}]
[para]
To set a value of a member, there are several ways to do this.
[list_begin enumerated]
[enum]
To set a member value, then use the instance built-in [method configure] method:
[para]
    [arg instanceName] [method configure] -mem1 val1
[enum]
To set multiple member values, you can specify them all in one command:
[para]
    [arg instanceName] [method configure] -mem1 va1 -mem2 val2
[enum]
To set a value of a nested member, then use the dot notation:
[para]
    [arg instanceName] [method configure] -mem3.nest1 value
[list_end]
[para]
[emph {Alias access}]
[para]
In the original implementation, access was done by using dot notation similar to how 'C' structures are accessed. However,
there was a concensus to make the interface more Tcl like, which made sense. However, the original alias access still
exists. It might prove to be helpful to some.
[para]
Basically, for every member of every instance, an alias is created. This alias is used to get and set values for that
member. An example will illustrate the point, using the above defined records:
[para]
[example_begin]
# Create an instance first
% myrecord inst1
::inst1
% # To get a member of an instance, just use the
% # alias (it behaves like a Tcl command):
% inst1.mem1
%
% # To set a member via the alias, just include
% # a value (optionally the equal sign - syntactic sugar)
% inst1.mem1 = 5
5
% inst1.mem1
5
% # For nested records, just continue with the
% # dot notation (note no equal sign)
% inst1.mem3.nest1 10
10
% inst1.mem3.nest1
10
% # just the instance by itself gives all
% # member/values pairs for that instance
% inst1
-mem1 5 -mem2 {} -mem3 {-nest1 10 -nest2 {}}
% # and to get all members within the nested record
% inst1.mem3
-nest1 10 -nest2 {}
%
[example_end]
[section {RECORD COMMAND}]
The following subcommands and corresponding arguments are available to any
record command:
[list_begin definitions]
[call [arg recordName] [method [arg instanceName|#auto]] [opt [arg "-member1 value1 -member2 value2 ..."]]]
Using the [arg recordName] object command that was created from the record definition,
instances of the record definition can be created. Once a instance is
created, then it inherits the members of the record definition, very
similar to how objects work. During instance generation, an object command for the instance
is created as well, using [arg instanceName]. This object command is used
to access the data members of the instance. During the instantiation, values for
that instance can be given, [emph but] all values must be given, and be given
in key/value pairs. Nested records, need to be in list format.
[para]
Optionally, [arg #auto] can be used in place of [arg instanceName]. When #auto is used,
then a instance name will automatically be generated, of the form recordName<integer>, where
<integer> is a unique integer (starting at 0) that is generated.
[list_end]
[para]
[section {INSTANCE COMMAND}]
The following subcommands and corresponding arguments are available to
any record instance command:
[list_begin definitions]
[call [arg instanceName] [method cget] [opt [arg "-member1 -member2 ..."]]]
Each instance has the sub command [method cget] associated with it. This
is very similar to how Tk widget's cget command works. It queries
the values of the member for that particular instance. If
no arguments are given, then a key/value list is returned.
[call [arg instanceName] [method configure] [opt [arg "-member1 value1 -member2 value2 ..."]]]
Each instance has the sub command [method configure] associated with it. This
is very similar to how Tk widget's configure command works. It sets
the values of the particular member for that particular instance. If
no arguments are given, then a key/value list is returned.
[list_end]
[section EXAMPLES]
Two examples are provided to give an good illustration on how to use
this package.
[para]
[emph {Example 1}]
[para]
Probably the most obvious example would be to hold contact information,
such as addresses, phone numbers, comments, etc. Since a person can have
multiple phone numbers, multiple email addresses, etc, we will use nested
records to define these. So, the first thing we do is define the nested
records:
[para]
[example {
##
##  This is an interactive example, to see what is
##  returned by each command as well.
##
% namespace import ::struct::record::*
% # define a nested record. Notice that country has default 'USA'.
% record define locations {
    street
    street2
    city
    state
    zipcode
    {country USA}
    phone
}
::locations
% # Define the main record. Notice that it uses the location record twice.
% record define contacts {
    first
    middle
    last
    {record locations home}
    {record locations work}
}
::contacts
% # Create an instance for the contacts record.
% contacts cont1
::cont1
% # Display some introspection values
% record show records
::contacts ::locations
% #
% record show values cont1
-first {} -middle {} -last {} -home {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}} -work {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}}
% #
% record show instances contacts
::cont1
% #
% cont1 config
-first {} -middle {} -last {} -home {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}} -work {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}}
% #
% cont1 cget
-first {} -middle {} -last {} -home {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}} -work {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}}
% # copy one record to another record
% record define contacts2 [record show members contacts]
::contacts2
% record show members contacts2
first middle last {record locations home} {record locations work}
% record show members contacts
first middle last {record locations home} {record locations work}
%
}]
[para]
[emph {Example 1}]
[para]
This next example just illustrates a simple linked list
[para]
[example {
% # define a very simple record for linked list
% record define llist {
    value
    next
}
::llist
% llist lstart
::lstart
% lstart config -value 1 -next [llist #auto]
% [lstart cget -next] config -value 2 -next [llist #auto]
% [[lstart cget -next] cget -next] config -value 3 -next "end"
% set next lstart
lstart
% while 1 {
lappend values [$next cget -value]
set next [$next cget -next]
if {[string match "end" $next]} {break}
}
% puts "$values"
1 2 3
% # cleanup linked list
% # We could just use delete record llist also
% foreach I [record show instances llist] {
record delete instance $I
}
% record show instances llist
%
}]
[para]
[vset CATEGORY {struct :: record}]
[include ../doctools2base/include/feedback.inc]
[manpage_end]
 |