OSDN Git Service

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