From c6f78735856c2314c8b3597ad1af2f2ed57a2a46 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Sat, 11 Jul 2015 09:31:51 +0200 Subject: [PATCH] Add QgsAttributes::operator== that takes care of NULL variants --- python/core/qgsfeature.sip | 8 ++--- src/core/qgsfeature.h | 45 ++++++++++++++++++++++++++++- tests/src/python/test_qgsfeature.py | 1 - 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/python/core/qgsfeature.sip b/python/core/qgsfeature.sip index e4f53eb1c7d..53f8447b983 100644 --- a/python/core/qgsfeature.sip +++ b/python/core/qgsfeature.sip @@ -1,15 +1,13 @@ typedef qint64 QgsFeatureId; - -// key = field index, value = field value typedef QMap QgsAttributeMap; - typedef QVector QgsAttributes; + // QgsAttributes is implemented as a Python list of Python objects. %MappedType QgsAttributes /DocType="list-of-attributes"/ { %TypeHeaderCode -#include +#include %End %ConvertFromTypeCode @@ -53,7 +51,7 @@ typedef QVector QgsAttributes; return 1; } - QVector *qv = new QVector; + QgsAttributes* qv = new QgsAttributes; for (SIP_SSIZE_T i = 0; i < PyList_GET_SIZE(sipPy); ++i) { diff --git a/src/core/qgsfeature.h b/src/core/qgsfeature.h index 7570285bf49..7f341ec53ae 100644 --- a/src/core/qgsfeature.h +++ b/src/core/qgsfeature.h @@ -103,7 +103,50 @@ typedef int QgsFeatureId; // key = field index, value = field value typedef QMap QgsAttributeMap; -typedef QVector QgsAttributes; +/** + * A vector of attributes. Mostly equal to QVector. + */ +class CORE_EXPORT QgsAttributes : public QVector +{ + public: + QgsAttributes() + : QVector() + {} + QgsAttributes( int size ) + : QVector( size ) + {} + QgsAttributes( int size, const QVariant& v ) + : QVector( size, v ) + {} + + QgsAttributes( const QVector& v ) + : QVector( v ) + {} + + /** + * @brief Compares two vectors of attributes. + * They are considered equal if all their members contain the same value and NULL flag. + * This was introduced because the default Qt implementation of QVariant comparison does not + * handle NULL values for certain types (like int). + * + * @param v The attributes to compare + * @return True if v is equal + */ + bool operator==( const QgsAttributes &v ) const + { + if ( size() != v.size() ) + return false; + const QVariant* b = constData(); + const QVariant* i = b + size(); + const QVariant* j = v.constData() + size(); + while ( i != b ) + if ( !( *--i == *--j && i->isNull() == j->isNull() ) ) + return false; + return true; + } + + inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); } +}; class QgsField; diff --git a/tests/src/python/test_qgsfeature.py b/tests/src/python/test_qgsfeature.py index 62ab1dc15a7..cda273bb250 100644 --- a/tests/src/python/test_qgsfeature.py +++ b/tests/src/python/test_qgsfeature.py @@ -70,7 +70,6 @@ class TestQgsFeature(TestCase): assert myAttributes == myExpectedAttributes, myMessage - @expectedFailure def test_SetAttribute(self): feat = QgsFeature() feat.initAttributes(1)