1

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!!

1
  • Did you find a way to get the variable's value in tcl script? If so, let me knw please. I am counting packets in recv function, I am displaying it as well, but want to perform some other operation on the value in tcl script.
    – Developer
    May 18, 2016 at 11:27

2 Answers 2

1

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.

2
  • Thanks alot! But I think my problem is not in getting value. Cause my goal is trying to let two node one is host with traffic generator and another is client.And the packet sended by host will be send back by client.Since the calculation is in host ,and it looks like the client didn't send the packet back. So the information I need in the host will be the default value.. May 11, 2014 at 8:54
  • Sorry for my stupid question :( and thanks for trying help me May 11, 2014 at 8:55
0

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.

2
  • sorry for not provide enough information. I'm writing a ns simulation use "mytcl.tcl" and I want to access some vriables which is declared in MyAgent.cc , the new Agent with a receive function May 10, 2014 at 16:40
  • And really sorry that could you comment more clear.. I'm not sure what's your mean..:( Is the Tcl_GetObjResult a tcl script to access the variables value in c++ file (MyAgent.cc)? May 10, 2014 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.