diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-10-23 16:47:58 +0200 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2018-11-29 17:12:44 +0000 |
commit | eb84213e9cd16abd8f9eeed465e14c8f9181b7b6 (patch) | |
tree | f838642024c946093b15facfa72d6ede07cc7d82 /sources/pyside2 | |
parent | 93b2892b2f63d785394f14fc5f853a9927ef59f7 (diff) |
libpyside: Refactor MetaObjectBuilderPrivate::parsePythonType()
- Use std::vector to collect the base types. Append the type
first to avoid reshuffling when prepending.
- Add properties immediately instead of storing in the 2nd
loop as signals are already added.
- Clang tidy: Use range based for, auto, nullptr
Task-number: PYSIDE-784
Change-Id: I765beb2d09600e78abd314cbb394cd106ec22f03
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'sources/pyside2')
-rw-r--r-- | sources/pyside2/libpyside/dynamicqmetaobject.cpp | 59 |
1 files changed, 26 insertions, 33 deletions
diff --git a/sources/pyside2/libpyside/dynamicqmetaobject.cpp b/sources/pyside2/libpyside/dynamicqmetaobject.cpp index 5cbfa70f9..b664e149b 100644 --- a/sources/pyside2/libpyside/dynamicqmetaobject.cpp +++ b/sources/pyside2/libpyside/dynamicqmetaobject.cpp @@ -424,31 +424,35 @@ void MetaObjectBuilderPrivate::parsePythonType(PyTypeObject *type) const PyObject *mro = type->tp_mro; const Py_ssize_t basesCount = PyTuple_GET_SIZE(mro); PyTypeObject *qObjectType = Shiboken::Conversions::getPythonTypeObject("QObject*"); - QVector<PyTypeObject *> basesToCheck; + + std::vector<PyTypeObject *> basesToCheck; + // Prepend the actual type that we are parsing. + basesToCheck.reserve(1u + basesCount); + basesToCheck.push_back(type); + + auto sbkObjTypeF = reinterpret_cast<PyTypeObject *>(SbkObject_TypeF()); + auto baseObjType = reinterpret_cast<PyTypeObject *>(&PyBaseObject_Type); for (Py_ssize_t i = 0; i < basesCount; ++i) { - PyTypeObject *baseType = reinterpret_cast<PyTypeObject *>(PyTuple_GET_ITEM(mro, i)); - if (PyType_IsSubtype(baseType, qObjectType) - || baseType == reinterpret_cast<PyTypeObject *>(SbkObject_TypeF()) - || baseType == reinterpret_cast<PyTypeObject *>(&PyBaseObject_Type)) { - continue; + auto baseType = reinterpret_cast<PyTypeObject *>(PyTuple_GET_ITEM(mro, i)); + if (baseType != sbkObjTypeF && baseType != baseObjType + && PyType_IsSubtype(baseType, qObjectType) == 0) { + basesToCheck.push_back(baseType); } - basesToCheck.append(baseType); } - // Prepend the actual type that we are parsing. - basesToCheck.prepend(type); // PYSIDE-315: Handle all signals first, in all involved types. - for (int baseIndex = 0, baseEnd = basesToCheck.size(); baseIndex < baseEnd; ++baseIndex) { - PyTypeObject *baseType = basesToCheck[baseIndex]; + // Leave the properties to be registered after signals because they may depend on + // notify signals. + for (PyTypeObject *baseType : basesToCheck) { PyObject *attrs = baseType->tp_dict; - PyObject *key = 0; - PyObject *value = 0; + PyObject *key = nullptr; + PyObject *value = nullptr; Py_ssize_t pos = 0; while (PyDict_Next(attrs, &pos, &key, &value)) { if (Signal::checkType(value)) { // Register signals. - PySideSignal *data = reinterpret_cast<PySideSignal *>(value); + auto data = reinterpret_cast<PySideSignal *>(value); const char *signalName = Shiboken::String::toCString(key); data->signalName = strdup(signalName); QByteArray sig; @@ -470,23 +474,17 @@ void MetaObjectBuilderPrivate::parsePythonType(PyTypeObject *type) // PYSIDE-315: Now take care of the rest. // Signals and slots should be separated, unless the types are modified, later. // We check for this using "is_sorted()". Sorting no longer happens at all. - for (int baseIndex = 0, baseEnd = basesToCheck.size(); baseIndex < baseEnd; ++baseIndex) { - PyTypeObject *baseType = basesToCheck[baseIndex]; + for (PyTypeObject *baseType : basesToCheck) { PyObject *attrs = baseType->tp_dict; - PyObject *key = 0; - PyObject *value = 0; + PyObject *key = nullptr; + PyObject *value = nullptr; Py_ssize_t pos = 0; - typedef std::pair<const char *, PyObject *> PropPair; - QVector<PropPair> properties; - while (PyDict_Next(attrs, &pos, &key, &value)) { if (Property::checkType(value)) { - // Leave the properties to be registered after signals because they may depend on - // notify signals. - int index = m_baseObject->indexOfProperty(Shiboken::String::toCString(key)); + const int index = m_baseObject->indexOfProperty(Shiboken::String::toCString(key)); if (index == -1) - properties << PropPair(Shiboken::String::toCString(key), value); + addProperty(Shiboken::String::toCString(key), value); } else if (PyFunction_Check(value)) { // Register slots. if (PyObject_HasAttr(value, slotAttrName)) { @@ -501,21 +499,16 @@ void MetaObjectBuilderPrivate::parsePythonType(PyTypeObject *type) type = signature.left(spacePos); signature.remove(0, spacePos + 1); } - int index = m_baseObject->indexOfSlot(signature); + const int index = m_baseObject->indexOfSlot(signature); if (index == -1) { - if (type.isEmpty() || type == "void") { + if (type.isEmpty() || type == "void") addSlot(signature); - } else { + else addSlot(signature, type); - } } } } } } - - // Register properties - for (const PropPair &propPair : qAsConst(properties)) - addProperty(propPair.first, propPair.second); } } |