aboutsummaryrefslogtreecommitdiffstats
path: root/PySide/QtCore/qmap_conversions.h
blob: f7cd2c85bbb4015bc9503aef5ec108b96e11a125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace Shiboken {
template <typename qmap>
struct Converter_qmap
{
    static bool isConvertible(PyObject* pyobj) {
        return PyDict_Check(pyobj);
    }

    static PyObject* toPython(const qmap& map)
    {
        PyObject* result = PyDict_New();

        QMapIterator<typename qmap::key_type, typename qmap::mapped_type> it(map);
        while (it.hasNext()) {
            it.next();
            PyDict_SetItem(result,
                           Converter<typename qmap::key_type>::toPython(it.key()),
                           Converter<typename qmap::mapped_type>::toPython(it.value()));
        }

        return result;
    }
    static qmap toCpp(PyObject* pyobj)
    {
        qmap result;

        PyObject* key;
        PyObject* value;
        Py_ssize_t pos = 0;

        Py_INCREF(pyobj);

        while (PyDict_Next(pyobj, &pos, &key, &value)) {
            result.insert(Converter<typename qmap::key_type>::toCpp(key),
                          Converter<typename qmap::mapped_type>::toCpp(value));
        }

        Py_DECREF(pyobj);

        return result;
    }
};

template<typename KT, typename VT>
struct Converter<QMap<KT, VT> > : Converter_qmap<QMap<KT, VT> > {};
}