Skip to content
Permalink
Browse files

Add QgsAttributes::operator== that takes care of NULL variants

  • Loading branch information
m-kuhn committed Jul 11, 2015
1 parent 160d1bf commit c6f78735856c2314c8b3597ad1af2f2ed57a2a46
Showing with 47 additions and 7 deletions.
  1. +3 −5 python/core/qgsfeature.sip
  2. +44 −1 src/core/qgsfeature.h
  3. +0 −1 tests/src/python/test_qgsfeature.py
@@ -1,15 +1,13 @@

typedef qint64 QgsFeatureId;

// key = field index, value = field value
typedef QMap<int, QVariant> QgsAttributeMap;

typedef QVector<QVariant> QgsAttributes;

// QgsAttributes is implemented as a Python list of Python objects.
%MappedType QgsAttributes /DocType="list-of-attributes"/
{
%TypeHeaderCode
#include <qvector.h>
#include <qgsfeature.h>
%End

%ConvertFromTypeCode
@@ -53,7 +51,7 @@ typedef QVector<QVariant> QgsAttributes;
return 1;
}

QVector<QVariant> *qv = new QVector<QVariant>;
QgsAttributes* qv = new QgsAttributes;

for (SIP_SSIZE_T i = 0; i < PyList_GET_SIZE(sipPy); ++i)
{
@@ -103,7 +103,50 @@ typedef int QgsFeatureId;
// key = field index, value = field value
typedef QMap<int, QVariant> QgsAttributeMap;

typedef QVector<QVariant> QgsAttributes;
/**
* A vector of attributes. Mostly equal to QVector<QVariant>.
*/
class CORE_EXPORT QgsAttributes : public QVector<QVariant>
{
public:
QgsAttributes()
: QVector<QVariant>()
{}
QgsAttributes( int size )
: QVector<QVariant>( size )
{}
QgsAttributes( int size, const QVariant& v )
: QVector<QVariant>( size, v )
{}

QgsAttributes( const QVector<QVariant>& v )
: QVector<QVariant>( 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;

@@ -70,7 +70,6 @@ def test_Attributes(self):

assert myAttributes == myExpectedAttributes, myMessage

@expectedFailure
def test_SetAttribute(self):
feat = QgsFeature()
feat.initAttributes(1)

0 comments on commit c6f7873

Please sign in to comment.
You can’t perform that action at this time.