QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsfeaturerequest.h"
19 #include "qgslogger.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();
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:
88  virtual bool fetchFeature( QgsFeature& f ) = 0;
89 
100  virtual bool nextFeatureFilterExpression( QgsFeature &f );
101 
113  virtual bool nextFeatureFilterFids( QgsFeature & f );
114 
117 
119  bool mClosed;
120 
128  bool mZombie;
129 
132  int refs;
133  void ref();
134  void deref();
135  friend class QgsFeatureIterator;
136 
139 
142 
144  virtual bool prepareSimplification( const QgsSimplifyMethod& simplifyMethod );
145 
146  private:
147  bool mUseCachedFeatures;
148  QList<QgsIndexedFeature> mCachedFeatures;
150 
152  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
153 
162  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause>& orderBys );
163 
170  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause>& orderBys );
171 };
172 
173 
174 
179 template<typename T>
181 {
182  public:
183  QgsAbstractFeatureIteratorFromSource( T* source, bool ownSource, const QgsFeatureRequest& request )
184  : QgsAbstractFeatureIterator( request )
185  , mSource( source )
186  , mOwnSource( ownSource )
187  {
188  mSource->iteratorOpened( this );
189  }
190 
192  {
193  if ( mOwnSource )
194  delete mSource;
195  }
196 
197  protected:
199  void iteratorClosed() { mSource->iteratorClosed( this ); }
200 
203 };
204 
209 class CORE_EXPORT QgsFeatureIterator
210 {
211  public:
220 
221  QgsFeatureIterator& operator=( const QgsFeatureIterator& other );
222 
223  bool nextFeature( QgsFeature& f );
224  bool rewind();
225  bool close();
226 
228  bool isClosed() const;
229 
237  void setInterruptionChecker( QgsInterruptionChecker* interruptionChecker );
238 
242  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
243 
244  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
245  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
246 
247  protected:
249 };
250 
252 
254  : mIter( nullptr )
255 {
256 }
257 
259  : mIter( iter )
260 {
261  if ( iter )
262  iter->ref();
263 }
264 
266  : mIter( fi.mIter )
267 {
268  if ( mIter )
269  mIter->ref();
270 }
271 
273 {
274  if ( mIter )
275  mIter->deref();
276 }
277 
279 {
280  return mIter ? mIter->nextFeature( f ) : false;
281 }
282 
284 {
285  if ( mIter )
286  mIter->mFetchedCount = 0;
287 
288  return mIter ? mIter->rewind() : false;
289 }
290 
292 {
293  if ( mIter )
294  mIter->mFetchedCount = 0;
295 
296  return mIter ? mIter->close() : false;
297 }
298 
299 inline bool QgsFeatureIterator::isClosed() const
300 {
301  return mIter ? mIter->mClosed && !mIter->mZombie : true;
302 }
303 
304 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
305 {
306  return fi1.mIter == fi2.mIter;
307 }
308 
309 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
310 {
311  return !( fi1 == fi2 );
312 }
313 
315 {
316  if ( mIter )
317  mIter->setInterruptionChecker( interruptionChecker );
318 }
319 
320 #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
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
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:187
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 optionaly 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...
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.