Soprano  2.9.4
iterator.h
Go to the documentation of this file.
1 /*
2  * This file is part of Soprano Project.
3  *
4  * Copyright (C) 2006 Daniele Galdi <daniele.galdi@gmail.com>
5  * Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef SOPRANO_ITERATOR_H
24 #define SOPRANO_ITERATOR_H
25 
26 #include <QtCore/QSharedDataPointer>
27 #include <QtCore/QList>
28 
29 #include "iteratorbackend.h"
30 #include "error.h"
31 
32 namespace Soprano {
33 
78  template<typename T> class Iterator : public Error::ErrorCache
79  {
80  public:
84  Iterator();
85 
91 
92  Iterator( const Iterator &sti );
93 
94  virtual ~Iterator();
95 
96  Iterator& operator=( const Iterator& );
97 
101  void close();
102 
108  bool next();
109 
116  T current() const;
117 
126  T operator*() const;
127 
132  bool isValid() const;
133 
144 
145  protected:
151  void setBackend( IteratorBackend<T>* b );
152 
153  IteratorBackend<T>* backend() const;
154 
155  private:
156  class Private;
158  };
159 }
160 
161 
163 template<typename T> class Soprano::Iterator<T>::Private : public QSharedData
164 {
165 public:
166  Private()
167  : backend( 0 ) {
168  }
169 
170  ~Private() {
171  if( backend ) {
172  backend->close();
173  delete backend;
174  }
175  }
176 
177  IteratorBackend<T>* backend;
178 };
179 
180 
181 template<typename T> Soprano::Iterator<T>::Iterator()
182  : Error::ErrorCache(),
183  d( new Private() )
184 
185 {
186 }
187 
188 template<typename T> Soprano::Iterator<T>::Iterator( IteratorBackend<T> *sti )
189  : Error::ErrorCache(),
190  d( new Private() )
191 {
192  d->backend = sti;
193 }
194 
195 template<typename T> Soprano::Iterator<T>::Iterator( const Iterator<T> &other )
196  : Error::ErrorCache(),
197  d( other.d )
198 {
199 }
200 
201 template<typename T> Soprano::Iterator<T>::~Iterator()
202 {
203 }
204 
205 template<typename T> Soprano::Iterator<T>& Soprano::Iterator<T>::operator=( const Iterator<T>& other )
206 {
207  d = other.d;
208  return *this;
209 }
210 
211  template<typename T> void Soprano::Iterator<T>::setBackend( IteratorBackend<T>* b )
212 {
213  if ( d->backend != b ) {
214  // now we want it to detach
215  d->backend = b;
216  }
217 }
218 
220 {
221  return d->backend;
222 }
223 
224 template<typename T> void Soprano::Iterator<T>::close()
225 {
226  // some evil hacking to avoid detachment of the shared data
227  if( isValid() ) {
228  const Private* cd = d.constData();
229  cd->backend->close();
230  setError( cd->backend->lastError() );
231  }
232 }
233 
234 template<typename T> bool Soprano::Iterator<T>::next()
235 {
236  // some evil hacking to avoid detachment of the shared data
237  const Private* cd = d.constData();
238  if( isValid() ) {
239  bool hasNext = cd->backend->next();
240  setError( cd->backend->lastError() );
241  if( !hasNext ) {
242  cd->backend->close();
243  }
244  return hasNext;
245  }
246  else {
247  setError( QString::fromLatin1( "Invalid iterator." ) );
248  return false;
249  }
250 }
251 
252 template<typename T> T Soprano::Iterator<T>::current() const
253 {
254  if( isValid() ){
255  T c = d->backend->current();
256  setError( d->backend->lastError() );
257  return c;
258  }
259  else {
260  setError( QString::fromLatin1( "Invalid iterator." ) );
261  return T();
262  }
263 }
264 
265 template<typename T> T Soprano::Iterator<T>::operator*() const
266 {
267  return current();
268 }
269 
270 template<typename T> bool Soprano::Iterator<T>::isValid() const
271 {
272  return d->backend != 0;
273 }
274 
275 
276 template<typename T> QList<T> Soprano::Iterator<T>::allElements()
277 {
278  QList<T> sl;
279  if( isValid() ) {
280  while ( next() ) {
281  sl.append( current() );
282  }
283  close();
284  }
285  return sl;
286 }
289 #endif
290 
virtual ~Iterator()
IteratorBackend< T > * backend() const
Iterator & operator=(const Iterator &)
T current() const
bool isValid() const
Core class of Soprano's exception system.
Definition: error.h:234
append(const T &value)
void setBackend(IteratorBackend< T > *b)
The basic Soprano iterator class.
Definition: iterator.h:78
T operator*() const
The actual work in a Iterator instance is done by an IteratorBackend.
fromLatin1(const char *str, int size=-1)
QList< T > allElements()