Skip to content

Commit 267d952

Browse files
committed
Implicit sharing for QgsFeature
1 parent c33b9fa commit 267d952

File tree

5 files changed

+423
-276
lines changed

5 files changed

+423
-276
lines changed

python/core/qgsfeature.sip

+114-74
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ typedef QMap<int, QgsField> QgsFieldMap;
104104
/** \ingroup core
105105
* The feature class encapsulates a single feature including its id,
106106
* geometry and a list of field/values attributes.
107-
*
107+
* \note QgsFeature objects are implicitly shared.
108108
* @author Gary E.Sherman
109109
*/
110110
class QgsFeature
@@ -218,42 +218,57 @@ class QgsFeature
218218
sipCpp->deleteAttribute(fieldIdx);
219219
%End
220220

221-
//! Constructor
221+
/** Constructor for QgsFeature
222+
* @param id feature id
223+
*/
222224
QgsFeature( qint64 id = 0 );
223225

226+
/** Constructor for QgsFeature
227+
* @param fields feature's fields
228+
* @param id feature id
229+
*/
224230
QgsFeature( const QgsFields& fields, qint64 id = 0 );
225231

226-
/** copy ctor needed due to internal pointer */
232+
/** Copy constructor
233+
*/
227234
QgsFeature( const QgsFeature & rhs );
228235

229236
//! Destructor
230237
~QgsFeature();
231238

232-
/**
233-
* Get the feature id for this feature
234-
* @return Feature id
239+
/** Get the feature ID for this feature.
240+
* @returns feature ID
241+
* @see setFeatureId
235242
*/
236243
qint64 id() const;
237244

238-
/**
239-
* Set the feature id for this feature
240-
* @param id Feature id
245+
/** Sets the feature ID for this feature.
246+
* @param id feature id
247+
* @see id
241248
*/
242249
void setFeatureId( qint64 id );
243250

251+
/** Returns the feature's attributes.
252+
* @link attributes @endlink method.
253+
* @returns list of feature's attributes
254+
* @see setAttributes
255+
* @note added in QGIS 2.9
256+
*/
244257
QgsAttributes attributes() const;
245258

259+
/** Sets the feature's attributes.
260+
* @param attrs attribute list
261+
* @see setAttribute
262+
* @see attributes
263+
*/
246264
void setAttributes( const QgsAttributes& attrs );
247265

248-
/**
249-
* Set an attribute by id
250-
*
251-
* @param field The index of the field to set
252-
* @param attr The value of the attribute
253-
*
254-
* @return false, if the field id does not exist
255-
*
266+
/** Set an attribute's value by field index.
267+
* @param field the index of the field to set
268+
* @param attr the value of the attribute
269+
* @return false, if the field index does not exist
256270
* @note For Python: raises a KeyError exception instead of returning false
271+
* @see setAttributes
257272
*/
258273
bool setAttribute( int field, const QVariant& attr /GetWrapper/);
259274
%MethodCode
@@ -275,17 +290,14 @@ class QgsFeature
275290
}
276291
%End
277292

278-
/**
279-
* Initialize this feature with the given number of fields. Discard any previously set attribute data.
293+
/** Initialize this feature with the given number of fields. Discard any previously set attribute data.
280294
* @param fieldCount Number of fields to initialize
281295
*/
282296
void initAttributes( int fieldCount );
283297

284-
/**
285-
* Deletes an attribute and its value
286-
*
287-
* @param field The index of the field
288-
*
298+
/** Deletes an attribute and its value.
299+
* @param field the index of the field
300+
* @see setAttribute
289301
* @note For Python: raises a KeyError exception if the field is not found
290302
*/
291303
void deleteAttribute( int field );
@@ -298,92 +310,123 @@ class QgsFeature
298310
sipIsErr = 1;
299311
}
300312
%End
301-
/**
302-
* Return the validity of this feature. This is normally set by
313+
/** Returns the validity of this feature. This is normally set by
303314
* the provider to indicate some problem that makes the feature
304315
* invalid or to indicate a null feature.
316+
* @see setValid
305317
*/
306318
bool isValid() const;
307319

308-
/**
309-
* Set the validity of the feature.
320+
/** Sets the validity of the feature.
321+
* @param validity set to true if feature is valid
322+
* @see isValid
310323
*/
311324
void setValid( bool validity );
312325

