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

This commit is contained in:
Matthias Kuhn 2015-07-11 09:31:51 +02:00
parent 160d1bf7f8
commit c6f7873585
3 changed files with 47 additions and 7 deletions

View File

@ -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)
{

View File

@ -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;

View File

@ -70,7 +70,6 @@ class TestQgsFeature(TestCase):
assert myAttributes == myExpectedAttributes, myMessage
@expectedFailure
def test_SetAttribute(self):
feat = QgsFeature()
feat.initAttributes(1)