Skip to content

Commit e683101

Browse files
committedJul 14, 2016
Make QgsVectorDataProvider::fields() return a copy
Implements a QGIS 3.0 TODO
1 parent 1bafa80 commit e683101

33 files changed

+70
-104
lines changed
 

‎doc/api_break.dox

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ attributeIndexes(), pkAttributeIndexes(), isSaveAndLoadStyleToDBSupported()</li>
3333
</li>
3434
</ul>
3535

36+
\subsection qgis_api_break_3_0_DataProviders Data Providers
37+
38+
<ul>
39+
<li>QgsVectorDataProvider::fields() now returns a copy, rather than a const reference. Since QgsFields
40+
objects are implicitly shared, returning a copy helps simplify and make code more robust. This change
41+
only affects third party c++ providers, and does not affect PyQGIS scripts.</li>
42+
</ul>
43+
3644
\subsection qgis_api_break_3_0_QgsVectorFileWriter QgsVectorFileWriter
3745

3846
<ul>

‎python/core/qgsvectordataprovider.sip

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@ class QgsVectorDataProvider : QgsDataProvider
116116
virtual long featureCount() const = 0;
117117

118118
/**
119-
* Return a map of indexes with field names for this layer
120-
* @return map of fields
121-
* @see QgsFields
119+
* Returns the fields associated with this data provider.
122120
*/
123-
virtual const QgsFields &fields() const = 0;
121+
virtual QgsFields fields() const = 0;
124122

125123
/**
126124
* Return a short comment for the data that this provider is

‎src/analysis/vector/qgszonalstatistics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,13 @@ QString QgsZonalStatistics::getUniqueFieldName( const QString& fieldName )
533533
return fieldName;
534534
}
535535

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

539539
bool found = false;
540540
for ( int idx = 0; idx < providerFields.count(); ++idx )
541541
{
542-
if ( shortName == providerFields[idx].name() )
542+
if ( shortName == providerFields.at( idx ).name() )
543543
{
544544
found = true;
545545
break;
@@ -559,7 +559,7 @@ QString QgsZonalStatistics::getUniqueFieldName( const QString& fieldName )
559559
found = false;
560560
for ( int idx = 0; idx < providerFields.count(); ++idx )
561561
{
562-
if ( shortName == providerFields[idx].name() )
562+
if ( shortName == providerFields.at( idx ).name() )
563563
{
564564
n += 1;
565565
if ( n < 9 )

‎src/core/qgsvectordataprovider.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ QMap<QString, int> QgsVectorDataProvider::fieldNameMap() const
266266
{
267267
QMap<QString, int> resultMap;
268268

269-
const QgsFields& theFields = fields();
269+
QgsFields theFields = fields();
270270
for ( int i = 0; i < theFields.count(); ++i )
271271
{
272-
resultMap.insert( theFields[i].name(), i );
272+
resultMap.insert( theFields.at( i ).name(), i );
273273
}
274274

275275
return resultMap;
@@ -431,20 +431,20 @@ void QgsVectorDataProvider::fillMinMaxCache() const
431431
if ( !mCacheMinMaxDirty )
432432
return;
433433

434-
const QgsFields& flds = fields();
434+
QgsFields flds = fields();
435435
for ( int i = 0; i < flds.count(); ++i )
436436
{
437-
if ( flds[i].type() == QVariant::Int )
437+
if ( flds.at( i ).type() == QVariant::Int )
438438
{
439439
mCacheMinValues[i] = QVariant( INT_MAX );
440440
mCacheMaxValues[i] = QVariant( INT_MIN );
441441
}
442-
else if ( flds[i].type() == QVariant::LongLong )
442+
else if ( flds.at( i ).type() == QVariant::LongLong )
443443
{
444444
mCacheMinValues[i] = QVariant( std::numeric_limits<qlonglong>::max() );
445445
mCacheMaxValues[i] = QVariant( std::numeric_limits<qlonglong>::min() );
446446
}
447-
else if ( flds[i].type() == QVariant::Double )
447+
else if ( flds.at( i ).type() == QVariant::Double )
448448
{
449449
mCacheMinValues[i] = QVariant( DBL_MAX );
450450
mCacheMaxValues[i] = QVariant( -DBL_MAX );
@@ -470,23 +470,23 @@ void QgsVectorDataProvider::fillMinMaxCache() const
470470
if ( varValue.isNull() )
471471
continue;
472472

473-
if ( flds[*it].type() == QVariant::Int )
473+
if ( flds.at( *it ).type() == QVariant::Int )
474474
{
475475
int value = varValue.toInt();
476476
if ( value < mCacheMinValues[*it].toInt() )
477477
mCacheMinValues[*it] = value;
478478
if ( value > mCacheMaxValues[*it].toInt() )
479479
mCacheMaxValues[*it] = value;
480480
}
481-
else if ( flds[*it].type() == QVariant::LongLong )
481+
else if ( flds.at( *it ).type() == QVariant::LongLong )
482482
{
483483
qlonglong value = varValue.toLongLong();
484484
if ( value < mCacheMinValues[*it].toLongLong() )
485485
mCacheMinValues[*it] = value;
486486
if ( value > mCacheMaxValues[*it].toLongLong() )
487487
mCacheMaxValues[*it] = value;
488488
}
489-
else if ( flds[*it].type() == QVariant::Double )
489+
else if ( flds.at( *it ).type() == QVariant::Double )
490490
{
491491
double value = varValue.toDouble();
492492
if ( value < mCacheMinValues[*it].toDouble() )

‎src/core/qgsvectordataprovider.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,9 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
166166
virtual long featureCount() const = 0;
167167

168168
/**
169-
* Return a map of indexes with field names for this layer
170-
* @return map of fields
171-
* @see QgsFields
169+
* Returns the fields associated with this data provider.
172170
*/
173-
// TODO QGIS 3: return by value
174-
virtual const QgsFields &fields() const = 0;
171+
virtual QgsFields fields() const = 0;
175172