313-
/**
314-
* Get the geometry object associated with this feature
326+
/** Get the geometry object associated with this feature. If the geometry
327+
* is not going to be modified than calling the const @link constGeometry @endlink
328+
* method is preferable as it avoids a potentially expensive detach operation.
315329
*
316330
* It is possible to modify the geometry in place but this will
317331
* be removed in 3.0 and therefore @link setGeometry @endlink should be called explicitly.
318332
*
319333
* @note will be modified to return by value in QGIS 3.0: `QgsGeometry geometry() const;`
334+
*
335+
* @returns pointer to feature's geometry
336+
* @see constGeometry
337+
* @see geometryAndOwnership
338+
* @see setGeometry
320339
*/
321340
QgsGeometry* geometry();
322341

323-
/** Gets a const pointer to the geometry object associated with this feature.
342+
/** Gets a const pointer to the geometry object associated with this feature. If the geometry
343+
* is not going to be modified than this method is preferable to the non-const
344+
* @link geometry @endlink method.
324345
* @note this is a temporary method for 2.x release cycle. Will be removed in QGIS 3.0.
346+
* @returns const pointer to feature's geometry
347+
* @see geometry
348+
* @see geometryAndOwnership
349+
* @see setGeometry
325350
* @note added in QGIS 2.9
326351
* @note will be removed in QGIS 3.0
327352
*/
328353
const QgsGeometry* constGeometry() const;
329354

330-
/**
331-
* Get the geometry object associated with this feature
332-
* The caller assumes responsibility for the QgsGeometry*'s destruction.
333-
* @deprecated will be removed in QGIS 3.0
355+
/** Get the geometry object associated with this feature, and transfer ownership of the
356+
* geometry to the caller. The caller assumes responsibility for the QgsGeometry*'s destruction.
357+
* @returns pointer to feature's geometry
358+
* @see geometry
359+
* @see setGeometry
334360
*/
335361
QgsGeometry *geometryAndOwnership() /Factory,Deprecated/;
336362

337-
/** Set this feature's geometry from another QgsGeometry object (deep copy)
363+
/** Set this feature's geometry from another QgsGeometry object. This method performs a deep copy
364+
* of the geometry.
365+
* @param geom new feature geometry
366+
* @see geometry
367+
* @see constGeometry
368+
* @see geometryAndOwnership
369+
* @see setGeometryAndOwnership
338370
*/
339371
void setGeometry( const QgsGeometry& geom );
340372

341-
/** Set this feature's geometry (takes geometry ownership)
342-
*
373+
/** Set this feature's geometry from a QgsGeometry pointer. Ownership of the geometry is transferred
374+
* to the feature.
375+
* @param geom new feature geometry
343376
* @note not available in python bindings
377+
* @see geometry
378+
* @see constGeometry
379+
* @see geometryAndOwnership
380+
* @see setGeometryAndOwnership
344381
* @deprecated will be removed in QGIS 3.0
345382
*/
346383
// void setGeometry( QgsGeometry* geom /Transfer/ );
347384

348-
/**
349-
* Set this feature's geometry from WKB
350-
*
351-
* This feature assumes responsibility for destroying geom.
352-
*
385+
/** Set this feature's geometry from WKB. This feature assumes responsibility for destroying the
386+
* created geometry.
387+
* @param geom geometry as WKB
388+
* @param length size of WKB
389+
* @see setGeometry
390+
* @see geometry
391+
* @see constGeometry
392+
* @see geometryAndOwnership
353393
* @deprecated will be removed in QGIS 3.0
354394
*/
355395
void setGeometryAndOwnership( unsigned char * geom /Transfer/, size_t length );
356396

357-
/** Assign a field map with the feature to allow attribute access by attribute name
358-
*
359-
* @param fields The attribute fields which this feature holds. When used from python, make sure
360-
* a copy of the fields is held by python, as ownership stays there.
361-
* I.e. Do not call feature.setFields( myDataProvider.fields() ) but instead call
362-
* myFields = myDataProvider.fields()
363-
* feature.setFields( myFields )
397+
/** Assign a field map with the feature to allow attribute access by attribute name.
398+
* @param fields The attribute fields which this feature holds
364399
* @param initAttributes If true, attributes are initialized. Clears any data previously assigned.
365400
* C++: Defaults to false
366401
* Python: Defaults to true
367-
*
368-
* TODO: QGIS3 - take reference, not pointer
402+
* @deprecated use setFields( const QgsFields& fields, bool initAttributes = false ) instead
403+
* @note not available in Python bindings
369404
*/
370-
void setFields( const QgsFields* fields, bool initAttributes = true );
405+
//void setFields( const QgsFields* fields, bool initAttributes = true );
371406

