| 1 |
'\" |
| 2 |
'\" Copyright (c) 1996-1997 Sun Microsystems, 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 |
.TH Tcl_ObjType 3 8.0 Tcl "Tcl Library Procedures" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
.SH NAME |
| 11 |
Tcl_RegisterObjType, Tcl_GetObjType, Tcl_AppendAllObjTypes, Tcl_ConvertToType \- manipulate Tcl value types |
| 12 |
.SH SYNOPSIS |
| 13 |
.nf |
| 14 |
\fB#include <tcl.h>\fR |
| 15 |
.sp |
| 16 |
\fBTcl_RegisterObjType\fR(\fItypePtr\fR) |
| 17 |
.sp |
| 18 |
const Tcl_ObjType * |
| 19 |
\fBTcl_GetObjType\fR(\fItypeName\fR) |
| 20 |
.sp |
| 21 |
int |
| 22 |
\fBTcl_AppendAllObjTypes\fR(\fIinterp, objPtr\fR) |
| 23 |
.sp |
| 24 |
int |
| 25 |
\fBTcl_ConvertToType\fR(\fIinterp, objPtr, typePtr\fR) |
| 26 |
.SH ARGUMENTS |
| 27 |
.AS "const char" *typeName |
| 28 |
.AP "const Tcl_ObjType" *typePtr in |
| 29 |
Points to the structure containing information about the Tcl value type. |
| 30 |
This storage must live forever, |
| 31 |
typically by being statically allocated. |
| 32 |
.AP "const char" *typeName in |
| 33 |
The name of a Tcl value type that \fBTcl_GetObjType\fR should look up. |
| 34 |
.AP Tcl_Interp *interp in |
| 35 |
Interpreter to use for error reporting. |
| 36 |
.AP Tcl_Obj *objPtr in |
| 37 |
For \fBTcl_AppendAllObjTypes\fR, this points to the value onto which |
| 38 |
it appends the name of each value type as a list element. |
| 39 |
For \fBTcl_ConvertToType\fR, this points to a value that |
| 40 |
must have been the result of a previous call to \fBTcl_NewObj\fR. |
| 41 |
.BE |
| 42 |
|
| 43 |
.SH DESCRIPTION |
| 44 |
.PP |
| 45 |
The procedures in this man page manage Tcl value types (sometimes |
| 46 |
referred to as object types or \fBTcl_ObjType\fRs for historical reasons). |
| 47 |
They are used to register new value types, look up types, |
| 48 |
and force conversions from one type to another. |
| 49 |
.PP |
| 50 |
\fBTcl_RegisterObjType\fR registers a new Tcl value type |
| 51 |
in the table of all value types that \fBTcl_GetObjType\fR |
| 52 |
can look up by name. There are other value types supported by Tcl |
| 53 |
as well, which Tcl chooses not to register. Extensions can likewise |
| 54 |
choose to register the value types they create or not. |
| 55 |
The argument \fItypePtr\fR points to a Tcl_ObjType structure that |
| 56 |
describes the new type by giving its name |
| 57 |
and by supplying pointers to four procedures |
| 58 |
that implement the type. |
| 59 |
If the type table already contains a type |
| 60 |
with the same name as in \fItypePtr\fR, |
| 61 |
it is replaced with the new type. |
| 62 |
The Tcl_ObjType structure is described |
| 63 |
in the section \fBTHE TCL_OBJTYPE STRUCTURE\fR below. |
| 64 |
.PP |
| 65 |
\fBTcl_GetObjType\fR returns a pointer to the registered Tcl_ObjType |
| 66 |
with name \fItypeName\fR. |
| 67 |
It returns NULL if no type with that name is registered. |
| 68 |
.PP |
| 69 |
\fBTcl_AppendAllObjTypes\fR appends the name of each registered value type |
| 70 |
as a list element onto the Tcl value referenced by \fIobjPtr\fR. |
| 71 |
The return value is \fBTCL_OK\fR unless there was an error |
| 72 |
converting \fIobjPtr\fR to a list value; |
| 73 |
in that case \fBTCL_ERROR\fR is returned. |
| 74 |
.PP |
| 75 |
\fBTcl_ConvertToType\fR converts a value from one type to another |
| 76 |
if possible. |
| 77 |
It creates a new internal representation for \fIobjPtr\fR |
| 78 |
appropriate for the target type \fItypePtr\fR |
| 79 |
and sets its \fItypePtr\fR member as determined by calling the |
| 80 |
\fItypePtr->setFromAnyProc\fR routine. |
| 81 |
Any internal representation for \fIobjPtr\fR's old type is freed. |
| 82 |
If an error occurs during conversion, it returns \fBTCL_ERROR\fR |
| 83 |
and leaves an error message in the result value for \fIinterp\fR |
| 84 |
unless \fIinterp\fR is NULL. |
| 85 |
Otherwise, it returns \fBTCL_OK\fR. |
| 86 |
Passing a NULL \fIinterp\fR allows this procedure to be used |
| 87 |
as a test whether the conversion can be done (and in fact was done). |
| 88 |
.PP |
| 89 |
In many cases, the \fItypePtr->setFromAnyProc\fR routine will |
| 90 |
set \fIobjPtr->typePtr\fR to the argument value \fItypePtr\fR, |
| 91 |
but that is no longer guaranteed. The \fIsetFromAnyProc\fR is |
| 92 |
free to set the internal representation for \fIobjPtr\fR to make |
| 93 |
use of another related Tcl_ObjType, if it sees fit. |
| 94 |
.SH "THE TCL_OBJTYPE STRUCTURE" |
| 95 |
.PP |
| 96 |
Extension writers can define new value types by defining four |
| 97 |
procedures and |
| 98 |
initializing a Tcl_ObjType structure to describe the type. |
| 99 |
Extension writers may also pass a pointer to their Tcl_ObjType |
| 100 |
structure to \fBTcl_RegisterObjType\fR if they wish to permit |
| 101 |
other extensions to look up their Tcl_ObjType by name with |
| 102 |
the \fBTcl_GetObjType\fR routine. |
| 103 |
The \fBTcl_ObjType\fR structure is defined as follows: |
| 104 |
.PP |
| 105 |
.CS |
| 106 |
typedef struct Tcl_ObjType { |
| 107 |
const char *\fIname\fR; |
| 108 |
Tcl_FreeInternalRepProc *\fIfreeIntRepProc\fR; |
| 109 |
Tcl_DupInternalRepProc *\fIdupIntRepProc\fR; |
| 110 |
Tcl_UpdateStringProc *\fIupdateStringProc\fR; |
| 111 |
Tcl_SetFromAnyProc *\fIsetFromAnyProc\fR; |
| 112 |
} \fBTcl_ObjType\fR; |
| 113 |
.CE |
| 114 |
.SS "THE NAME FIELD" |
| 115 |
.PP |
| 116 |
The \fIname\fR member describes the name of the type, e.g. \fBint\fR. |
| 117 |
When a type is registered, this is the name used by callers |
| 118 |
of \fBTcl_GetObjType\fR to lookup the type. For unregistered |
| 119 |
types, the \fIname\fR field is primarily of value for debugging. |
| 120 |
The remaining four members are pointers to procedures |
| 121 |
called by the generic Tcl value code: |
| 122 |
.SS "THE SETFROMANYPROC FIELD" |
| 123 |
.PP |
| 124 |
The \fIsetFromAnyProc\fR member contains the address of a function |
| 125 |
called to create a valid internal representation |
| 126 |
from a value's string representation. |
| 127 |
.PP |
| 128 |
.CS |
| 129 |
typedef int \fBTcl_SetFromAnyProc\fR( |
| 130 |
Tcl_Interp *\fIinterp\fR, |
| 131 |
Tcl_Obj *\fIobjPtr\fR); |
| 132 |
.CE |
| 133 |
.PP |
| 134 |
If an internal representation cannot be created from the string, |
| 135 |
it returns \fBTCL_ERROR\fR and puts a message |
| 136 |
describing the error in the result value for \fIinterp\fR |
| 137 |
unless \fIinterp\fR is NULL. |
| 138 |
If \fIsetFromAnyProc\fR is successful, |
| 139 |
it stores the new internal representation, |
| 140 |
sets \fIobjPtr\fR's \fItypePtr\fR member to point to |
| 141 |
the \fBTcl_ObjType\fR struct corresponding to the new |
| 142 |
internal representation, and returns \fBTCL_OK\fR. |
| 143 |
Before setting the new internal representation, |
| 144 |
the \fIsetFromAnyProc\fR must free any internal representation |
| 145 |
of \fIobjPtr\fR's old type; |
| 146 |
it does this by calling the old type's \fIfreeIntRepProc\fR |
| 147 |
if it is not NULL. |
| 148 |
.PP |
| 149 |
As an example, the \fIsetFromAnyProc\fR for the built-in Tcl list type |
| 150 |
gets an up-to-date string representation for \fIobjPtr\fR |
| 151 |
by calling \fBTcl_GetStringFromObj\fR. |
| 152 |
It parses the string to verify it is in a valid list format and |
| 153 |
to obtain each element value in the list, and, if this succeeds, |
| 154 |
stores the list elements in \fIobjPtr\fR's internal representation |
| 155 |
and sets \fIobjPtr\fR's \fItypePtr\fR member to point to the list type's |
| 156 |
Tcl_ObjType structure. |
| 157 |
.PP |
| 158 |
Do not release \fIobjPtr\fR's old internal representation unless you |
| 159 |
replace it with a new one or reset the \fItypePtr\fR member to NULL. |
| 160 |
.PP |
| 161 |
The \fIsetFromAnyProc\fR member may be set to NULL, if the routines |
| 162 |
making use of the internal representation have no need to derive that |
| 163 |
internal representation from an arbitrary string value. However, in |
| 164 |
this case, passing a pointer to the type to \fBTcl_ConvertToType\fR will |
| 165 |
lead to a panic, so to avoid this possibility, the type |
| 166 |
should \fInot\fR be registered. |
| 167 |
.SS "THE UPDATESTRINGPROC FIELD" |
| 168 |
.PP |
| 169 |
The \fIupdateStringProc\fR member contains the address of a function |
| 170 |
called to create a valid string representation |
| 171 |
from a value's internal representation. |
| 172 |
.PP |
| 173 |
.CS |
| 174 |
typedef void \fBTcl_UpdateStringProc\fR( |
| 175 |
Tcl_Obj *\fIobjPtr\fR); |
| 176 |
.CE |
| 177 |
.PP |
| 178 |
\fIobjPtr\fR's \fIbytes\fR member is always NULL when it is called. |
| 179 |
It must always set \fIbytes\fR non-NULL before returning. |
| 180 |
We require the string representation's byte array |
| 181 |
to have a null after the last byte, at offset \fIlength\fR, |
| 182 |
and to have no null bytes before that; this allows string representations |
| 183 |
to be treated as conventional null character-terminated C strings. |
| 184 |
These restrictions are easily met by using Tcl's internal UTF encoding |
| 185 |
for the string representation, same as one would do for other |
| 186 |
Tcl routines accepting string values as arguments. |
| 187 |
Storage for the byte array must be allocated in the heap by \fBTcl_Alloc\fR |
| 188 |
or \fBckalloc\fR. Note that \fIupdateStringProc\fRs must allocate |
| 189 |
enough storage for the string's bytes and the terminating null byte. |
| 190 |
.PP |
| 191 |
The \fIupdateStringProc\fR for Tcl's built-in double type, for example, |
| 192 |
calls Tcl_PrintDouble to write to a buffer of size TCL_DOUBLE_SPACE, |
| 193 |
then allocates and copies the string representation to just enough |
| 194 |
space to hold it. A pointer to the allocated space is stored in |
| 195 |
the \fIbytes\fR member. |
| 196 |
.PP |
| 197 |
The \fIupdateStringProc\fR member may be set to NULL, if the routines |
| 198 |
making use of the internal representation are written so that the |
| 199 |
string representation is never invalidated. Failure to meet this |
| 200 |
obligation will lead to panics or crashes when \fBTcl_GetStringFromObj\fR |
| 201 |
or other similar routines ask for the string representation. |
| 202 |
.SS "THE DUPINTREPPROC FIELD" |
| 203 |
.PP |
| 204 |
The \fIdupIntRepProc\fR member contains the address of a function |
| 205 |
called to copy an internal representation from one value to another. |
| 206 |
.PP |
| 207 |
.CS |
| 208 |
typedef void \fBTcl_DupInternalRepProc\fR( |
| 209 |
Tcl_Obj *\fIsrcPtr\fR, |
| 210 |
Tcl_Obj *\fIdupPtr\fR); |
| 211 |
.CE |
| 212 |
.PP |
| 213 |
\fIdupPtr\fR's internal representation is made a copy of \fIsrcPtr\fR's |
| 214 |
internal representation. |
| 215 |
Before the call, |
| 216 |
\fIsrcPtr\fR's internal representation is valid and \fIdupPtr\fR's is not. |
| 217 |
\fIsrcPtr\fR's value type determines what |
| 218 |
copying its internal representation means. |
| 219 |
.PP |
| 220 |
For example, the \fIdupIntRepProc\fR for the Tcl integer type |
| 221 |
simply copies an integer. |
| 222 |
The built-in list type's \fIdupIntRepProc\fR uses a far more |
| 223 |
sophisticated scheme to continue sharing storage as much as it |
| 224 |
reasonably can. |
| 225 |
.SS "THE FREEINTREPPROC FIELD" |
| 226 |
.PP |
| 227 |
The \fIfreeIntRepProc\fR member contains the address of a function |
| 228 |
that is called when a value is freed. |
| 229 |
.PP |
| 230 |
.CS |
| 231 |
typedef void \fBTcl_FreeInternalRepProc\fR( |
| 232 |
Tcl_Obj *\fIobjPtr\fR); |
| 233 |
.CE |
| 234 |
.PP |
| 235 |
The \fIfreeIntRepProc\fR function can deallocate the storage |
| 236 |
for the value's internal representation |
| 237 |
and do other type-specific processing necessary when a value is freed. |
| 238 |
.PP |
| 239 |
For example, the list type's \fIfreeIntRepProc\fR respects |
| 240 |
the storage sharing scheme established by the \fIdupIntRepProc\fR |
| 241 |
so that it only frees storage when the last value sharing it |
| 242 |
is being freed. |
| 243 |
.PP |
| 244 |
The \fIfreeIntRepProc\fR member can be set to NULL |
| 245 |
to indicate that the internal representation does not require freeing. |
| 246 |
The \fIfreeIntRepProc\fR implementation must not access the |
| 247 |
\fIbytes\fR member of the value, since Tcl makes its own internal |
| 248 |
uses of that field during value deletion. The defined tasks for |
| 249 |
the \fIfreeIntRepProc\fR have no need to consult the \fIbytes\fR |
| 250 |
member. |
| 251 |
.SH "SEE ALSO" |
| 252 |
Tcl_NewObj(3), Tcl_DecrRefCount(3), Tcl_IncrRefCount(3) |
| 253 |
.SH KEYWORDS |
| 254 |
internal representation, value, value type, string representation, type conversion |