Skip to content

Commit

Permalink
Make QgsVectorDataProvider::fields() return a copy
Browse files Browse the repository at this point in the history
Implements a QGIS 3.0 TODO
  • Loading branch information
nyalldawson committed Jul 14, 2016
1 parent 1bafa80 commit e683101
Show file tree
Hide file tree
Showing 33 changed files with 70 additions and 104 deletions.
8 changes: 8 additions & 0 deletions doc/api_break.dox
Expand Up @@ -33,6 +33,14 @@ attributeIndexes(), pkAttributeIndexes(), isSaveAndLoadStyleToDBSupported()</li>
</li>
</ul>

\subsection qgis_api_break_3_0_DataProviders Data Providers

<ul>
<li>QgsVectorDataProvider::fields() now returns a copy, rather than a const reference. Since QgsFields
objects are implicitly shared, returning a copy helps simplify and make code more robust. This change
only affects third party c++ providers, and does not affect PyQGIS scripts.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorFileWriter QgsVectorFileWriter

<ul>
Expand Down
6 changes: 2 additions & 4 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -116,11 +116,9 @@ class QgsVectorDataProvider : QgsDataProvider
virtual long featureCount() const = 0;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
* @see QgsFields
* Returns the fields associated with this data provider.
*/
virtual const QgsFields &fields() const = 0;
virtual QgsFields fields() const = 0;

/**
* Return a short comment for the data that this provider is
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/vector/qgszonalstatistics.cpp
Expand Up @@ -533,13 +533,13 @@ QString QgsZonalStatistics::getUniqueFieldName( const QString& fieldName )
return fieldName;
}

const QgsFields& providerFields = dp->fields();
QgsFields providerFields = dp->fields();
QString shortName = fieldName.mid( 0, 10 );

bool found = false;
for ( int idx = 0; idx < providerFields.count(); ++idx )
{
if ( shortName == providerFields[idx].name() )
if ( shortName == providerFields.at( idx ).name() )
{
found = true;
break;
Expand All @@ -559,7 +559,7 @@ QString QgsZonalStatistics::getUniqueFieldName( const QString& fieldName )
found = false;
for ( int idx = 0; idx < providerFields.count(); ++idx )
{
if ( shortName == providerFields[idx].name() )
if ( shortName == providerFields.at( idx ).name() )
{
n += 1;
if ( n < 9 )
Expand Down
18 changes: 9 additions & 9 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -266,10 +266,10 @@ QMap<QString, int> QgsVectorDataProvider::fieldNameMap() const
{
QMap<QString, int> resultMap;

const QgsFields& theFields = fields();
QgsFields theFields = fields();
for ( int i = 0; i < theFields.count(); ++i )
{
resultMap.insert( theFields[i].name(), i );
resultMap.insert( theFields.at( i ).name(), i );
}

return resultMap;
Expand Down Expand Up @@ -431,20 +431,20 @@ void QgsVectorDataProvider::fillMinMaxCache() const
if ( !mCacheMinMaxDirty )
return;

const QgsFields& flds = fields();
QgsFields flds = fields();
for ( int i = 0; i < flds.count(); ++i )
{
if ( flds[i].type() == QVariant::Int )
if ( flds.at( i ).type() == QVariant::Int )
{
mCacheMinValues[i] = QVariant( INT_MAX );
mCacheMaxValues[i] = QVariant( INT_MIN );
}
else if ( flds[i].type() == QVariant::LongLong )
else if ( flds.at( i ).type() == QVariant::LongLong )
{
mCacheMinValues[i] = QVariant( std::numeric_limits<qlonglong>::max() );
mCacheMaxValues[i] = QVariant( std::numeric_limits<qlonglong>::min() );
}
else if ( flds[i].type() == QVariant::Double )
else if ( flds.at( i ).type() == QVariant::Double )
{
mCacheMinValues[i] = QVariant( DBL_MAX );
mCacheMaxValues[i] = QVariant( -DBL_MAX );
Expand All @@ -470,23 +470,23 @@ void QgsVectorDataProvider::fillMinMaxCache() const
if ( varValue.isNull() )
continue;

if ( flds[*it].type() == QVariant::Int )
if ( flds.at( *it ).type() == QVariant::Int )
{
int value = varValue.toInt();
if ( value < mCacheMinValues[*it].toInt() )
mCacheMinValues[*it] = value;
if ( value > mCacheMaxValues[*it].toInt() )
mCacheMaxValues[*it] = value;
}
else if ( flds[*it].type() == QVariant::LongLong )
else if ( flds.at( *it ).type() == QVariant::LongLong )
{
qlonglong value = varValue.toLongLong();
if ( value < mCacheMinValues[*it].toLongLong() )
mCacheMinValues[*it] = value;
if ( value > mCacheMaxValues[*it].toLongLong() )
mCacheMaxValues[*it] = value;
}
else if ( flds[*it].type() == QVariant::Double )
else if ( flds.at( *it ).type() == QVariant::Double )
{
double value = varValue.toDouble();
if ( value < mCacheMinValues[*it].toDouble() )
Expand Down
7 changes: 2 additions & 5 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -166,12 +166,9 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
virtual long featureCount() const = 0;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
* @see QgsFields
* Returns the fields associated with this data provider.
*/
// TODO QGIS 3: return by value
virtual const QgsFields &fields() const = 0;
virtual QgsFields fields() const = 0;

