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