/* This file is part of the Opie Project Copyright (C) The Main Author =. Copyright (C) The Opie Team .=l. .>+-= _;:, .> :=|. This program is free software; you can .> <`_, > . <= redistribute it and/or modify it under :`=1 )Y*s>-.-- : the terms of the GNU Library General Public .="- .-=="i, .._ License as published by the Free Software - . .-<_> .<> Foundation; either version 2 of the License, ._= =} : or (at your option) any later version. .%`+i> _;_. .i_,=:_. -`: PARTICULAR PURPOSE. See the GNU ..}^=.= = ; Library General Public License for more ++= -. .` .: details. : = ...= . :.=- -. .:....=;==+<; You should have received a copy of the GNU -_. . . )=. = Library General Public License along with -- :-=` this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef OPIMCACHE_H #define OPIMCACHE_H /* OPIE */ #include /* QT */ #include namespace Opie { 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