【发布时间】:2019-06-18 02:50:54
【问题描述】:
我正在尝试键入 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"
【问题讨论】: