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: body.n,v 1.1.1.1 2002-01-16 10:24:46 markom Exp $
|
8 |
|
|
'\"
|
9 |
|
|
.so man.macros
|
10 |
|
|
.TH body n 3.0 itcl "[incr\ Tcl]"
|
11 |
|
|
.BS
|
12 |
|
|
'\" Note: do not modify the .SH NAME line immediately below!
|
13 |
|
|
.SH NAME
|
14 |
|
|
body \- change the body for a class method/proc
|
15 |
|
|
.SH SYNOPSIS
|
16 |
|
|
\fBbody \fIclassName\fB::\fIfunction args body\fR
|
17 |
|
|
.BE
|
18 |
|
|
|
19 |
|
|
.SH DESCRIPTION
|
20 |
|
|
.PP
|
21 |
|
|
The \fBbody\fR command is used outside of an \fB[incr\ Tcl]\fR
|
22 |
|
|
class definition to define or redefine the body of a class
|
23 |
|
|
method or proc. This facility allows a class definition
|
24 |
|
|
to have separate "interface" and "implementation" parts.
|
25 |
|
|
The "interface" part is a \fBclass\fR command with declarations
|
26 |
|
|
for methods, procs, instance variables and common variables.
|
27 |
|
|
The "implementation" part is a series of \fBbody\fR and
|
28 |
|
|
\fBconfigbody\fR commands. If the "implementation" part
|
29 |
|
|
is kept in a separate file, it can be sourced again and
|
30 |
|
|
again as bugs are fixed, to support interactive development.
|
31 |
|
|
When using the "tcl" mode in the \fBemacs\fR editor, the
|
32 |
|
|
"interface" and "implementation" parts can be kept in the
|
33 |
|
|
same file; as bugs are fixed, individual bodies can be
|
34 |
|
|
highlighted and sent to the test application.
|
35 |
|
|
.PP
|
36 |
|
|
The name "\fIclassName\fB::\fIfunction\fR"
|
37 |
|
|
identifies the method/proc being changed.
|
38 |
|
|
.PP
|
39 |
|
|
If an \fIargs\fR list was specified when the \fIfunction\fR was
|
40 |
|
|
defined in the class definition, the \fIargs\fR list for the
|
41 |
|
|
\fBbody\fR command must match in meaning. Variable names
|
42 |
|
|
can change, but the argument lists must have the same required
|
43 |
|
|
arguments and the same default values for optional arguments.
|
44 |
|
|
The special \fBargs\fR argument acts as a wildcard when included
|
45 |
|
|
in the \fIargs\fR list in the class definition; it will match
|
46 |
|
|
zero or more arguments of any type when the body is redefined.
|
47 |
|
|
.PP
|
48 |
|
|
If the \fIbody\fR string starts with "\fB@\fR", it is treated
|
49 |
|
|
as the symbolic name for a C procedure. The \fIargs\fR list
|
50 |
|
|
has little meaning for the C procedure, except to document
|
51 |
|
|
the expected usage. (The C procedure is not guaranteed to
|
52 |
|
|
use arguments in this manner.) If \fIbody\fR does not start
|
53 |
|
|
with "\fB@\fR", it is treated as a Tcl command script. When
|
54 |
|
|
the function is invoked, command line arguments are matched
|
55 |
|
|
against the \fIargs\fR list, and local variables are created
|
56 |
|
|
to represent each argument. This is the usual behavior for
|
57 |
|
|
a Tcl-style proc.
|
58 |
|
|
.PP
|
59 |
|
|
Symbolic names for C procedures are established by registering
|
60 |
|
|
procedures via \fBItcl_RegisterC()\fR. This is usually done
|
61 |
|
|
in the \fBTcl_AppInit()\fR procedure, which is automatically called
|
62 |
|
|
when the interpreter starts up. In the following example,
|
63 |
|
|
the procedure \fCMy_FooCmd()\fR is registered with the
|
64 |
|
|
symbolic name "foo". This procedure can be referenced in
|
65 |
|
|
the \fBbody\fR command as "\fC@foo\fR".
|
66 |
|
|
.CS
|
67 |
|
|
int
|
68 |
|
|
Tcl_AppInit(interp)
|
69 |
|
|
Tcl_Interp *interp; /* Interpreter for application. */
|
70 |
|
|
{
|
71 |
|
|
if (Itcl_Init(interp) == TCL_ERROR) {
|
72 |
|
|
return TCL_ERROR;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
if (Itcl_RegisterC(interp, "foo", My_FooCmd) != TCL_OK) {
|
76 |
|
|
return TCL_ERROR;
|
77 |
|
|
}
|
78 |
|
|
}
|
79 |
|
|
.CE
|
80 |
|
|
|
81 |
|
|
.SH EXAMPLE
|
82 |
|
|
In the following example, a "File" class is defined to represent
|
83 |
|
|
open files. The method bodies are included below the class
|
84 |
|
|
definition via the \fBbody\fR command. Note that the bodies
|
85 |
|
|
of the constructor/destructor must be included in the class
|
86 |
|
|
definition, but they can be redefined via the \fBbody\fR command
|
87 |
|
|
as well.
|
88 |
|
|
.CS
|
89 |
|
|
class File {
|
90 |
|
|
private variable fid ""
|
91 |
|
|
constructor {name access} {
|
92 |
|
|
set fid [open $name $access]
|
93 |
|
|
}
|
94 |
|
|
destructor {
|
95 |
|
|
close $fid
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
method get {}
|
99 |
|
|
method put {line}
|
100 |
|
|
method eof {}
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
body File::get {} {
|
104 |
|
|
return [gets $fid]
|
105 |
|
|
}
|
106 |
|
|
body File::put {line} {
|
107 |
|
|
puts $fid $line
|
108 |
|
|
}
|
109 |
|
|
body File::eof {} {
|
110 |
|
|
return [::eof $fid]
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
#
|
114 |
|
|
# See the File class in action:
|
115 |
|
|
#
|
116 |
|
|
File x /etc/passwd "r"
|
117 |
|
|
while {![x eof]} {
|
118 |
|
|
puts "=> [x get]"
|
119 |
|
|
}
|
120 |
|
|
delete object x
|
121 |
|
|
.CE
|
122 |
|
|
|
123 |
|
|
.SH KEYWORDS
|
124 |
|
|
class, object, procedure
|