Small fixes for Qt containers converters.
This commit is contained in:
parent
1e8fae534a
commit
ef1ed30d11
2 changed files with 48 additions and 42 deletions
|
|
@ -1,44 +1,4 @@
|
||||||
namespace Shiboken {
|
namespace Shiboken {
|
||||||
template <typename qset>
|
|
||||||
struct Converter_qset
|
|
||||||
{
|
|
||||||
static bool isConvertible(PyObject* pyobj) {
|
|
||||||
return PyIter_Check(pyobj);
|
|
||||||
}
|
|
||||||
|
|
||||||
static PyObject* toPython(const qset& set)
|
|
||||||
{
|
|
||||||
PyObject* result = PySet_New(NULL);
|
|
||||||
foreach(typename qset::value_type x, set) {
|
|
||||||
PyObject *pyObj = Converter<typename qset::value_type>::toPython(x);
|
|
||||||
PySet_Add(result, pyObj);
|
|
||||||
Py_DECREF(pyObj);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
static qset toCpp(PyObject* pyobj)
|
|
||||||
{
|
|
||||||
qset result;
|
|
||||||
PyObject *iterator = PyObject_GetIter(pyobj);
|
|
||||||
|
|
||||||
if (iterator == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
PyObject *pyItem = NULL;
|
|
||||||
while (pyItem = PyIter_Next(iterator)) {
|
|
||||||
result.insert(Converter<typename qset::value_type>::toCpp(pyItem));
|
|
||||||
Py_DECREF(pyItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
Py_XDECREF(iterator);
|
|
||||||
|
|
||||||
if (PyErr_Occurred())
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct Converter<QSet<T> > : Converter_qset<QSet<T> > {};
|
struct Converter<QSet<T> > : QSequenceConverter<QSet<T> > {};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,9 @@
|
||||||
#ifndef PYSIDECONVERSIONS_H
|
#ifndef PYSIDECONVERSIONS_H
|
||||||
#define PYSIDECONVERSIONS_H
|
#define PYSIDECONVERSIONS_H
|
||||||
|
|
||||||
|
#include <Python.h>
|
||||||
|
#include <conversions.h>
|
||||||
|
|
||||||
template <typename QtDict>
|
template <typename QtDict>
|
||||||
struct QtDictConverter
|
struct QtDictConverter
|
||||||
{
|
{
|
||||||
|
|
@ -43,7 +46,7 @@ struct QtDictConverter
|
||||||
if (PyObject_TypeCheck(pyObj, Shiboken::SbkType<QtDict>()))
|
if (PyObject_TypeCheck(pyObj, Shiboken::SbkType<QtDict>()))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (!PyDict_Check(pyObj))
|
if ((Shiboken::SbkType<QtDict>() && Shiboken::isShibokenType(pyObj)) || !PyDict_Check(pyObj))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
PyObject* key;
|
PyObject* key;
|
||||||
|
|
@ -74,6 +77,9 @@ struct QtDictConverter
|
||||||
}
|
}
|
||||||
static inline QtDict toCpp(PyObject* pyobj)
|
static inline QtDict toCpp(PyObject* pyobj)
|
||||||
{
|
{
|
||||||
|
if (PyObject_TypeCheck(pyobj, Shiboken::SbkType<QtDict>()))
|
||||||
|
return *reinterpret_cast<QtDict*>(SbkBaseWrapper_cptr(pyobj));
|
||||||
|
|
||||||
QtDict result;
|
QtDict result;
|
||||||
|
|
||||||
PyObject* key;
|
PyObject* key;
|
||||||
|
|
@ -86,4 +92,44 @@ struct QtDictConverter
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct QSequenceConverter
|
||||||
|
{
|
||||||
|
static inline bool isConvertible(PyObject* pyObj)
|
||||||
|
{
|
||||||
|
if (PyObject_TypeCheck(pyObj, Shiboken::SbkType<T>()))
|
||||||
|
return true;
|
||||||
|
if ((Shiboken::SbkType<T>() && Shiboken::isShibokenType(pyObj)) || !PySequence_Check(pyObj))
|
||||||
|
return false;
|
||||||
|
for (int i = 0, max = PySequence_Length(pyObj); i < max; ++i) {
|
||||||
|
Shiboken::AutoDecRef item(PySequence_GetItem(pyObj, i));
|
||||||
|
if (!Shiboken::Converter<typename T::value_type>::isConvertible(item))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
static PyObject* toPython(const T& cppobj)
|
||||||
|
{
|
||||||
|
PyObject* result = PyList_New((int) cppobj.size());
|
||||||
|
typename T::const_iterator it = cppobj.begin();
|
||||||
|
for (int idx = 0; it != cppobj.end(); ++it, ++idx) {
|
||||||
|
typename T::value_type vh(*it);
|
||||||
|
PyList_SET_ITEM(result, idx, Shiboken::Converter<typename T::value_type>::toPython(vh));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
static T toCpp(PyObject* pyobj)
|
||||||
|
{
|
||||||
|
if (PyObject_TypeCheck(pyobj, Shiboken::SbkType<T>()))
|
||||||
|
return *reinterpret_cast<T*>(SbkBaseWrapper_cptr(pyobj));
|
||||||
|
|
||||||
|
T result;
|
||||||
|
for (int i = 0; i < PySequence_Size(pyobj); i++) {
|
||||||
|
PyObject* pyItem = PySequence_GetItem(pyobj, i);
|
||||||
|
result << Shiboken::Converter<typename T::value_type>::toCpp(pyItem);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue