【问题标题】:SWIG generated TCL-wrappers with arbitrary number of argumentsSWIG 生成具有任意数量参数的 TCL 包装器
【发布时间】: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&lt;std::string&gt; 作为唯一的函数参数时,它接受任意数量的 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&lt;string&gt; 很适合 Tcl 列表...
  • @DonalFellows 你是什么意思?

标签: c++ tcl swig


【解决方案1】:

我现在不在电脑前给你一个经过验证的工作示例,但是你需要将 Tcl 列表转换为向量的代码看起来像:

%include <std_vector.i>
%include <std_string.i>
// instantiate template and give it a Pythonic name.
%template(vstr) std::vector<std::string>;

然后是 C++ 函数,如:

int some_function(const std::vector<std::string> args);

可以在 Tcl 中调用:

some_function {arg1 arg2 ... argN}

编辑

抱歉,我想我没有从头到尾阅读有关限制的内容。 SWIG 并不真正支持变量参数列表,但您可以在 Tcl 中为 SWIG 函数编写代理函数:

proc report_timing {args} {
        swigs_report_timing $args
}

这允许report_timing 使用的变量参数作为单个参数列表传递。

【讨论】:

  • 这不是提问者所要求的,但如果他们要使用字符串向量,这就是他们得到的,而且这似乎是 正确的方法 .一个简单的 Tcl 包装器可以处理剩下的事情,让事情看起来像他们想要的那样(这将是最简单的方法)。
  • 正确的方式到底是什么意思?传递单个列表来表示多个函数参数与 TCL 自己的内置命令和我使用的所有其他 EDA 工具不一致。
  • @Adam:但用户可能永远不需要知道“真实”函数不能接受多个参数。只需编写另一个函数作为包装器可以接受多个参数并正确格式化这些参数以调用“真实”函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多