libopie PIM API Documentation

orecordlist.h

Go to the documentation of this file.
00001 
00002 #ifndef OPIE_RECORD_LIST_H
00003 #define OPIE_RECORD_LIST_H
00004 
00005 #include <qarray.h>
00006 
00007 #include "otemplatebase.h"
00008 #include "opimrecord.h"
00009 
00010 class ORecordListIteratorPrivate;
00018 template <class T> class ORecordList;
00019 template <class T = OPimRecord>
00020 class ORecordListIterator {
00021     friend class ORecordList<T>;
00022 public:
00023     typedef OTemplateBase<T> Base;
00024 
00029     ORecordListIterator( const QArray<int>, const Base* );
00030 
00034     ORecordListIterator();
00035     ~ORecordListIterator();
00036 
00037     ORecordListIterator( const ORecordListIterator& );
00038     ORecordListIterator &operator=(const ORecordListIterator& );
00039 
00044     T operator*();
00045     ORecordListIterator &operator++();
00046     ORecordListIterator &operator--();
00047 
00048     bool operator==( const ORecordListIterator& it );
00049     bool operator!=( const ORecordListIterator& it );
00050 
00054     uint current()const;
00055 
00059     uint count()const;
00060 
00064     void setCurrent( uint cur );
00065 
00066 private:
00067     QArray<int> m_uids;
00068     uint m_current;
00069     const Base* m_temp;
00070     bool m_end : 1;
00071     T m_record;
00072     bool m_direction :1;
00073 
00074     /* d pointer for future versions */
00075     ORecordListIteratorPrivate *d;
00076 };
00077 
00078 class ORecordListPrivate;
00083 template <class T = OPimRecord >
00084 class ORecordList {
00085 public:
00086     typedef OTemplateBase<T> Base;
00087     typedef ORecordListIterator<T> Iterator;
00088 
00092     ORecordList () {
00093     }
00094 ORecordList( const QArray<int>& ids,
00095                  const Base* );
00096     ~ORecordList();
00097 
00101     Iterator begin();
00102 
00106     Iterator end();
00107 
00111      uint count()const;
00112 
00113     T operator[]( uint i );
00114     int uidAt(uint i );
00115 
00119     bool remove( int uid );
00120 
00121     /*
00122       ConstIterator begin()const;
00123       ConstIterator end()const;
00124     */
00125 private:
00126     QArray<int> m_ids;
00127     const Base* m_acc;
00128     ORecordListPrivate *d;
00129 };
00130 
00131 /* ok now implement it  */
00132 template <class T>
00133 ORecordListIterator<T>::ORecordListIterator() {
00134     m_current = 0;
00135     m_temp = 0l;
00136     m_end = true;
00137     m_record = T();
00138     /* forward */
00139     m_direction = TRUE;
00140 }
00141 template <class T>
00142 ORecordListIterator<T>::~ORecordListIterator() {
00143 /* nothing to delete */
00144 }
00145 
00146 template <class T>
00147 ORecordListIterator<T>::ORecordListIterator( const ORecordListIterator<T>& it) {
00148 //    qWarning("ORecordListIterator copy c'tor");
00149     m_uids = it.m_uids;
00150     m_current = it.m_current;
00151     m_temp = it.m_temp;
00152     m_end = it.m_end;
00153     m_record = it.m_record;
00154     m_direction = it.m_direction;
00155 }
00156 
00157 template <class T>
00158 ORecordListIterator<T> &ORecordListIterator<T>::operator=( const ORecordListIterator<T>& it) {
00159     m_uids = it.m_uids;
00160     m_current = it.m_current;
00161     m_temp = it.m_temp;
00162     m_end = it.m_end;
00163     m_record = it.m_record;
00164 
00165     return *this;
00166 }
00167 
00168 template <class T>
00169 T ORecordListIterator<T>::operator*() {
00170     //qWarning("operator* %d %d", m_current,  m_uids[m_current] );
00171     if (!m_end )
00172         m_record = m_temp->find( m_uids[m_current], m_uids, m_current,
00173                                  m_direction ? Base::Forward :
00174                                  Base::Reverse  );
00175     else
00176         m_record = T();
00177 
00178     return m_record;
00179 }
00180 
00181 template <class T>
00182 ORecordListIterator<T> &ORecordListIterator<T>::operator++() {
00183     m_direction = true;
00184     if (m_current < m_uids.count() ) {
00185         m_end = false;
00186         ++m_current;
00187     }else
00188         m_end = true;
00189 
00190     return *this;
00191 }
00192 template <class T>
00193 ORecordListIterator<T> &ORecordListIterator<T>::operator--() {
00194     m_direction = false;
00195     if ( m_current > 0 ) {
00196         --m_current;
00197         m_end = false;
00198     }  else
00199         m_end = true;
00200 
00201     return *this;
00202 }
00203 
00204 template <class T>
00205 bool ORecordListIterator<T>::operator==( const ORecordListIterator<T>& it ) {
00206 
00207     /* if both are at we're the same.... */
00208     if ( m_end == it.m_end ) return true;
00209 
00210     if ( m_uids != it.m_uids ) return false;
00211     if ( m_current != it.m_current ) return false;
00212     if ( m_temp != it.m_temp ) return false;
00213 
00214     return true;
00215 }
00216 template <class T>
00217 bool ORecordListIterator<T>::operator!=( const ORecordListIterator<T>& it ) {
00218     return !(*this == it );
00219 }
00220 template <class T>
00221 ORecordListIterator<T>::ORecordListIterator( const QArray<int> uids,
00222                                   const Base* t )
00223     : m_uids( uids ), m_current( 0 ),  m_temp( t ), m_end( false ),
00224       m_direction( false )
00225 {
00226     /* if the list is empty we're already at the end of the list */
00227     if (uids.count() == 0 )
00228         m_end = true;
00229 }
00230 template <class T>
00231 uint ORecordListIterator<T>::current()const {
00232     return m_current;
00233 }
00234 template <class T>
00235 void ORecordListIterator<T>::setCurrent( uint cur ) {
00236     if( cur < m_uids.count() ) {
00237     m_end = false;
00238     m_current= cur;
00239     }
00240 }
00241 template <class T>
00242 uint ORecordListIterator<T>::count()const {
00243     return m_uids.count();
00244 }
00245 template <class T>
00246 ORecordList<T>::ORecordList( const QArray<int>& ids,
00247                              const Base* acc )
00248     : m_ids( ids ), m_acc( acc )
00249 {
00250 }
00251 template <class T>
00252 ORecordList<T>::~ORecordList() {
00253 /* nothing to do here */
00254 }
00255 template <class T>
00256 typename ORecordList<T>::Iterator ORecordList<T>::begin() {
00257     Iterator it( m_ids, m_acc );
00258     return it;
00259 }
00260 template <class T>
00261 typename ORecordList<T>::Iterator ORecordList<T>::end() {
00262     Iterator it( m_ids, m_acc );
00263     it.m_end = true;
00264     it.m_current = m_ids.count();
00265 
00266     return it;
00267 }
00268 template <class T>
00269 uint ORecordList<T>::count()const {
00270 return m_ids.count();
00271 }
00272 template <class T>
00273 T ORecordList<T>::operator[]( uint i ) {
00274     if ( i >= m_ids.count() )
00275         return T();
00276     /* forward */
00277     return m_acc->find( m_ids[i], m_ids, i );
00278 }
00279 template <class T>
00280 int ORecordList<T>::uidAt( uint i ) {
00281     return m_ids[i];
00282 }
00283 
00284 template <class T>
00285 bool ORecordList<T>::remove( int uid ) {
00286     QArray<int> copy( m_ids.count() );
00287     int counter = 0;
00288     bool ret_val = false;
00289 
00290     for (uint i = 0; i < m_ids.count(); i++){
00291         if ( m_ids[i] != uid ){
00292             copy[counter++] = m_ids[i];
00293 
00294         }else
00295             ret_val = true;
00296     }
00297 
00298     copy.resize( counter );
00299     m_ids = copy;
00300 
00301 
00302     return ret_val;
00303 }
00304 
00305 
00306 #endif
KDE Logo
This file is part of the documentation for OPIE Version 1.1.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:25:21 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001