Swig:如何键入map c ++ string const&到python字符串?

时间:2017-10-03 14:55:09

标签: python c++ swig

我有一个包含这些原型的构造函数:

YCPTerm(const string& s);
YCPTerm(const string& s, const YCPList& l);
YCPTerm(bytecodeistream & str);

我使用swig生成python绑定。在python中,我尝试调用构造函数,并得到它:

>>> import ycp
>>> ycp.YCPTerm("Empty")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ycp.py", line 575, in __init__
    this = _ycp.new_YCPTerm(*args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'new_YCPTerm'.
  Possible C/C++ prototypes are:
    YCPTerm::YCPTerm(string const &)
    YCPTerm::YCPTerm(string const &,YCPList const &)
    YCPTerm::YCPTerm(bytecodeistream &)

我发现this answer似乎是一个类似的问题,但是类型映射类型检查并不适用于我。

这就是我的尝试:

%typemap(typecheck,precedence=141) const std::string& str {
  $1 = Z_TYPE_PP($input) == IS_STRING;
}

我该如何解决这个问题?我是否需要输入和输出类型图来转换值?我已经reading about typemap's here,并且不确定如何键入一个const字符串&amp;。

2 个答案:

答案 0 :(得分:0)

我找到了answer here

将这些行添加到我的.i文件中修复了错误:

%include std_string.i
%inline %{
using namespace std;
%}

我不明白为什么。也许其他人可以回答这个问题。

答案 1 :(得分:0)

std_string.i库包含在std::string和Python字符串之间来回转换的typedef。有关详细信息,请参阅the documentation on the std::string library in SWIG