2012-10-23 25 views
8

std::stringのC#への参照でC++関数を変換しようとしています。std :: stringとC#の参照文字列を交換する方法

私のAPIは、次のようになります。

void GetStringDemo(std::string& str); 

理想的には、私はC#

void GetStringDemoWrap(ref string); 

からこのようなものを見てみたい私は、このためのタイプマップを作成する必要があります理解し、私が試しましたstd_string.iファイルで遊んでいるものはほとんどありませんが、私はどこにでもいるとは思いません。誰もが例を持っていますか?私はSwigとC#の両方に新しいので、私は本当のアイデアを考え出すことができません。

答えて

5

誰かがこれを将来的に探している場合には、私はstd_string.iをC#のように作成しました。私のために働くと思われる。私の場合はもっと適切だったのでrefを変更しましたが、refもうまくいくはずです。

私は%と呼ばれるが、私はconstのSTDのためargout定義する必要がある理由

/* ----------------------------------------------------------------------------- 
* std_string_ref.i 
* 
* Typemaps for std::string& and const std::string& 
* These are mapped to a C# String and are passed around by reference 
* 
* ----------------------------------------------------------------------------- */ 

%{ 
#include <string> 
%} 

namespace std { 

%naturalvar string; 

class string; 

// string & 

%typemap(ctype) std::string & "char**" 
%typemap(imtype) std::string & "/*imtype*/ out string" 
%typemap(cstype) std::string & "/*cstype*/ out string" 

//C++ 
%typemap(in, canthrow=1) std::string & 
%{ //typemap in 
    std::string temp; 
    $1 = &temp; 
%} 

//C++ 
%typemap(argout) std::string & 
%{ 
    //Typemap argout in c++ file. 
    //This will convert c++ string to c# string 
    *$input = SWIG_csharp_string_callback($1->c_str()); 
%} 

%typemap(argout) const std::string & 
%{ 
    //argout typemap for const std::string& 
%} 

%typemap(csin) std::string & "out $csinput" 

%typemap(throws, canthrow=1) string & 
%{ SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, $1.c_str()); 
    return $null; %} 

} 

.iファイルから「std_string.i」が含ま:: SWIGは混乱とconstのSTDを上書きするための文字列&は以下のとおりです。 :文字列&もtypemapで指定します。

%typemap(argout)std::string& 
{ 
    //typemap argout std::string& 
    PyObject* obj = PyUnicode_FromStringAndSize((*$1).c_str(),(*$1).length()); 

    $result=SWIG_Python_AppendOutput($result, obj); 
} 

%typemap(argout) const std::string & 
%{ 
    //argout typemap for const std::string& 
%} 
:だから私は、明示的に、私はこのようなものを作成したPython用

(あなたは異なるユースケースを持っている場合があります)私の場合には上書きしないようにそれを伝えます

関連する問題