我正在使用SWIG为我的qt应用程序生成Python绑定。我在多个地方使用QList,并且希望集成SWIG库中的std :: vector之类的QList(请参阅http://www.swig.org/Doc1.3/Library.html#Library_nn15)。
这意味着:
为此,我使用以下代码:https : //github.com/osmandapp/OsmAnd-core/blob/master/swig/java/QList.i
稍后在我的使用QList的类中,我添加了以下代码:
%import "qlist.i"
%template(listfilter) QList<Interface_Filter*>;
class A {
public:
//.....
QList<Interface_Filter*> get_filters();
};
到目前为止,该方法仍然有效,但是并没有给我与std :: vector集成的那种感觉。我在找出std_vector.i,std_container.i,...的哪些部分使对象可迭代时遇到了麻烦。
我如何扩展QList接口文件以使QList可迭代?
您要的是一个qlist.i swig文件,该文件QList
在python中与std_vector.i达到了相同的集成水平std::vector
。
我提供了一个非常基本的扩展qlist.i文件(以及qlisttest.i来向您展示如何使用它),并将尝试解释所需的步骤。
qlist.i
:
%{
#include <QList>
%}
%pythoncode %{
class QListIterator:
def __init__(self, qlist):
self.index = 0
self.qlist = qlist
def __iter__(self):
return self
def next(self):
if self.index >= self.qlist.size():
raise StopIteration;
ret = self.qlist.get(self.index)
self.index += 1
return ret
__next__ = next
%}
template<class T> class QList {
public:
class iterator;
typedef size_t size_type;
typedef T value_type;
typedef const value_type& const_reference;
QList();
size_type size() const;
void reserve(size_type n);
%rename(isEmpty) empty;
bool empty() const;
void clear();
%rename(add) push_back;
void push_back(const value_type& x);
%extend {
const_reference get(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("QList index out of range");
}
void set(int i, const value_type& val) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
(*self)[i] = val;
else
throw std::out_of_range("QList index out of range");
}
int __len__() {
return self->size();
}
const_reference __getitem__(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("QList index out of range");
}
%pythoncode %{
def __iter__(self):
return QListIterator(self)
%}
}
};
%define %qlist_conversions(Type...)
%typemap(in) const QList< Type > & (bool free_qlist)
{
free_qlist = false;
if ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 0)) == -1) {
if (!PyList_Check($input)) {
PyErr_Format(PyExc_TypeError, "QList or python list required.");
SWIG_fail;
}
Py_ssize_t len = PyList_Size($input);
QList< Type > * qlist = new QList< Type >();
free_qlist = true;
qlist->reserve(len);
for (Py_ssize_t index = 0; index < len; ++index) {
PyObject *item = PyList_GetItem($input,index);
Type* c_item;
if ((SWIG_ConvertPtr(item, (void **) &c_item, $descriptor(Type *),0)) == -1) {
delete qlist;
free_qlist = false;
PyErr_Format(PyExc_TypeError, "List element of wrong type encountered.");
SWIG_fail;
}
qlist->append(*c_item);
}
$1 = qlist;
}
}
%typemap(freearg) const QList< Type > &
{ if (free_qlist$argnum and $1) delete $1; }
%enddef
qlisttest.i
:
%module qlist;
%include "qlist.i"
%inline %{
class Foo {
public:
int foo;
};
%}
%template(QList_Foo) QList<Foo>;
%qlist_conversions(Foo);
%inline %{
int sumList(const QList<Foo> & list) {
int sum = 0;
for (int i = 0; i < list.size(); ++i) {
sum += list[i].foo;
}
return sum;
}
%}
包装QList
以使其可从python访问,
这是通过使(部分)类定义可用于swig来实现的。那就是你目前的所作所为qlist.i
。
注意:对于QList<T*>
typedefs const_reference
,您可能需要添加“模板专门化”,const T*
因为您正在使用QList
指针。否则,QList<T*>::const_reference
将是const T*&
,这显然会使swig感到困惑。(参见swig / Lib / std / std_vector.i)
python list和之间的自动转换QList
通常通过使用swig typemaps实现。例如,如果您希望函数f(const QList<int>& list)
能够接受python列表,则需要指定一个输入类型映射,以执行从python列表到a的转换QList<int>
:
%typemap(in) const QList<int> &
{
PyObject * py_list = $input;
[check if py_list is really a python list of integers]
QList<int>* qlist = new QList<int>();
[copy the data from the py_list to the qlist]
$1 = qlist;
}
%typemap(freearg) const QList<int> &
{ if ($1) delete $1; }
在这里,这种情况在几种方面更加困难:
QList
:为此,您需要在typemap中处理这两种情况。T
为a QList<T>
:T
转换为普通类型的转换T
。这是通过Swig功能实现的SWIG_ConvertPtr
。%qlist_conversions(Type)
,您可以使用该宏将typemap附加到QList<Type>
特定的Type
。对于其他转换方向(QList
-> python列表),您应该首先考虑所需的内容。考虑一个返回的C ++函数QList<int>
。从python调用此QList
函数,这应该返回包装的对象,还是应该自动将转换QList
为python列表?
QList
作为python序列访问包装的字符串,即从python进行创建len
和[]
工作
。为此,您需要QList
使用%extend { ... }
并实现__len__
和__getitem__
方法来扩展qlist.i文件中的类。
如果切片也应该起作用,则需要提供__getitem__(PySliceObject *slice)__
成员方法以及的输入和“ typecheck”类型映射PySliceObject
。
如果您希望能够QList
通过[]
python修改包装后的值,则需要实现__setitem__
。
有关可以实现以实现更好的集成的所有有用方法的列表,请参见有关“内置类型”和“容器的抽象基类”的python文档。
注意:如果使用swig-builtin
功能,则需要使用以下命令将上述功能另外注册到相应的“插槽”中
%feature("python:slot", "sq_length", functype="lenfunc") __len__;
QList
从python使包装的迭代成为可实现的
为此,您需要扩展QList
类并实现一个__iter__()
返回python迭代器对象的方法。
python迭代器对象是提供方法__iter__()
和__next__()
(next()
对于较旧的python)的对象,其中__next__()
返回下一个值并引发python异常StopIteration
以指示结束。
如前所述,您可以在python或C ++中实现迭代器对象。我展示了在python中执行此操作的示例。
我希望这有助于您调整所需的功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句