From ea3945a9bd8f9830f70b1efa133f9df13b19362f Mon Sep 17 00:00:00 2001 From: mickeyl Date: Tue, 16 Nov 2004 19:14:18 +0000 Subject: libopie1 goes into unsupported --- (limited to 'noncore/unsupported/libopie/pim/opimcache.h') diff --git a/noncore/unsupported/libopie/pim/opimcache.h b/noncore/unsupported/libopie/pim/opimcache.h new file mode 100644 index 0000000..7f7cff5 --- a/dev/null +++ b/noncore/unsupported/libopie/pim/opimcache.h @@ -0,0 +1,131 @@ +#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 -- cgit v0.9.0.2