| 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 | '\" RCS: @(#) $Id: ObjectType.3,v 1.16 2007/12/13 15:22:31 dgp Exp $ |
|---|
| 8 | '\" |
|---|
| 9 | .so man.macros |
|---|
| 10 | .TH Tcl_ObjType 3 8.0 Tcl "Tcl Library Procedures" |
|---|
| 11 | .BS |
|---|
| 12 | .SH NAME |
|---|
| 13 | Tcl_RegisterObjType, Tcl_GetObjType, Tcl_AppendAllObjTypes, Tcl_ConvertToType \- manipulate Tcl object types |
|---|
| 14 | .SH SYNOPSIS |
|---|
| 15 | .nf |
|---|
| 16 | \fB#include <tcl.h>\fR |
|---|
| 17 | .sp |
|---|
| 18 | \fBTcl_RegisterObjType\fR(\fItypePtr\fR) |
|---|
| 19 | .sp |
|---|
| 20 | Tcl_ObjType * |
|---|
| 21 | \fBTcl_GetObjType\fR(\fItypeName\fR) |
|---|
| 22 | .sp |
|---|
| 23 | int |
|---|
| 24 | \fBTcl_AppendAllObjTypes\fR(\fIinterp, objPtr\fR) |
|---|
| 25 | .sp |
|---|
| 26 | int |
|---|
| 27 | \fBTcl_ConvertToType\fR(\fIinterp, objPtr, typePtr\fR) |
|---|
| 28 | .SH ARGUMENTS |
|---|
| 29 | .AS "const char" *typeName |
|---|
| 30 | .AP Tcl_ObjType *typePtr in |
|---|
| 31 | Points to the structure containing information about the Tcl object type. |
|---|
| 32 | This storage must live forever, |
|---|
| 33 | typically by being statically allocated. |
|---|
| 34 | .AP "const char" *typeName in |
|---|
| 35 | The name of a Tcl object type that \fBTcl_GetObjType\fR should look up. |
|---|
| 36 | .AP Tcl_Interp *interp in |
|---|
| 37 | Interpreter to use for error reporting. |
|---|
| 38 | .AP Tcl_Obj *objPtr in |
|---|
| 39 | For \fBTcl_AppendAllObjTypes\fR, this points to the object onto which |
|---|
| 40 | it appends the name of each object type as a list element. |
|---|
| 41 | For \fBTcl_ConvertToType\fR, this points to an object that |
|---|
| 42 | must have been the result of a previous call to \fBTcl_NewObj\fR. |
|---|
| 43 | .BE |
|---|
| 44 | |
|---|
| 45 | .SH DESCRIPTION |
|---|
| 46 | .PP |
|---|
| 47 | The procedures in this man page manage Tcl object types. |
|---|
| 48 | They are used to register new object types, look up types, |
|---|
| 49 | and force conversions from one type to another. |
|---|
| 50 | .PP |
|---|
| 51 | \fBTcl_RegisterObjType\fR registers a new Tcl object type |
|---|
| 52 | in the table of all object types supported by Tcl. |
|---|
| 53 | The argument \fItypePtr\fR points to a Tcl_ObjType structure that |
|---|
| 54 | describes the new type by giving its name |
|---|
| 55 | and by supplying pointers to four procedures |
|---|
| 56 | that implement the type. |
|---|
| 57 | If the type table already contains a type |
|---|
| 58 | with the same name as in \fItypePtr\fR, |
|---|
| 59 | it is replaced with the new type. |
|---|
| 60 | The Tcl_ObjType structure is described |
|---|
| 61 | in the section \fBTHE TCL_OBJTYPE STRUCTURE\fR below. |
|---|
| 62 | .PP |
|---|
| 63 | \fBTcl_GetObjType\fR returns a pointer to the Tcl_ObjType |
|---|
| 64 | with name \fItypeName\fR. |
|---|
| 65 | It returns NULL if no type with that name is registered. |
|---|
| 66 | .PP |
|---|
| 67 | \fBTcl_AppendAllObjTypes\fR appends the name of each object type |
|---|
| 68 | as a list element onto the Tcl object referenced by \fIobjPtr\fR. |
|---|
| 69 | The return value is \fBTCL_OK\fR unless there was an error |
|---|
| 70 | converting \fIobjPtr\fR to a list object; |
|---|
| 71 | in that case \fBTCL_ERROR\fR is returned. |
|---|
| 72 | .PP |
|---|
| 73 | \fBTcl_ConvertToType\fR converts an object from one type to another |
|---|
| 74 | if possible. |
|---|
| 75 | It creates a new internal representation for \fIobjPtr\fR |
|---|
| 76 | appropriate for the target type \fItypePtr\fR |
|---|
| 77 | and sets its \fItypePtr\fR member to that type. |
|---|
| 78 | Any internal representation for \fIobjPtr\fR's old type is freed. |
|---|
| 79 | If an error occurs during conversion, it returns \fBTCL_ERROR\fR |
|---|
| 80 | and leaves an error message in the result object for \fIinterp\fR |
|---|
| 81 | unless \fIinterp\fR is NULL. |
|---|
| 82 | Otherwise, it returns \fBTCL_OK\fR. |
|---|
| 83 | Passing a NULL \fIinterp\fR allows this procedure to be used |
|---|
| 84 | as a test whether the conversion can be done (and in fact was done). |
|---|
| 85 | .SH "THE TCL_OBJTYPE STRUCTURE" |
|---|
| 86 | .PP |
|---|
| 87 | Extension writers can define new object types by defining four |
|---|
| 88 | procedures, |
|---|
| 89 | initializing a Tcl_ObjType structure to describe the type, |
|---|
| 90 | and calling \fBTcl_RegisterObjType\fR. |
|---|
| 91 | The \fBTcl_ObjType\fR structure is defined as follows: |
|---|
| 92 | .PP |
|---|
| 93 | .CS |
|---|
| 94 | typedef struct Tcl_ObjType { |
|---|
| 95 | char *\fIname\fR; |
|---|
| 96 | Tcl_FreeInternalRepProc *\fIfreeIntRepProc\fR; |
|---|
| 97 | Tcl_DupInternalRepProc *\fIdupIntRepProc\fR; |
|---|
| 98 | Tcl_UpdateStringProc *\fIupdateStringProc\fR; |
|---|
| 99 | Tcl_SetFromAnyProc *\fIsetFromAnyProc\fR; |
|---|
| 100 | } Tcl_ObjType; |
|---|
| 101 | .CE |
|---|
| 102 | .SS "THE NAME FIELD" |
|---|
| 103 | .PP |
|---|
| 104 | The \fIname\fR member describes the name of the type, e.g. \fBint\fR. |
|---|
| 105 | Extension writers can look up an object type using its name |
|---|
| 106 | with the \fBTcl_GetObjType\fR procedure. |
|---|
| 107 | The remaining four members are pointers to procedures |
|---|
| 108 | called by the generic Tcl object code: |
|---|
| 109 | .SS "THE SETFROMANYPROC FIELD" |
|---|
| 110 | .PP |
|---|
| 111 | The \fIsetFromAnyProc\fR member contains the address of a function |
|---|
| 112 | called to create a valid internal representation |
|---|
| 113 | from an object's string representation. |
|---|
| 114 | .PP |
|---|
| 115 | .CS |
|---|
| 116 | typedef int (Tcl_SetFromAnyProc) (Tcl_Interp *\fIinterp\fR, |
|---|
| 117 | Tcl_Obj *\fIobjPtr\fR); |
|---|
| 118 | .CE |
|---|
| 119 | .PP |
|---|
| 120 | If an internal representation cannot be created from the string, |
|---|
| 121 | it returns \fBTCL_ERROR\fR and puts a message |
|---|
| 122 | describing the error in the result object for \fIinterp\fR |
|---|
| 123 | unless \fIinterp\fR is NULL. |
|---|
| 124 | If \fIsetFromAnyProc\fR is successful, |
|---|
| 125 | it stores the new internal representation, |
|---|
| 126 | sets \fIobjPtr\fR's \fItypePtr\fR member to point to |
|---|
| 127 | \fIsetFromAnyProc\fR's \fBTcl_ObjType\fR, and returns \fBTCL_OK\fR. |
|---|
| 128 | Before setting the new internal representation, |
|---|
| 129 | the \fIsetFromAnyProc\fR must free any internal representation |
|---|
| 130 | of \fIobjPtr\fR's old type; |
|---|
| 131 | it does this by calling the old type's \fIfreeIntRepProc\fR |
|---|
| 132 | if it is not NULL. |
|---|
| 133 | As an example, the \fIsetFromAnyProc\fR for the built-in Tcl integer type |
|---|
| 134 | gets an up-to-date string representation for \fIobjPtr\fR |
|---|
| 135 | by calling \fBTcl_GetStringFromObj\fR. |
|---|
| 136 | It parses the string to obtain an integer and, |
|---|
| 137 | if this succeeds, |
|---|
| 138 | stores the integer in \fIobjPtr\fR's internal representation |
|---|
| 139 | and sets \fIobjPtr\fR's \fItypePtr\fR member to point to the integer type's |
|---|
| 140 | Tcl_ObjType structure. |
|---|
| 141 | Do not release \fIobjPtr\fR's old internal representation unless you |
|---|
| 142 | replace it with a new one or reset the \fItypePtr\fR member to NULL. |
|---|
| 143 | .SS "THE UPDATESTRINGPROC FIELD" |
|---|
| 144 | .PP |
|---|
| 145 | The \fIupdateStringProc\fR member contains the address of a function |
|---|
| 146 | called to create a valid string representation |
|---|
| 147 | from an object's internal representation. |
|---|
| 148 | .PP |
|---|
| 149 | .CS |
|---|
| 150 | typedef void (Tcl_UpdateStringProc) (Tcl_Obj *\fIobjPtr\fR); |
|---|
| 151 | .CE |
|---|
| 152 | .PP |
|---|
| 153 | \fIobjPtr\fR's \fIbytes\fR member is always NULL when it is called. |
|---|
| 154 | It must always set \fIbytes\fR non-NULL before returning. |
|---|
| 155 | We require the string representation's byte array |
|---|
| 156 | to have a null after the last byte, at offset \fIlength\fR; |
|---|
| 157 | this allows string representations that do not contain null bytes |
|---|
| 158 | to be treated as conventional null character-terminated C strings. |
|---|
| 159 | Storage for the byte array must be allocated in the heap by \fBTcl_Alloc\fR |
|---|
| 160 | or \fBckalloc\fR. Note that \fIupdateStringProc\fRs must allocate |
|---|
| 161 | enough storage for the string's bytes and the terminating null byte. |
|---|
| 162 | The \fIupdateStringProc\fR for Tcl's built-in list type, for example, |
|---|
| 163 | builds an array of strings for each element object |
|---|
| 164 | and then calls \fBTcl_Merge\fR |
|---|
| 165 | to construct a string with proper Tcl list structure. |
|---|
| 166 | It stores this string as the list object's string representation. |
|---|
| 167 | .SS "THE DUPINTREPPROC FIELD" |
|---|
| 168 | .PP |
|---|
| 169 | The \fIdupIntRepProc\fR member contains the address of a function |
|---|
| 170 | called to copy an internal representation from one object to another. |
|---|
| 171 | .PP |
|---|
| 172 | .CS |
|---|
| 173 | typedef void (Tcl_DupInternalRepProc) (Tcl_Obj *\fIsrcPtr\fR, |
|---|
| 174 | Tcl_Obj *\fIdupPtr\fR); |
|---|
| 175 | .CE |
|---|
| 176 | .PP |
|---|
| 177 | \fIdupPtr\fR's internal representation is made a copy of \fIsrcPtr\fR's |
|---|
| 178 | internal representation. |
|---|
| 179 | Before the call, |
|---|
| 180 | \fIsrcPtr\fR's internal representation is valid and \fIdupPtr\fR's is not. |
|---|
| 181 | \fIsrcPtr\fR's object type determines what |
|---|
| 182 | copying its internal representation means. |
|---|
| 183 | For example, the \fIdupIntRepProc\fR for the Tcl integer type |
|---|
| 184 | simply copies an integer. |
|---|
| 185 | The built-in list type's \fIdupIntRepProc\fR |
|---|
| 186 | allocates a new array that points at the original element objects; |
|---|
| 187 | the elements are shared between the two lists |
|---|
| 188 | (and their reference counts are incremented to reflect the new references). |
|---|
| 189 | .SS "THE FREEINTREPPROC FIELD" |
|---|
| 190 | .PP |
|---|
| 191 | The \fIfreeIntRepProc\fR member contains the address of a function |
|---|
| 192 | that is called when an object is freed. |
|---|
| 193 | .PP |
|---|
| 194 | .CS |
|---|
| 195 | typedef void (Tcl_FreeInternalRepProc) (Tcl_Obj *\fIobjPtr\fR); |
|---|
| 196 | .CE |
|---|
| 197 | .PP |
|---|
| 198 | The \fIfreeIntRepProc\fR function can deallocate the storage |
|---|
| 199 | for the object's internal representation |
|---|
| 200 | and do other type-specific processing necessary when an object is freed. |
|---|
| 201 | For example, Tcl list objects have an \fIinternalRep.otherValuePtr\fR |
|---|
| 202 | that points to an array of pointers to each element in the list. |
|---|
| 203 | The list type's \fIfreeIntRepProc\fR decrements |
|---|
| 204 | the reference count for each element object |
|---|
| 205 | (since the list will no longer refer to those objects), |
|---|
| 206 | then deallocates the storage for the array of pointers. |
|---|
| 207 | The \fIfreeIntRepProc\fR member can be set to NULL |
|---|
| 208 | to indicate that the internal representation does not require freeing. |
|---|
| 209 | The \fIfreeIntRepProc\fR implementation should not access the |
|---|
| 210 | \fIbytes\fR member of the object, as this may potentially have already |
|---|
| 211 | been deleted. |
|---|
| 212 | .SH "SEE ALSO" |
|---|
| 213 | Tcl_NewObj, Tcl_DecrRefCount, Tcl_IncrRefCount |
|---|
| 214 | .SH KEYWORDS |
|---|
| 215 | internal representation, object, object type, string representation, type conversion |
|---|