【发布时间】:2012-02-16 23:01:55
【问题描述】:
我有一些生成 StringIO 变量的 python 代码。我想使用 stringstream 参数将此变量传递给 C++ 函数(假设 C++ stringstream 是与 python StringIO 最接近的匹配项)。有没有一种使用 swig 进行翻译的简单方法?
我在 swig 模块中看到一个 std_sstream.i,其中包含一些 stringstream 代码,但我在 swig 文档中找不到对它的任何引用,或者在网络搜索中使用它的任何示例。
为了让它更清晰一点,我将在下面包含一些非常简单的代码。 这是 test.cpp,它是固定的,我无法更改:
#include <iostream>
#include <sstream>
#include "test.h"
using namespace std;
void StTest(stringstream &ss)
{
cout << ss << endl;
}
这里的 test.h 也是固定的:
#include <sstream>
using namespace std;
void StTest(stringstream &ss);
这是我可以编辑并调用 C++ StTest 函数的 python 代码(我们称之为 test_test.py):
#!/usr/bin/env python
import StringIO
import test
ss = StringIO.StringIO()
ss.write("Hello")
# Maybe some thing here to convert ss from StringIO to stringstream
test.StTest(ss)
所以我在上面的注释行中寻找可以从 StringIO 转换为 stringstream 的东西。
这是我对 swig 的 test.i 文件的初步挖掘:
/* test.i */
%module test
%{
#include "test.h"
%}
%include "std_sstream.i"
%include "test.h"
我在 test.i 中包含了“std_sstream.i”,因为这是应该使用的,我想。我需要在这个文件中添加一些东西来让它做一些事情吗?
目前运行test_test.py的报错是:
TypeError: in method 'StTest', argument 1 of type 'stringstream &'
【问题讨论】:
标签: c++ python swig stringstream stringio