176173
/**
177174
* Return a short comment for the data that this provider is

‎src/core/qgsvirtuallayerdefinitionutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ QgsVirtualLayerDefinition QgsVirtualLayerDefinitionUtils::fromJoinedLayer( QgsVe
2727
QStringList columns;
2828

2929
// look for the uid
30-
const QgsFields& fields = layer->dataProvider()->fields();
30+
QgsFields fields = layer->dataProvider()->fields();
3131
{
3232
QgsAttributeList pk = layer->dataProvider()->pkAttributeIndexes();
3333
if ( pk.size() == 1 )

‎src/plugins/evis/eventbrowser/evisgenericeventbrowsergui.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ bool eVisGenericEventBrowserGui::initBrowser()
282282
return false;
283283
}
284284

285-
const QgsFields& myFields = mDataProvider->fields();
285+
QgsFields myFields = mDataProvider->fields();
286286
mIgnoreEvent = true; //Ignore indexChanged event when adding items to combo boxes
287287
for ( int x = 0; x < myFields.count(); x++ )
288288
{
289-
QString name = myFields[x].name();
289+
QString name = myFields.at( x ).name();
290290
cboxEventImagePathField->addItem( name );
291291
cboxCompassBearingField->addItem( name );
292292
cboxCompassOffsetField->addItem( name );
@@ -594,13 +594,13 @@ void eVisGenericEventBrowserGui::loadRecord()
594594
QString myCompassBearingField = cboxCompassBearingField->currentText();
595595
QString myCompassOffsetField = cboxCompassOffsetField->currentText();
596596
QString myEventImagePathField = cboxEventImagePathField->currentText();
597-
const QgsFields& myFields = mDataProvider->fields();
597+
QgsFields myFields = mDataProvider->fields();
598598
QgsAttributes myAttrs = myFeature->attributes();
599599
//loop through the attributes and display their contents
600600
for ( int i = 0; i < myAttrs.count(); ++i )
601601
{
602602
QStringList myValues;
603-
QString fieldName = myFields[i].name();
603+
QString fieldName = myFields.at( i ).name();
604604
myValues << fieldName << myAttrs.at( i ).toString();
605605
QTreeWidgetItem* myItem = new QTreeWidgetItem( myValues );
606606
if ( fieldName == myEventImagePathField )
@@ -836,7 +836,7 @@ void eVisGenericEventBrowserGui::on_cboxEventImagePathField_currentIndexChanged(
836836
{
837837
mConfiguration.setEventImagePathField( cboxEventImagePathField->currentText() );
838838

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

842842
if ( !myFeature )
@@ -864,7 +864,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassBearingField_currentIndexChanged(
864864
{
865865
mConfiguration.setCompassBearingField( cboxCompassBearingField->currentText() );
866866

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

870870
if ( !myFeature )
@@ -873,7 +873,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassBearingField_currentIndexChanged(
873873
QgsAttributes myAttrs = myFeature->attributes();
874874
for ( int i = 0; i < myAttrs.count(); ++i )
875875
{
876-
if ( myFields[i].name() == cboxCompassBearingField->currentText() )
876+
if ( myFields.at( i ).name() == cboxCompassBearingField->currentText() )
877877
{
878878
mCompassBearing = myAttrs.at( i ).toDouble();
879879
}
@@ -892,7 +892,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassOffsetField_currentIndexChanged(
892892
{
893893
mConfiguration.setCompassOffsetField( cboxCompassOffsetField->currentText() );
894894

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

898898
if ( !myFeature )
@@ -901,7 +901,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassOffsetField_currentIndexChanged(
901901
QgsAttributes myAttrs = myFeature->attributes();
902902
for ( int i = 0; i < myAttrs.count(); ++i )
903903
{
904-
if ( myFields[i].name() == cboxCompassOffsetField->currentText() )
904+
if ( myFields.at( i ).name() == cboxCompassOffsetField->currentText() )
905905
{
906906
mCompassOffset = myAttrs.at( i ).toDouble();
907907
}

‎src/providers/arcgisrest/qgsafsprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class QgsAfsProvider : public QgsVectorDataProvider
4343
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const override;
4444
QGis::WkbType geometryType() const override { return static_cast<QGis::WkbType>( mGeometryType ); }
4545
long featureCount() const override { return mObjectIds.size(); }
46-
const QgsFields &fields() const override { return mFields; }
46+
QgsFields fields() const override { return mFields; }
4747
/* Read only for the moment
4848
bool addFeatures( QgsFeatureList &flist ) override{ return false; }
4949
bool deleteFeatures( const QgsFeatureIds &id ) override{ return false; }

‎src/providers/db2/qgsdb2provider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ long QgsDb2Provider::featureCount() const
494494
}
495495
}
496496

497-
const QgsFields &QgsDb2Provider::fields() const
497+
QgsFields QgsDb2Provider::fields() const
498498
{
499499
return mAttributeFields;
500500
}

‎src/providers/db2/qgsdb2provider.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,8 @@ class QgsDb2Provider : public QgsVectorDataProvider
7070
*/
7171
void updateStatistics() const;
7272

