File: Class.man

package info (click to toggle)
nsf 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 13,208 kB
  • sloc: ansic: 32,687; tcl: 10,723; sh: 660; pascal: 176; javascript: 135; lisp: 41; makefile: 24
file content (528 lines) | stat: -rw-r--r-- 20,066 bytes parent folder | download
1
2
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
[comment {-*- Tcl -*- nx::Class manpage}]

[include version.inc]
[manpage_begin nx::Class 3 [vset VERSION]]

[comment {For the time being, we do not render keywords & terms; and
the corresponding reverse index}] 
[proc keywords args {}]
[proc term v {return $v}]


[keywords "base metaclass"]
[keywords NX]
[keywords "mixin class"]
[keywords "re-classification"]
[keywords "submethod"]
[keywords "method ensemble"]
[keywords "linearization"]
[keywords "filter specification"]
[keywords "metaclass"]

[vset SCOPE "class"]
[vset CMD "cls"]
[vset MODIFIER ""]

[copyright {2014-2019 Stefan Sobernig <stefan.sobernig@wu.ac.at>, Gustaf Neumann <gustaf.neumann@wu.ac.at>; available under the Creative Commons Attribution 3.0 Austria license (CC BY 3.0 AT).}]
[titledesc {API reference of the base-metaclass of the NX objectsystem}]
[moddesc {NX API}]

[description]
[para]

[cmd nx::Class] is the [term "base metaclass"] of the [term NX] object
system. All classes (e.g. [emph cls]) are (direct or indirect)
instances of [cmd nx::Class]. Therefore, the methods provided by [cmd nx::Class] are
available to all classes. A class [emph cls] which does
not have [cmd nx::Class] as its direct or indirect superclass is
referred to as an [emph "application class"]. By default, when
instantiating a new class from [cmd nx::Class], it becomes an
application class with [cmd nx::Object] being set as its superclass. A
class [emph cls] which is explicitly declared as a (direct or
indirect) subclass of [cmd nx::Class] is referred to as a [emph metaclass], that
is, its instances will become classes as well. In other words, a
metaclass instantiates and subclasses [cmd nx::Class] at the same
time.

[example {
+---------+
| ::nx::* |
+---------+--------------------------------------Y
|                                                |
|  instance of                                   |
|   .-------.                                    |
|  +--------'+     instance of     +----------+  |
|  |         |<....................|          |  |
|  |  Class  |                     |  Object  |  |
|  |         |....................>|          |  |
|  +---------+     subclass of     +-----+----+  |
|   ^   ^                                ^       |
\...|...|................................|......./
    |   |                                |
    |   |subclass.....(xor)......subclass|
    |   |of       +-----------+        of|
    |   |.........|           |..........|
    | (metaclass) |   /cls/   | (application class)
    |.............|           |
    instance of   +-----------+
}]

Classes can be created in the following ways:

[list_begin definitions]

[call [cmd nx::Class] [method create] [arg cls] [opt "[option -superclasses] [arg superClassNames]"] [opt "[option -mixins] [arg mixinSpec]"] [opt "[option -filters] [arg filterSpec]"] [opt "[arg option] [arg value] ..."] [opt [arg initBlock]]]

To create a class having the explicit name [arg cls], use [method create].

  
  [call [cmd nx::Class] [method new] [opt "[option -superclasses] [arg superClassNames]"] [opt "[option -mixins] [arg mixinSpec]"] [opt "[option -filters] [arg filterSpec]"] [opt [arg initBlock]]]

To create a class having an automatically assigned, implicit name, use [method new].

[list_end]

The configuration options for direct and indirect instances of [cmd nx::Class],
which can be passed when calling [method create] and [method new], are
documented in the subsequent section.

[section {Configuration Options for Instances of nx::Class}]

[para] Configuration options can be used for configuring objects during
their creation by passing the options as non-positional arguments into calls
of [method new] and [method create] (see [cmd nx::Class]). An
existing object can be queried for its current configuration using
[method cget] and it can be re-configured using [method configure].

[list_begin options]

[opt_def -superclasses [opt [arg superClassNames]]]

If [arg superClassNames] is not specified, returns the superclasses of
the class. If provided, the class becomes the subclass of [arg superClassNames].

[opt_def -filters [opt [arg filterSpecs]]]

Retrieves the list of filter methods currently active on instances of
the class, if [arg filterSpecs] is not set. Otherwise, activates a
list of filter methods for the instances of the class. Filters are
returned or set in terms of a list of [term "filter specification"]s.

[opt_def -mixins [opt [arg mixinSpecs]]]

