如何SWIG std::string&;至C#参考字符串
我试图将C++函数转换为<代码> STD::String 引用到Cy. 我的API如下所示:如何SWIG std::string&;至C#参考字符串,c#,c++,string,swig,C#,C++,String,Swig,我试图将C++函数转换为 STD::String 引用到Cy. 我的API如下所示: void GetStringDemo(std::string& str); %typemap(argout)std::string& { //typemap argout std::string& PyObject* obj = PyUnicode_FromStringAndSize((*$1).c_str(),(*$1).length()); $result
void GetStringDemo(std::string& str);
%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&
%}
理想情况下,我希望从C#
我知道我需要为此创建一个typemap,我尝试了一些方法,使用std_字符串.I文件,但我认为我没有任何进展。有人举过例子吗。我对Swig和C#都是新手,所以我想不出任何真正的想法 为了防止将来有人找这个,我创建了std_字符串。我喜欢C#的这个。似乎对我有用。注意,我将ref改为out,因为它更适合我的情况,但ref也应该工作 我从.I文件中调用了%include“std_string.I”
/* -----------------------------------------------------------------------------
* 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; %}
}
%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&
%}