QGIS API Documentation  2.99.0-Master (716ff6c)
qgsfeatureiterator.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeatureiterator.h
3  ---------------------
4  begin : Juli 2012
5  copyright : (C) 2012 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSFEATUREITERATOR_H
16 #define QGSFEATUREITERATOR_H
17 
18 #include "qgis_core.h"
19 #include "qgsfeaturerequest.h"
20 #include "qgsindexedfeature.h"
21 
22 
29 class CORE_EXPORT QgsInterruptionChecker
30 {
31  public:
33  virtual bool mustStop() const = 0;
34 };
35 
39 class CORE_EXPORT QgsAbstractFeatureIterator
40 {
41  public:
42 
45  {
49  };
50 
53 
55  virtual ~QgsAbstractFeatureIterator() = default;
56 
58  virtual bool nextFeature( QgsFeature &f );
59 
61  virtual bool rewind() = 0;
63  virtual bool close() = 0;
64 
73  virtual void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker );
74 
78  CompileStatus compileStatus() const { return mCompileStatus; }
79 
80  protected:
81 
89  virtual bool fetchFeature( QgsFeature &f ) = 0;
90 
101  virtual bool nextFeatureFilterExpression( QgsFeature &f );
102 
114  virtual bool nextFeatureFilterFids( QgsFeature &f );
115 
118 
120  bool mClosed;
121 
129  bool mZombie;
130 
133  int refs;
134  void ref();
135  void deref();
136  friend class QgsFeatureIterator;
137 
140 
143 
145  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
146 
147  private:
148  bool mUseCachedFeatures;
149  QList<QgsIndexedFeature> mCachedFeatures;
150  QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
151 
153  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
154 
163  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
164 
171  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
172 };
173 
174 
175 
180 template<typename T>
182 {
183  public:
184  QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
185  : QgsAbstractFeatureIterator( request )
186  , mSource( source )
187  , mOwnSource( ownSource )
188  {
189  mSource->iteratorOpened( this );
190  }
191 
193  {
194  if ( mOwnSource )
195  delete mSource;
196  }
197 
198  protected:
200  void iteratorClosed() { mSource->iteratorClosed( this ); }
201 
202  T *mSource = nullptr;
204 };
205 
210 class CORE_EXPORT QgsFeatureIterator
211 {
212  public:
221 
222  QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
223 
224  bool nextFeature( QgsFeature &f );
225  bool rewind();
226  bool close();
227 
229  bool isClosed() const;
230 
238  void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker );
239 
243  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
244 
245  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
246  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
247 
248  protected:
249  QgsAbstractFeatureIterator *mIter = nullptr;
250 };
251 
253 
255  : mIter( nullptr )
256 {
257 }
258 
260  : mIter( iter )
261 {
262  if ( iter )
263  iter->ref();
264 }
265 
267  : mIter( fi.mIter )
268 {
269  if ( mIter )
270  mIter->ref();
271 }
272 
274 {
275  if ( mIter )
276  mIter->deref();
277 }
278 
280 {
281  return mIter ? mIter->nextFeature( f ) : false;
282 }
283 
285 {
286  if ( mIter )
287  mIter->mFetchedCount = 0;
288 
289  return mIter ? mIter->rewind() : false;
290 }
291 
293 {
294  if ( mIter )
295  mIter->mFetchedCount = 0;
296 
297  return mIter ? mIter->close() : false;
298 }
299 
300 inline bool QgsFeatureIterator::isClosed() const
301 {
302  return mIter ? mIter->mClosed && !mIter->mZombie : true;
303 }
304 
305 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
306 {
307  return fi1.mIter == fi2.mIter;
308 }
309 
310 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
311 {
312  return !( fi1 == fi2 );
313 }
314 
316 {
317  if ( mIter )
318  mIter->setInterruptionChecker( interruptionChecker );
319 }
320 
321 #endif // QGSFEATUREITERATOR_H
Wrapper for iterator of features from vector data provider or vector layer.
virtual bool close()=0
end of iterating: free the resources / lock
Expression could not be compiled or not attempt was made to compile expression.
long mFetchedCount
Number of features already fetched by iterator.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
virtual bool rewind()=0
reset the iterator to the starting position
Expression was fully compiled and delegated to data provider source.
bool mClosed
Set to true, as soon as the iterator is closed.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
friend bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
CompileStatus mCompileStatus
Status of compilation of filter expression.
Interface that can be optionally attached to an iterator so its nextFeature() implementaton can check...
void iteratorClosed()
to be called by from subclass in close()
virtual bool nextFeature(QgsFeature &f)
fetch next feature, return true on success
CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
Internal feature iterator to be implemented within data providers.
CompileStatus
Status of expression compilation for filter expression requests.
QgsFeatureIterator()
construct invalid iterator
bool mZombie
A feature iterator may be closed already but still be serving features from the cache.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void deref()
Remove reference, delete if refs == 0.
friend bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsFeatureRequest mRequest
A copy of the feature request.
virtual void setInterruptionChecker(QgsInterruptionChecker *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
Expression was partially compiled, but extra checks need to be applied to features.
QgsAbstractFeatureIterator * mIter
~QgsFeatureIterator()
destructor deletes the iterator if it has no more references
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
int refs
reference counting (to allow seamless copying of QgsFeatureIterator instances) TODO QGIS3: make this ...
bool nextFeature(QgsFeature &f)
bool isClosed() const
find out whether the iterator is still valid or closed already
QgsAbstractFeatureIteratorFromSource(T *source, bool ownSource, const QgsFeatureRequest &request)
void setInterruptionChecker(QgsInterruptionChecker *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
Helper template that cares of two things: 1.