如何 SWIG std::string&到 C# 引用字符串 [英] How to SWIG std::string& to C# ref string

查看:40
本文介绍了如何 SWIG std::string&到 C# 引用字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将带有 std::string 引用的 C++ 函数转换为 C#.

I'm trying to convert a C++ function with std::string reference to C#.

我的 API 如下所示:

My API looks like this:

void GetStringDemo(std::string& str);

理想情况下,我希望从 C# 中看到类似的东西

Ideally I would like to see something like this from C#

void GetStringDemoWrap(ref string);

我知道我需要为此创建一个类型映射,并且我通过使用 std_string.i 文件尝试了一些事情,但我认为我没有任何进展.有没有人有例子.我是 Swig 和 C# 的新手,所以我无法提出任何真正的想法.

I understand I need to create a typemap for this, and I tried a few things by playing around with the std_string.i file, but I don't think I'm getting anywhere. Does anyone has any example. I'm new to both Swig and C# so I'm not able to come up with any genuine ideas.

推荐答案

以防万一有人在将来寻找这个,我为 C# 创建了这样的 std_string.i.似乎对我有用.请注意,我将 ref 更改为 out,因为它更适合我的情况,但 ref 也应该有效.

Just in case somebody is looking for this in the future, I created std_string.i like this for C#. Seems to work for me. Note that I changed the ref to out since it was more appropriate in my case but ref should work as well.

我从 .i 文件中调用了 %include "std_string.i"

I called %include "std_string.i" from the .i file

/* -----------------------------------------------------------------------------
 * 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; %}

}

我需要为 const std::string& 定义 argout 的原因是因为 SWIG 会感到困惑并覆盖 const std::string&也与类型图.所以我明确告诉它不要在我的情况下覆盖(你可能有不同的用例)

The reason I need to define argout for const std::string& is because SWIG will get confused and override const std::string& also with the typemap. So I explicitly tell it not to override in my case (You may have a different use case)

对于 Python,我创建了这样的东西:

For Python I created something like this:

%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&
%}

这篇关于如何 SWIG std::string&amp;到 C# 引用字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