OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [itcl/] [itcl/] [doc/] [class.n] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
'\"
2
'\" Copyright (c) 1993-1998  Lucent Technologies, Inc.
3
'\"
4
'\" See the file "license.terms" for information on usage and redistribution
5
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6
'\"
7
'\" RCS: $Id: class.n,v 1.1.1.1 2002-01-16 10:24:46 markom Exp $
8
'\"
9
.so man.macros
10
.TH class n "" itcl "[incr\ Tcl]"
11
.BS
12
'\" Note:  do not modify the .SH NAME line immediately below!
13
.SH NAME
14
class \- create a class of objects
15
.SH SYNOPSIS
16
\fBclass \fIclassName\fR \fB{
17
.br
18
    \fBinherit \fIbaseClass\fR ?\fIbaseClass\fR...?
19
.br
20
    \fBconstructor \fIargs\fR ?\fIinit\fR? \fIbody\fR
21
.br
22
    \fBdestructor \fIbody\fR
23
.br
24
    \fBmethod \fIname\fR ?\fIargs\fR? ?\fIbody\fR?
25
.br
26
    \fBproc \fIname ?\fIargs\fR? ?\fIbody\fR?
27
.br
28
    \fBvariable \fIvarName\fR ?\fIinit\fR? ?\fIconfig\fR?
29
.br
30
    \fBcommon \fIvarName\fR ?\fIinit\fR?
31
.sp
32
    \fBpublic \fIcommand\fR ?\fIarg arg ...\fR?
33
.br
34
    \fBprotected \fIcommand\fR ?\fIarg arg ...\fR?
35
.br
36
    \fBprivate \fIcommand\fR ?\fIarg arg ...\fR?
37
.sp
38
    \fBset \fIvarName\fR ?\fIvalue\fR?
39
.br
40
    \fBarray \fIoption\fR ?\fIarg arg ...\fR?
41
.br
42
\fB}\fR
43
.sp
44
\fIclassName objName\fR ?\fIarg arg ...\fR?
45
.sp
46
\fIobjName method\fR ?\fIarg arg ...\fR?
47
.sp
48
\fIclassName::proc ?\fIarg arg ...\fR?
49
.BE
50
 
51
.SH DESCRIPTION
52
.PP
53
The fundamental construct in \fB[incr\ Tcl]\fR is the class definition.
54
Each class acts as a template for actual objects that can be created.
55
The class itself is a namespace which contains things common to all
56
objects.  Each object has its own unique bundle of data which contains
57
instances of the "variables" defined in the class definition.  Each
58
object also has a built-in variable named "this", which contains the
59
name of the object.  Classes can also have "common" data members that
60
are shared by all objects in a class.
61
.PP
62
Two types of functions can be included in the class definition.
63
"Methods" are functions which operate on a specific object, and
64
therefore have access to both "variables" and "common" data members.
65
"Procs" are ordinary procedures in the class namespace, and only
66
have access to "common" data members.
67
.PP
68
If the body of any method or proc starts with "\fB@\fR", it is treated
69
as the symbolic name for a C procedure.  Otherwise, it is treated as
70
a Tcl code script.  See below for details on registering and using
71
C procedures.
72
.PP
73
A class can only be defined once, although the bodies of class
74
methods and procs can be defined again and again for interactive
75
debugging.  See the \fBbody\fR and \fBconfigbody\fR commands for
76
details.
77
.PP
78
Each namespace can have its own collection of objects and classes.
79
The list of classes available in the current context can be queried
80
using the "\fBitcl::find classes\fR" command, and the list of objects,
81
with the "\fBitcl::find objects\fR" command.
82
.PP
83
A class can be deleted using the "\fBdelete class\fR" command.
84
Individual objects can be deleted using the "\fBdelete object\fR"
85
command.
86
 
