如何在c++中计算应该接受Tcl_Obj的Tcl表达式
How to eval a Tcl expression that should take a Tcl_Obj in C++
在c++代码中,我有一个(swig生成的)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 *
引用是很有意义的。
相关文章:
- 如何在.obj文件上运行IlDasm?
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 在 opengl 中渲染 obj 文件时出现黑窗口
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- 正在读取 obj 文件!(指数)
- 我在主函数的左括号上不断收到错误,消息为obj\Debug\main.o||在函数"ZN11linked_listC1Ev"中:|
- float* 已在 Gameobject.obj 中定义
- 可执行文件C++包括.obj,.lib和.dll吗?
- C++成功复制动态分配的 obj 而不复制 ctor?
- OBJ 文件C++与 ubuntu 问题
- 为什么定义复制构造函数会给我错误:无法将类型 'obj&' 的非常量左值引用绑定到类型为"obj"的右值?
- OBJ 向量到成员函数
- 如何在C++代码上使用 Tcl-C API
- 在istream_iterator中使用后的 C++ FSTREAM obj 回滚
- 使用sscaf_s从obj读取顶点线不起作用
- 函数 C++>Tcl 与 Critcl
- 移植DXUT SDKMesh以导入OBJ文件,在哪里添加纹理坐标和索引
- /usr/bin/ld: build-debug/obj/gpsitem.o: 文件无法识别: 文件被截断
- 将多个网格放入单个波前 OBJ 文件中
- :-1: 错误: LNK1104: 无法打开文件"我的项目.obj"