Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/ObjectType.3 @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

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