user3500804
user3500804

Reputation: 11

how to access value in tcl script from .cc files

I'm trying to get value from .cc. In my file , AgentCPU.h and AgentCPU.cc there is a integer called "npkts_" and a function "recv" for receive Packets when I finally finished a packet , I will increase the "npkts_" 1 In further , in my tcl script I want to access the "npkts_" my code is like this

set cpu [new Agent\AgentCPU]
puts [format "%d" "$cpu set npkts_"]

However , ther value is not correct it is same to the value when I construct my AgentCPU like this

AgentCPU::AgentCPU(): Agent(PT_TASK)
{
...
npkts_=199;
...}

the value will be 199, and in the "recv" function , I use "printf" to check if there is any problem

...
npkts_++;
printf("npkts is %d\n",npkts);
...

and the value here is correct,every time I receive Packet will increase the "npkts" Is there any code wrong?? On the other hand, I use another way to debug In my "recv" function

...
npkts_++;
Tcl& tcl = Tcl:;instance();
tcl.evalf("puts \" npkts is %d""",npkts_);
..

In this way the message will be 1, and stop to print Can sombody give me a hand? How to get the correct value from .cc file? Any suggestion will be very thankful!!

Upvotes: 1

Views: 1611

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137567

In Tcl, every script evaluation produces a result value (which could be the empty value, or could be something else, depending on what happened). That result value may be retrieved from the interpreter context with Tcl_GetStringResult or Tcl_GetObjResult; the former produces (effectively) a const char * and the latter a Tcl_Obj * (i.e., a Tcl value reference; the name Tcl_Obj is for historical reasons). That value is there until the next piece of script is evaluated. Do not modify the Tcl_Obj* returned (except via Tcl's API) unless you know exactly what you're doing (i.e., are writing code to extend Tcl itself). Integers may be efficiently retrieved from a Tcl_Obj* using Tcl_GetIntFromObj (assuming that the value is an integer), floating point numbers with Tcl_GetDoubleFromObj, and a const char * can be retrieved from a Tcl_Obj* using Tcl_GetString or Tcl_GetStringFromObj (slightly different API).

There are also variables in Tcl. You can read those directly with Tcl_GetVar — which returns another const char * — or Tcl_GetVar2Ex (with an ugly name but convenient API) that returns another Tcl_Obj *.


These are all C API functions and all take a Tcl_Interp * (which is a reference to a Tcl evaluation context, which is always bound to a single thread). How they have been wrapped in the C++ API… I can't tell you that.

Upvotes: 1

patthoyts
patthoyts

Reputation: 33193

You are using some weird C++ library you are not giving any information about. However, when using Tcl from a C/C++ program when you evaluate some Tcl code the result is available attached to the interpreter object. You access this using Tcl_GetObjResult and then use an appropriate Tcl_GetIntFromObj or Tcl_GetDoubleFromObj or whatever to read the Tcl_Obj value into some C++ form.

Upvotes: 0

Related Questions