/**
* Return a short comment for the data that this provider is
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvirtuallayerdefinitionutils.cpp
Expand Up @@ -27,7 +27,7 @@ QgsVirtualLayerDefinition QgsVirtualLayerDefinitionUtils::fromJoinedLayer( QgsVe
QStringList columns;

// look for the uid
const QgsFields& fields = layer->dataProvider()->fields();
QgsFields fields = layer->dataProvider()->fields();
{
QgsAttributeList pk = layer->dataProvider()->pkAttributeIndexes();
if ( pk.size() == 1 )
Expand Down
18 changes: 9 additions & 9 deletions src/plugins/evis/eventbrowser/evisgenericeventbrowsergui.cpp
Expand Up @@ -282,11 +282,11 @@ bool eVisGenericEventBrowserGui::initBrowser()
return false;
}

const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
mIgnoreEvent = true; //Ignore indexChanged event when adding items to combo boxes
for ( int x = 0; x < myFields.count(); x++ )
{
QString name = myFields[x].name();
QString name = myFields.at( x ).name();
cboxEventImagePathField->addItem( name );
cboxCompassBearingField->addItem( name );
cboxCompassOffsetField->addItem( name );
Expand Down Expand Up @@ -594,13 +594,13 @@ void eVisGenericEventBrowserGui::loadRecord()
QString myCompassBearingField = cboxCompassBearingField->currentText();
QString myCompassOffsetField = cboxCompassOffsetField->currentText();
QString myEventImagePathField = cboxEventImagePathField->currentText();
const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
QgsAttributes myAttrs = myFeature->attributes();
//loop through the attributes and display their contents
for ( int i = 0; i < myAttrs.count(); ++i )
{
QStringList myValues;
QString fieldName = myFields[i].name();
QString fieldName = myFields.at( i ).name();
myValues << fieldName << myAttrs.at( i ).toString();
QTreeWidgetItem* myItem = new QTreeWidgetItem( myValues );
if ( fieldName == myEventImagePathField )
Expand Down Expand Up @@ -836,7 +836,7 @@ void eVisGenericEventBrowserGui::on_cboxEventImagePathField_currentIndexChanged(
{
mConfiguration.setEventImagePathField( cboxEventImagePathField->currentText() );

const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
QgsFeature* myFeature = featureAtId( mFeatureIds.at( mCurrentFeatureIndex ) );

if ( !myFeature )
Expand Down Expand Up @@ -864,7 +864,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassBearingField_currentIndexChanged(
{
mConfiguration.setCompassBearingField( cboxCompassBearingField->currentText() );

const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
QgsFeature* myFeature = featureAtId( mFeatureIds.at( mCurrentFeatureIndex ) );

if ( !myFeature )
Expand All @@ -873,7 +873,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassBearingField_currentIndexChanged(
QgsAttributes myAttrs = myFeature->attributes();
for ( int i = 0; i < myAttrs.count(); ++i )
{
if ( myFields[i].name() == cboxCompassBearingField->currentText() )
if ( myFields.at( i ).name() == cboxCompassBearingField->currentText() )
{
mCompassBearing = myAttrs.at( i ).toDouble();
}
Expand All @@ -892,7 +892,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassOffsetField_currentIndexChanged(
{
mConfiguration.setCompassOffsetField( cboxCompassOffsetField->currentText() );

const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
QgsFeature* myFeature = featureAtId( mFeatureIds.at( mCurrentFeatureIndex ) );

if ( !myFeature )
Expand All @@ -901,7 +901,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassOffsetField_currentIndexChanged(
QgsAttributes myAttrs = myFeature->attributes();
for ( int i = 0; i < myAttrs.count(); ++i )
{
if ( myFields[i].name() == cboxCompassOffsetField->currentText() )
if ( myFields.at( i ).name() == cboxCompassOffsetField->currentText() )
{
mCompassOffset = myAttrs.at( i ).toDouble();
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/arcgisrest/qgsafsprovider.h
Expand Up @@ -43,7 +43,7 @@ class QgsAfsProvider : public QgsVectorDataProvider
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const override;
QGis::WkbType geometryType() const override { return static_cast<QGis::WkbType>( mGeometryType ); }
long featureCount() const override { return mObjectIds.size(); }
const QgsFields &fields() const override { return mFields; }
QgsFields fields() const override { return mFields; }
/* Read only for the moment
bool addFeatures( QgsFeatureList &flist ) override{ return false; }
bool deleteFeatures( const QgsFeatureIds &id ) override{ return false; }
Expand Down
2 changes: 1 addition & 1 deletion src/providers/db2/qgsdb2provider.cpp
Expand Up @@ -494,7 +494,7 @@ long QgsDb2Provider::featureCount() const
}
}

const QgsFields &QgsDb2Provider::fields() const
QgsFields QgsDb2Provider::fields() const
{
return mAttributeFields;
}
Expand Down
7 changes: 2 additions & 5 deletions src/providers/db2/qgsdb2provider.h
Expand Up @@ -70,11 +70,8 @@ class QgsDb2Provider : public QgsVectorDataProvider
*/
void updateStatistics() const;

