#ifndef OPIE_PIM_CACHE_H #define OPIE_PIM_CACHE_H #include #include "opimrecord.h" class OPimCacheItemPrivate; template class OPimCacheItem { public: OPimCacheItem( const T& t = T() ); OPimCacheItem( const OPimCacheItem& ); ~OPimCacheItem(); OPimCacheItem &operator=( const OPimCacheItem& ); T record()const; void setRecord( const T& ); private: T m_t; OPimCacheItemPrivate *d; }; class OPimCachePrivate; /** * OPimCache for caching the items * We support adding, removing * and finding */ template class OPimCache { public: typedef OPimCacheItem Item; OPimCache(); OPimCache( const OPimCache& ); ~OPimCache(); OPimCache &operator=( const OPimCache& ); bool contains(int uid)const; void invalidate(); void setSize( int size ); T find(int uid )const; void add( const T& ); void remove( int uid ); void replace( const T& ); private: QIntCache m_cache; OPimCachePrivate* d; }; // Implementation template OPimCacheItem::OPimCacheItem( const T& t ) : m_t(t) { } template OPimCacheItem::~OPimCacheItem() { } template T OPimCacheItem::record()const { return m_t; } template void OPimCacheItem::setRecord( const T& t ) { m_t = t; } // Cache template OPimCache::OPimCache() : m_cache(100, 53 ) { m_cache.setAutoDelete( TRUE ); } template OPimCache::~OPimCache() { } template bool OPimCache::contains(int uid )const { Item* it = m_cache.find( uid, FALSE ); if (!it) return false; return true; } template void OPimCache::invalidate() { m_cache.clear(); } template void OPimCache::setSize( int size ) { m_cache.setMaxCost( size ); } template T OPimCache::find(int uid )const { Item *it = m_cache.find( uid ); if (it) return it->record(); return T(); } template void OPimCache::add( const T& t ) { Item* it = 0l; it = m_cache.find(t.uid(), FALSE ); if (it ) it->setRecord( t ); it = new Item( t ); if (!m_cache.insert( t.uid(), it ) ) delete it; } template void OPimCache::remove( int uid ) { m_cache.remove( uid ); } template void OPimCache::replace( const T& t) { Item *it = m_cache.find( t.uid() ); if ( it ) { it->setRecord( t ); } } #endif