【问题标题】:Adding a custom Tcl procedure to a SWIG generated Tcl module?将自定义 Tcl 过程添加到 SWIG 生成的 Tcl 模块?
【发布时间】:2014-08-25 12:23:55
【问题描述】:

我正在尝试使用 SWIG 将旧的 Tcl 接口替换为 C++。这是一个示例类:

class test {
  std::string str;
 public:
  test(const char * s):str(s) {}
  void print() const {std::cout << str << std::endl;}
};

这是使用它的标准方法:

load ./example.so example
test s "this is a test string"
s print

但我想保留不使用"" 的旧界面的简单性。我发现我可以这样做:

load ./example.so example

proc TEST {args} { test [lindex $args 0] [lrange $args 1 end] }

TEST s2 this is another test string
s2 print

这看起来很简单而且工作完美,但是,当然,我不能在用户脚本中定义proc。我不确定我还能把它放在哪里。有没有办法把它放在 .i 文件中?

【问题讨论】:

    标签: tcl swig


    【解决方案1】:

    认为你可以把它放在你的.i文件中:

    %init %{
        Tcl_Eval(interp, "proc TEST {args} { ... }");
    %}
    

    这将在模块的初始化函数中插入对Tcl_Eval 的调用(这将使过程),并且只要interp 上下文在那里可用(警告!检查这个!)然后你会得到一切包装精美。

    如果您有很多代码,请考虑改为调用Tcl_EvalFile,或者调用将source 脚本代码的Tcl 代码(如果您需要复杂的脚本来定位要读取的文件,则这是必需的) .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多