【发布时间】:2012-02-17 12:41:57
【问题描述】:
我正在使用 SWIG 生成用于从 TCL 调用 C++ 函数的包装器代码。我的 .i 文件仅包含以下内容:
%module baseFunc
%{
#include "tclFunctions.cpp"
%}
%include "tclFunctions.h"
头文件本质上是几十个这样的声明:
static signed int some_function(const char * argN=NULL); //Maybe more arguments
虽然所有 C++ 函数都有固定数量的参数,但来自 TCL 的调用必须有可变数量的参数。我想通过将 TCL 调用的参数列表转换为 std::vector 并将该向量传递给 C++ 函数来实现此行为。这将与许多 C++ 函数中现有的参数检查代码更加兼容。
我尝试将以下内容添加到我的 .h 和 .i 文件中,但它不起作用,因为 SWIG 添加了代码来检查 TCL 端的参数数量。
//tclFunctions.h
static signed int some_function(const std::vector<std::string> args);
//baseFunc.i
%typemap(in) const std::vector<std::string> {
Tcl_Obj **listobjv;
int nitems;
int i;
if (Tcl_ListObjGetElements(interp, $input, &nitems, &listobjv) == TCL_ERROR){
return TCL_ERROR;
}
for (i = 0; i < nitems; i++) {
$1.push_back(Tcl_GetStringFromObj(listobjv[i],0));
}
}
这是检查生成的 _wrap.c 文件中的参数的内容
if(SWIG_GetArgs(interp,objc,objv,"o:test_function args ",(void *)0)==TCL_ERROR)
SWIG_fail;
我如何定义一种方法,以便每当 SWIG 遇到 std::vector<std::string> 作为唯一的函数参数时,它接受任意数量的 TCL 参数并将它们全部放入 std::vector 中? SWIG 不是执行此操作的正确工具吗?我在这里遇到的主要障碍是 SWIG 强制执行参数计数,并且大多数命令以任意顺序采用任意参数。
应用程序不能要求用户执行$some_function {arg1 arg2 arg3} 之类的操作,因为它必须支持此类命令。
#Only 2 args required but they can appear in any order.
report_timing -from [all_registers -clock_pins] \
-to [all_registers -data_pins] -delay_type max \
-path_type full_clock –nosplit \
-max_paths 1 -nworst 1 \
-trans -cap -net > tc_reg2reg_setup.rpt
【问题讨论】:
-
我原以为
vector<string>很适合 Tcl 列表... -
@DonalFellows 你是什么意思?