使用SWIG在python中嵌套结构数组访问

时间:2011-10-10 13:15:45

标签: python arrays structure swig

我无法弄清楚如何在以下嵌套结构中访问数组元素SubStatus。我似乎能够看到第一个元素,但不明白如何强制索引,例如,作为列表。

非常感谢任何帮助。

status.h:

// Data Types
typedef char           CHAR;   // 8 bits signed
typedef short          SHORT;  // 16 bits signed
typedef long           LONG;   // 32 bits signed
typedef unsigned char  UCHAR;  // 8 bits unsigned
typedef unsigned short USHORT; // 16 bits usigned

#define FUNC_TYPE       // built in C, leave reference as C
#define DLL_API extern FUNC_TYPE __declspec(dllimport)

// Sub Status Data
typedef struct
{
    LONG  xyz;                              
    LONG  abc;                                           
} SUB_STATUS;


// Status Info
typedef struct
{
    UCHAR  qrs;             
    UCHAR  tuv;             
    SUB_STATUS SubStatus[4];     
    LONG   wxy;     
} STATUS;

DLL_API  SHORT  GetStatus( STATUS *Status );

status.i

%module status
 %{
 /* Includes the header in the wrapper code */
 #include "status.h"
 %}

 /* Parse the header file to generate wrappers */
 %include "windows.i"
 %include "typemaps.i" 
 %include "status.h"

1 个答案:

答案 0 :(得分:1)

你可以包装这个标题,而不必修改它做类似的事情:

%module status

%immutable;
%inline %{
template <typename Type, size_t N>
struct wrapped_array {
  Type (&data)[N];
  wrapped_array(Type (&data)[N]) : data(data) { }
};
%}
%mutable;

%{
#include "status.h"
%}

%include "typemaps.i"
%include "std_except.i"

// Only expose a reduced STATUS, without the Array:
typedef struct
{
    UCHAR  qrs;
    UCHAR  tuv;
    LONG   wxy;
} STATUS;

%extend wrapped_array {
  inline size_t __len__() const { return N; }

  inline const Type& __getitem__(size_t i) const throw(std::out_of_range) {
    if (i >= N || i < 0)
      throw std::out_of_range("out of bounds access");
    return $self->data[i];
  }

  inline void __setitem__(size_t i, const Type& v) throw(std::out_of_range) {
    if (i >= N || i < 0)
      throw std::out_of_range("out of bounds access");
    $self->data[i] = v;
  }
}

%template (SubStatusArray) wrapped_array<SUB_STATUS,4>;

// Hide the real array in our helper

%extend STATUS {
  wrapped_array<SUB_STATUS,4> getSubStatus() {
    return wrapped_array<SUB_STATUS,4>($self->SubStatus);
  }
}

%ignore STATUS; // We've specified an alternative way of wrapping this
%include "status.h"

这与my answer here基本相同,但我们使用wrapped_array告诉SWIG我们将提供自己的%ignore定义,而不是修改标头以使用STATUS。 {1}}它包装。 (这是完全合法的,SWIG生成的包装器仍将使用status.h中的实际定义)

我们在这个修改后的定义中注入一个getSubStatus(),它返回一个对象,作为STATUS中真实数组的代理。这个代理反过来提供python寻找的__getitem____setitem____len__来使用下标运算符。

可能有一种方法可以在Python中正确地执行此操作而无需getSubStatus(),从而使SWIG设置为__swig_setmethods__["SubStatus"]__swig_getmethods__["SubStatus"],但我不确定如何让SWIG python做到这一点。

如果你正在使用C,而不是使用C ++,你会想要放弃模板而只支持普通struct并使用指针代替对数组的引用。

相关问题