如何在 perl 中将多维映射从 c++ 转换为哈希
How to convert multidimensional map from c++ into hash in perl
我是swig的新手,我正在尝试将c ++映射转换为Perl中的哈希。
有一个使用 c++ 的 Perl 扩展需要将多维数组或映射返回到 Perl。 我在 swig 界面文件中使用了模板,但它给出了空结果。
SWIG 接口文件 :
%include "exception.i"
%exception {
try {
$action
} catch (const std::exception &e) {
SWIG_exception_fail(SWIG_RuntimeError, e.what());
}
}
%module new_tr
%{
/* Put headers and other declarations here */
#include "new_tr.h"
%}
%include <std_map.i>
%template(StringStringMap) std::map<int, std::map<std::string,std::string>>;
%include "new_tr.h"
CPP 文件
std::map<int, std::map<std::string,std::string>> Memberfunction::m;
std::map<int, std::map<std::string,std::string>> Memberfunction::getResult()
{
return m;
}
珀尔文件
use strict;
use new_tr;
use Data::Dumper;
my $new=new new_tr::some();
print Dumper($new->getResult());
电流输出 :
$VAR1 = bless( {}, 'new_tr::StringStringMap' );
预期输出:(多维哈希(
$VAR1 = bless( 0 => {
'Brown' => 'Manager',
'Smith' => 'Salesman',
'Albert' => 'Salesman',
},
1 => {
'Penfold' => 'Designer',
'Evans' => 'Tea-person',
'Jurgens' => 'Manager',
}, 'new_tr::StringStringMap' );
下面是如何将%typemap
用于嵌套std::map
的示例。
new_tr.i:
%module new_tr
%typemap(out) std::map<int, std::map<std::string,std::string>> {
HV *hash = (HV *) newHV();
for (auto const& item : $1) {
HV *subhash;
char *keysv;
auto map = item.second;
subhash = (HV *) newHV();
for (auto const &item2 : map) {
SV *sv;
auto key2 = item2.first.c_str();
auto value = item2.second.c_str();
sv = newSVpvn( value, strlen(value) );
hv_store (subhash, key2, strlen(key2), sv, 0);
}
auto key = std::to_string(item.first).c_str();
hv_store (hash, key, strlen(key), (SV*) newRV_noinc((SV*)subhash), 0);
}
$result = newRV_noinc((SV*) hash);
sv_2mortal($result);
argvi++;
}
%{
#include "new_tr.h"
%}
%include "new_tr.h"
new_tr.h:
#include <map>
#include <string>
using ssmap = std::map<int, std::map<std::string,std::string>>;
ssmap getResult();
new_tr.cxx:
#include "new_tr.h"
ssmap getResult()
{
ssmap map = {
{1, {{"Brown","Manager"}, {"Smith", "Salesman"}, {"Albert", "Salesman"}}},
{2, {{"Penfold", "Designer"}, {"Evans", "Tea-person"}, {"Jurgens", "Manager"}}}
};
return map;
}
然后使用以下命令编译模块:
perl_include_dir=$(perl -MConfig -e'print $Config{archlib}')"/CORE"
swig -perl5 -c++ -I/usr/include new_tr.i
g++ -fPIC -c new_tr.cxx
g++ -I${perl_include_dir} -c -fPIC -g -o new_tr_wrap.o new_tr_wrap.cxx
g++ -shared -L. new_tr.o new_tr_wrap.o -o new_tr.so
并用test.pl进行测试:
use strict;
use warnings;
use Data::Dumper;
use lib '.';
use new_tr;
my $map = new_tr::getResult();
print Dumper( $map );
输出:
$VAR1 = {
'1' => {
'Albert' => 'Salesman',
'Smith' => 'Salesman',
'Brown' => 'Manager'
},
'2' => {
'Penfold' => 'Designer',
'Jurgens' => 'Manager',
'Evans' => 'Tea-person'
}
};
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 内联映射初始化的动态atexit析构函数崩溃
- 使用"std::unordereded_map"映射到"std::list"对象
- 如何从多映射中删除特定的重复项
- 在未初始化映射的情况下,将值插入到映射的映射中
- QT通过C++添加映射QML项目
- 在c++中访问int到类对象的映射时出错
- 在C++中搜索嵌套多映射值
- 错误处理.将系统错误代码映射到泛型
- C++匿名结构作为std::映射值
- 如何从存储在std::映射中的std::集中删除元素
- 递归无序映射
- 正在VS调试器中监视映射条目