summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/core/opimcache.h
Unidiff
Diffstat (limited to 'libopie2/opiepim/core/opimcache.h') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/core/opimcache.h117
1 files changed, 117 insertions, 0 deletions
diff --git a/libopie2/opiepim/core/opimcache.h b/libopie2/opiepim/core/opimcache.h
new file mode 100644
index 0000000..067f6e7
--- a/dev/null
+++ b/libopie2/opiepim/core/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
8template <class T = OPimRecord>
9class OPimCacheItem {
10public:
11 OPimCacheItem( const T& t = T() );
12 ~OPimCacheItem();
13
14 T record()const;
15 void setRecord( const T& );
16private:
17 T m_t;
18};
19
20/**
21 * OPimCache for caching the items
22 * We support adding, removing
23 * and finding
24 */
25template <class T = OPimRecord>
26class OPimCache {
27public:
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
41private:
42 QIntCache<Item> m_cache;
43};
44
45// Implementation
46template <class T>
47OPimCacheItem<T>::OPimCacheItem( const T& t )
48 : m_t(t) {
49}
50template <class T>
51OPimCacheItem<T>::~OPimCacheItem() {
52
53}
54template <class T>
55T OPimCacheItem<T>::record()const {
56 return m_t;
57}
58template <class T>
59void OPimCacheItem<T>::setRecord( const T& t ) {
60 m_t = t;
61}
62// Cache
63template <class T>
64OPimCache<T>::OPimCache() {
65 m_cache.setAutoDelete( TRUE );
66}
67template <class T>
68OPimCache<T>::~OPimCache() {
69
70}
71template <class T>
72bool OPimCache<T>::contains(int uid )const {
73 Item* it = m_cache.find( uid, FALSE );
74 if (!it)
75 return false;
76 return true;
77}
78template <class T>
79void OPimCache<T>::invalidate() {
80 m_cache.clear();
81}
82template <class T>
83void OPimCache<T>::setSize( int size ) {
84 m_cache.setMaxCost( size );
85}
86template <class T>
87T OPimCache<T>::find(int uid )const {
88 Item *it = m_cache.find( uid );
89 if (it)
90 return it->record();
91 return T();
92}
93template <class T>
94void 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}
105template <class T>
106void OPimCache<T>::remove( int uid ) {
107 m_cache.remove( uid );
108}
109template <class T>
110void 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