73-
/**
74-
* Return a map of indexes with field names for this layer.
75-
* @return map of fields
76-
*/
77-
virtual const QgsFields &fields() const override;
73+
74+
virtual QgsFields fields() const override;
7875

7976
virtual QgsCoordinateReferenceSystem crs() const override;
8077
virtual QgsRectangle extent() const override;

‎src/providers/delimitedtext/qgsdelimitedtextprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ long QgsDelimitedTextProvider::featureCount() const
11351135
}
11361136

11371137

1138-
const QgsFields & QgsDelimitedTextProvider::fields() const
1138+
QgsFields QgsDelimitedTextProvider::fields() const
11391139
{
11401140
return attributeFields;
11411141
}

‎src/providers/delimitedtext/qgsdelimitedtextprovider.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,7 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
101101
*/
102102
virtual long featureCount() const override;
103103

104-
/**
105-
* Return a map of indexes with field names for this layer
106-
* @return map of fields
107-
*/
108-
virtual const QgsFields & fields() const override;
104+
virtual QgsFields fields() const override;
109105

110106
/** Returns a bitmask containing the supported capabilities
111107
* Note, some capabilities may change depending on whether

‎src/providers/gpx/qgsgpxprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ long QgsGPXProvider::featureCount() const
171171
}
172172

173173

174-
const QgsFields& QgsGPXProvider::fields() const
174+
QgsFields QgsGPXProvider::fields() const
175175
{
176176
return attributeFields;
177177
}

‎src/providers/gpx/qgsgpxprovider.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ class QgsGPXProvider : public QgsVectorDataProvider
6969
*/
7070
virtual long featureCount() const override;
7171

72-
/**
73-
* Get the field information for the layer
74-
*/
75-
virtual const QgsFields& fields() const override;
72+
virtual QgsFields fields() const override;
7673

