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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [itcl/] [itcl/] [doc/] [configbody.n] - Blame information for rev 1771

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: configbody.n,v 1.1.1.1 2002-01-16 10:24:46 markom Exp $
8
'\"
9
.so man.macros
10
.TH configbody n 3.0 itcl "[incr\ Tcl]"
11
.BS
12
'\" Note:  do not modify the .SH NAME line immediately below!
13
.SH NAME
14
configbody \- change the "config" code for a public variable
15
.SH SYNOPSIS
16
\fBconfigbody \fIclassName\fB::\fIvarName body\fR
17
.BE
18
 
19
.SH DESCRIPTION
20
.PP
21
The \fBconfigbody\fR command is used outside of an \fB[incr\ Tcl]\fR
22
class definition to define or redefine the configuration code
23
associated with a public variable.  Public variables act like
24
configuration options for an object.  They can be modified
25
outside the class scope using the built-in \fBconfigure\fR method.
26
Each variable can have a bit of "config" code associate with it
27
that is automatically executed when the variable is configured.
28
The \fBconfigbody\fR command can be used to define or redefine
29
this body of code.
30
.PP
31
Like the \fBbody\fR command, this facility allows a class definition
32
to have separate "interface" and "implementation" parts.
33
The "interface" part is a \fBclass\fR command with declarations
34
for methods, procs, instance variables and common variables.
35
The "implementation" part is a series of \fBbody\fR and
36
\fBconfigbody\fR commands.  If the "implementation" part
37
is kept in a separate file, it can be sourced again and
38
again as bugs are fixed, to support interactive development.
39
When using the "tcl" mode in the \fBemacs\fR editor, the
40
"interface" and "implementation" parts can be kept in the
41
same file; as bugs are fixed, individual bodies can be
42
highlighted and sent to the test application.
43
.PP
44
The name "\fIclassName\fB::\fIvarName\fR"
45
identifies the public variable being updated.
46
If the \fIbody\fR string starts with "\fB@\fR", it is treated
47
as the symbolic name for a C procedure.  Otherwise, it is
48
treated as a Tcl command script.
49
.PP
50
Symbolic names for C procedures are established by registering
51
procedures via \fBItcl_RegisterC()\fR.  This is usually done
52
in the \fBTcl_AppInit()\fR procedure, which is automatically called
53
when the interpreter starts up.  In the following example,
54
the procedure \fCMy_FooCmd()\fR is registered with the
55
symbolic name "foo".  This procedure can be referenced in
56
the \fBconfigbody\fR command as "\fC@foo\fR".
57
.CS
58
int
59
Tcl_AppInit(interp)
60
    Tcl_Interp *interp;     /* Interpreter for application. */
61
{
62
    if (Itcl_Init(interp) == TCL_ERROR) {
63
        return TCL_ERROR;
64
    }
65
 
66
    if (Itcl_RegisterC(interp, "foo", My_FooCmd) != TCL_OK) {
67
        return TCL_ERROR;
68
    }
69
}
70
.CE
71
 
72
.SH EXAMPLE
73
In the following example, a "File" class is defined to represent
74
open files.  Whenever the "-name" option is configured, the
75
existing file is closed, and a new file is opened.  Note that
76
the "config" code for a public variable is optional.  The "-access"
77
option, for example, does not have it.
78
.CS
79
class File {
80
    private variable fid ""
81
 
82
    public variable name ""
83
    public variable access "r"
84
 
85
    constructor {args} {
86
        eval configure $args
87
    }
88
    destructor {
89
        if {$fid != ""} {
90
            close $fid
91
        }
92
    }
93
 
94
    method get {}
95
    method put {line}
96
    method eof {}
97
}
98
 
99
body File::get {} {
100
    return [gets $fid]
101
}
102
body File::put {line} {
103
    puts $fid $line
104
}
105
body File::eof {} {
106
    return [::eof $fid]
107
}
108
 
109
configbody File::name {
110
    if {$fid != ""} {
111
        close $fid
112
    }
113
    set fid [open $name $access]
114
}
115
 
116
#
117
# See the File class in action:
118
#
119
File x
120
 
121
x configure -name /etc/passwd
122
while {![x eof]} {
123
    puts "=> [x get]"
124
}
125
delete object x
126
.CE
127
 
128
.SH KEYWORDS
129
class, object, variable, configure

powered by: WebSVN 2.1.0

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