summaryrefslogtreecommitdiff
path: root/noncore/unsupported/libopie/pim/opimcache.h
Unidiff
Diffstat (limited to 'noncore/unsupported/libopie/pim/opimcache.h') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/unsupported/libopie/pim/opimcache.h131
1 files changed, 131 insertions, 0 deletions
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 @@
1#ifndef OPIE_PIM_CACHE_H
2#define OPIE_PIM_CACHE_H
3
4#include <qintcache.h>
5
6#include "opimrecord.h"
7
8class OPimCacheItemPrivate;
9
10template <class T = OPimRecord>
11class OPimCacheItem {
12public:
13 OPimCacheItem( const T& t = T() );
14 OPimCacheItem( const OPimCacheItem& );
15 ~OPimCacheItem();
16
17 OPimCacheItem &operator=( const OPimCacheItem& );
18
19 T record()const;
20 void setRecord( const T& );
21private:
22 T m_t;
23 OPimCacheItemPrivate *d;
24};
25
26
27class OPimCachePrivate;
28/**
29 * OPimCache for caching the items
30 * We support adding, removing
31 * and finding
32 */
33template <class T = OPimRecord>
34class OPimCache {
35public:
36 typedef OPimCacheItem<T> Item;
37 OPimCache();
38 OPimCache( const OPimCache& );
39 ~OPimCache();
40
41 OPimCache &operator=( const OPimCache& );
42
43 bool contains(int uid)const;
44 void invalidate();
45 void setSize( int size );
46
47 T find(int uid )const;
48 void add( const T& );
49 void remove( int uid );
50 void replace( const T& );
51
52private:
53 QIntCache<Item> m_cache;
54 OPimCachePrivate* d;
55};
56
57// Implementation
58template <class T>
59OPimCacheItem<T>::OPimCacheItem( const T& t )
60 : m_t(t) {
61}
62template <class T>
63OPimCacheItem<T>::~OPimCacheItem() {
64
65}
66template <class T>
67T OPimCacheItem<T>::record()const {
68 return m_t;
69}
70template <class T>
71void OPimCacheItem<T>::setRecord( const T& t ) {
72 m_t = t;
73}
74// Cache
75template <class T>
76OPimCache<T>::OPimCache()
77 : m_cache(100, 53 )
78{
79 m_cache.setAutoDelete( TRUE );
80}
81template <class T>
82OPimCache<T>::~OPimCache() {
83
84}
85template <class T>
86bool OPimCache<T>::contains(int uid )const {
87 Item* it = m_cache.find( uid, FALSE );
88 if (!it)
89 return false;
90 return true;
91}
92template <class T>
93void OPimCache<T>::invalidate() {
94 m_cache.clear();
95}
96template <class T>
97void OPimCache<T>::setSize( int size ) {
98 m_cache.setMaxCost( size );
99}
100template <class T>
101T OPimCache<T>::find(int uid )const {
102 Item *it = m_cache.find( uid );
103 if (it)
104 return it->record();
105 return T();
106}
107template <class T>
108void OPimCache<T>::add( const T& t ) {
109 Item* it = 0l;
110 it = m_cache.find(t.uid(), FALSE );
111
112 if (it )
113 it->setRecord( t );
114
115 it = new Item( t );
116 if (!m_cache.insert( t.uid(), it ) )
117 delete it;
118}
119template <class T>
120void OPimCache<T>::remove( int uid ) {
121 m_cache.remove( uid );
122}
123template <class T>
124void OPimCache<T>::replace( const T& t) {
125 Item *it = m_cache.find( t.uid() );
126 if ( it ) {
127 it->setRecord( t );
128 }
129}
130
131#endif