就像$ 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



