1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
* ------------------------------------------------------------------------
* PACKAGE: [incr Tcl]
* DESCRIPTION: Object-Oriented Extensions to Tcl
*
* [incr Tcl] provides object-oriented extensions to Tcl, much as
* C++ provides object-oriented extensions to C. It provides a means
* of encapsulating related procedures together with their shared data
* in a local namespace that is hidden from the outside world. It
* promotes code re-use through inheritance. More than anything else,
* it encourages better organization of Tcl applications through the
* object-oriented paradigm, leading to code that is easier to
* understand and maintain.
*
* This part adds a mechanism for integrating C procedures into
* [incr Tcl] classes as methods and procs. Each C procedure must
* either be declared via Itcl_RegisterC() or dynamically loaded.
*
* ========================================================================
* AUTHOR: Michael J. McLennan
* Bell Labs Innovations for Lucent Technologies
* mmclennan@lucent.com
* http://www.tcltk.com/itcl
* ========================================================================
* Copyright (c) 1993-1998 Lucent Technologies, Inc.
* ------------------------------------------------------------------------
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "itclInt.h"
/*
* These records store the pointers for all "RegisterC" functions.
*/
typedef struct ItclCfunc {
Tcl_CmdProc *argCmdProc; /* old-style (argc,argv) command handler */
Tcl_ObjCmdProc *objCmdProc; /* new (objc,objv) command handler */
ClientData clientData; /* client data passed into this function */
Tcl_CmdDeleteProc *deleteProc; /* proc called to free clientData */
} ItclCfunc;
static Tcl_HashTable* ItclGetRegisteredProcs _ANSI_ARGS_((Tcl_Interp *interp));
static void ItclFreeC _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp));
/*
* ------------------------------------------------------------------------
* Itcl_RegisterC()
*
* Used to associate a symbolic name with an (argc,argv) C procedure
* that handles a Tcl command. Procedures that are registered in this
* manner can be referenced in the body of an [incr Tcl] class
* definition to specify C procedures to acting as methods/procs.
* Usually invoked in an initialization routine for an extension,
* called out in Tcl_AppInit() at the start of an application.
*
* Each symbolic procedure can have an arbitrary client data value
* associated with it. This value is passed into the command
* handler whenever it is invoked.
*
* A symbolic procedure name can be used only once for a given style
* (arg/obj) handler. If the name is defined with an arg-style
* handler, it can be redefined with an obj-style handler; or if
* the name is defined with an obj-style handler, it can be redefined
* with an arg-style handler. In either case, any previous client
* data is discarded and the new client data is remembered. However,
* if a name is redefined to a different handler of the same style,
* this procedure returns an error.
*
* Returns TCL_OK on success, or TCL_ERROR (along with an error message
* in interp->result) if anything goes wrong.
* ------------------------------------------------------------------------
*/
int
Itcl_RegisterC(interp, name, proc, clientData, deleteProc)
Tcl_Interp *interp; /* interpreter handling this registration */
CONST char *name; /* symbolic name for procedure */
Tcl_CmdProc *proc; /* procedure handling Tcl command */
ClientData clientData; /* client data associated with proc */
Tcl_CmdDeleteProc *deleteProc; /* proc called to free up client data */
{
int newEntry;
Tcl_HashEntry *entry;
Tcl_HashTable *procTable;
ItclCfunc *cfunc;
/*
* Make sure that a proc was specified.
*/
if (!proc) {
Tcl_AppendResult(interp, "initialization error: null pointer for ",
"C procedure \"", name, "\"",
(char*)NULL);
return TCL_ERROR;
}
/*
* Add a new entry for the given procedure. If an entry with
* this name already exists, then make sure that it was defined
* with the same proc.
*/
procTable = ItclGetRegisteredProcs(interp);
entry = Tcl_CreateHashEntry(procTable, name, &newEntry);
if (!newEntry) {
cfunc = (ItclCfunc*)Tcl_GetHashValue(entry);
if (cfunc->argCmdProc != NULL && cfunc->argCmdProc != proc) {
Tcl_AppendResult(interp, "initialization error: C procedure ",
"with name \"", name, "\" already defined",
(char*)NULL);
return TCL_ERROR;
}
if (cfunc->deleteProc != NULL) {
(*cfunc->deleteProc)(cfunc->clientData);
}
}
else {
cfunc = (ItclCfunc*)ckalloc(sizeof(ItclCfunc));
cfunc->objCmdProc = NULL;
}
cfunc->argCmdProc = proc;
cfunc->clientData = clientData;
cfunc->deleteProc = deleteProc;
Tcl_SetHashValue(entry, (ClientData)cfunc);
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_RegisterObjC()
*
* Used to associate a symbolic name with an (objc,objv) C procedure
* that handles a Tcl command. Procedures that are registered in this
* manner can be referenced in the body of an [incr Tcl] class
* definition to specify C procedures to acting as methods/procs.
* Usually invoked in an initialization routine for an extension,
* called out in Tcl_AppInit() at the start of an application.
*
* Each symbolic procedure can have an arbitrary client data value
* associated with it. This value is passed into the command
* handler whenever it is invoked.
*
* A symbolic procedure name can be used only once for a given style
* (arg/obj) handler. If the name is defined with an arg-style
* handler, it can be redefined with an obj-style handler; or if
* the name is defined with an obj-style handler, it can be redefined
* with an arg-style handler. In either case, any previous client
* data is discarded and the new client data is remembered. However,
* if a name is redefined to a different handler of the same style,
* this procedure returns an error.
*
* Returns TCL_OK on success, or TCL_ERROR (along with an error message
* in interp->result) if anything goes wrong.
* ------------------------------------------------------------------------
*/
int
Itcl_RegisterObjC(interp, name, proc, clientData, deleteProc)
Tcl_Interp *interp; /* interpreter handling this registration */
CONST char *name; /* symbolic name for procedure */
Tcl_ObjCmdProc *proc; /* procedure handling Tcl command */
ClientData clientData; /* client data associated with proc */
Tcl_CmdDeleteProc *deleteProc; /* proc called to free up client data */
{
int newEntry;
Tcl_HashEntry *entry;
Tcl_HashTable *procTable;
ItclCfunc *cfunc;
/*
* Make sure that a proc was specified.
*/
if (!proc) {
Tcl_AppendResult(interp, "initialization error: null pointer for ",
"C procedure \"", name, "\"",
(char*)NULL);
return TCL_ERROR;
}
/*
* Add a new entry for the given procedure. If an entry with
* this name already exists, then make sure that it was defined
* with the same proc.
*/
procTable = ItclGetRegisteredProcs(interp);
entry = Tcl_CreateHashEntry(procTable, name, &newEntry);
if (!newEntry) {
cfunc = (ItclCfunc*)Tcl_GetHashValue(entry);
if (cfunc->objCmdProc != NULL && cfunc->objCmdProc != proc) {
Tcl_AppendResult(interp, "initialization error: C procedure ",
"with name \"", name, "\" already defined",
(char*)NULL);
return TCL_ERROR;
}
if (cfunc->deleteProc != NULL) {
(*cfunc->deleteProc)(cfunc->clientData);
}
}
else {
cfunc = (ItclCfunc*)ckalloc(sizeof(ItclCfunc));
cfunc->argCmdProc = NULL;
}
cfunc->objCmdProc = proc;
cfunc->clientData = clientData;
cfunc->deleteProc = deleteProc;
Tcl_SetHashValue(entry, (ClientData)cfunc);
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_FindC()
*
* Used to query a C procedure via its symbolic name. Looks at the
* list of procedures registered previously by either Itcl_RegisterC
* or Itcl_RegisterObjC and returns pointers to the appropriate
* (argc,argv) or (objc,objv) handlers. Returns non-zero if the
* name is recognized and pointers are returned; returns zero
* otherwise.
* ------------------------------------------------------------------------
*/
int
Itcl_FindC(interp, name, argProcPtr, objProcPtr, cDataPtr)
Tcl_Interp *interp; /* interpreter handling this registration */
CONST char *name; /* symbolic name for procedure */
Tcl_CmdProc **argProcPtr; /* returns (argc,argv) command handler */
Tcl_ObjCmdProc **objProcPtr; /* returns (objc,objv) command handler */
ClientData *cDataPtr; /* returns client data */
{
Tcl_HashEntry *entry;
Tcl_HashTable *procTable;
ItclCfunc *cfunc;
*argProcPtr = NULL; /* assume info won't be found */
*objProcPtr = NULL;
*cDataPtr = NULL;
if (interp) {
procTable = (Tcl_HashTable*)Tcl_GetAssocData(interp,
"itcl_RegC", (Tcl_InterpDeleteProc**)NULL);
if (procTable) {
entry = Tcl_FindHashEntry(procTable, name);
if (entry) {
cfunc = (ItclCfunc*)Tcl_GetHashValue(entry);
*argProcPtr = cfunc->argCmdProc;
*objProcPtr = cfunc->objCmdProc;
*cDataPtr = cfunc->clientData;
}
}
}
return (*argProcPtr != NULL || *objProcPtr != NULL);
}
/*
* ------------------------------------------------------------------------
* ItclGetRegisteredProcs()
*
* Returns a pointer to a hash table containing the list of registered
* procs in the specified interpreter. If the hash table does not
* already exist, it is created.
* ------------------------------------------------------------------------
*/
static Tcl_HashTable*
ItclGetRegisteredProcs(interp)
Tcl_Interp *interp; /* interpreter handling this registration */
{
Tcl_HashTable* procTable;
/*
* If the registration table does not yet exist, then create it.
*/
procTable = (Tcl_HashTable*)Tcl_GetAssocData(interp, "itcl_RegC",
(Tcl_InterpDeleteProc**)NULL);
if (!procTable) {
procTable = (Tcl_HashTable*)ckalloc(sizeof(Tcl_HashTable));
Tcl_InitHashTable(procTable, TCL_STRING_KEYS);
Tcl_SetAssocData(interp, "itcl_RegC", ItclFreeC,
(ClientData)procTable);
}
return procTable;
}
/*
* ------------------------------------------------------------------------
* ItclFreeC()
*
* When an interpreter is deleted, this procedure is called to
* free up the associated data created by Itcl_RegisterC and
* Itcl_RegisterObjC.
* ------------------------------------------------------------------------
*/
static void
ItclFreeC(clientData, interp)
ClientData clientData; /* associated data */
Tcl_Interp *interp; /* intepreter being deleted */
{
Tcl_HashTable *tablePtr = (Tcl_HashTable*)clientData;
Tcl_HashSearch place;
Tcl_HashEntry *entry;
ItclCfunc *cfunc;
entry = Tcl_FirstHashEntry(tablePtr, &place);
while (entry) {
cfunc = (ItclCfunc*)Tcl_GetHashValue(entry);
if (cfunc->deleteProc != NULL) {
(*cfunc->deleteProc)(cfunc->clientData);
}
ckfree ( (char*)cfunc );
entry = Tcl_NextHashEntry(&place);
}
Tcl_DeleteHashTable(tablePtr);
ckfree((char*)tablePtr);
}
|