如何从 .cc 文件访问 tcl 脚本中的值 [英] how to access value in tcl script from .cc files

查看:21
本文介绍了如何从 .cc 文件访问 tcl 脚本中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 .cc 中获取价值.在我的文件中,AgentCPU.h 和 AgentCPU.cc有一个名为npkts_"的整数和一个用于接收数据包的函数recv"当我最终完成一个数据包时,我会增加npkts_"1此外,在我的 tcl 脚本中,我想访问npkts_"我的代码是这样的

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

然而,该值不正确它与我这样构造 AgentCPU 时的值相同

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

该值为 199,在recv"函数中,我使用printf"来检查是否有任何问题

<代码>...npkts_++;printf("npkts 是 %d\n",npkts);...

而且这里的值是正确的,每次收到Packet都会增加npkts"代码有错吗??另一方面,我使用另一种方式来调试在我的recv"函数中

<代码>...npkts_++;Tcl&tcl = tcl:;实例();tcl.evalf("puts \" npkts 是 %d""",npkts_);..

这样message就会为1,停止打印有人可以帮我一把吗?如何从 .cc 文件中获取正确的值?任何建议将不胜感激!!

解决方案

在 Tcl 中,每个脚本评估都会产生一个结果值(可能是空值,也可能是其他值,具体取决于发生的情况).可以使用 Tcl_GetStringResultTcl_GetObjResult 从解释器上下文中检索该结果值;前者(有效地)产生一个const char *,后者产生一个Tcl_Obj *(即一个Tcl 值引用;名称Tcl_Obj 用于历史原因).在评估下一段脚本之前,该值一直存在.不要修改返回的 Tcl_Obj*(通过 Tcl 的 API 除外),除非您确切地知道自己在做什么(即,正在编写代码来扩展 Tcl 本身).可以使用 Tcl_GetIntFromObj(假设值是整数)、使用 Tcl_GetDoubleFromObj 和一个浮点数从 Tcl_Obj* 有效地检索整数const char * 可以使用 Tcl_GetStringTcl_GetStringFromObj(稍微不同的 API)从 Tcl_Obj* 中检索.>

Tcl 中也有变量.您可以使用 Tcl_GetVar 直接读取它们——它返回另一个 const char *——或 Tcl_GetVar2Ex(名称丑陋但 API 方便)返回另一个Tcl_Obj *.

<小时>

这些都是 C API 函数,并且都采用 Tcl_Interp *(这是对 Tcl 评估上下文的引用,它总是绑定到单个线程).它们是如何被包装在 C++ API 中的……我不能告诉你.

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

解决方案

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.

这篇关于如何从 .cc 文件访问 tcl 脚本中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