1 |
578 |
markom |
'\"
|
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.1.1.1 2002-01-16 10:25:24 markom 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 \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 Tcl_ObjType *typeName in
|
30 |
|
|
.AP Tcl_ObjType *typePtr in
|
31 |
|
|
Points to the structure containing information about the Tcl object type.
|
32 |
|
|
This storage must must live forever,
|
33 |
|
|
typically by being statically allocated.
|
34 |
|
|
.AP 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 |
|
|
The are used to register new object types,
|
49 |
|
|
look up types,
|
50 |
|
|
and force conversions from one type to another.
|
51 |
|
|
.PP
|
52 |
|
|
\fBTcl_RegisterObjType\fR registers a new Tcl object type
|
53 |
|
|
in the table of all object types supported by Tcl.
|
54 |
|
|
The argument \fItypePtr\fR points to a Tcl_ObjType structure that
|
55 |
|
|
describes the new type by giving its name
|
56 |
|
|
and by supplying pointers to four procedures
|
57 |
|
|
that implement the type.
|
58 |
|
|
If the type table already containes a type
|
59 |
|
|
with the same name as in \fItypePtr\fR,
|
60 |
|
|
it is replaced with the new type.
|
61 |
|
|
The Tcl_ObjType structure is described
|
62 |
|
|
in the section \fBTHE TCL_OBJTYPE STRUCTURE\fR below.
|
63 |
|
|
.PP
|
64 |
|
|
\fBTcl_GetObjType\fR returns a pointer to the Tcl_ObjType
|
65 |
|
|
with name \fItypeName\fR.
|
66 |
|
|
It returns NULL if no type with that name is registered.
|
67 |
|
|
.PP
|
68 |
|
|
\fBTcl_AppendAllObjTypes\fR appends the name of each object type
|
69 |
|
|
as a list element onto the Tcl object referenced by \fIobjPtr\fR.
|
70 |
|
|
The return value is \fBTCL_OK\fR unless there was an error
|
71 |
|
|
converting \fIobjPtr\fR to a list object;
|
72 |
|
|
in that case \fBTCL_ERROR\fR is returned.
|
73 |
|
|
.PP
|
74 |
|
|
\fBTcl_ConvertToType\fR converts an object from one type to another
|
75 |
|
|
if possible.
|
76 |
|
|
It creates a new internal representation for \fIobjPtr\fR
|
77 |
|
|
appropriate for the target type \fItypePtr\fR
|
78 |
|
|
and sets its \fItypePtr\fR member to that type.
|
79 |
|
|
Any internal representation for \fIobjPtr\fR's old type is freed.
|
80 |
|
|
If an error occurs during conversion, it returns \fBTCL_ERROR\fR
|
81 |
|
|
and leaves an error message in the result object for \fIinterp\fR
|
82 |
|
|
unless \fIinterp\fR is NULL.
|
83 |
|
|
Otherwise, it returns \fBTCL_OK\fR.
|
84 |
|
|
Passing a NULL \fIinterp\fR allows this procedure to be used
|
85 |
|
|
as a test whether the conversion can be done (and in fact was done).
|
86 |
|
|
|
87 |
|
|
.SH "THE TCL_OBJTYPE STRUCTURE"
|
88 |
|
|
.PP
|
89 |
|
|
Extension writers can define new object types by defining four
|
90 |
|
|
procedures,
|
91 |
|
|
initializing a Tcl_ObjType structure to describe the type,
|
92 |
|
|
and calling \fBTcl_RegisterObjType\fR.
|
93 |
|
|
The \fBTcl_ObjType\fR structure is defined as follows:
|
94 |
|
|
.CS
|
95 |
|
|
typedef struct Tcl_ObjType {
|
96 |
|
|
char *\fIname\fR;
|
97 |
|
|
Tcl_FreeInternalRepProc *\fIfreeIntRepProc\fR;
|
98 |
|
|
Tcl_DupInternalRepProc *\fIdupIntRepProc\fR;
|
99 |
|
|
Tcl_UpdateStringProc *\fIupdateStringProc\fR;
|
100 |
|
|
Tcl_SetFromAnyProc *\fIsetFromAnyProc\fR;
|
101 |
|
|
} Tcl_ObjType;
|
102 |
|
|
.CE
|
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 |
|
|
.PP
|
110 |
|
|
The \fIsetFromAnyProc\fR member contains the address of a function
|
111 |
|
|
called to create a valid internal representation
|
112 |
|
|
from an object's string representation.
|
113 |
|
|
.CS
|
114 |
|
|
typedef int (Tcl_SetFromAnyProc) (Tcl_Interp *\fIinterp\fR, Tcl_Obj *\fIobjPtr\fR);
|
115 |
|
|
.CE
|
116 |
|
|
If an internal representation can't be created from the string,
|
117 |
|
|
it returns \fBTCL_ERROR\fR and puts a message
|
118 |
|
|
describing the error in the result object for \fIinterp\fR
|
119 |
|
|
unless \fIinterp\fR is NULL.
|
120 |
|
|
If \fIsetFromAnyProc\fR is successful,
|
121 |
|
|
it stores the new internal representation,
|
122 |
|
|
sets \fIobjPtr\fR's \fItypePtr\fR member to point to
|
123 |
|
|
\fIsetFromAnyProc\fR's \fBTcl_ObjType\fR, and returns \fBTCL_OK\fR.
|
124 |
|
|
Before setting the new internal representation,
|
125 |
|
|
the \fIsetFromAnyProc\fR must free any internal representation
|
126 |
|
|
of \fIobjPtr\fR's old type;
|
127 |
|
|
it does this by calling the old type's \fIfreeIntRepProc\fR
|
128 |
|
|
if it is not NULL.
|
129 |
|
|
As an example, the \fIsetFromAnyProc\fR for the builtin Tcl integer type
|
130 |
|
|
gets an up-to-date string representation for \fIobjPtr\fR
|
131 |
|
|
by calling \fBTcl_GetStringFromObj\fR.
|
132 |
|
|
It parses the string to obtain an integer and,
|
133 |
|
|
if this succeeds,
|
134 |
|
|
stores the integer in \fIobjPtr\fR's internal representation
|
135 |
|
|
and sets \fIobjPtr\fR's \fItypePtr\fR member to point to the integer type's
|
136 |
|
|
Tcl_ObjType structure.
|
137 |
|
|
.PP
|
138 |
|
|
The \fIupdateStringProc\fR member contains the address of a function
|
139 |
|
|
called to create a valid string representation
|
140 |
|
|
from an object's internal representation.
|
141 |
|
|
.CS
|
142 |
|
|
typedef void (Tcl_UpdateStringProc) (Tcl_Obj *\fIobjPtr\fR);
|
143 |
|
|
.CE
|
144 |
|
|
\fIobjPtr\fR's \fIbytes\fR member is always NULL when it is called.
|
145 |
|
|
It must always set \fIbytes\fR non-NULL before returning.
|
146 |
|
|
We require the string representation's byte array
|
147 |
|
|
to have a null after the last byte, at offset \fIlength\fR;
|
148 |
|
|
this allows string representations that do not contain null bytes
|
149 |
|
|
to be treated as conventional null character-terminated C strings.
|
150 |
|
|
Storage for the byte array must be allocated in the heap by \fBTcl_Alloc\fR.
|
151 |
|
|
Note that \fIupdateStringProc\fRs must allocate
|
152 |
|
|
enough storage for the string's bytes and the terminating null byte.
|
153 |
|
|
The \fIupdateStringProc\fR for Tcl's builtin list type, for example,
|
154 |
|
|
builds an array of strings for each element object
|
155 |
|
|
and then calls \fBTcl_Merge\fR
|
156 |
|
|
to construct a string with proper Tcl list structure.
|
157 |
|
|
It stores this string as the list object's string representation.
|
158 |
|
|
.PP
|
159 |
|
|
The \fIdupIntRepProc\fR member contains the address of a function
|
160 |
|
|
called to copy an internal representation from one object to another.
|
161 |
|
|
.CS
|
162 |
|
|
typedef void (Tcl_DupInternalRepProc) (Tcl_Obj *\fIsrcPtr\fR, Tcl_Obj *\fIdupPtr\fR);
|
163 |
|
|
.CE
|
164 |
|
|
\fIdupPtr\fR's internal representation is made a copy of \fIsrcPtr\fR's
|
165 |
|
|
internal representation.
|
166 |
|
|
Before the call,
|
167 |
|
|
\fIsrcPtr\fR's internal representation is valid and \fIdupPtr\fR's is not.
|
168 |
|
|
\fIsrcPtr\fR's object type determines what
|
169 |
|
|
copying its internal representation means.
|
170 |
|
|
For example, the \fIdupIntRepProc\fR for the Tcl integer type
|
171 |
|
|
simply copies an integer.
|
172 |
|
|
The builtin list type's \fIdupIntRepProc\fR
|
173 |
|
|
allocates a new array that points at the original element objects;
|
174 |
|
|
the elements are shared between the two lists
|
175 |
|
|
(and their reference counts are incremented to reflect the new references).
|
176 |
|
|
.PP
|
177 |
|
|
The \fIfreeIntRepProc\fR member contains the address of a function
|
178 |
|
|
that is called when an object is freed.
|
179 |
|
|
.CS
|
180 |
|
|
typedef void (Tcl_FreeInternalRepProc) (Tcl_Obj *\fIobjPtr\fR);
|
181 |
|
|
.CE
|
182 |
|
|
The \fIfreeIntRepProc\fR function can deallocate the storage
|
183 |
|
|
for the object's internal representation
|
184 |
|
|
and do other type-specific processing necessary when an object is freed.
|
185 |
|
|
For example, Tcl list objects have an \fIinternalRep.otherValuePtr\fR
|
186 |
|
|
that points to an array of pointers to each element in the list.
|
187 |
|
|
The list type's \fIfreeIntRepProc\fR decrements
|
188 |
|
|
the reference count for each element object
|
189 |
|
|
(since the list will no longer refer to those objects),
|
190 |
|
|
then deallocates the storage for the array of pointers.
|
191 |
|
|
The \fIfreeIntRepProc\fR member can be set to NULL
|
192 |
|
|
to indicate that the internal representation does not require freeing.
|
193 |
|
|
|
194 |
|
|
.SH "SEE ALSO"
|
195 |
|
|
Tcl_NewObj, Tcl_DecrRefCount, Tcl_IncrRefCount
|
196 |
|
|
|
197 |
|
|
.SH KEYWORDS
|
198 |
|
|
internal representation, object, object type, string representation, type conversion
|