libopie PIM API Documentation

opimaccesstemplate.h

Go to the documentation of this file.
00001 #ifndef OPIE_PIM_ACCESS_TEMPLATE_H
00002 #define OPIE_PIM_ACCESS_TEMPLATE_H
00003 
00004 #include <qarray.h>
00005 
00006 #include <opie/opimrecord.h>
00007 #include <opie/opimaccessbackend.h>
00008 #include <opie/orecordlist.h>
00009 
00010 #include "opimcache.h"
00011 #include "otemplatebase.h"
00012 
00013 class OPimAccessTemplatePrivate;
00023 template <class T = OPimRecord >
00024 class OPimAccessTemplate : public OTemplateBase<T> {
00025 public:
00026     enum Access {
00027         Random = 0,
00028         SortedAccess
00029     };
00030     typedef ORecordList<T> List;
00031     typedef OPimAccessBackend<T> BackEnd;
00032     typedef OPimCache<T> Cache;
00033 
00038     OPimAccessTemplate( BackEnd* end);
00039 
00040     virtual ~OPimAccessTemplate();
00041 
00045     bool load();
00046 
00053     virtual bool reload();
00054 
00059     bool save();
00060 
00066     bool wasChangedExternally()const;
00067 
00072     virtual List allRecords()const;
00073  
00078     virtual List matchRegexp(  const QRegExp &r ) const;   
00079 
00084     virtual List queryByExample( const T& t, int querySettings, const QDateTime& d = QDateTime() );
00085 
00089     virtual T find( int uid )const;
00090 
00094     virtual T find( int uid, const QArray<int>&,
00095                     uint current, typename OTemplateBase<T>::CacheDirection dir = OTemplateBase<T>::Forward )const;
00096 
00097     /* invalidate cache here */
00101     void clear() ;
00102 
00108     virtual bool add( const T& t ) ;
00109     bool add( const OPimRecord& );
00110 
00111     /* only the uid matters */
00117     virtual bool remove( const T& t );
00118 
00124     bool remove( int uid );
00125     bool remove( const OPimRecord& );
00126 
00132     virtual bool replace( const T& t) ;
00133 
00134     void setReadAhead( uint count );
00138     void cache( const T& )const;
00139     void setSaneCacheSize( int );
00140 
00141     QArray<int> records()const;
00142 protected:
00146     void invalidateCache();
00147 
00148     void setBackEnd( BackEnd* end );
00152     BackEnd* backEnd();
00153     BackEnd* m_backEnd;
00154     Cache m_cache;
00155 
00156 private:
00157     OPimAccessTemplatePrivate *d;
00158 
00159 };
00160 
00161 template <class T>
00162 OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end )
00163     : OTemplateBase<T>(), m_backEnd( end )
00164 {
00165     if (end )
00166         end->setFrontend( this );
00167 }
00168 template <class T>
00169 OPimAccessTemplate<T>::~OPimAccessTemplate() {
00170     qWarning("~OPimAccessTemplate<T>");
00171     delete m_backEnd;
00172 }
00173 template <class T>
00174 bool OPimAccessTemplate<T>::load() {
00175     invalidateCache();
00176     return m_backEnd->load();
00177 }
00178 template <class T>
00179 bool OPimAccessTemplate<T>::reload() {
00180     invalidateCache();  // zecke: I think this should be added (se)
00181     return m_backEnd->reload();
00182 }
00183 template <class T>
00184 bool OPimAccessTemplate<T>::save() {
00185     return m_backEnd->save();
00186 }
00187 template <class T>
00188 typename OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const {
00189     QArray<int> ints = m_backEnd->allRecords();
00190     List lis(ints, this );
00191     return lis;
00192 }
00193 template <class T>
00194 typename OPimAccessTemplate<T>::List OPimAccessTemplate<T>::matchRegexp( const QRegExp &r )const {
00195     QArray<int> ints = m_backEnd->matchRegexp( r );
00196     List lis(ints, this );
00197     return lis;
00198 }
00199 template <class T>
00200 QArray<int> OPimAccessTemplate<T>::records()const {
00201     return m_backEnd->allRecords();
00202 }
00203 template <class T>
00204 typename OPimAccessTemplate<T>::List
00205 OPimAccessTemplate<T>::queryByExample( const T& t, int settings, const QDateTime& d ) {
00206     QArray<int> ints = m_backEnd->queryByExample( t, settings, d );
00207 
00208     List lis(ints, this );
00209     return lis;
00210 }
00211 template <class T>
00212 T OPimAccessTemplate<T>::find( int uid ) const{
00213     T t = m_backEnd->find( uid );
00214     cache( t );
00215     return t;
00216 }
00217 template <class T>
00218 T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar,
00219                                uint current, typename OTemplateBase<T>::CacheDirection dir )const {
00220     /*
00221      * better do T.isEmpty()
00222      * after a find this way we would
00223      * avoid two finds in QCache...
00224      */
00225     // qWarning("find it now %d", uid );
00226     if (m_cache.contains( uid ) ) {
00227         return m_cache.find( uid );
00228     }
00229 
00230     T t = m_backEnd->find( uid, ar, current, dir );
00231     cache( t );
00232     return t;
00233 }
00234 template <class T>
00235 void OPimAccessTemplate<T>::clear() {
00236     invalidateCache();
00237     m_backEnd->clear();
00238 }
00239 template <class T>
00240 bool OPimAccessTemplate<T>::add( const T& t ) {
00241     cache( t );
00242     return m_backEnd->add( t );
00243 }
00244 template <class T>
00245 bool OPimAccessTemplate<T>::add( const OPimRecord& rec) {
00246     /* same type */
00247     if ( rec.rtti() == T::rtti() ) {
00248         const T &t = static_cast<const T&>(rec);
00249         return add(t);
00250     }
00251     return false;
00252 }
00253 template <class T>
00254 bool OPimAccessTemplate<T>::remove( const T& t ) {
00255     return remove( t.uid() );
00256 }
00257 template <class T>
00258 bool OPimAccessTemplate<T>::remove( int uid ) {
00259     m_cache.remove( uid );
00260     return m_backEnd->remove( uid );
00261 }
00262 template <class T>
00263 bool OPimAccessTemplate<T>::remove( const OPimRecord& rec) {
00264     return remove( rec.uid() );
00265 }
00266 template <class T>
00267 bool OPimAccessTemplate<T>::replace( const T& t ) {
00268     m_cache.replace( t );
00269     return m_backEnd->replace( t );
00270 }
00271 template <class T>
00272 void OPimAccessTemplate<T>::invalidateCache() {
00273     m_cache.invalidate();
00274 }
00275 template <class T>
00276 typename OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() {
00277     return m_backEnd;
00278 }
00279 template <class T>
00280 bool OPimAccessTemplate<T>::wasChangedExternally()const {
00281     return false;
00282 }
00283 template <class T>
00284 void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) {
00285     m_backEnd = end;
00286     if (m_backEnd )
00287         m_backEnd->setFrontend( this );
00288 }
00289 template <class T>
00290 void OPimAccessTemplate<T>::cache( const T& t ) const{
00291     /* hacky we need to work around the const*/
00292     ((OPimAccessTemplate<T>*)this)->m_cache.add( t );
00293 }
00294 template <class T>
00295 void OPimAccessTemplate<T>::setSaneCacheSize( int size ) {
00296     m_cache.setSize( size );
00297 }
00298 template <class T>
00299 void OPimAccessTemplate<T>::setReadAhead( uint count ) {
00300     m_backEnd->setReadAhead( count );
00301 }
00302 #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:20 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001