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

Python SWIG:将C ++返回参数转换为返回值,并将原始C ++类型转换为Python类型

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

Python SWIG:将C ++返回参数转换为返回值,并将原始C ++类型转换为Python类型

就像$ 1是对输入类型图中的Python输入对象的引用一样,$ 1是对argout类型图中的C
++输出变量的引用。使用此方法,您可以为该数据生成一个Python对象,并将其附加到结果中。

这是Windows的功能示例:

测试

#ifdef EXPORT#define API __declspec(dllexport)#else#define API __declspec(dllimport)#endifstruct ResultType{    int x;    double y;};API void execute(int x, double y, ResultType& result1, ResultType& result2);

测试文件

#define EXPORT#include "test.h"API void execute(int x, double y, ResultType& result1, ResultType& result2){    result1.x = 2 * x;    result1.y = 2 * y;    result2.x = 3 * x;    result2.y = 3 * y;}

测试

%module test%{#include "test.h"%}%include <windows.i>%typemap(in,numinputs=0) ResultType& %{    // Create a persistent object to hold the result;    $1 = new ResultType;%}%typemap(argout) ResultType& (PyObject* tmp) %{    // Store the persistent object in a PyObject* that will be destroyed    // when it goes out of scope.    tmp = SWIG_NewPointerObj($1, $1_descriptor, SWIG_POINTER_OWN);    $result = SWIG_Python_AppendOutput($result, tmp);%}%include "test.h"

输出量

>>> import test>>> r = test.execute(2,3)>>> r[0].x4>>> r[0].y6.0>>> r[1].x6>>> r[1].y9.0


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

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

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