87
.SH CLASS DEFINITIONS
88
.TP
89
\fBclass \fIclassName definition\fR
90
Provides the definition for a class named \fIclassName\fR.  If
91
the class \fIclassName\fR already exists, or if a command called
92
\fIclassName\fR exists in the current namespace context, this
93
command returns an error.  If the class definition is successfully
94
parsed, \fIclassName\fR becomes a command in the current context,
95
handling the creation of objects for this class.
96
.PP
97
The class \fIdefinition\fR is evaluated as a series of Tcl
98
statements that define elements within the class.  The following
99
class definition commands are recognized:
100
.RS
101
.TP
102
\fBinherit \fIbaseClass\fR ?\fIbaseClass\fR...?
103
Causes the current class to inherit characteristics from one or
104
more base classes.  Classes must have been defined by a previous
105
\fBclass\fR command, or must be available to the auto-loading
106
facility (see "AUTO-LOADING" below).  A single class definition
107
can contain no more than one \fBinherit\fR command.
108
.sp
109
The order of \fIbaseClass\fR names in the \fBinherit\fR list
110
affects the name resolution for class members.  When the same
111
member name appears in two or more base classes, the base class
112
that appears first in the \fBinherit\fR list takes precedence.
113
For example, if classes "Foo" and "Bar" both contain the member
114
"x", and if another class has the "\fBinherit\fR" statement:
115
.CS
116
inherit Foo Bar
117
.CE
118
then the name "x" means "Foo::x".  Other inherited members named
119
"x" must be referenced with their explicit name, like "Bar::x".
120
.TP
121
\fBconstructor \fIargs\fR ?\fIinit\fR? \fIbody\fR
122
Declares the \fIargs\fR argument list and \fIbody\fR used for
123
the constructor, which is automatically invoked whenever an
124
object is created.
125
.sp
126
Before the \fIbody\fR is executed, the
127
optional \fIinit\fR statement is used to invoke any base class
128
constructors that require arguments.  Variables in the \fIargs\fR
129
specification can be accessed in the \fIinit\fR code fragment,
130
and passed to base class constructors.  After evaluating the
131
\fIinit\fR statement, any base class constructors that have
132
not been executed are invoked automatically without arguments.
133
This ensures that all base classes are fully constructed before
134
the constructor \fIbody\fR is executed.  By default, this
135
scheme causes constructors to be invoked in order from least-
136
to most-specific.  This is exactly the opposite of the order
137
that classes are reported by the \fBinfo heritage\fR command.
138
.sp
139
If construction is successful, the constructor always returns
140
the object name\-regardless of how the \fIbody\fR is defined\-and
141
the object name becomes a command in the current namespace context.
142
If construction fails, an error message is returned.
143
.TP
144
\fBdestructor \fIbody\fR
145
Declares the \fIbody\fR used for the destructor, which is automatically
146
invoked when an object is deleted.  If the destructor is successful,
147
the object data is destroyed and the object name is removed as a command
148
from the interpreter.  If destruction fails, an error message is returned
149
and the object remains.
150
.sp
151
When an object is destroyed, all destructors in its class hierarchy
152
are invoked in order from most- to least-specific.  This is the
153
order that the classes are reported by the "\fBinfo heritage\fR"
154
command, and it is exactly the opposite of the default constructor
155
order.
156
.TP
157
\fBmethod \fIname\fR ?\fIargs\fR? ?\fIbody\fR?
158
Declares a method called \fIname\fR.  When the method \fIbody\fR is
159
executed, it will have automatic access to object-specific variables
160
and common data members.
161
.sp
162
If the \fIargs\fR list is specified, it establishes the usage
163
information for this method.  The \fBbody\fR command can be used
164
to redefine the method body, but the \fIargs\fR list must match
165
this specification.
166
.sp
167
Within the body of another class method, a method can be invoked
168
like any other command\-simply by using its name.  Outside of the
169
class context, the method name must be prefaced an object name,
170
which provides the context for the data that it manipulates.
171
Methods in a base class that are redefined in the current class,
172
or hidden by another base class, can be qualified using the
173
"\fIclassName\fR::\fImethod\fR" syntax.
174
.TP
175
\fBproc \fIname\fR ?\fIargs\fR? ?\fIbody\fR?
176
Declares a proc called \fIname\fR.  A proc is an ordinary procedure
177
within the class namespace.  Unlike a method, a proc is invoked
178
without referring to a specific object.  When the proc \fIbody\fR is
179
executed, it will have automatic access only to common data members.
180
.sp
181
If the \fIargs\fR list is specified, it establishes the usage
182
information for this proc.  The \fBbody\fR command can be used
183
to redefine the proc body, but the \fIargs\fR list must match
184
this specification.
185
.sp
186
Within the body of another class method or proc, a proc can be
187
invoked like any other command\-simply by using its name.
188
In any other namespace context, the proc is invoked using a
189
qualified name like "\fIclassName\fB::\fIproc\fR".  Procs in
190
a base class that are redefined in the current class, or hidden
191
by another base class, can also be accessed via their qualified
192
name.
193
.TP
194
\fBvariable \fIvarName\fR ?\fIinit\fR? ?\fIconfig\fR?
195
Defines an object-specific variable named \fIvarName\fR.  All
196
object-specific variables are automatically available in class
197
methods.  They need not be declared with anything like the
198
\fBglobal\fR command.
199
.sp
200
If the optional \fIinit\fR string is specified, it is used as the
201
initial value of the variable when a new object is created.
202
Initialization forces the variable to be a simple scalar
203
value; uninitialized variables, on the other hand, can be set
204
within the constructor and used as arrays.
205
.sp
206
The optional \fIconfig\fR script is only allowed for public variables.
207
If specified, this code fragment is executed whenever a public
208
variable is modified by the built-in "configure" method.  The
209
\fIconfig\fR script can also be specified outside of the class
210
definition using the \fBconfigbody\fR command.
211
.TP
212
\fBcommon \fIvarName\fR ?\fIinit\fR?
213
Declares a common variable named \fIvarName\fR.  Common variables
214
reside in the class namespace and are shared by all objects belonging
215
to the class.  They are just like global variables, except that
216
they need not be declared with the usual \fBglobal\fR command.
217
They are automatically visible in all class methods and procs.
218
.sp
219
If the optional \fIinit\fR string is specified, it is used as the
220
initial value of the variable.  Initialization forces the variable
221
to be a simple scalar value; uninitialized variables, on the other
222
hand, can be set with subsequent \fBset\fR and \fBarray\fR commands
223
and used as arrays.
224
.sp
225
Once a common data member has been defined, it can be set using
226
\fBset\fR and \fBarray\fR commands within the class definition.
227
This allows common data members to be initialized as arrays.
228
For example:
229
.CS
230
class Foo {
231
    common boolean
232
    set boolean(true) 1
233
    set boolean(false) 0
234
}
235
.CE
236
Note that if common data members are initialized within the
237
constructor, they get initialized again and again whenever new
238
objects are created.
239
.TP
240
\fBpublic \fIcommand\fR ?\fIarg arg ...\fR?
241
.TP
242
\fBprotected \fIcommand\fR ?\fIarg arg ...\fR?
243
.TP
244
\fBprivate \fIcommand\fR ?\fIarg arg ...\fR?
245
These commands are used to set the protection level for class
246
members that are created when \fIcommand\fR is evaluated.
247
The \fIcommand\fR is usually \fBmethod\fR, \fBproc\fR,
248
\fBvariable\fR or\fBcommon\fR, and the remaining \fIarg\fR's
249
complete the member definition.  However, \fIcommand\fR can
250
also be a script containing many different member definitions,
251
and the protection level will apply to all of the members
252
that are created.
253
 
