使用 SWIG 在 python 中访问嵌套结构数组
我无法弄清楚如何访问以下嵌套结构中的数组元素 SubStatus 。我似乎确实能够看到第一个元素,但不明白如何强制索引,例如作为列表。
非常感谢任何帮助。
状态.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 );
状态.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"
I haven't been able to figure out how to access the array elements SubStatus in the following nested structure. I do seem to be able to see the first element, but don't understand how to force indexing, e.g., as a list.
Any help is much appreciated.
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以包装此标头,而无需修改它,购买执行以下操作:
这基本上与我在这里的答案相同,但是我们没有修改标头以使用
wrapped_array
,而是使用%ignore
告诉 SWIG 我们将为它提供我们自己的STATUS
定义包裹。 (这是完全合法的,SWIG 生成的包装器仍将使用 status.h 中的真实定义)我们将
getSubStatus()
注入到这个更改的定义中,它返回一个充当代理的对象STATUS
中的真实数组。该代理依次提供 python 查找以使用下标运算符的__getitem__
、__setitem__
和__len__
。可能有一种方法可以在 Python 中正确执行此操作,而不需要
getSubStatus()
,使 SWIG 设置__swig_setmethods__["SubStatus"]
和__swig_getmethods__["SubStatus" ]
适当,但我不确定如何让 SWIG python 做到这一点。如果您使用的是 C,而不是 C++,您将需要放弃模板而只使用简单的 struct 并使用指针而不是对数组的引用。
You can wrap this header without having to modify it buy doing something like:
This is basically the same as my answer here, but instead of modifying the header to use the
wrapped_array
, we've used%ignore
to tell SWIG we will supply our own definition ofSTATUS
for it to wrap. (This is perfectly legal, the SWIG generated wrapper will still use the real definition from status.h)We inject into this altered definition a
getSubStatus()
, which returns an object that acts as a proxy to the real array inSTATUS
. This proxy in turn supplies__getitem__
,__setitem__
and__len__
that python looks for to use the subscript operator.There might be a way to do this properly in Python without needing the
getSubStatus()
, making SWIG set__swig_setmethods__["SubStatus"]
and__swig_getmethods__["SubStatus"]
appropriately, but I'm not sure off the top of my head how to make SWIG python do that.If you're using C, not using C++ you'll want to drop the template in favour of just a plain
struct
and use a pointer instead of a reference to an array.