Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SWIG在python中访问嵌套结构数组_Python_Arrays_Structure_Swig - Fatal编程技术网

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

使用SWIG在python中访问嵌套结构数组,python,arrays,structure,swig,Python,Arrays,Structure,Swig,我还没有弄清楚如何在下面的嵌套结构中访问数组元素子状态。我似乎能够看到第一个元素,但不知道如何强制索引,例如作为列表 非常感谢您的帮助 现状.h: 地位,我 您可以包装此标题,而无需进行如下修改: %module status %immutable; %inline %{ template <typename Type, size_t N> struct wrapped_array { Type (&data)[N]; wrapped_array(Type (&am

我还没有弄清楚如何在下面的嵌套结构中访问数组元素子状态。我似乎能够看到第一个元素,但不知道如何强制索引,例如作为列表

非常感谢您的帮助

现状.h: 地位,我
您可以包装此标题,而无需进行如下修改:

%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"
%模块状态
%不变的;
%内联%{
模板
结构包裹数组{
类型和数据[N];
包装的数组(类型(&数据)[N]):数据(数据){
};
%}
%可变的;
%{
#包括“status.h”
%}
%包括“typemaps.i”
%包括“std_除外,i”
//仅公开减少的状态,而不公开阵列:
类型定义结构
{
UCHAR qrs;
乌查尔图夫;
长wxy;
}地位;
%扩展包装数组{
内联大小\u t\u len\u()常量{return N;}
内联常量类型和getitem常量抛出(大小)常量抛出(标准::超出范围){
如果(i>=N | | i<0)
抛出标准::超出范围(“越界访问”);
返回$self->data[i];
}
内联void\uuuuu setitem\uuuuuu(大小、常数类型和v)抛出(标准::超出范围){
如果(i>=N | | i<0)
抛出标准::超出范围(“越界访问”);
$self->data[i]=v;
}
}
%模板(子状态数组)包装的数组;
//在助手中隐藏真实数组
%扩展状态{
包装数组getSubStatus(){
返回包装的数组($self->SubStatus);
}
}
%忽略状态;//我们已经指定了另一种包装方式
%包括“status.h”
这与基本相同,但我们没有修改标题以使用
包装的数组
,而是使用
%ignore
告诉SWIG我们将提供自己的
状态定义
,以便包装。(这是完全合法的,SWIG生成的包装器仍将使用status.h中的真实定义)

我们将一个
getSubStatus()
注入到这个修改过的定义中,它返回一个对象,该对象充当
状态下真实数组的代理。该代理依次提供python使用下标操作符所需的
\uuuuuu getitem\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
\uuuuuuuuuuuuuuuuuuuuuuuuuuu

在Python中,可能有一种方法可以正确地实现这一点,而不需要适当地使用
getSubStatus()
,使SWIG set
\uuu SWIG\u setmethods\uuuuu[“SubStatus”]
\uuu SWIG\u getmethods\uuuuuu[“SubStatus”]
,但我不知道如何让SWIG Python做到这一点


如果你使用C,而不是使用C++,你会想放弃模板,仅仅是一个普通的<代码>结构> /CUT>,使用一个指针而不是一个数组的引用。< /P>可能是感谢Ad林地的复制,但是我不能控制.h文件,因为这是来自供应商的分布。如果我正确理解了其他解决方案,我就必须修改c源代码,对吗?我希望得到一个只会影响.I文件的答案。你可以在不修改标题的情况下应用它,尽管这有点棘手。我希望稍后会给出一个例子。Flexo,我试图用python/swig做类似的事情,但在我的例子中,我想使用C。所以我做了一些类似于Lou K的代码,但使用了一个玩具结构,

typdef struct{int I;int ai[10];}mystruct我试图用
typdef struct{int data[10];}包装的_数组_int_10
包装的数组\u int\u 10*getai()
。我编译了代码,但是从python尝试时,我得到了一个错误,mystruct没有构造函数。你能告诉我我错过了什么吗?谢谢……你是用C还是C++建造的?我用C来建造。
%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"
%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"