C# SWIG 矢量<string>到字符串[] [英] C# SWIG vector&lt;string&gt; to string[]

查看:22
本文介绍了C# SWIG 矢量<string>到字符串[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定的 C++ 方法如

std::vector

我怎样才能让 SWIG 像这样向 C# 公开它

string[] getFoo();setFoo(string[] foo);

我意识到很多人们询问关于vector和SWIG,但我没有看到任何解决与 string[] 之间的转换的问题.每个人都提到使用 SWIG 模板,就像这样

%template(StringVector) vector;

但这只是创建了一个数据类型,调用者必须知道手动编组.理想情况下,调用者只需处理 string[] 并让 SWIG 层解决.

如何在 C# 中简单地使用 string[] 而在 C++ 中将其转换为 vector?

解决方案

一种方法可能是添加 隐式转换到 SWIG 生成的 StringVector,以便它能够将自身转换为 string[].>

我尝试通过 typemap 执行此操作,但无法使其正常工作.但是,它可以通过 部分类.

它看起来像这样.在 SWIG 中,

%include std_vector.i%include std_string.i/* 允许部分 c# 类 */%typemap(csclassmodifiers) SWIGTYPE公共部分类"/* 围绕vector生成模板*/%template(StringVector) std::vector

然后在 C# 中,创建 StringVector 的部分版本:

公共部分类 StringVector: IDisposable, System.Collections.IEnumerable#if !SWIG_DOTNET_1, System.Collections.Generic.IList#万一{//从 C# 字符串数组转换公共静态隐式运算符 StringVector(string[] inVal) {var outVal= new StringVector();foreach(inVal 中的字符串元素){outVal.Add(元素);}返回值;}//转换为 C# 字符串数组公共静态隐式运算符 string[](StringVector inVal) {var outVal= 新字符串[inVal.Count];inVal.CopyTo(outVal);返回值;}}

这允许您编写如下所示的 C# 代码:

string[] foo= mymodule.getFoo();string[] thenewfoo= { "一", "二", "三" };mymodule.setFoo(thenewfoo);

它仍然是 C# 方法签名中的 StringVector,但由于客户端编码器只能使用 string[],所以这并不重要.

Given C++ methods like

std::vector< std::string > getFoo();
void setFoo( const std::vector< std::string > &foo);

How can I get SWIG to expose it like this to C#

string[] getFoo();
setFoo(string[] foo);

I realize that lots of people have asked about vector<string> and SWIG, but I haven't seen anything addressing the conversion to/from string[]. Everyone mentions using the SWIG templates, like this

%template(StringVector) vector<string>;

But that just creates a datatype that callers will have to know to marshal manually. Ideally, a caller would only have to deal with string[] and let the SWIG layer figure it out.

How can I simply use string[] in C# but have it turn into vector<string> in C++?

解决方案

One approach could be to add implicit casting to the SWIG-generated StringVector, so that it would be able to cast itself to/from string[].

I tried doing this via a typemap, but couldn't make it work. However, it can be done with partial classes.

It looks something like this. In SWIG,

%include std_vector.i
%include std_string.i

/* allow partial c# classes */
%typemap(csclassmodifiers) SWIGTYPE "public partial class"

/* generate template around vector<string> */
%template(StringVector) std::vector< std::string >;

Then in C#, create a partial version of StringVector:

public partial class StringVector: IDisposable, System.Collections.IEnumerable
#if !SWIG_DOTNET_1
    , System.Collections.Generic.IList<string>
#endif
{
    // cast from C# string array
    public static implicit operator StringVector(string[] inVal) {
        var outVal= new StringVector();
        foreach (string element in inVal) {
            outVal.Add(element);
        }
        return outVal;
    }

    // cast to C# string array
    public static implicit operator string[](StringVector inVal) {
        var outVal= new string[inVal.Count];
        inVal.CopyTo(outVal);
        return outVal;
    }
}

This allows you to write C# code that looks like this:

string[] foo= mymodule.getFoo();
string[] thenewfoo= { "one", "two", "three" };
mymodule.setFoo(thenewfoo);

It's still StringVector in the C# method signatures, but since the client coder can just use string[] it doesn't really matter.

这篇关于C# SWIG 矢量<string>到字符串[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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