1Tcl_ObjType(3) Tcl Library Procedures Tcl_ObjType(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_RegisterObjType, Tcl_GetObjType, Tcl_AppendAllObjTypes, Tcl_Con‐
9 vertToType - manipulate Tcl object types
10
12 #include <tcl.h>
13
14 Tcl_RegisterObjType(typePtr)
15
16 Tcl_ObjType *
17 Tcl_GetObjType(typeName)
18
19 int
20 Tcl_AppendAllObjTypes(interp, objPtr)
21
22 int
23 Tcl_ConvertToType(interp, objPtr, typePtr)
24
26 Tcl_ObjType *typePtr (in) Points to the structure containing
27 information about the Tcl object
28 type. This storage must live for‐
29 ever, typically by being statically
30 allocated.
31
32 CONST char *typeName (in) The name of a Tcl object type that
33 Tcl_GetObjType should look up.
34
35 Tcl_Interp *interp (in) Interpreter to use for error
36 reporting.
37
38 Tcl_Obj *objPtr (in) For Tcl_AppendAllObjTypes, this
39 points to the object onto which it
40 appends the name of each object
41 type as a list element. For
42 Tcl_ConvertToType, this points to
43 an object that must have been the
44 result of a previous call to
45 Tcl_NewObj.
46_________________________________________________________________
47
48
50 The procedures in this man page manage Tcl object types. The are used
51 to register new object types, look up types, and force conversions from
52 one type to another.
53
54 Tcl_RegisterObjType registers a new Tcl object type in the table of all
55 object types supported by Tcl. The argument typePtr points to a
56 Tcl_ObjType structure that describes the new type by giving its name
57 and by supplying pointers to four procedures that implement the type.
58 If the type table already contains a type with the same name as in
59 typePtr, it is replaced with the new type. The Tcl_ObjType structure
60 is described in the section THE TCL_OBJTYPE STRUCTURE below.
61
62 Tcl_GetObjType returns a pointer to the Tcl_ObjType with name typeName.
63 It returns NULL if no type with that name is registered.
64
65 Tcl_AppendAllObjTypes appends the name of each object type as a list
66 element onto the Tcl object referenced by objPtr. The return value is
67 TCL_OK unless there was an error converting objPtr to a list object; in
68 that case TCL_ERROR is returned.
69
70 Tcl_ConvertToType converts an object from one type to another if possi‐
71 ble. It creates a new internal representation for objPtr appropriate
72 for the target type typePtr and sets its typePtr member to that type.
73 Any internal representation for objPtr's old type is freed. If an
74 error occurs during conversion, it returns TCL_ERROR and leaves an
75 error message in the result object for interp unless interp is NULL.
76 Otherwise, it returns TCL_OK. Passing a NULL interp allows this proce‐
77 dure to be used as a test whether the conversion can be done (and in
78 fact was done).
79
80
82 Extension writers can define new object types by defining four proce‐
83 dures, initializing a Tcl_ObjType structure to describe the type, and
84 calling Tcl_RegisterObjType. The Tcl_ObjType structure is defined as
85 follows:
86 typedef struct Tcl_ObjType {
87 char *name;
88 Tcl_FreeInternalRepProc *freeIntRepProc;
89 Tcl_DupInternalRepProc *dupIntRepProc;
90 Tcl_UpdateStringProc *updateStringProc;
91 Tcl_SetFromAnyProc *setFromAnyProc;
92 } Tcl_ObjType;
93
94 The name member describes the name of the type, e.g. int. Extension
95 writers can look up an object type using its name with the Tcl_GetObj‐
96 Type procedure. The remaining four members are pointers to procedures
97 called by the generic Tcl object code:
98
99 The setFromAnyProc member contains the address of a function called to
100 create a valid internal representation from an object's string repre‐
101 sentation.
102 typedef int (Tcl_SetFromAnyProc) (Tcl_Interp *interp, Tcl_Obj *objPtr);
103 If an internal representation can't be created from the string, it
104 returns TCL_ERROR and puts a message describing the error in the result
105 object for interp unless interp is NULL. If setFromAnyProc is success‐
106 ful, it stores the new internal representation, sets objPtr's typePtr
107 member to point to setFromAnyProc's Tcl_ObjType, and returns TCL_OK.
108 Before setting the new internal representation, the setFromAnyProc must
109 free any internal representation of objPtr's old type; it does this by
110 calling the old type's freeIntRepProc if it is not NULL. As an exam‐
111 ple, the setFromAnyProc for the builtin Tcl integer type gets an up-to-
112 date string representation for objPtr by calling Tcl_GetStringFromObj.
113 It parses the string to obtain an integer and, if this succeeds, stores
114 the integer in objPtr's internal representation and sets objPtr's type‐
115 Ptr member to point to the integer type's Tcl_ObjType structure. Do
116 not release objPtr's old internal representation unless you replace it
117 with a new one or reset the typePtr member to NULL.
118
119 The updateStringProc member contains the address of a function called
120 to create a valid string representation from an object's internal rep‐
121 resentation.
122 typedef void (Tcl_UpdateStringProc) (Tcl_Obj *objPtr);
123 objPtr's bytes member is always NULL when it is called. It must always
124 set bytes non-NULL before returning. We require the string representa‐
125 tion's byte array to have a null after the last byte, at offset length;
126 this allows string representations that do not contain null bytes to be
127 treated as conventional null character-terminated C strings. Storage
128 for the byte array must be allocated in the heap by Tcl_Alloc or
129 ckalloc. Note that updateStringProcs must allocate enough storage for
130 the string's bytes and the terminating null byte. The updateStringProc
131 for Tcl's builtin list type, for example, builds an array of strings
132 for each element object and then calls Tcl_Merge to construct a string
133 with proper Tcl list structure. It stores this string as the list
134 object's string representation.
135
136 The dupIntRepProc member contains the address of a function called to
137 copy an internal representation from one object to another.
138 typedef void (Tcl_DupInternalRepProc) (Tcl_Obj *srcPtr, Tcl_Obj *dupPtr);
139 dupPtr's internal representation is made a copy of srcPtr's internal
140 representation. Before the call, srcPtr's internal representation is
141 valid and dupPtr's is not. srcPtr's object type determines what copy‐
142 ing its internal representation means. For example, the dupIntRepProc
143 for the Tcl integer type simply copies an integer. The builtin list
144 type's dupIntRepProc allocates a new array that points at the original
145 element objects; the elements are shared between the two lists (and
146 their reference counts are incremented to reflect the new references).
147
148 The freeIntRepProc member contains the address of a function that is
149 called when an object is freed.
150 typedef void (Tcl_FreeInternalRepProc) (Tcl_Obj *objPtr);
151 The freeIntRepProc function can deallocate the storage for the object's
152 internal representation and do other type-specific processing necessary
153 when an object is freed. For example, Tcl list objects have an inter‐
154 nalRep.otherValuePtr that points to an array of pointers to each ele‐
155 ment in the list. The list type's freeIntRepProc decrements the refer‐
156 ence count for each element object (since the list will no longer refer
157 to those objects), then deallocates the storage for the array of point‐
158 ers. The freeIntRepProc member can be set to NULL to indicate that the
159 internal representation does not require freeing.
160
161
163 Tcl_NewObj, Tcl_DecrRefCount, Tcl_IncrRefCount
164
165
167 internal representation, object, object type, string representation,
168 type conversion
169
170
171
172Tcl 8.0 Tcl_ObjType(3)