Returns the list of [term "mixin class"]es currently active on
instances of the class, if [arg mixinSpecs] is not specified. Otherwise, the class
is extended by the list of [term "mixin class"]es provided by [arg mixinSpecs]. 
[term "mixin class"]es are returned or set in terms of a list of [term "mixin specification"]s.

[list_end]

The configuration options provided by [cmd nx::Object] are equally
available because an application class [arg cls] is an indirect
instance of [cmd nx::Object].

[section {Methods for Instances of nx::Class}]

[list_begin commands]

[cmd_def alias]
[list_begin definitions]

[include alias.man.inc]

[list_end]

[cmd_def __class_configureparameter]

[list_begin definitions]

[def "[arg cls] [method "__class_configureparameter"]"]

Computes and returns the configuration options available for [arg cls] instances, to be consumed as method-parameter specification by [method configure].

[list_end]

[cmd_def create]

[list_begin definitions]

[call [arg cls] [method create] [arg instanceName] [opt "[arg option] [arg value] [arg option] [arg value] ..."]] 

This factory method creates an instance [arg instanceName] of [arg cls]
and returns [arg instanceName].

[example {
% nx::Class create AClass {
    :method init args {
      next
    }; # initialization method for instances of 'AClass'
  }; # defines a class 'AClass' being an instance of 'nx::Class'
::AClass
% ::AClass create anInstance; # defines an object 'anInstance' being an instance of 'AClass'
::anInstance
% ::anInstance info class
::AClass
% ::AClass info class
::nx::Class
}]

[method create] accepts the configuration options [arg option]
available for this instance, such as those defined by properties of
[arg cls] (see [method "property"]).

[para]

Note that [method create] is called internally when defining an
instance of [arg cls] using [method new].

[para]

By calling [method create] on [cmd nx::Class] itself, the created
instance will become a new application class [arg instanceName] on
which [method create] can also be applied (i.e., it can be
instantiated). If the so-created class has [cmd ::nx::Class] its
direct or indirect superclass, [arg instanceName] is referred to as a
[term "metaclass"]; that is, a class whose instances are again
classes.

[list_end]

[cmd_def delete]

[list_begin definitions]

[include delete.man.inc]

[list_end]

[cmd_def filters]

[list_begin definitions]

[include filter.man.inc]

[list_end]

[cmd_def forward]

[list_begin definitions]

[include forward.man.inc]

[list_end]

[cmd_def info]

A collection of introspection submethods on the structural features (e.g.
configuration options, superclasses) and the behavioral features (e.g.
methods, [term "filter"]s) provided by [arg cls] to its instances.

[list_begin definitions]

[call [arg cls] [method "info heritage"] [opt [arg pattern]]]

If [arg pattern] is omitted, returns the list of object names of all the direct and indirect
superclasses and [emph per-class] [term "mixin class"]es of [arg cls], in
their order of precedence, which are active for instances of [arg cls]. If
[arg pattern] is specified, only superclasses and [term "mixin class"]es whose names
match [arg pattern] will be listed (see [cmd "string match"]).

[call [arg cls] [method "info instances"] [opt [option -closure]] [opt [arg pattern]]]

If [arg pattern] is not specified, returns a list of the object names
of all the direct instances of [arg cls]. If the [term "switch"]
[option -closure] is set, indirect instances are also returned. A
direct instance is created by using [method create] or [method new] on
[arg cls], an indirect instance was created from a direct or indirect
subclass of [arg cls]. If [arg pattern] is specified, only instances
whose names match [arg pattern] will be listed (see [cmd "string match"]).

[call [arg cls] [method "info mixinof"] [opt [option -closure]] [opt "[option -scope] [arg option]"] [opt [arg pattern]]]

If [arg pattern] is not specified, returns a list of the object names
of all the objects for which [arg cls] is active as a
direct [term "mixin class"]. If the [term "switch"]
[option -closure] is set, objects which have [arg cls] as an indirect
[term "mixin class"] are also returned. If [arg pattern] is
specified, only objects whose names match [arg pattern] will
be listed (see [cmd "string match"]). Valid values of [arg option] are
[const all], [const object], and [const class]. Passing [const object]
will have only objects returned which have [arg cls] as [emph per-object]
[term "mixin class"]. Passing [const class] will have only classes
returned which have [arg cls] as [emph per-class] [term "mixin class"]. [const all] (the
default) will have contained both in the returned list.


[call [arg cls] [method "info subclasses"] [opt [option -closure]] [opt [option -dependent]] [opt [arg pattern]]]

If [arg pattern] is not specified, returns a list of the object names
of the direct subclasses of [arg cls]. If the [term "switch"] [option -closure] is
set, indirect subclasses are also returned. If the [term "switch"] [option -dependent] is on, indirect subclasses introduced by [term "mixin class"] relations of subclasses of [arg cls] are also reported. [option -closure] and [option -dependent] are mutually exclusive. If [arg pattern] is specified, only subclasses whose names match [arg pattern] will be listed (see [cmd "string match"]).

