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 #ifndef OPIMCACHE_H 00030 #define OPIMCACHE_H 00031 00032 /* OPIE */ 00033 #include <opie2/opimrecord.h> 00034 00035 /* QT */ 00036 #include <qintcache.h> 00037 00038 namespace Opie { 00039 class OPimCacheItemPrivate; 00040 00041 template <class T = OPimRecord> 00042 class OPimCacheItem { 00043 public: 00044 OPimCacheItem( const T& t = T() ); 00045 OPimCacheItem( const OPimCacheItem& ); 00046 ~OPimCacheItem(); 00047 00048 OPimCacheItem &operator=( const OPimCacheItem& ); 00049 00050 T record()const; 00051 void setRecord( const T& ); 00052 private: 00053 T m_t; 00054 OPimCacheItemPrivate *d; 00055 }; 00056 00057 00058 class OPimCachePrivate; 00064 template <class T = OPimRecord> 00065 class OPimCache { 00066 public: 00067 typedef OPimCacheItem<T> Item; 00068 OPimCache(); 00069 OPimCache( const OPimCache& ); 00070 ~OPimCache(); 00071 00072 OPimCache &operator=( const OPimCache& ); 00073 00074 bool contains(int uid)const; 00075 void invalidate(); 00076 void setSize( int size ); 00077 00078 T find(int uid )const; 00079 void add( const T& ); 00080 void remove( int uid ); 00081 void replace( const T& ); 00082 00083 private: 00084 QIntCache<Item> m_cache; 00085 OPimCachePrivate* d; 00086 }; 00087 00088 // Implementation 00089 template <class T> 00090 OPimCacheItem<T>::OPimCacheItem( const T& t ) 00091 : m_t(t) { 00092 } 00093 template <class T> 00094 OPimCacheItem<T>::~OPimCacheItem() { 00095 00096 } 00097 template <class T> 00098 T OPimCacheItem<T>::record()const { 00099 return m_t; 00100 } 00101 template <class T> 00102 void OPimCacheItem<T>::setRecord( const T& t ) { 00103 m_t = t; 00104 } 00105 // Cache 00106 template <class T> 00107 OPimCache<T>::OPimCache() 00108 : m_cache(100, 53 ) 00109 { 00110 m_cache.setAutoDelete( TRUE ); 00111 } 00112 template <class T> 00113 OPimCache<T>::~OPimCache() { 00114 00115 } 00116 template <class T> 00117 bool OPimCache<T>::contains(int uid )const { 00118 Item* it = m_cache.find( uid, FALSE ); 00119 if (!it) 00120 return false; 00121 return true; 00122 } 00123 template <class T> 00124 void OPimCache<T>::invalidate() { 00125 m_cache.clear(); 00126 } 00127 template <class T> 00128 void OPimCache<T>::setSize( int size ) { 00129 m_cache.setMaxCost( size ); 00130 } 00131 template <class T> 00132 T OPimCache<T>::find(int uid )const { 00133 Item *it = m_cache.find( uid ); 00134 if (it) 00135 return it->record(); 00136 return T(); 00137 } 00138 template <class T> 00139 void OPimCache<T>::add( const T& t ) { 00140 Item* it = 0l; 00141 it = m_cache.find(t.uid(), FALSE ); 00142 00143 if (it ) 00144 it->setRecord( t ); 00145 00146 it = new Item( t ); 00147 if (!m_cache.insert( t.uid(), it ) ) 00148 delete it; 00149 } 00150 template <class T> 00151 void OPimCache<T>::remove( int uid ) { 00152 m_cache.remove( uid ); 00153 } 00154 template <class T> 00155 void OPimCache<T>::replace( const T& t) { 00156 Item *it = m_cache.find( t.uid() ); 00157 if ( it ) { 00158 it->setRecord( t ); 00159 } 00160 } 00161 00162 } 00163 00164 #endif
1.4.2