#include <tcl.h> Tcl_RegisterObjType(typePtr) Tcl_ObjType * Tcl_GetObjType(typeName) int Tcl_GetAllObjTypes(interp, objPtrPtr) int Tcl_ConvertToType(interp, objPtr, typePtr)
Tcl_RegisterObjType registers a new Tcl object type in the table of all object types supported by Tcl. The argument typePtr points to a Tcl_ObjType structure that describes the new type by giving its name and by supplying pointers to four procedures. If the type table already containes a type with the same name as in typePtr, it is replaced with the new type. The Tcl_ObjType structure is described in the section THE TCL_OBJTYPE STRUCTURE below.
Tcl_GetObjType returns a pointer to the Tcl_ObjType with name typeName. It returns NULL if no type with that name is registered.
Tcl_GetAllObjTypes returns a new list object whose elements hold the names of each object type. A pointer to the new object is stored at the location objPtrPtr. The return value is normally TCL_OK; in this case objPtrPtr is set to point to a new list object containing the type names. If an error occurs, *objPtrPtr is left unchanged, TCL_ERROR is returned, and interp->objResult holds an error message that can be retrieved using Tcl_GetObjResult.
Tcl_ConvertToType converts an object from one type to another if possible. It creates a new internal representation for objPtr appropriate for the target type typePtr and sets its typePtr member to that type. Any internal representation for objPtr's old type is freed. If an error occurs during conversion, it returns TCL_ERROR and leaves an error message in interp->objResult unless interp is NULL. This message object can be retrieved using Tcl_GetObjResult. Otherwise, it returns TCL_OK. Passing a NULL interp allows this procedure to be used as a test whether the conversion can be done (and in fact was done).
typedef struct Tcl_ObjType { char *name; Tcl_FreeInternalRepProc *freeIntRepProc; Tcl_DupInternalRepProc *dupIntRepProc; Tcl_UpdateStringProc *updateStringProc; Tcl_SetFromAnyProc *setFromAnyProc; } Tcl_ObjType;
The name member describes the name of the type, e.g. int. Extension writers can look up an object type using its name with the Tcl_GetObjType procedure. The remaining four members are pointers to procedures called by the generic Tcl object code:
The setFromAnyProc member contains the address of a function called to create a valid internal representation from an object's string representation.
If an internal representation can't be created from the string, it returns TCL_ERROR and puts a message describing the error in the interp->objResult that can be retrieved using Tcl_GetObjResult. If setFromAnyProc is successful, it stores the new internal representation, sets objPtr's typePtr member to point to setFromAnyProc's Tcl_ObjType, and returns TCL_OK. Before setting the new internal representation, the setFromAnyProc must free any internal representation of objPtr's old type; it does this by calling the old type's freeIntRepProc if it is not NULL. As an example, the setFromAnyProc for the builtin Tcl integer type gets an up-to-date string representation for objPtr by calling Tcl_GetStringFromObj. It parses the string to obtain an integer and, if this succeeds, stores the integer in objPtr's internal representation and sets objPtr's typePtr member to point to the integer type's Tcl_ObjType structure.typedef int (Tcl_SetFromAnyProc) (Tcl_Interp *interp, Tcl_Obj *objPtr);
The updateStringProc member contains the address of a function called to create a valid string representation from an object's internal representation.
objPtr's bytes member is always NULL when it is called. It must always set bytes non-NULL before returning. We require the string representation's byte array to have a null after the last byte, at offset length; this allows string representations that do not contain null bytes to be treated as conventional null character-terminated C strings. Storage for the byte array must be allocated in the heap by Tcl_Alloc. Note that updateStringProcs must allocate enough storage for the string's bytes and the terminating null byte. The updateStringProc for Tcl's builtin list type, for example, builds an array of strings for each element object and then calls Tcl_Merge to construct a string with proper Tcl list structure. It stores this string as the list object's string representation.typedef void (Tcl_UpdateStringProc) (Tcl_Interp *interp, Tcl_Obj *objPtr);
The dupIntRepProc member contains the address of a function called to copy an internal representation from one object to another.
dupPtr's internal representation is made a copy of srcPtr's internal representation. Before the call, srcPtr's internal representation is valid and dupPtr's is not. srcPtr's object type determines what copying its internal representation means. For example, the dupIntRepProc for the Tcl integer type simply copies an integer. The builtin list type's dupIntRepProc allocates a new array that points at the original element objects; the elements are shared between the two lists (and their reference counts are incremented to reflect the new references).typedef void (Tcl_DupInternalRepProc) (Tcl_Obj *srcPtr, Tcl_Obj *dupPtr);
The freeIntRepProc member contains the address of a function called to free the storage for an object's internal representation when an object is freed.
The structure's freeIntRepProc member can be set NULL to indicate that the internal representation does not require freeing.typedef void (Tcl_FreeInternalRepProc) (Tcl_Obj *objPtr);
For an example of how to define a new object type, look at the implementation of the integer type in tclObj.c.
Copyright © 1989-1994 The Regents of the University of California.
Copyright © 1994-1996 Sun Microsystems, Inc.