[call [arg cls] [method "info superclasses"] [opt [option -closure]] [opt [arg pattern]]]

If [arg pattern] is not specified, returns a list of the object names
of all direct superclasses of [arg cls]. If the [term "switch"] [option -closure] is
set, indirect superclasses will also be returned. If [arg pattern] is specified, only superclasses whose names match [arg pattern] will be listed (see [cmd "string match"]).

[include info.man.inc]

[list_end]


[cmd_def method]

[list_begin definitions]

[include method.man.inc]

[list_end]

[cmd_def mixins]
[list_begin definitions]

[include mixin.man.inc]

[list_end]

[cmd_def new]

[list_begin definitions]

[call [arg cls] [method new] [opt "[option "-childof"] [arg parentName]"] [opt "[arg option] [arg value] [arg option] [arg value] ..."]]

A factory method to create autonamed instances of [arg cls]. It
returns the name of the newly created instance. For example:

[example {
% nx::Class create AClass; # defines a class 'AClass' being an instance of 'nx::Class'
::AClass
% set inst [::AClass new]; # defines an autonamed object being an instance of 'AClass'
::nsf::__#0
% $inst info class
::AClass
}]

The factory method will provide computed object names of the form,
e.g. [const ::nsf::__#0]. The uniqueness of generated object names is
guaranteed for the scope of the current Tcl interpreter only.

[para]

It is a frontend to [method create] which will be called by [method new] once
the name of the instance has been computed, passing along the
arguments [arg option] to [method new] as the configuration options
(see [method create]).

[para]
If [option -childof] is provided, the new object will be created as a
nested object of [arg parentName]. [arg parentName] can be the name of
either an existing NX object or an existing Tcl namespace. If
non-existing, a Tcl namespace [arg parentName] will be created on the
fly.

[list_end]

[cmd_def property]

[list_begin definitions]

[call [arg cls] [method property] [opt "[option -accessor] [const public] | [const protected] | [const private]"] [opt "[option -class] [arg className]"] [opt "[option -configurable] [arg trueFalse]"] [opt [option -incremental]] [opt "[option -trace] [const set] | [const get] | [const default]"] [arg spec] [opt [arg initBlock]]]

[include property.man.inc]

[list_end]

[cmd_def require]

[list_begin definitions]

[include require.man.inc]

[list_end]

[cmd_def variable]

[list_begin definitions]

[comment {::nx::Object variable ?-accessor /value/? ?-incremental? ?-class /value/? ?-configurable /boolean/? ?-initblock /value/? ?-nocomplain? /spec/ ?/defaultValue/?}]

[call [arg cls] [method variable] [opt "[option -accessor] [const public] | [const protected] | [const private]"] [opt [option -incremental]] [opt "[option -class] [arg className]"] [opt "[option -configurable] [arg trueFalse]"] [opt "[option -initblock] [opt "[option -trace] [const set] | [const get] | [const default]"] [arg script]"] [arg spec] [opt [arg defaultValue]]]

[include variable.man.inc]

[list_end]

[list_end]

[section {Object Life Cycle}]

[cmd nx::Class] provides means to control important stages through
which an NX object passes between and including its creation and its
destruction: allocation, recreation, deallocation.

[example {
        /cls/->create(/instance/)
                  .---------------.   exists?    [false]   .----------------.                   .-------------------.
             ---->|Class::create()|----><>---------------->|Class::__alloc()|-----------><>---->|Object::configure()|
                  `---------------'      |      (1)        `----------------'             ^ (3) `---------+---------'
                                  [true] |                                                |               | (4)
                                         |  .-------------------.                         |      .------------------.
                                         `->|Class::__recreate()|-------------------------'      |/instance/->init()|
                                      (2)   `-------------------'                                `------------------'
    /instance/->destroy()
        .-----------------.     .------------------.
   ---->|Object::destroy()|---->|Class::__dealloc()|
        `-----------------' (5) `------------------'
}]

Object creation is controlled by the factory method [method create], provided by [cmd nx::Class] to its
instance [arg cls]. [method create] produces a new object [arg instance] as an
instance of [arg cls] in a number of steps.

[list_begin enumerated]

[enum] If [arg instance] does not represent an existing object, an
internal call to [method "__alloc"], provided by [cmd nx::Class], runs
the [emph {allocation}] procedure for a fresh [arg instance] of [arg cls].

[enum] If [arg instance] corresponds to an existing object, the
[emph {recreation}] procedure is triggered by calling [method "__recreate"]
defined by [cmd nx::Class].
  
[enum] The newly allocated or recreated object [arg instance] is then
configured by dispatching [method configure], provided by [cmd nx::Object], which
consumes the configuration options passed into [method create]. This
will establish the instance's initial state, e.g. by setting object
variables and object relations according to the configuration options
and corresponding default values.
  
[enum] Finally, the initialization method [method init] is dispatched, if
available for [arg instance]. [method "init"] can be defined by [arg cls] on
behalf of its instance [arg instance], e.g. to lay out a
class-specific initialization behavior.

[example_begin]
% nx::Class create Foo {:property x}
% Foo method init {} {set :y [lb]expr {${:x} + 1}[rb]}
% Foo public method bar {} {return ${:y}}
% Foo create f1 -x 101
% f1 cget -x
101
% f1 bar
102
[example_end]

Alternatively, the object [arg instance] may define a per-object
[method init] on its own. A per-object [method init] can be chained to
a class-level [method init] using [cmd nx::next], just like a regular
method.

[para]

Note that the definition of an [method init] method must contain an
empty parameter specification, since [method init] is always called
with an empty argument list.

[list_end]

Object destruction, such as triggered by an application-level
[method destroy] call (5), is finalized by [method {__dealloc}] offered by
[cmd nx::Class]. 

[para]

In the following, the three built-in procedures ---
allocation, recreation, and deallocation --- are explained:

[list_begin itemized]

[item] [emph {Allocation}]: [method "__alloc"] creates a blank object [arg instance] as an instance of [arg cls] and returns the fully-qualified [arg instance]. [method __alloc] is primarily used internally by [method create] to allocate a Tcl memory storage for [arg instance] and to register [arg instance] with the Tcl
interpreter as a new command.

[item] [emph {Recreation}]:

Recreation is the NX scheme for resolving naming conflicts between
objects: An object is requested to be created using [method create] or
[method new] while an object of an identical object name, e.g. [arg instance], already
exists:

[example {
% Object create Bar
::Bar
% Object create Bar; # calls Object->__recreate(::Bar, ...)
::Bar
}]

In such a situation, the built-in [method "__recreate"] first unsets
the object state (i.e., Tcl variables held by the object) and removes
relations of the object under recreation with other objects. Then,
second, standard object initialization is performed by calling [method configure] and
[method init], if any.


[para]

Alternatively, recreation will be performed as a sequence of [method destroy] and
[method create] calls in the following recreation scenarios:

[list_begin itemized]
[item] An existing class is requested to be recreated as an object.
[item] An existing object is requested to be recreated as a class.
[example {
  % Object create Bar
  ::Bar
  % Class create Bar; # calls Bar->destroy() & Class::create(::Bar, ...)
}]
[item] An object of an object system other than NX (e.g. XOTcl2) is asked to be recreated.
[list_end]



[item] [emph {Deallocation}]: [method __dealloc] marks an instance [arg instance] of [arg cls] for
deletion by returning its Tcl memory representation to the Tcl memory pool and by 
unregistering the corresponding Tcl command with the Tcl interpreter.

[para]

Beware that [method __dealloc] does not necessarily
cause the object to be deleted immediately. Depending on the lifecycle
of the object's environment (e.g. the Tcl interp interpreter, the containing
namespace) and on call references down the callstack, the actual
memory freeing/returning operation may occur at a later point.

