![](/img/trans.png)
[英]How can I find out if a Tcl_Obj is a certain kind of ttk:: entity, such as ttk:entry?
[英]How to eval a Tcl expression that should take a Tcl_Obj in C++
在C ++代码中,我有一个(生成的Tcl_Obj*
) Tcl_Obj*
和一个表示简单Tcl表达式的字符串,例如: return [[obj get_a] + [obj get_b]]
。 这看起来很愚蠢,但是我是Tcl的新手,我无法理解如何将这两个内容结合在一起调用Tcl解释器,以使用Tcl_Obj*
对我的表达式求值:
double eval(Tcl_Interp *interp, Tcl_Obj *obj, const char * cmd)
{
//substiture obj in cmd and call the interpreter?
return Tcl_GetResult(interp); //with proper checks and so on...
}
我是否错过了执行此操作的正确命令? 非常感谢!
您在某个地方有一个Tcl_Obj *
,您想将其作为一个表达式求值并得到double
结果? 使用Tcl_ExprDoubleObj
。
Tcl_Obj *theExpressionObj = ...; // OK, maybe an argument...
double resultValue;
int resultCode;
// Need to increase the reference count of a Tcl_Obj when you evaluate it
// whether as a Tcl script or a Tcl expression
Tcl_IncrRefCount(theExpressionObj);
resultCode = Tcl_ExprLongObj(interp, theExpressionObj, &resultValue);
// Drop the reference; _may_ deallocate or might not, but you shouldn't
// have to care (but don't use theExpressionObj after this, OK?)
Tcl_DecrRefCount(theExpressionObj);
// Need to check for an error here
if (resultCode != TCL_OK) {
// Oh no!
cerr << "Oh no! " << Tcl_GetResult(interp) << endl;
// You need to handle what happens when stuff goes wrong;
// resultValue will *not* have been written do at this point
} else {
// resultValue has what you want now
return resultValue;
}
Tcl是一个完全C的库,因此没有RAII包装器,但是使用一个(可能与智能指针结合)为您管理Tcl_Obj *
引用是很有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.