栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

SWIG包装的向量载体(从C ++到python)-如何将内部向量识别为代理对象?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SWIG包装的向量载体(从C ++到python)-如何将内部向量识别为代理对象?

我曾与我的一位同事合作过,我们设法提出了一些解决方案。

首先,在SWIG .i文件中,定义此预处理器变量很重要:

%{#   define SWIG_PYTHON_EXTRA_NATIVE_ConTAINERS %}

然后,为了确保从诸如front(),back(),operator []等方法返回的引用实际上已映射到内部向量的正确代理类型,以下类型映射帮助:

// In pop()%typemap(out) std::vector<std::vector<ns::uint64_t> >::value_type { $result = SWIG_NewPointerObj(SWIG_as_voidptr(&$1), $descriptor(std::vector<ns::uint64_t>), 0 |  0 ); }// In front(), back(), __getitem__()%typemap(out) std::vector<std::vector<ns::uint64_t> >::value_type & {     $result = SWIG_NewPointerObj(SWIG_as_voidptr($1), $descriptor(std::vector<ns::uint64_t>), 0 |  0 ); }

我们还发现,如果您想将ns :: uint64_t视为python long变量(等效于C unsigned long
long),则还需要一些其他类型映射,以确保使用值和引用的矢量方法仅使用64位整数值。

// In __getitem__()%typemap(out) ns::uint64_t {    $result = PyLong_FromUnsignedLongLong($1);}// Not used (but probably useful to have, just in case)%typemap(in) ns::uint64_t {    $1 = PyLong_AsUnsignedLongLong($input);}// In pop()%typemap(out) std::vector<ns::uint64_t>::value_type {    $result = PyLong_FromUnsignedLongLong($1);}// In __getitem__(), front(), back()%typemap(out) std::vector<ns::uint64_t>::value_type & {    $result = PyLong_FromUnsignedLongLong(*$1);}// In __setitem__(), append(), new Uint64Vector, push_back(), assign(), resize(), insert()// This allows a python long literal number to be used as a parameter to the above methods. // Note the use of a local variable declared at the SWIG wrapper function scope,// by placing the variable declaration in parentheses () prior to the open brace {%typemap(in) std::vector<ns::uint64_t>::value_type & (std::vector<ns::uint64_t>::value_type temp) {    temp = PyLong_AsUnsignedLongLong($input);    $1 = &temp;}

我希望这种解决方案将来对人们有所帮助!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611732.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号