Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef SOPRANO_ITERATOR_H
00024 #define SOPRANO_ITERATOR_H
00025
00026 #include <QtCore/QSharedDataPointer>
00027 #include <QtCore/QList>
00028
00029 #include "iteratorbackend.h"
00030 #include "error.h"
00031
00032 namespace Soprano {
00033
00078 template<typename T> class Iterator : public Error::ErrorCache
00079 {
00080 public:
00084 Iterator();
00085
00090 Iterator( IteratorBackend<T> *sti );
00091
00092 Iterator( const Iterator &sti );
00093
00094 virtual ~Iterator();
00095
00096 Iterator& operator=( const Iterator& );
00097
00101 void close();
00102
00108 bool next();
00109
00116 T current() const;
00117
00126 T operator*() const;
00127
00132 bool isValid() const;
00133
00143 QList<T> allElements();
00144
00145 protected:
00151 void setBackend( IteratorBackend<T>* b );
00152
00153 IteratorBackend<T>* backend() const;
00154
00155 private:
00156 class Private;
00157 QSharedDataPointer<Private> d;
00158 };
00159 }
00160
00161
00163 template<typename T> class Soprano::Iterator<T>::Private : public QSharedData
00164 {
00165 public:
00166 Private()
00167 : backend( 0 ) {
00168 }
00169
00170 ~Private() {
00171 if( backend ) {
00172 backend->close();
00173 delete backend;
00174 }
00175 }
00176
00177 IteratorBackend<T>* backend;
00178 };
00179
00180
00181 template<typename T> Soprano::Iterator<T>::Iterator()
00182 : Error::ErrorCache(),
00183 d( new Private() )
00184
00185 {
00186 }
00187
00188 template<typename T> Soprano::Iterator<T>::Iterator( IteratorBackend<T> *sti )
00189 : Error::ErrorCache(),
00190 d( new Private() )
00191 {
00192 d->backend = sti;
00193 }
00194
00195 template<typename T> Soprano::Iterator<T>::Iterator( const Iterator<T> &other )
00196 : Error::ErrorCache(),
00197 d( other.d )
00198 {
00199 }
00200
00201 template<typename T> Soprano::Iterator<T>::~Iterator()
00202 {
00203 }
00204
00205 template<typename T> Soprano::Iterator<T>& Soprano::Iterator<T>::operator=( const Iterator<T>& other )
00206 {
00207 d = other.d;
00208 return *this;
00209 }
00210
00211 template<typename T> void Soprano::Iterator<T>::setBackend( IteratorBackend<T>* b )
00212 {
00213 if ( d->backend != b ) {
00214
00215 d->backend = b;
00216 }
00217 }
00218
00219 template<typename T> Soprano::IteratorBackend<T>* Soprano::Iterator<T>::backend() const
00220 {
00221 return d->backend;
00222 }
00223
00224 template<typename T> void Soprano::Iterator<T>::close()
00225 {
00226
00227 if( isValid() ) {
00228 const Private* cd = d.constData();
00229 cd->backend->close();
00230 setError( cd->backend->lastError() );
00231 }
00232 }
00233
00234 template<typename T> bool Soprano::Iterator<T>::next()
00235 {
00236
00237 const Private* cd = d.constData();
00238 if( isValid() ) {
00239 bool hasNext = cd->backend->next();
00240 setError( cd->backend->lastError() );
00241 if( !hasNext ) {
00242 cd->backend->close();
00243 }
00244 return hasNext;
00245 }
00246 else {
00247 setError( QString::fromLatin1( "Invalid iterator." ) );
00248 return false;
00249 }
00250 }
00251
00252 template<typename T> T Soprano::Iterator<T>::current() const
00253 {
00254 if( isValid() ){
00255 T c = d->backend->current();
00256 setError( d->backend->lastError() );
00257 return c;
00258 }
00259 else {
00260 setError( QString::fromLatin1( "Invalid iterator." ) );
00261 return T();
00262 }
00263 }
00264
00265 template<typename T> T Soprano::Iterator<T>::operator*() const
00266 {
00267 return current();
00268 }
00269
00270 template<typename T> bool Soprano::Iterator<T>::isValid() const
00271 {
00272 return d->backend != 0;
00273 }
00274
00275
00276 template<typename T> QList<T> Soprano::Iterator<T>::allElements()
00277 {
00278 QList<T> sl;
00279 if( isValid() ) {
00280 while ( next() ) {
00281 sl.append( current() );
00282 }
00283 close();
00284 }
00285 return sl;
00286 }
00289 #endif
00290