簡體   English   中英

如何在C ++中評估應采用Tcl_Obj的Tcl表達式

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM