author | mickeyl <mickeyl> | 2004-11-16 19:14:18 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2004-11-16 19:14:18 (UTC) |
commit | ea3945a9bd8f9830f70b1efa133f9df13b19362f (patch) (unidiff) | |
tree | f2ea22cc50e9aa8aa73ee7dea148f41c563c9666 /noncore/unsupported/libopie/pim/orecordlist.h | |
parent | 1c6f490e8541626f68422e0a3a7c7281d7f5b7d3 (diff) | |
download | opie-ea3945a9bd8f9830f70b1efa133f9df13b19362f.zip opie-ea3945a9bd8f9830f70b1efa133f9df13b19362f.tar.gz opie-ea3945a9bd8f9830f70b1efa133f9df13b19362f.tar.bz2 |
libopie1 goes into unsupported
Diffstat (limited to 'noncore/unsupported/libopie/pim/orecordlist.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/unsupported/libopie/pim/orecordlist.h | 306 |
1 files changed, 306 insertions, 0 deletions
diff --git a/noncore/unsupported/libopie/pim/orecordlist.h b/noncore/unsupported/libopie/pim/orecordlist.h new file mode 100644 index 0000000..5211f57 --- a/dev/null +++ b/noncore/unsupported/libopie/pim/orecordlist.h | |||
@@ -0,0 +1,306 @@ | |||
1 | |||
2 | #ifndef OPIE_RECORD_LIST_H | ||
3 | #define OPIE_RECORD_LIST_H | ||
4 | |||
5 | #include <qarray.h> | ||
6 | |||
7 | #include "otemplatebase.h" | ||
8 | #include "opimrecord.h" | ||
9 | |||
10 | class ORecordListIteratorPrivate; | ||
11 | /** | ||
12 | * Our List Iterator | ||
13 | * it behaves like STL or Qt | ||
14 | * | ||
15 | * for(it = list.begin(); it != list.end(); ++it ) | ||
16 | * doSomeCoolStuff( (*it) ); | ||
17 | */ | ||
18 | template <class T> class ORecordList; | ||
19 | template <class T = OPimRecord> | ||
20 | class ORecordListIterator { | ||
21 | friend class ORecordList<T>; | ||
22 | public: | ||
23 | typedef OTemplateBase<T> Base; | ||
24 | |||
25 | /** | ||
26 | * The c'tor used internally from | ||
27 | * ORecordList | ||
28 | */ | ||
29 | ORecordListIterator( const QArray<int>, const Base* ); | ||
30 | |||
31 | /** | ||
32 | * The standard c'tor | ||
33 | */ | ||
34 | ORecordListIterator(); | ||
35 | ~ORecordListIterator(); | ||
36 | |||
37 | ORecordListIterator( const ORecordListIterator& ); | ||
38 | ORecordListIterator &operator=(const ORecordListIterator& ); | ||
39 | |||
40 | /** | ||
41 | * a * operator ;) | ||
42 | * use it like this T = (*it); | ||
43 | */ | ||
44 | T operator*(); | ||
45 | ORecordListIterator &operator++(); | ||
46 | ORecordListIterator &operator--(); | ||
47 | |||
48 | bool operator==( const ORecordListIterator& it ); | ||
49 | bool operator!=( const ORecordListIterator& it ); | ||
50 | |||
51 | /** | ||
52 | * the current item | ||
53 | */ | ||
54 | uint current()const; | ||
55 | |||
56 | /** | ||
57 | * the number of items | ||
58 | */ | ||
59 | uint count()const; | ||
60 | |||
61 | /** | ||
62 | * sets the current item | ||
63 | */ | ||
64 | void setCurrent( uint cur ); | ||
65 | |||
66 | private: | ||
67 | QArray<int> m_uids; | ||
68 | uint m_current; | ||
69 | const Base* m_temp; | ||
70 | bool m_end : 1; | ||
71 | T m_record; | ||
72 | bool m_direction :1; | ||
73 | |||
74 | /* d pointer for future versions */ | ||
75 | ORecordListIteratorPrivate *d; | ||
76 | }; | ||
77 | |||
78 | class ORecordListPrivate; | ||
79 | /** | ||
80 | * The recordlist used as a return type | ||
81 | * from OPimAccessTemplate | ||
82 | */ | ||
83 | template <class T = OPimRecord > | ||
84 | class ORecordList { | ||
85 | public: | ||
86 | typedef OTemplateBase<T> Base; | ||
87 | typedef ORecordListIterator<T> Iterator; | ||
88 | |||
89 | /** | ||
90 | * c'tor | ||
91 | */ | ||
92 | ORecordList () { | ||
93 | } | ||
94 | ORecordList( const QArray<int>& ids, | ||
95 | const Base* ); | ||
96 | ~ORecordList(); | ||
97 | |||
98 | /** | ||
99 | * the first iterator | ||
100 | */ | ||
101 | Iterator begin(); | ||
102 | |||
103 | /** | ||
104 | * the end | ||
105 | */ | ||
106 | Iterator end(); | ||
107 | |||
108 | /** | ||
109 | * the number of items in the list | ||
110 | */ | ||
111 | uint count()const; | ||
112 | |||
113 | T operator[]( uint i ); | ||
114 | int uidAt(uint i ); | ||
115 | |||
116 | /** | ||
117 | * Remove the contact with given uid | ||
118 | */ | ||
119 | bool remove( int uid ); | ||
120 | |||
121 | /* | ||
122 | ConstIterator begin()const; | ||
123 | ConstIterator end()const; | ||
124 | */ | ||
125 | private: | ||
126 | QArray<int> m_ids; | ||
127 | const Base* m_acc; | ||
128 | ORecordListPrivate *d; | ||
129 | }; | ||
130 | |||
131 | /* ok now implement it */ | ||
132 | template <class T> | ||
133 | ORecordListIterator<T>::ORecordListIterator() { | ||
134 | m_current = 0; | ||
135 | m_temp = 0l; | ||
136 | m_end = true; | ||
137 | m_record = T(); | ||
138 | /* forward */ | ||
139 | m_direction = TRUE; | ||
140 | } | ||
141 | template <class T> | ||
142 | ORecordListIterator<T>::~ORecordListIterator() { | ||
143 | /* nothing to delete */ | ||
144 | } | ||
145 | |||
146 | template <class T> | ||
147 | ORecordListIterator<T>::ORecordListIterator( const ORecordListIterator<T>& it) { | ||
148 | // qWarning("ORecordListIterator copy c'tor"); | ||
149 | m_uids = it.m_uids; | ||
150 | m_current = it.m_current; | ||
151 | m_temp = it.m_temp; | ||
152 | m_end = it.m_end; | ||
153 | m_record = it.m_record; | ||
154 | m_direction = it.m_direction; | ||
155 | } | ||
156 | |||
157 | template <class T> | ||
158 | ORecordListIterator<T> &ORecordListIterator<T>::operator=( const ORecordListIterator<T>& it) { | ||
159 | m_uids = it.m_uids; | ||
160 | m_current = it.m_current; | ||
161 | m_temp = it.m_temp; | ||
162 | m_end = it.m_end; | ||
163 | m_record = it.m_record; | ||
164 | |||
165 | return *this; | ||
166 | } | ||
167 | |||
168 | template <class T> | ||
169 | T ORecordListIterator<T>::operator*() { | ||
170 | //qWarning("operator* %d %d", m_current, m_uids[m_current] ); | ||
171 | if (!m_end ) | ||
172 | m_record = m_temp->find( m_uids[m_current], m_uids, m_current, | ||
173 | m_direction ? Base::Forward : | ||
174 | Base::Reverse ); | ||
175 | else | ||
176 | m_record = T(); | ||
177 | |||
178 | return m_record; | ||
179 | } | ||
180 | |||
181 | template <class T> | ||
182 | ORecordListIterator<T> &ORecordListIterator<T>::operator++() { | ||
183 | m_direction = true; | ||
184 | if (m_current < m_uids.count() ) { | ||
185 | m_end = false; | ||
186 | ++m_current; | ||
187 | }else | ||
188 | m_end = true; | ||
189 | |||
190 | return *this; | ||
191 | } | ||
192 | template <class T> | ||
193 | ORecordListIterator<T> &ORecordListIterator<T>::operator--() { | ||
194 | m_direction = false; | ||
195 | if ( m_current > 0 ) { | ||
196 | --m_current; | ||
197 | m_end = false; | ||
198 | } else | ||
199 | m_end = true; | ||
200 | |||
201 | return *this; | ||
202 | } | ||
203 | |||
204 | template <class T> | ||
205 | bool ORecordListIterator<T>::operator==( const ORecordListIterator<T>& it ) { | ||
206 | |||
207 | /* if both are at we're the same.... */ | ||
208 | if ( m_end == it.m_end ) return true; | ||
209 | |||
210 | if ( m_uids != it.m_uids ) return false; | ||
211 | if ( m_current != it.m_current ) return false; | ||
212 | if ( m_temp != it.m_temp ) return false; | ||
213 | |||
214 | return true; | ||
215 | } | ||
216 | template <class T> | ||
217 | bool ORecordListIterator<T>::operator!=( const ORecordListIterator<T>& it ) { | ||
218 | return !(*this == it ); | ||
219 | } | ||
220 | template <class T> | ||
221 | ORecordListIterator<T>::ORecordListIterator( const QArray<int> uids, | ||
222 | const Base* t ) | ||
223 | : m_uids( uids ), m_current( 0 ), m_temp( t ), m_end( false ), | ||
224 | m_direction( false ) | ||
225 | { | ||
226 | /* if the list is empty we're already at the end of the list */ | ||
227 | if (uids.count() == 0 ) | ||
228 | m_end = true; | ||
229 | } | ||
230 | template <class T> | ||
231 | uint ORecordListIterator<T>::current()const { | ||
232 | return m_current; | ||
233 | } | ||
234 | template <class T> | ||
235 | void ORecordListIterator<T>::setCurrent( uint cur ) { | ||
236 | if( cur < m_uids.count() ) { | ||
237 | m_end = false; | ||
238 | m_current= cur; | ||
239 | } | ||
240 | } | ||
241 | template <class T> | ||
242 | uint ORecordListIterator<T>::count()const { | ||
243 | return m_uids.count(); | ||
244 | } | ||
245 | template <class T> | ||
246 | ORecordList<T>::ORecordList( const QArray<int>& ids, | ||
247 | const Base* acc ) | ||
248 | : m_ids( ids ), m_acc( acc ) | ||
249 | { | ||
250 | } | ||
251 | template <class T> | ||
252 | ORecordList<T>::~ORecordList() { | ||
253 | /* nothing to do here */ | ||
254 | } | ||
255 | template <class T> | ||
256 | typename ORecordList<T>::Iterator ORecordList<T>::begin() { | ||
257 | Iterator it( m_ids, m_acc ); | ||
258 | return it; | ||
259 | } | ||
260 | template <class T> | ||
261 | typename ORecordList<T>::Iterator ORecordList<T>::end() { | ||
262 | Iterator it( m_ids, m_acc ); | ||
263 | it.m_end = true; | ||
264 | it.m_current = m_ids.count(); | ||
265 | |||
266 | return it; | ||
267 | } | ||
268 | template <class T> | ||
269 | uint ORecordList<T>::count()const { | ||
270 | return m_ids.count(); | ||
271 | } | ||
272 | template <class T> | ||
273 | T ORecordList<T>::operator[]( uint i ) { | ||
274 | if ( i >= m_ids.count() ) | ||
275 | return T(); | ||
276 | /* forward */ | ||
277 | return m_acc->find( m_ids[i], m_ids, i ); | ||
278 | } | ||
279 | template <class T> | ||
280 | int ORecordList<T>::uidAt( uint i ) { | ||
281 | return m_ids[i]; | ||
282 | } | ||
283 | |||
284 | template <class T> | ||
285 | bool ORecordList<T>::remove( int uid ) { | ||
286 | QArray<int> copy( m_ids.count() ); | ||
287 | int counter = 0; | ||
288 | bool ret_val = false; | ||
289 | |||
290 | for (uint i = 0; i < m_ids.count(); i++){ | ||
291 | if ( m_ids[i] != uid ){ | ||
292 | copy[counter++] = m_ids[i]; | ||
293 | |||
294 | }else | ||
295 | ret_val = true; | ||
296 | } | ||
297 | |||
298 | copy.resize( counter ); | ||
299 | m_ids = copy; | ||
300 | |||
301 | |||
302 | return ret_val; | ||
303 | } | ||
304 | |||
305 | |||
306 | #endif | ||