gpt4 book ai didi

c++ - swig perl typemap(out) std::vector 没有在 perl 中返回所需的输出

转载 作者:太空狗 更新时间:2023-10-29 20:50:29 27 4
gpt4 key购买 nike

我正在尝试键入一个 typemap(out) std::vector。我希望它以数组的形式到达 perl 代码,而不是我得到一个数组数组,在双重取消引用之后它包含所需的数据。我怎样才能使它成为 perl 中的字符串数组?

我尝试自己编辑类型映射,并在不进行编辑的情况下使用“std_vector.i”和“std_string.i”中的类型映射,它们都给出了相同的结果。

这是类型映射代码:

%typemap(out) std::vector<std::string> {
int len = $1.size();
SV *svs = new SV[len];
for (int x = 0; x < len; x++) {
SV* sv = sv_newmortal();
sv_setpvn(sv, $1[x].data(), $1[x].size());
svs[x] = SvPV(sv, $1[x].size());
}
AV *myav = av_make(len, svs);
delete[] svs;
$result = newRV_noinc((SV*) myav);
sv_2mortal($result);
argvi++;
}

我测试输出的代码:

#this return a std vector<string> in the cpp code
my @commitReturn = $SomeClass->commit();
print "\n";
#this should return a string instead it returns an array.
print $commitReturn[0];
print "\n";
#this should not work, instead it returns the desired output.
print $commitReturn[0][0];

输出是:

ARRAY(0x908c88)
20790

代替:

20790
Can't use string ("20791") as an ARRAY ref while "strict refs"

最佳答案

您的commit 方法只是返回一个数组引用,而不是数组引用的数组。也许它看起来像一个数组引用数组,因为您正在将结果分配给一个数组?

无论如何,在不触及类型映射代码的情况下,您可以取消引用函数调用

@commitReturn = @{$SomeClass->commit()};

或者创建一个包装器方法来为你取消引用它

package SomeClass;
...
sub commit_list {
my $self = shift;
@{$self->commit()};
}
...
@commitReturn = $SomeClass->commit_list();

关于c++ - swig perl typemap(out) std::vector<std::string> 没有在 perl 中返回所需的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54345460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com