/**
* Return a map of indexes with field names for this layer.
* @return map of fields
*/
virtual const QgsFields &fields() const override;

virtual QgsFields fields() const override;

virtual QgsCoordinateReferenceSystem crs() const override;
virtual QgsRectangle extent() const override;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Expand Up @@ -1135,7 +1135,7 @@ long QgsDelimitedTextProvider::featureCount() const
}


const QgsFields & QgsDelimitedTextProvider::fields() const
QgsFields QgsDelimitedTextProvider::fields() const
{
return attributeFields;
}
Expand Down
6 changes: 1 addition & 5 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.h
Expand Up @@ -101,11 +101,7 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
*/
virtual long featureCount() const override;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
*/
virtual const QgsFields & fields() const override;
virtual QgsFields fields() const override;

/** Returns a bitmask containing the supported capabilities
* Note, some capabilities may change depending on whether
Expand Down
2 changes: 1 addition & 1 deletion src/providers/gpx/qgsgpxprovider.cpp
Expand Up @@ -171,7 +171,7 @@ long QgsGPXProvider::featureCount() const
}


const QgsFields& QgsGPXProvider::fields() const
QgsFields QgsGPXProvider::fields() const
{
return attributeFields;
}
Expand Down
5 changes: 1 addition & 4 deletions src/providers/gpx/qgsgpxprovider.h
Expand Up @@ -69,10 +69,7 @@ class QgsGPXProvider : public QgsVectorDataProvider
*/
virtual long featureCount() const override;

/**
* Get the field information for the layer
*/
virtual const QgsFields& fields() const override;
virtual QgsFields fields() const override;

