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