254
.SH CLASS USAGE
255
.PP
256
Once a class has been defined, the class name can be used as a
257
command to create new objects belonging to the class.
258
.TP
259
\fIclassName objName\fR ?\fIargs...\fR?
260
Creates a new object in class \fIclassName\fR with the name \fIobjName\fR.
261
Remaining arguments are passed to the constructor of the most-specific
262
class.  This in turn passes arguments to base class constructors before
263
invoking its own body of commands.  If construction is successful, a
264
command called \fIobjName\fR is created in the current namespace context,
265
and \fIobjName\fR is returned as the result of this operation.
266
If an error is encountered during construction, the destructors are
267
automatically invoked to free any resources that have been allocated,
268
the object is deleted, and an error is returned.
269
.sp
270
If \fIobjName\fR contains the string "\fB#auto\fR", that string is
271
replaced with an automatically generated name.  Names have the
272
form \fIclassName\fR, where the \fIclassName\fR part is
273
modified to start with a lowercase letter.  In class "Toaster",
274
for example, the "\fB#auto\fR" specification would produce names
275
like toaster0, toaster1, etc.  Note that "\fB#auto\fR" can be
276
also be buried within an object name:
277
.CS
278
fileselectiondialog .foo.bar.#auto -background red
279
.CE
280
This would generate an object named ".foo.bar.fileselectiondialog0".
281
 
