Le lundi 5 juin 2023 à 17:05:22 UTC+2,
[email protected] a écrit :
Create the list this way:
set myList [list 1 2 3 4 [::oo::Obj10 get] foo 10 bar]
@Rich ,
I can't, the goal here is to check each type, so I need to keep the name of the object.
You might be better off trying to avoid having to guess how to process the list objects.
However given that you are creating and executing a command from Tcl_Objs, consider using Tcl_EvalObjv, and instead of checking that the string value looks like an object name just execute the get method on the Tcl_Obj and check if there was an error.
// set up command object array (consider doing this only once in your application)
// I think this is the correct way to initalize a C array to NULLs ?? Tcl_Obj* cmd[3] = {NULL, NULL, NULL};
// second word of command is "get"
cmd[1] = Tcl_NewStringObj("get", -1);
// make sure the "get" does not disappear at an inopportune time. Tcl_IncrRefCount(cmd[1]);
// need to decrement the reference count (Tcl_DecrRefCount(cmd[1]);)
// just before the cmd[] array goes out of scope to avoid a memory leak
...
// try executing a get method on the current list entry
// if there is a chance the sub element has a zero reference count (unlikely for a list element),
// increment it here (and remember to decrement it later to avoid a memory leak)
cmd[0] = sub_elements[i];
if (TCL_OK == Tcl_EvalObjv(interp, cmd, 0)) {
DO SOMETHING with the result in interp
} else {
TRY SOMETHING ELSE
}
I did not compile or execute this YMMV
@Dave
If I understand , In that way so in Tcl :
foreach obj $myList {
if {![catch {$obj get} value]} {
# Do something
}
}
Maybe I'm wrong but in pure Tcl, I would never do like that, It's maybe different in C.
That said
Below my C code :
Tcl_Obj* cmd[2];
cmd[1] = Tcl_NewStringObj ("get", -1);
Tcl_IncrRefCount(cmd[1]);
for (int i = 0; i < count; ++i) {
cmd[0] = sub_elements[i];
Tcl_IncrRefCount(cmd[0]);
if (Tcl_EvalObjv(interp, 2, cmd, 0) == TCL_OK) {
Tcl_DecrRefCount(cmd[0]);
Tcl_ListObjAppendElement(interp, data, Tcl_GetObjResult(interp));
} else {
// Do something...
}
}
Tcl_DecrRefCount(cmd[1]);
I’m not sure about myself, it works , but I tested on a list of 20000 items and my performance is bad, my C code is correct ?
Thanks
Nicolas
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)