summaryrefslogtreecommitdiff
path: root/noncore/unsupported/libopie/pim/orecordlist.h
Unidiff
Diffstat (limited to 'noncore/unsupported/libopie/pim/orecordlist.h') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/unsupported/libopie/pim/orecordlist.h306
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
10class 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 */
18template <class T> class ORecordList;
19template <class T = OPimRecord>
20class ORecordListIterator {
21 friend class ORecordList<T>;
22public:
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
66private:
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
78class ORecordListPrivate;
79/**
80 * The recordlist used as a return type
81 * from OPimAccessTemplate
82 */
83template <class T = OPimRecord >
84class ORecordList {
85public:
86 typedef OTemplateBase<T> Base;
87 typedef ORecordListIterator<T> Iterator;
88
89 /**
90 * c'tor
91 */
92 ORecordList () {
93 }
94ORecordList( 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 */
125private:
126 QArray<int> m_ids;
127 const Base* m_acc;
128 ORecordListPrivate *d;
129};
130
131/* ok now implement it */
132template <class T>
133ORecordListIterator<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}
141template <class T>
142ORecordListIterator<T>::~ORecordListIterator() {
143/* nothing to delete */
144}
145
146template <class T>
147ORecordListIterator<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
157template <class T>
158ORecordListIterator<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
168template <class T>
169T 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
181template <class T>
182ORecordListIterator<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}
192template <class T>
193ORecordListIterator<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
204template <class T>
205bool 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}
216template <class T>
217bool ORecordListIterator<T>::operator!=( const ORecordListIterator<T>& it ) {
218 return !(*this == it );
219}
220template <class T>
221ORecordListIterator<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}
230template <class T>
231uint ORecordListIterator<T>::current()const {
232 return m_current;
233}
234template <class T>
235void ORecordListIterator<T>::setCurrent( uint cur ) {
236 if( cur < m_uids.count() ) {
237 m_end = false;
238 m_current= cur;
239 }
240}
241template <class T>
242uint ORecordListIterator<T>::count()const {
243 return m_uids.count();
244}
245template <class T>
246ORecordList<T>::ORecordList( const QArray<int>& ids,
247 const Base* acc )
248 : m_ids( ids ), m_acc( acc )
249{
250}
251template <class T>
252ORecordList<T>::~ORecordList() {
253/* nothing to do here */
254}
255template <class T>
256typename ORecordList<T>::Iterator ORecordList<T>::begin() {
257 Iterator it( m_ids, m_acc );
258 return it;
259}
260template <class T>
261typename 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}
268template <class T>
269uint ORecordList<T>::count()const {
270return m_ids.count();
271}
272template <class T>
273T 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}
279template <class T>
280int ORecordList<T>::uidAt( uint i ) {
281 return m_ids[i];
282}
283
284template <class T>
285bool 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