StringIO 変数を生成する Python コードがいくつかあります。stringstream パラメータを使用して、この変数を C++ 関数に渡したいと思います (C++ stringstream が python StringIO に最も近いと仮定して)。swig を使用してこの翻訳を行う簡単な方法はありますか?
swigモジュールには、いくつかのstringstreamコードを含むstd_sstream.iがあることがわかりますが、swigドキュメントでそれへの参照や、Web検索での使用例が見つかりません。
もう少しわかりやすくするために、いくつかの非常に単純なコードを以下に示します。これは修正済みで変更できない 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"
「std_sstream.i」を test.i に含めました。これを使用する必要があると思います。何かを行うために、このファイルに何かを追加する必要がありますか?
現在、test_test.py を実行したときのエラー レポートは次のとおりです。
TypeError: in method 'StTest', argument 1 of type 'stringstream &'