7774
/**
7875
* Adds a list of features

‎src/providers/grass/qgsgrassprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ long QgsGrassProvider::featureCount() const
453453
return mNumberFeatures;
454454
}
455455

456-
const QgsFields & QgsGrassProvider::fields() const
456+
QgsFields QgsGrassProvider::fields() const
457457
{
458458
if ( isTopoType() )
459459
{

‎src/providers/grass/qgsgrassprovider.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ class GRASS_LIB_EXPORT QgsGrassProvider : public QgsVectorDataProvider
8888

8989
virtual QgsRectangle extent() const override;
9090

91-
/**
92-
* Get the field information for the layer
93-
*/
94-
const QgsFields & fields() const override;
91+
QgsFields fields() const override;
9592

9693
// ! Key (category) field index
9794
int keyField();

‎src/providers/memory/qgsmemoryprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ long QgsMemoryProvider::featureCount() const
309309
return count;
310310
}
311311

312-
const QgsFields & QgsMemoryProvider::fields() const
312+
QgsFields QgsMemoryProvider::fields() const
313313
{
314314
return mFields;
315315
}

‎src/providers/memory/qgsmemoryprovider.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,7 @@ class QgsMemoryProvider : public QgsVectorDataProvider
6161
*/
6262
virtual long featureCount() const override;
6363

64-
/**
65-
* Return a map of indexes with field names for this layer
66-
* @return map of fields
67-
*/
68-
virtual const QgsFields & fields() const override;
69-
64+
virtual QgsFields fields() const override;
7065

7166
/**
7267
* Adds a list of features

‎src/providers/mssql/qgsmssqlprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ long QgsMssqlProvider::featureCount() const
778778
}
779779
}
780780

781-
const QgsFields & QgsMssqlProvider::fields() const
781+
QgsFields QgsMssqlProvider::fields() const
782782
{
783783
return mAttributeFields;
784784
}

‎src/providers/mssql/qgsmssqlprovider.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ class QgsMssqlProvider : public QgsVectorDataProvider
9393
/** Update the extent, feature count, wkb type and srid for this layer */
9494
void UpdateStatistics( bool estimate ) const;
9595

96-
/**
97-
* Return a map of indexes with field names for this layer
98-
* @return map of fields
99-
*/
100-
virtual const QgsFields & fields() const override;
96+
virtual QgsFields fields() const override;
10197

10298
QString subsetString() const override;
10399

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ long QgsOgrProvider::featureCount() const
10351035
}
10361036

10371037

1038-
const QgsFields & QgsOgrProvider::fields() const
1038+
QgsFields QgsOgrProvider::fields() const
10391039
{
10401040
return mAttributeFields;
10411041
}
@@ -1666,7 +1666,7 @@ bool QgsOgrProvider::createAttributeIndex( int field )
16661666
QByteArray quotedLayerName = quotedIdentifier( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrOrigLayer ) ) );
16671667
QByteArray dropSql = "DROP INDEX ON " + quotedLayerName;
16681668
OGR_DS_ExecuteSQL( ogrDataSource, dropSql.constData(), OGR_L_GetSpatialFilter( ogrOrigLayer ), nullptr );
1669-
QByteArray createSql = "CREATE INDEX ON " + quotedLayerName + " USING " + mEncoding->fromUnicode( fields()[field].name() );
1669+
QByteArray createSql = "CREATE INDEX ON " + quotedLayerName + " USING " + mEncoding->fromUnicode( fields().at( field ).name() );
16701670
OGR_DS_ExecuteSQL( ogrDataSource, createSql.constData(), OGR_L_GetSpatialFilter( ogrOrigLayer ), nullptr );
16711671

16721672
QFileInfo fi( mFilePath ); // to get the base name

‎src/providers/ogr/qgsogrprovider.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ class QgsOgrProvider : public QgsVectorDataProvider
117117
*/
118118
virtual long featureCount() const override;
119119

120-
/**
121-
* Get the field information for the layer
122-
*/
123-
virtual const QgsFields & fields() const override;
120+
virtual QgsFields fields() const override;
124121

125122
virtual QgsRectangle extent() const override;
126123

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ const QgsField &QgsPostgresProvider::field( int index ) const
703703
return mAttributeFields[index];
704704
}
705705

706-
const QgsFields & QgsPostgresProvider::fields() const
706+
QgsFields QgsPostgresProvider::fields() const
707707
{
708708
return mAttributeFields;
709709
}

0 commit comments

Comments
 (0)
Please sign in to comment.