Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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中声明的模板结构变量? 我试图用Sigg 3.0.5从C++生成Python定义文件。这些定义是模板结构,在我的玩具foo.h中定义为: template<typename T> struct LimitDef { T min; T max; int otherstuff; int etc; } namespace ProjectLimits { const LimitDef<int> Limit1 = { -5, 100, 42, 0}; const LimitDef<double> Limit2 = {-1.0, 1.0, 0, 42}; ... }_Python_C++_Global Variables_Swig - Fatal编程技术网

如何访问SWIG中声明的模板结构变量? 我试图用Sigg 3.0.5从C++生成Python定义文件。这些定义是模板结构,在我的玩具foo.h中定义为: template<typename T> struct LimitDef { T min; T max; int otherstuff; int etc; } namespace ProjectLimits { const LimitDef<int> Limit1 = { -5, 100, 42, 0}; const LimitDef<double> Limit2 = {-1.0, 1.0, 0, 42}; ... }

如何访问SWIG中声明的模板结构变量? 我试图用Sigg 3.0.5从C++生成Python定义文件。这些定义是模板结构,在我的玩具foo.h中定义为: template<typename T> struct LimitDef { T min; T max; int otherstuff; int etc; } namespace ProjectLimits { const LimitDef<int> Limit1 = { -5, 100, 42, 0}; const LimitDef<double> Limit2 = {-1.0, 1.0, 0, 42}; ... },python,c++,global-variables,swig,Python,C++,Global Variables,Swig,编译成Python后,我可以访问新实例化的模板名称(并创建新的LimitDef_int对象,没有问题),我可以看到声明的Limit变量,但类型没有对齐——已经声明的变量是空的、不可访问的对象指针,没有\uuuuuu getmethods\uuu等: >>> import foo >>> newlim = foo.LimitDef_int() >>> newlim.min = 5 >>> print newlim.min 5

编译成Python后,我可以访问新实例化的模板名称(并创建新的
LimitDef_int
对象,没有问题),我可以看到声明的
Limit
变量,但类型没有对齐——已经声明的变量是空的、不可访问的对象指针,没有
\uuuuuu getmethods\uuu
等:

>>> import foo
>>> newlim = foo.LimitDef_int()
>>> newlim.min = 5
>>> print newlim.min
5
>>> print newlim
<foo.LimitDef_int; proxy of <Swig Object of type 'LimitDef< int > *' at 0x17f2338> >
>>> foo.Limit1
<Swig Object of type 'LimitDef< int> *' at 0x17f2b30>
>>> print foo.Limit1.min
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'SwigPyObject' object has no attribute 'min'
>>> dir(foo.Limit1.min)
['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__hex__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__ne__', '__new__', '__oct__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'acquire', 'append', 'disown', 'next', 'own']
但同样,这只适用于新创建的
LimitDef_int
类型及其实例<代码>限制1等不受影响(即使
%extend
块在
%include“foo.h”
之前)

我不太关心创建新实例,因为我能够访问那些现有的
Limit
变量。如果可能的话,我不想修改源代码;我的实际项目文件定义了100多个这样的常量

我缺少什么可以让我拥有
foo.Limit1.min
return
-5

SWIG手册-

为了提供对C全局变量的访问,SWIG创建了一个名为
cvar
的特殊对象,该对象添加到每个SWIG生成的模块中。然后将全局变量作为该对象的属性进行访问

因此,
Limit1
的代理对象可以在
foo.cvar.Limit1
中找到


另请参见

Try
foo.cvar.Limit1
()哇,成功了。非常不明显,因为
Limit1
本身也存在于
foo
的命名空间中。尽管如此,我还是觉得自己很笨。谢谢@Oktalist!SWIG在总体上并不明显。由于这似乎不是重复的,我把我的评论变成了答案,并调整了问题的标签,以帮助其他人搜索这个问题。
>>> import foo
>>> newlim = foo.LimitDef_int()
>>> newlim.min = 5
>>> print newlim.min
5
>>> print newlim
<foo.LimitDef_int; proxy of <Swig Object of type 'LimitDef< int > *' at 0x17f2338> >
>>> foo.Limit1
<Swig Object of type 'LimitDef< int> *' at 0x17f2b30>
>>> print foo.Limit1.min
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'SwigPyObject' object has no attribute 'min'
>>> dir(foo.Limit1.min)
['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__hex__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__ne__', '__new__', '__oct__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'acquire', 'append', 'disown', 'next', 'own']
%extend LimitDef<int> {
    int get_min() { return (*$self).min; }
};