SWIG Python固定大小的数组通过引用传递 [英] SWIG Python fixed size array passed by reference

查看:705
本文介绍了SWIG Python固定大小的数组通过引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出如何传递一个固定大小的数组通过引用使用SWIG到python。主要是我一直在考虑numpy.i接口。

I have been trying to figure out how to pass a fixed size array by reference using SWIG to python. Mostly I have been considering the numpy.i interface for this. However, I can't seem to find any reference to this online.

对于常规数组传递给numpy,你所做的是首先是<$ c中的C ++函数$ c> foo.h :

For regular array passing to numpy the way you do is it first the C++ function in foo.h:

void foo(double* array, int length);

SWIG文件的相关部分是:

And the relevant part of the SWIG file is:

%include "numpy.i"

%init %{
import_array();
%}

%apply (unsigned char* IN_ARRAY1, int DIM1) {(unsigned char* frame, int len)};

%include "foo.h"

问题是,当C ++函数为:

The question is how do you perform this when the C++ function is:

void bar(double (&array)[3]);


推荐答案

这是一个3个双精度数组的引用,对?根据 34.3.9指针,引用,值和数组,您应该能够使用。

This is a reference to an array of 3 doubles, right? According to 34.3.9 Pointers, references, values, and arrays, you should be able to use as is.

否则,您可以尝试 34.9.4将Python元组映射到小数组,或者之后的部分。或者在您的问题中提供有关您希望能够编写哪些Python代码的更多详细信息,如果您只是让SWIG导出 bar()函数没有类型。

Otherwise, you could try 34.9.4 Mapping Python tuples into small arrays, or the section after that. Or provide further details in your question about what Python code you would like to be able to write vs the code you have to write if you just let SWIG export the bar() function as is, without typemaps.

更新:您还应该能够将%extend或%inline转换为阵列版本的适配器函数,例如:

Update: You should also be able to %extend or %inline an adapter function that forwards to the array version, something like:

%inline %{
    void bar(double* array, int length) {
         typedef double triplet[3]; // array of 3 doubles
         bar((triplet&)array); // shouldselect your bar since only one arg
    }
%}

警告:未测试;)

这篇关于SWIG Python固定大小的数组通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