From: Neil M. <ne...@cs...> - 2006-03-10 21:57:55
|
Michael Norton (mnorton) wrote: > hi, > > Does a C extension library exist for setting a Tcl Obj? > For instance, > > result = Tcl_ListObjGetElements(interp, listPtr, &len, &elemPtrs); > : > strPtr = Tcl_GetString(elemPtrs[0]); > > Now I want to write back a new value to elemPtrs[0]. Can I do that? How > do I do that? > Is there a Tcl_SetObj? (probably). You want Tcl_ListObjReplace, I suspect. Tcl_Obj's are conceptually immutable (they represent values), so you are only allowed to update them if you hold the only reference. The Tcl_Set<type>Obj functions will abort (panic) if you call them with a Tcl_Obj with refcount > 1. So, unless you know that you will always have a unique reference to the object, then you need code like the following: int result; if (Tcl_IsShared(listPtr)) { // Make a copy listPtr = Tcl_DuplicateObj(listPtr); } result = Tcl_ListObjReplace(interp, listPtr, 0, 0, 1, &newValuePtr); if (result != TCL_OK) { return TCL_ERROR; } Tcl_SetObjResult(interp, listPtr); return TCL_OK; See the Tcl_DuplicateObj manpage for more information. -- Neil |