[list_end]

The three methods [method __alloc], [method __recreate], and [method __dealloc] are
internally provided and internally called. By default, they are not part of
the method interface of [arg cls] and cannot be called directly by clients of [arg cls].
In addition, [method __alloc], [method __recreate], and [method __dealloc] are protected from
redefinition by a script.

[para]

To extend or to replace the built-in allocation, recreation, and
deallocation procedure, the methods [method __alloc], [method __recreate], and
[method __dealloc] can be refined by providing a custom method
implementation:
[list_begin itemized]
[item] as a per-object method of [arg cls];
[item] as a method of a per-object [term "mixin class"] extending [arg cls];
[item] as a method of a per-class [term "mixin class"] extending [cmd nx::Class];
[item] as a method of a subclass specializing [cmd nx::Class], from which [arg cls] is to be instantiated.
[list_end]

This custom implementation can redirect to the built-in [method __alloc], [method __recreate], and
[method __dealloc], respectively, by using [cmd nx::next]. By
providing such a custom implementation, [method __alloc], [method __recreate], and
[method __dealloc], respectively, become available as callable methods
of [arg cls]:

[list_begin definitions]
[def "[arg cls] [method "__alloc"] [arg instance]"]
[def "[arg cls] [method "__recreate"] [arg instance] [opt "[arg arg] ..."]"]
[def "[arg cls] [method "__dealloc"] [arg instance]"]
[list_end]


[manpage_end]