Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

opimrecordlist.h

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003                              Copyright (C) The Main Author <main-author@whereever.org>
00004               =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
00005             .=l.
00006            .>+-=
00007  _;:,     .>    :=|.         This program is free software; you can
00008 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00009 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00010 .="- .-=="i,     .._         License as published by the Free Software
00011  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00012      ._= =}       :          or (at your option) any later version.
00013     .%`+i>       _;_.
00014     .i_,=:_.      -<s.       This program is distributed in the hope that
00015      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00016     : ..    .:,     . . .    without even the implied warranty of
00017     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00018   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00019 ..}^=.=       =       ;      Library General Public License for more
00020 ++=   -.     .`     .:       details.
00021  :     =  ...= . :.=-
00022  -.   .:....=;==+<;          You should have received a copy of the GNU
00023   -_. . .   )=.  =           Library General Public License along with
00024     --        :-=`           this library; see the file COPYING.LIB.
00025                              If not, write to the Free Software Foundation,
00026                              Inc., 59 Temple Place - Suite 330,
00027                              Boston, MA 02111-1307, USA.
00028 */
00029 
00030 #ifndef ORECORDLIST_H
00031 #define ORECORDLIST_H
00032 
00033 /* OPIE */
00034 #include <opie2/opimtemplatebase.h>
00035 #include <opie2/opimrecord.h>
00036 //#include <opie2/odebug.h>
00037 
00038 /* QT */
00039 #include <qarray.h>
00040 
00041 namespace Opie
00042 {
00043 
00044 template<class T> class OPimAccessTemplate;
00045 class OPimRecordListIteratorPrivate;
00053 template <class T> class OPimRecordList;
00054 template <class T = OPimRecord>
00055 class OPimRecordListIterator
00056 {
00057     friend class OPimRecordList<T>;
00058 
00059   public:
00060     typedef OTemplateBase<T> Base;
00061 
00066     OPimRecordListIterator( const QArray<int>, const Base* );
00067 
00071     OPimRecordListIterator();
00072     ~OPimRecordListIterator();
00073 
00074     OPimRecordListIterator( const OPimRecordListIterator& );
00075     OPimRecordListIterator &operator=( const OPimRecordListIterator& );
00076 
00081     T operator*();
00082     OPimRecordListIterator &operator++();
00083     OPimRecordListIterator &operator--();
00084 
00085     bool operator==( const OPimRecordListIterator& it );
00086     bool operator!=( const OPimRecordListIterator& it );
00087 
00091     uint current() const;
00092 
00096     uint count() const;
00097 
00101     void setCurrent( uint cur );
00102 
00103   private:
00104     QArray<int> m_uids;
00105     uint m_current;
00106     const Base* m_temp;
00107     bool m_end : 1;
00108     T m_record;
00109     bool m_direction : 1;
00110 
00111     /* d pointer for future versions */
00112     OPimRecordListIteratorPrivate *d;
00113 };
00114 
00115 
00116 class OPimRecordListPrivate;
00121 template <class T = OPimRecord >
00122 class OPimRecordList
00123 {
00124    template<class> friend class OPimAccessTemplate;
00125   public:
00126     typedef OTemplateBase<T> Base;
00127     typedef OPimRecordListIterator<T> Iterator;
00128 
00132     OPimRecordList (){}
00133     OPimRecordList( const QArray<int>& ids,
00134                     const Base* );
00135     ~OPimRecordList();
00136 
00140     Iterator begin();
00141 
00145     Iterator end();
00146 
00150     uint count() const;
00151 
00152     T operator[] ( uint i );
00153     int uidAt( uint i );
00154 
00158     bool remove( int uid );
00159 
00160     /*
00161       ConstIterator begin()const;
00162       ConstIterator end()const;
00163     */
00164   protected:
00165     UIDArray uids()const;
00166 
00167   private:
00168     QArray<int> m_ids;
00169     const Base* m_acc;
00170     OPimRecordListPrivate *d;
00171 };
00172 
00173 
00174 /* ok now implement it  */
00175 template <class T>
00176 OPimRecordListIterator<T>::OPimRecordListIterator()
00177 {
00178     m_current = 0;
00179     m_temp = 0l;
00180     m_end = true;
00181     m_record = T();
00182     /* forward */
00183     m_direction = TRUE;
00184 }
00185 
00186 
00187 template <class T>
00188 OPimRecordListIterator<T>::~OPimRecordListIterator()
00189 {
00190     /* nothing to delete */
00191 }
00192 
00193 
00194 template <class T>
00195 OPimRecordListIterator<T>::OPimRecordListIterator( const OPimRecordListIterator<T>& it )
00196 {
00197     m_uids = it.m_uids;
00198     m_current = it.m_current;
00199     m_temp = it.m_temp;
00200     m_end = it.m_end;
00201     m_record = it.m_record;
00202     m_direction = it.m_direction;
00203 }
00204 
00205 
00206 template <class T>
00207 OPimRecordListIterator<T> &OPimRecordListIterator<T>::operator=( const OPimRecordListIterator<T>& it )
00208 {
00209     m_uids = it.m_uids;
00210     m_current = it.m_current;
00211     m_temp = it.m_temp;
00212     m_end = it.m_end;
00213     m_record = it.m_record;
00214 
00215     return *this;
00216 }
00217 
00218 
00219 template <class T>
00220 T OPimRecordListIterator<T>::operator*()
00221 {
00222     if ( !m_end )
00223         m_record = m_temp->find( m_uids[ m_current ], m_uids, m_current,
00224                                  m_direction ? Base::Forward :
00225                                  Base::Reverse );
00226     else
00227         m_record = T();
00228 
00229     return m_record;
00230 }
00231 
00232 
00233 template <class T>
00234 OPimRecordListIterator<T> &OPimRecordListIterator<T>::operator++()
00235 {
00236     m_direction = true;
00237     if ( m_current < m_uids.count() )
00238     {
00239         m_end = false;
00240         ++m_current;
00241     }
00242     else
00243         m_end = true;
00244 
00245     return *this;
00246 }
00247 
00248 
00249 template <class T>
00250 OPimRecordListIterator<T> &OPimRecordListIterator<T>::operator--()
00251 {
00252     m_direction = false;
00253     if ( m_current > 0 )
00254     {
00255         --m_current;
00256         m_end = false;
00257     }
00258     else
00259         m_end = true;
00260 
00261     return *this;
00262 }
00263 
00264 
00265 template <class T>
00266 bool OPimRecordListIterator<T>::operator==( const OPimRecordListIterator<T>& it )
00267 {
00268 
00269     /* if both are at we're the same.... */
00270     if ( m_end == it.m_end ) return true;
00271 
00272     if ( m_uids != it.m_uids ) return false;
00273     if ( m_current != it.m_current ) return false;
00274     if ( m_temp != it.m_temp ) return false;
00275 
00276     return true;
00277 }
00278 
00279 
00280 template <class T>
00281 bool OPimRecordListIterator<T>::operator!=( const OPimRecordListIterator<T>& it )
00282 {
00283     return !( *this == it );
00284 }
00285 
00286 
00287 template <class T>
00288 OPimRecordListIterator<T>::OPimRecordListIterator( const QArray<int> uids,
00289         const Base* t )
00290         : m_uids( uids ), m_current( 0 ), m_temp( t ), m_end( false ),
00291         m_direction( false )
00292 {
00293     /* if the list is empty we're already at the end of the list */
00294     if ( uids.count() == 0 )
00295         m_end = true;
00296 }
00297 
00298 
00299 template <class T>
00300 uint OPimRecordListIterator<T>::current() const
00301 {
00302     return m_current;
00303 }
00304 
00305 
00306 template <class T>
00307 void OPimRecordListIterator<T>::setCurrent( uint cur )
00308 {
00309     if ( cur < m_uids.count() )
00310     {
00311         m_end = false;
00312         m_current = cur;
00313     }
00314 }
00315 template <class T>
00316 uint OPimRecordListIterator<T>::count() const
00317 {
00318     return m_uids.count();
00319 }
00320 
00321 
00322 template <class T>
00323 OPimRecordList<T>::OPimRecordList( const QArray<int>& ids,
00324                              const Base* acc )
00325         : m_ids( ids ), m_acc( acc )
00326 {}
00327 
00328 
00329 template <class T>
00330 OPimRecordList<T>::~OPimRecordList()
00331 {
00332     /* nothing to do here */
00333 }
00334 
00335 
00336 template <class T>
00337 typename OPimRecordList<T>::Iterator OPimRecordList<T>::begin()
00338 {
00339     Iterator it( m_ids, m_acc );
00340     return it;
00341 }
00342 
00343 
00344 template <class T>
00345 typename OPimRecordList<T>::Iterator OPimRecordList<T>::end()
00346 {
00347     Iterator it( m_ids, m_acc );
00348     it.m_end = true;
00349     it.m_current = m_ids.count();
00350 
00351     return it;
00352 }
00353 
00354 
00355 template <class T>
00356 uint OPimRecordList<T>::count() const
00357 {
00358     return m_ids.count();
00359 }
00360 
00361 
00362 template <class T>
00363 T OPimRecordList<T>::operator[] ( uint i )
00364 {
00365     if ( i >= m_ids.count() )
00366         return T();
00367     /* forward */
00368     return m_acc->find( m_ids[ i ], m_ids, i );
00369 }
00370 
00371 
00372 template <class T>
00373 int OPimRecordList<T>::uidAt( uint i )
00374 {
00375     return m_ids[ i ];
00376 }
00377 
00378 
00379 template <class T>
00380 bool OPimRecordList<T>::remove( int uid )
00381 {
00382     QArray<int> copy( m_ids.count() );
00383     int counter = 0;
00384     bool ret_val = false;
00385 
00386     for ( uint i = 0; i < m_ids.count(); i++ )
00387     {
00388         if ( m_ids[ i ] != uid )
00389         {
00390             copy[ counter++ ] = m_ids[ i ];
00391 
00392         }
00393         else
00394             ret_val = true;
00395     }
00396 
00397     copy.resize( counter );
00398     m_ids = copy;
00399 
00400 
00401     return ret_val;
00402 }
00403 
00404 template<class T>
00405 UIDArray OPimRecordList<T>::uids()const {
00406     return m_ids;
00407 }
00408 
00409 }
00410 #endif

Generated on Sat Nov 5 16:16:21 2005 for OPIE by  doxygen 1.4.2