| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH Tcl_LinkVar 3 7.5 Tcl "Tcl Library Procedures" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
.SH NAME |
| 12 |
Tcl_LinkVar, Tcl_UnlinkVar, Tcl_UpdateLinkedVar \- link Tcl variable to C variable |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
int |
| 18 |
\fBTcl_LinkVar\fR(\fIinterp, varName, addr, type\fR) |
| 19 |
.sp |
| 20 |
\fBTcl_UnlinkVar\fR(\fIinterp, varName\fR) |
| 21 |
.sp |
| 22 |
\fBTcl_UpdateLinkedVar\fR(\fIinterp, varName\fR) |
| 23 |
.SH ARGUMENTS |
| 24 |
.AS Tcl_Interp writable |
| 25 |
.AP Tcl_Interp *interp in |
| 26 |
Interpreter that contains \fIvarName\fR. |
| 27 |
Also used by \fBTcl_LinkVar\fR to return error messages. |
| 28 |
.AP "const char" *varName in |
| 29 |
Name of global variable. |
| 30 |
.AP char *addr in |
| 31 |
Address of C variable that is to be linked to \fIvarName\fR. |
| 32 |
.AP int type in |
| 33 |
Type of C variable. Must be one of \fBTCL_LINK_INT\fR, |
| 34 |
\fBTCL_LINK_UINT\fR, \fBTCL_LINK_CHAR\fR, \fBTCL_LINK_UCHAR\fR, |
| 35 |
\fBTCL_LINK_SHORT\fR, \fBTCL_LINK_USHORT\fR, \fBTCL_LINK_LONG\fR, |
| 36 |
\fBTCL_LINK_ULONG\fR, \fBTCL_LINK_WIDE_INT\fR, |
| 37 |
\fBTCL_LINK_WIDE_UINT\fR, \fBTCL_LINK_FLOAT\fR, |
| 38 |
\fBTCL_LINK_DOUBLE\fR, \fBTCL_LINK_BOOLEAN\fR, or |
| 39 |
\fBTCL_LINK_STRING\fR, optionally OR'ed with \fBTCL_LINK_READ_ONLY\fR |
| 40 |
to make Tcl variable read-only. |
| 41 |
.BE |
| 42 |
.SH DESCRIPTION |
| 43 |
.PP |
| 44 |
\fBTcl_LinkVar\fR uses variable traces to keep the Tcl variable |
| 45 |
named by \fIvarName\fR in sync with the C variable at the address |
| 46 |
given by \fIaddr\fR. |
| 47 |
Whenever the Tcl variable is read the value of the C variable will |
| 48 |
be returned, and whenever the Tcl variable is written the C |
| 49 |
variable will be updated to have the same value. |
| 50 |
\fBTcl_LinkVar\fR normally returns \fBTCL_OK\fR; if an error occurs |
| 51 |
while setting up the link (e.g. because \fIvarName\fR is the |
| 52 |
name of array) then \fBTCL_ERROR\fR is returned and the interpreter's result |
| 53 |
contains an error message. |
| 54 |
.PP |
| 55 |
The \fItype\fR argument specifies the type of the C variable, |
| 56 |
and must have one of the following values, optionally OR'ed with |
| 57 |
\fBTCL_LINK_READ_ONLY\fR: |
| 58 |
.TP |
| 59 |
\fBTCL_LINK_INT\fR |
| 60 |
The C variable is of type \fBint\fR. |
| 61 |
Any value written into the Tcl variable must have a proper integer |
| 62 |
form acceptable to \fBTcl_GetIntFromObj\fR; attempts to write |
| 63 |
non-integer values into \fIvarName\fR will be rejected with |
| 64 |
Tcl errors. Incomplete integer representations (like the empty |
| 65 |
string, '+', '-' or the hex/octal/binary prefix) are accepted |
| 66 |
as if they are valid too. |
| 67 |
.TP |
| 68 |
\fBTCL_LINK_UINT\fR |
| 69 |
The C variable is of type \fBunsigned int\fR. |
| 70 |
Any value written into the Tcl variable must have a proper unsigned |
| 71 |
integer form acceptable to \fBTcl_GetWideIntFromObj\fR and in the |
| 72 |
platform's defined range for the \fBunsigned int\fR type; attempts to |
| 73 |
write non-integer values (or values outside the range) into |
| 74 |
\fIvarName\fR will be rejected with Tcl errors. Incomplete integer |
| 75 |
representations (like the empty string, '+', '-' or the hex/octal/binary |
| 76 |
prefix) are accepted as if they are valid too. |
| 77 |
.TP |
| 78 |
\fBTCL_LINK_CHAR\fR |
| 79 |
The C variable is of type \fBchar\fR. |
| 80 |
Any value written into the Tcl variable must have a proper integer |
| 81 |
form acceptable to \fBTcl_GetIntFromObj\fR and be in the range of the |
| 82 |
\fBchar\fR datatype; attempts to write non-integer or out-of-range |
| 83 |
values into \fIvarName\fR will be rejected with Tcl errors. Incomplete |
| 84 |
integer representations (like the empty string, '+', '-' or the |
| 85 |
hex/octal/binary prefix) are accepted as if they are valid too. |
| 86 |
.TP |
| 87 |
\fBTCL_LINK_UCHAR\fR |
| 88 |
The C variable is of type \fBunsigned char\fR. |
| 89 |
Any value written into the Tcl variable must have a proper unsigned |
| 90 |
integer form acceptable to \fBTcl_GetIntFromObj\fR and in the |
| 91 |
platform's defined range for the \fBunsigned char\fR type; attempts to |
| 92 |
write non-integer values (or values outside the range) into |
| 93 |
\fIvarName\fR will be rejected with Tcl errors. Incomplete integer |
| 94 |
representations (like the empty string, '+', '-' or the hex/octal/binary |
| 95 |
prefix) are accepted as if they are valid too. |
| 96 |
.TP |
| 97 |
\fBTCL_LINK_SHORT\fR |
| 98 |
The C variable is of type \fBshort\fR. |
| 99 |
Any value written into the Tcl variable must have a proper integer |
| 100 |
form acceptable to \fBTcl_GetIntFromObj\fR and be in the range of the |
| 101 |
\fBshort\fR datatype; attempts to write non-integer or out-of-range |
| 102 |
values into \fIvarName\fR will be rejected with Tcl errors. Incomplete |
| 103 |
integer representations (like the empty string, '+', '-' or the |
| 104 |
hex/octal/binary prefix) are accepted as if they are valid too. |
| 105 |
.TP |
| 106 |
\fBTCL_LINK_USHORT\fR |
| 107 |
The C variable is of type \fBunsigned short\fR. |
| 108 |
Any value written into the Tcl variable must have a proper unsigned |
| 109 |
integer form acceptable to \fBTcl_GetIntFromObj\fR and in the |
| 110 |
platform's defined range for the \fBunsigned short\fR type; attempts to |
| 111 |
write non-integer values (or values outside the range) into |
| 112 |
\fIvarName\fR will be rejected with Tcl errors. Incomplete integer |
| 113 |
representations (like the empty string, '+', '-' or the hex/octal/binary |
| 114 |
prefix) are accepted as if they are valid too. |
| 115 |
.TP |
| 116 |
\fBTCL_LINK_LONG\fR |
| 117 |
The C variable is of type \fBlong\fR. |
| 118 |
Any value written into the Tcl variable must have a proper integer |
| 119 |
form acceptable to \fBTcl_GetLongFromObj\fR; attempts to write |
| 120 |
non-integer or out-of-range |
| 121 |
values into \fIvarName\fR will be rejected with Tcl errors. Incomplete |
| 122 |
integer representations (like the empty string, '+', '-' or the |
| 123 |
hex/octal/binary prefix) are accepted as if they are valid too. |
| 124 |
.TP |
| 125 |
\fBTCL_LINK_ULONG\fR |
| 126 |
The C variable is of type \fBunsigned long\fR. |
| 127 |
Any value written into the Tcl variable must have a proper unsigned |
| 128 |
integer form acceptable to \fBTcl_GetWideIntFromObj\fR and in the |
| 129 |
platform's defined range for the \fBunsigned long\fR type; attempts to |
| 130 |
write non-integer values (or values outside the range) into |
| 131 |
\fIvarName\fR will be rejected with Tcl errors. Incomplete integer |
| 132 |
representations (like the empty string, '+', '-' or the hex/octal/binary |
| 133 |
prefix) are accepted as if they are valid too. |
| 134 |
.TP |
| 135 |
\fBTCL_LINK_DOUBLE\fR |
| 136 |
The C variable is of type \fBdouble\fR. |
| 137 |
Any value written into the Tcl variable must have a proper real |
| 138 |
form acceptable to \fBTcl_GetDoubleFromObj\fR; attempts to write |
| 139 |
non-real values into \fIvarName\fR will be rejected with |
| 140 |
Tcl errors. Incomplete integer or real representations (like the |
| 141 |
empty string, '.', '+', '-' or the hex/octal/binary prefix) are |
| 142 |
accepted as if they are valid too. |
| 143 |
.TP |
| 144 |
\fBTCL_LINK_FLOAT\fR |
| 145 |
The C variable is of type \fBfloat\fR. |
| 146 |
Any value written into the Tcl variable must have a proper real |
| 147 |
form acceptable to \fBTcl_GetDoubleFromObj\fR and must be within the |
| 148 |
range acceptable for a \fBfloat\fR; attempts to |
| 149 |
write non-real values (or values outside the range) into |
| 150 |
\fIvarName\fR will be rejected with Tcl errors. Incomplete integer |
| 151 |
or real representations (like the empty string, '.', '+', '-' or |
| 152 |
the hex/octal/binary prefix) are accepted as if they are valid too. |
| 153 |
.TP |
| 154 |
\fBTCL_LINK_WIDE_INT\fR |
| 155 |
The C variable is of type \fBTcl_WideInt\fR (which is an integer type |
| 156 |
at least 64-bits wide on all platforms that can support it.) |
| 157 |
Any value written into the Tcl variable must have a proper integer |
| 158 |
form acceptable to \fBTcl_GetWideIntFromObj\fR; attempts to write |
| 159 |
non-integer values into \fIvarName\fR will be rejected with |
| 160 |
Tcl errors. Incomplete integer representations (like the empty |
| 161 |
string, '+', '-' or the hex/octal/binary prefix) are accepted |
| 162 |
as if they are valid too. |
| 163 |
.TP |
| 164 |
\fBTCL_LINK_WIDE_UINT\fR |
| 165 |
The C variable is of type \fBTcl_WideUInt\fR (which is an unsigned |
| 166 |
integer type at least 64-bits wide on all platforms that can support |
| 167 |
it.) |
| 168 |
Any value written into the Tcl variable must have a proper unsigned |
| 169 |
integer form acceptable to \fBTcl_GetWideIntFromObj\fR (it will be |
| 170 |
cast to unsigned); |
| 171 |
.\" FIXME! Use bignums instead. |
| 172 |
attempts to write non-integer values into \fIvarName\fR will be |
| 173 |
rejected with Tcl errors. Incomplete integer representations (like |
| 174 |
the empty string, '+', '-' or the hex/octal/binary prefix) are accepted |
| 175 |
as if they are valid too. |
| 176 |
.TP |
| 177 |
\fBTCL_LINK_BOOLEAN\fR |
| 178 |
The C variable is of type \fBint\fR. |
| 179 |
If its value is zero then it will read from Tcl as |
| 180 |
.QW 0 ; |
| 181 |
otherwise it will read from Tcl as |
| 182 |
.QW 1 . |
| 183 |
Whenever \fIvarName\fR is |
| 184 |
modified, the C variable will be set to a 0 or 1 value. |
| 185 |
Any value written into the Tcl variable must have a proper boolean |
| 186 |
form acceptable to \fBTcl_GetBooleanFromObj\fR; attempts to write |
| 187 |
non-boolean values into \fIvarName\fR will be rejected with |
| 188 |
Tcl errors. |
| 189 |
.TP |
| 190 |
\fBTCL_LINK_STRING\fR |
| 191 |
The C variable is of type \fBchar *\fR. |
| 192 |
If its value is not NULL then it must be a pointer to a string |
| 193 |
allocated with \fBTcl_Alloc\fR or \fBckalloc\fR. |
| 194 |
Whenever the Tcl variable is modified the current C string will be |
| 195 |
freed and new memory will be allocated to hold a copy of the variable's |
| 196 |
new value. |
| 197 |
If the C variable contains a NULL pointer then the Tcl variable |
| 198 |
will read as |
| 199 |
.QW NULL . |
| 200 |
.PP |
| 201 |
If the \fBTCL_LINK_READ_ONLY\fR flag is present in \fItype\fR then the |
| 202 |
variable will be read-only from Tcl, so that its value can only be |
| 203 |
changed by modifying the C variable. |
| 204 |
Attempts to write the variable from Tcl will be rejected with errors. |
| 205 |
.PP |
| 206 |
\fBTcl_UnlinkVar\fR removes the link previously set up for the |
| 207 |
variable given by \fIvarName\fR. If there does not exist a link |
| 208 |
for \fIvarName\fR then the procedure has no effect. |
| 209 |
.PP |
| 210 |
\fBTcl_UpdateLinkedVar\fR may be invoked after the C variable has |
| 211 |
changed to force the Tcl variable to be updated immediately. |
| 212 |
In many cases this procedure is not needed, since any attempt to |
| 213 |
read the Tcl variable will return the latest value of the C variable. |
| 214 |
However, if a trace has been set on the Tcl variable (such as a |
| 215 |
Tk widget that wishes to display the value of the variable), the |
| 216 |
trace will not trigger when the C variable has changed. |
| 217 |
\fBTcl_UpdateLinkedVar\fR ensures that any traces on the Tcl |
| 218 |
variable are invoked. |
| 219 |
.PP |
| 220 |
Note that, as with any call to a Tcl interpreter, \fBTcl_UpdateLinkedVar\fR |
| 221 |
must be called from the same thread that created the interpreter. The safest |
| 222 |
mechanism is to ensure that the C variable is only ever updated from the same |
| 223 |
thread that created the interpreter (possibly in response to an event posted |
| 224 |
with \fBTcl_ThreadQueueEvent\fR), but when it is necessary to update the |
| 225 |
variable in a separate thread, it is advised that \fBTcl_AsyncMark\fR be used |
| 226 |
to indicate to the thread hosting the interpreter that it is ready to run |
| 227 |
\fBTcl_UpdateLinkedVar\fR. |
| 228 |
.SH "SEE ALSO" |
| 229 |
Tcl_TraceVar(3) |
| 230 |
.SH KEYWORDS |
| 231 |
boolean, integer, link, read-only, real, string, trace, variable |