1configbody(n)                     [incr Tcl]                     configbody(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       itcl::configbody - change the "config" code for a public variable
9

SYNOPSIS

11       itcl::configbody className::varName body
12______________________________________________________________________________
13
14

DESCRIPTION

16       The  configbody  command is used outside of an [incr Tcl] class defini‐
17       tion to define or redefine the configuration  code  associated  with  a
18       public  variable.   Public variables act like configuration options for
19       an object.  They can be modified outside  the  class  scope  using  the
20       built-in  configure  method.   Each variable can have a bit of "config"
21       code associate with it that is automatically executed when the variable
22       is  configured.   The configbody command can be used to define or rede‐
23       fine this body of code.
24
25       Like the body command, this facility allows a class definition to  have
26       separate  "interface" and "implementation" parts.  The "interface" part
27       is a class command with declarations for methods, procs, instance vari‐
28       ables  and  common variables.  The "implementation" part is a series of
29       body and configbody commands.  If the "implementation" part is kept  in
30       a  separate  file, it can be sourced again and again as bugs are fixed,
31       to support interactive development.  When using the "tcl" mode  in  the
32       emacs editor, the "interface" and "implementation" parts can be kept in
33       the same file; as bugs are fixed, individual bodies can be  highlighted
34       and sent to the test application.
35
36       The  name  "className::varName"  identifies  the  public variable being
37       updated.  If the body string starts with "@", it is treated as the sym‐
38       bolic  name  for a C procedure.  Otherwise, it is treated as a Tcl com‐
39       mand script.
40
41       Symbolic names for C procedures are established by  registering  proce‐
42       dures  via Itcl_RegisterC().  This is usually done in the Tcl_AppInit()
43       procedure, which is automatically called when  the  interpreter  starts
44       up.   In the following example, the procedure My_FooCmd() is registered
45       with the symbolic name "foo".  This procedure can be referenced in  the
46       configbody command as "@foo".
47              int
48              Tcl_AppInit(interp)
49                  Tcl_Interp *interp;     /* Interpreter for application. */
50              {
51                  if (Itcl_Init(interp) == TCL_ERROR) {
52                      return TCL_ERROR;
53                  }
54
55                  if (Itcl_RegisterC(interp, "foo", My_FooCmd) != TCL_OK) {
56                      return TCL_ERROR;
57                  }
58              }
59
60

EXAMPLE

62       In  the  following example, a "File" class is defined to represent open
63       files.  Whenever the "-name" option is configured, the existing file is
64       closed,  and  a  new file is opened.  Note that the "config" code for a
65       public variable is optional.  The "-access" option, for  example,  does
66       not have it.
67              itcl::class File {
68                  private variable fid ""
69
70                  public variable name ""
71                  public variable access "r"
72
73                  constructor {args} {
74                      eval configure $args
75                  }
76                  destructor {
77                      if {$fid != ""} {
78                          close $fid
79                      }
80                  }
81
82                  method get {}
83                  method put {line}
84                  method eof {}
85              }
86
87              itcl::body File::get {} {
88                  return [gets $fid]
89              }
90              itcl::body File::put {line} {
91                  puts $fid $line
92              }
93              itcl::body File::eof {} {
94                  return [::eof $fid]
95              }
96
97              itcl::configbody File::name {
98                  if {$fid != ""} {
99                      close $fid
100                  }
101                  set fid [open $name $access]
102              }
103
104              #
105              # See the File class in action:
106              #
107              File x
108
109              x configure -name /etc/passwd
110              while {![x eof]} {
111                  puts "=> [x get]"
112              }
113              itcl::delete object x
114
115

KEYWORDS

117       class, object, variable, configure
118
119
120
121itcl                                  3.0                        configbody(n)
Impressum