opimcache.h
Go to the documentation of this file.00001 #ifndef OPIE_PIM_CACHE_H 00002 #define OPIE_PIM_CACHE_H 00003 00004 #include <qintcache.h> 00005 00006 #include "opimrecord.h" 00007 00008 class OPimCacheItemPrivate; 00009 00010 template <class T = OPimRecord> 00011 class OPimCacheItem { 00012 public: 00013 OPimCacheItem( const T& t = T() ); 00014 OPimCacheItem( const OPimCacheItem& ); 00015 ~OPimCacheItem(); 00016 00017 OPimCacheItem &operator=( const OPimCacheItem& ); 00018 00019 T record()const; 00020 void setRecord( const T& ); 00021 private: 00022 T m_t; 00023 OPimCacheItemPrivate *d; 00024 }; 00025 00026 00027 class OPimCachePrivate; 00033 template <class T = OPimRecord> 00034 class OPimCache { 00035 public: 00036 typedef OPimCacheItem<T> Item; 00037 OPimCache(); 00038 OPimCache( const OPimCache& ); 00039 ~OPimCache(); 00040 00041 OPimCache &operator=( const OPimCache& ); 00042 00043 bool contains(int uid)const; 00044 void invalidate(); 00045 void setSize( int size ); 00046 00047 T find(int uid )const; 00048 void add( const T& ); 00049 void remove( int uid ); 00050 void replace( const T& ); 00051 00052 private: 00053 QIntCache<Item> m_cache; 00054 OPimCachePrivate* d; 00055 }; 00056 00057 // Implementation 00058 template <class T> 00059 OPimCacheItem<T>::OPimCacheItem( const T& t ) 00060 : m_t(t) { 00061 } 00062 template <class T> 00063 OPimCacheItem<T>::~OPimCacheItem() { 00064 00065 } 00066 template <class T> 00067 T OPimCacheItem<T>::record()const { 00068 return m_t; 00069 } 00070 template <class T> 00071 void OPimCacheItem<T>::setRecord( const T& t ) { 00072 m_t = t; 00073 } 00074 // Cache 00075 template <class T> 00076 OPimCache<T>::OPimCache() 00077 : m_cache(100, 53 ) 00078 { 00079 m_cache.setAutoDelete( TRUE ); 00080 } 00081 template <class T> 00082 OPimCache<T>::~OPimCache() { 00083 00084 } 00085 template <class T> 00086 bool OPimCache<T>::contains(int uid )const { 00087 Item* it = m_cache.find( uid, FALSE ); 00088 if (!it) 00089 return false; 00090 return true; 00091 } 00092 template <class T> 00093 void OPimCache<T>::invalidate() { 00094 m_cache.clear(); 00095 } 00096 template <class T> 00097 void OPimCache<T>::setSize( int size ) { 00098 m_cache.setMaxCost( size ); 00099 } 00100 template <class T> 00101 T OPimCache<T>::find(int uid )const { 00102 Item *it = m_cache.find( uid ); 00103 if (it) 00104 return it->record(); 00105 return T(); 00106 } 00107 template <class T> 00108 void OPimCache<T>::add( const T& t ) { 00109 Item* it = 0l; 00110 it = m_cache.find(t.uid(), FALSE ); 00111 00112 if (it ) 00113 it->setRecord( t ); 00114 00115 it = new Item( t ); 00116 if (!m_cache.insert( t.uid(), it ) ) 00117 delete it; 00118 } 00119 template <class T> 00120 void OPimCache<T>::remove( int uid ) { 00121 m_cache.remove( uid ); 00122 } 00123 template <class T> 00124 void OPimCache<T>::replace( const T& t) { 00125 Item *it = m_cache.find( t.uid() ); 00126 if ( it ) { 00127 it->setRecord( t ); 00128 } 00129 } 00130 00131 #endif