282
.SH OBJECT USAGE
283
Once an object has been created, the object name can be used
284
as a command to invoke methods that operate on the object.
285
.TP
286
\fIobjName method\fR ?\fIargs...\fR?
287
Invokes a method named \fImethod\fR on an object named \fIobjName\fR.
288
Remaining arguments are passed to the argument list for the
289
method.  The method name can be "constructor", "destructor",
290
any method name appearing in the class definition, or any of
291
the following built-in methods.
292
.SH BUILT-IN METHODS
293
.TP
294
\fIobjName\fR \fBcget option\fR
295
Provides access to public variables as configuration options.  This
296
mimics the behavior of the usual "cget" operation for Tk widgets.
297
The \fIoption\fR argument is a string of the form "\fB-\fIvarName\fR",
298
and this method returns the current value of the public variable
299
\fIvarName\fR.
300
.TP
301
\fIobjName\fR \fBconfigure\fR ?\fIoption\fR? ?\fIvalue option value ...\fR?
302
Provides access to public variables as configuration options.  This
303
mimics the behavior of the usual "configure" operation for Tk widgets.
304
With no arguments, this method returns a list of lists describing
305
all of the public variables.  Each list has three elements:  the
306
variable name, its initial value and its current value.
307
.sp
308
If a single \fIoption\fR of the form "\fB-\fIvarName\fR" is specified,
309
then this method returns the information for that one variable.
310
.sp
311
Otherwise, the arguments are treated as \fIoption\fR/\fIvalue\fR
312
pairs assigning new values to public variables.  Each variable
313
is assigned its new value, and if it has any "config" code associated
314
with it, it is executed in the context of the class where it was
315
defined.  If the "config" code generates an error, the variable
316
is set back to its previous value, and the \fBconfigure\fR method
317
returns an error.
318
.TP
319
\fIobjName\fR \fBisa \fIclassName\fR
320
Returns non-zero if the given \fIclassName\fR can be found in the
321
object's heritage, and zero otherwise.
322
.TP
323
\fIobjName\fR \fBinfo \fIoption\fR ?\fIargs...\fR?
324
Returns information related to a particular object named
325
\fIobjName\fR, or to its class definition.  The \fIoption\fR
326
parameter includes the following things, as well as the options
327
recognized by the usual Tcl "info" command:
328
.RS
329
.TP
330
\fIobjName\fR \fBinfo class\fR
331
Returns the name of the most-specific class for object \fIobjName\fR.
332
.TP
333
\fIobjName\fR \fBinfo inherit\fR
334
Returns the list of base classes as they were defined in the
335
"\fBinherit\fR" command, or an empty string if this class
336
has no base classes.
337
.TP
338
\fIobjName\fR \fBinfo heritage\fR
339
Returns the current class name and the entire list of base classes
340
in the order that they are traversed for member lookup and object
341
destruction.
342
.TP
343
\fIobjName\fR \fBinfo function\fR ?\fIcmdName\fR? ?\fB-protection\fR? ?\fB-type\fR? ?\fB-name\fR? ?\fB-args\fR? ?\fB-body\fR?
344
With no arguments, this command returns a list of all class methods
345
and procs.  If \fIcmdName\fR is specified, it returns information
346
for a specific method or proc.  If no flags are specified, this
347
command returns a list with the following elements:  the protection
348
level, the type (method/proc), the qualified name, the argument list
349
and the body.  Flags can be used to request specific elements from
350
this list.
351
.TP
352
\fIobjName\fR \fBinfo variable\fR ?\fIvarName\fR? ?\fB-protection\fR? ?\fB-type\fR? ?\fB-name\fR? ?\fB-init\fR? ?\fB-value\fR? ?\fB-config\fR?
353
With no arguments, this command returns a list of all object-specific
354
variables and common data members.  If \fIvarName\fR is specified, it
355
returns information for a specific data member.  If no flags are
356
specified, this command returns a list with the following elements:  the
357
protection level, the type (variable/common), the qualified name, the
358
initial value, and the current value.  If \fIvarName\fR is a public
359
variable, the "config" code is included on this list.  Flags can be
360
used to request specific elements from this list.
361
 