/**
* Adds a list of features
Expand Down
2 changes: 1 addition & 1 deletion src/providers/grass/qgsgrassprovider.cpp
Expand Up @@ -453,7 +453,7 @@ long QgsGrassProvider::featureCount() const
return mNumberFeatures;
}

const QgsFields & QgsGrassProvider::fields() const
QgsFields QgsGrassProvider::fields() const
{
if ( isTopoType() )
{
Expand Down
5 changes: 1 addition & 4 deletions src/providers/grass/qgsgrassprovider.h
Expand Up @@ -88,10 +88,7 @@ class GRASS_LIB_EXPORT QgsGrassProvider : public QgsVectorDataProvider

virtual QgsRectangle extent() const override;

/**
* Get the field information for the layer
*/
const QgsFields & fields() const override;
QgsFields fields() const override;

// ! Key (category) field index
int keyField();
Expand Down
2 changes: 1 addition & 1 deletion src/providers/memory/qgsmemoryprovider.cpp
Expand Up @@ -309,7 +309,7 @@ long QgsMemoryProvider::featureCount() const
return count;
}

const QgsFields & QgsMemoryProvider::fields() const
QgsFields QgsMemoryProvider::fields() const
{
return mFields;
}
Expand Down
7 changes: 1 addition & 6 deletions src/providers/memory/qgsmemoryprovider.h
Expand Up @@ -61,12 +61,7 @@ class QgsMemoryProvider : public QgsVectorDataProvider
*/
virtual long featureCount() const override;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
*/
virtual const QgsFields & fields() const override;

virtual QgsFields fields() const override;

/**
* Adds a list of features
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mssql/qgsmssqlprovider.cpp
Expand Up @@ -778,7 +778,7 @@ long QgsMssqlProvider::featureCount() const
}
}

const QgsFields & QgsMssqlProvider::fields() const
QgsFields QgsMssqlProvider::fields() const
{
return mAttributeFields;
}
Expand Down
6 changes: 1 addition & 5 deletions src/providers/mssql/qgsmssqlprovider.h
Expand Up @@ -93,11 +93,7 @@ class QgsMssqlProvider : public QgsVectorDataProvider
/** Update the extent, feature count, wkb type and srid for this layer */
void UpdateStatistics( bool estimate ) const;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
*/
virtual const QgsFields & fields() const override;
virtual QgsFields fields() const override;

QString subsetString() const override;

Expand Down
4 changes: 2 additions & 2 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -1035,7 +1035,7 @@ long QgsOgrProvider::featureCount() const
}


const QgsFields & QgsOgrProvider::fields() const
QgsFields QgsOgrProvider::fields() const
{
return mAttributeFields;
}
Expand Down Expand Up @@ -1666,7 +1666,7 @@ bool QgsOgrProvider::createAttributeIndex( int field )
QByteArray quotedLayerName = quotedIdentifier( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrOrigLayer ) ) );
QByteArray dropSql = "DROP INDEX ON " + quotedLayerName;
OGR_DS_ExecuteSQL( ogrDataSource, dropSql.constData(), OGR_L_GetSpatialFilter( ogrOrigLayer ), nullptr );
QByteArray createSql = "CREATE INDEX ON " + quotedLayerName + " USING " + mEncoding->fromUnicode( fields()[field].name() );
QByteArray createSql = "CREATE INDEX ON " + quotedLayerName + " USING " + mEncoding->fromUnicode( fields().at( field ).name() );
OGR_DS_ExecuteSQL( ogrDataSource, createSql.constData(), OGR_L_GetSpatialFilter( ogrOrigLayer ), nullptr );

QFileInfo fi( mFilePath ); // to get the base name
Expand Down
5 changes: 1 addition & 4 deletions src/providers/ogr/qgsogrprovider.h
Expand Up @@ -117,10 +117,7 @@ class QgsOgrProvider : public QgsVectorDataProvider
*/
virtual long featureCount() const override;

/**
* Get the field information for the layer
*/
virtual const QgsFields & fields() const override;
virtual QgsFields fields() const override;

virtual QgsRectangle extent() const override;

Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -703,7 +703,7 @@ const QgsField &QgsPostgresProvider::field( int index ) const
return mAttributeFields[index];
}

const QgsFields & QgsPostgresProvider::fields() const
QgsFields QgsPostgresProvider::fields() const
{
return mAttributeFields;
}
Expand Down

0 comments on commit e683101

Please sign in to comment.