372-
/** Get associated field map.
373-
*
374-
* TODO: QGIS 3 - return reference or value, not pointer
407+
/** Assign a field map with the feature to allow attribute access by attribute name.
408+
* @param fields The attribute fields which this feature holds
409+
* @param initAttributes If true, attributes are initialized. Clears any data previously assigned.
410+
* C++: Defaults to false
411+
* Python: Defaults to true
412+
* @note added in QGIS 2.9
413+
* @see fields
414+
*/
415+
void setFields( const QgsFields& fields, bool initAttributes = true );
416+
417+
/** Returns the field map associated with the feature.
418+
* @see setFields
419+
* TODO: QGIS 3 - return value, not pointer
375420
*/
376421
const QgsFields* fields() const;
377422

378423
/** Insert a value into attribute. Returns false if attribute name could not be converted to index.
379-
* Field map must be associated to make this work.
380-
*
424+
* Field map must be associated using @link setFields @endlink before this method can be used.
381425
* @param name The name of the field to set
382426
* @param value The value to set
383-
*
384427
* @return false if attribute name could not be converted to index (C++ only)
385-
*
386428
* @note For Python: raises a KeyError exception instead of returning false
429+
* @see setFields
387430
*/
388431
void setAttribute( const QString& name, QVariant value /GetWrapper/ );
389432
%MethodCode
@@ -405,15 +448,12 @@ class QgsFeature
405448
}
406449
}
407450
%End
408-
/** Remove an attribute value.
409-
* Returns false if attribute name could not be converted to index.
410-
* Field map must be associated to make this work.
411-
*
451+
/** Removes an attribute value by field name. Field map must be associated using @link setFields @endlink
452+
* before this method can be used.
412453
* @param name The name of the field to delete
413-
*
414454
* @return false if attribute name could not be converted to index (C++ only)
415-
*
416455
* @note For Python: raises a KeyError exception instead of returning false
456+
* @see setFields
417457
*/
418458
bool deleteAttribute( const QString& name );
419459
%MethodCode
@@ -428,15 +468,12 @@ class QgsFeature
428468
sipCpp->deleteAttribute( fieldIdx );
429469
}
430470
%End
431-
/** Lookup attribute value from attribute name.
432-
* Returns invalid variant if attribute name could not be converted to index (C++ only)
433-
* Field map must be associated to make this work.
434-
*
471+
/** Lookup attribute value from attribute name. Field map must be associated using @link setFields @endlink
472+
* before this method can be used.
435473
* @param name The name of the attribute to get
436-
*
437474
* @return The value of the attribute (C++: Invalid variant if no such name exists )
438-
*
439475
* @note For Python: raises a KeyError exception if field is not found
476+
* @see setFields
440477
*/
441478
SIP_PYOBJECT attribute( const QString& name ) const;
442479
%MethodCode
@@ -452,8 +489,11 @@ class QgsFeature
452489
sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
453490
}
454491
%End
455-
/** Utility method to get attribute index from name. Returns -1 if field does not exist or field map is not associated.
456-
* Field map must be associated to make this work.
492+
/** Utility method to get attribute index from name. Field map must be associated using @link setFields @endlink
493+
* before this method can be used.
494+
* @param fieldName name of field to get attribute index of
495+
* @returns -1 if field does not exist or field map is not associated.
496+
* @see setFields
457497
*/
458498
int fieldNameIndex( const QString& fieldName ) const;
459499

src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ SET(QGIS_CORE_HDRS
503503
qgsexpression.h
504504
qgsexpressionfieldbuffer.h
505505
qgsfeature.h
506+
qgsfeature_p.h
506507
qgsfeatureiterator.h
507508
qgsfeaturerequest.h
508509
qgsfeaturestore.h

0 commit comments

Comments
 (0)