362
.SH CHAINING METHODS/PROCS
363
Sometimes a base class has a method or proc that is redefined with
364
the same name in a derived class.  This is a way of making the
365
derived class handle the same operations as the base class, but
366
with its own specialized behavior.  For example, suppose we have
367
a Toaster class that looks like this:
368
.CS
369
class Toaster {
370
    variable crumbs 0
371
    method toast {nslices} {
372
        if {$crumbs > 50} {
373
            error "== FIRE! FIRE! =="
374
        }
375
        set crumbs [expr $crumbs+4*$nslices]
376
    }
377
    method clean {} {
378
        set crumbs 0
379
    }
380
}
381
.CE
382
We might create another class like SmartToaster that redefines
383
the "toast" method.  If we want to access the base class method,
384
we can qualify it with the base class name, to avoid ambiguity:
385
.CS
386
class SmartToaster {
387
    inherit Toaster
388
    method toast {nslices} {
389
        if {$crumbs > 40} {
390
            clean
391
        }
392
        return [Toaster::toast $nslices]
393
    }
394
}
395
.CE
396
Instead of hard-coding the base class name, we can use the
397
"chain" command like this:
398
.CS
399
class SmartToaster {
400
    inherit Toaster
401
    method toast {nslices} {
402
        if {$crumbs > 40} {
403
            clean
404
        }
405
        return [chain $nslices]
406
    }
407
}
408
.CE
409
The chain command searches through the class hierarchy for
410
a slightly more generic (base class) implementation of a method
411
or proc, and invokes it with the specified arguments.  It starts
412
at the current class context and searches through base classes
413
in the order that they are reported by the "info heritage" command.
414
If another implementation is not found, this command does nothing
415
and returns the null string.
416
 
417
.SH AUTO-LOADING
418
.PP
419
Class definitions need not be loaded explicitly; they can be loaded as
420
needed by the usual Tcl auto-loading facility.  Each directory containing
421
class definition files should have an accompanying "tclIndex" file.
422
Each line in this file identifies a Tcl procedure or \fB[incr\ Tcl]\fR
423
class definition and the file where the definition can be found.
424
.PP
425
For example, suppose a directory contains the definitions for classes
426
"Toaster" and "SmartToaster".  Then the "tclIndex" file for this
427
directory would look like:
428
.CS
429
# Tcl autoload index file, version 2.0 for [incr Tcl]
430
# This file is generated by the "auto_mkindex" command
431
# and sourced to set up indexing information for one or
432
# more commands.  Typically each line is a command that
433
# sets an element in the auto_index array, where the
434
# element name is the name of a command and the value is
435
# a script that loads the command.
436
 
437
set auto_index(::Toaster) "source $dir/Toaster.itcl"
438
set auto_index(::SmartToaster) "source $dir/SmartToaster.itcl"
439
.PP
440
The \fBauto_mkindex\fR command is used to automatically
441
generate "tclIndex" files.
442
.CE
443
The auto-loader must be made aware of this directory by appending
444
the directory name to the "auto_path" variable.  When this is in
445
place, classes will be auto-loaded as needed when used in an
446
application.
447
 
448
.SH C PROCEDURES
449
.PP
450
C procedures can be integrated into an \fB[incr\ Tcl]\fR class
451
definition to implement methods, procs, and the "config" code
452
for public variables.  Any body that starts with "\fB@\fR"
453
is treated as the symbolic name for a C procedure.
454
.PP
455
Symbolic names are established by registering procedures via
456
\fBItcl_RegisterC()\fR.  This is usually done in the \fBTcl_AppInit()\fR
457
procedure, which is automatically called when the interpreter starts up.
458
In the following example, the procedure \fCMy_FooCmd()\fR is registered
459
with the symbolic name "foo".  This procedure can be referenced in
460
the \fBbody\fR command as "\fC@foo\fR".
461
.CS
462
int
463
Tcl_AppInit(interp)
464
    Tcl_Interp *interp;     /* Interpreter for application. */
465
{
466
    if (Itcl_Init(interp) == TCL_ERROR) {
467
        return TCL_ERROR;
468
    }
469
 
470
    if (Itcl_RegisterC(interp, "foo", My_FooCmd) != TCL_OK) {
471
        return TCL_ERROR;
472
    }
473
}
474
.CE
475
C procedures are implemented just like ordinary Tcl commands.
476
See the \fBCrtCommand\fR man page for details.  Within the procedure,
477
class data members can be accessed like ordinary variables
478
using \fBTcl_SetVar()\fR, \fBTcl_GetVar()\fR, \fBTcl_TraceVar()\fR,
479
etc.  Class methods and procs can be executed like ordinary commands
480
using \fBTcl_Eval()\fR.  \fB[incr\ Tcl]\fR makes this possible by
481
automatically setting up the context before executing the C procedure.
482
.PP
483
This scheme provides a natural migration path for code development.
484
Classes can be developed quickly using Tcl code to implement the
485
bodies.  An entire application can be built and tested.  When
486
necessary, individual bodies can be implemented with C code to
487
improve performance.
488
 
489
.SH KEYWORDS
490
class, object, object-oriented

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.