4.2.7r1
[enigma2.git] / usr / include / enigma2 / lib / base / eptrlist.h
1 #ifndef _E_PTRLIST_
2 #define _E_PTRLIST_
3
4 #include <list>
5 #include <vector>
6 #include <algorithm>
7 #include <lib/base/smartptr.h>
8 #include <lib/base/eerror.h>
9
10 template <class T>
11 class ePtrList : public std::list<T*>
12 {
13 public:
14         typedef typename std::list<T*, std::allocator<T*> >::iterator std_list_T_iterator;  // to remove compiler warnings
15         typedef typename std::list<T*, std::allocator<T*> >::const_iterator std_list_T_const_iterator;
16         typedef typename std::list<T*, std::allocator<T*> >::reverse_iterator std_list_T_reverse_iterator;
17         typedef typename std::list<T*, std::allocator<T*> >::const_reverse_iterator std_list_T_const_reverse_iterator;
18         typedef typename ePtrList<T>::iterator T_iterator;
19         typedef typename ePtrList<T>::const_iterator T_const_iterator;
20         typedef typename ePtrList<T>::reverse_iterator T_reverse_iterator;
21         typedef typename ePtrList<T>::const_reverse_iterator T_const_reverse_iterator;
22
23 // Iterator classes
24         class iterator : public std::list<T*>::iterator
25         {
26         public:
27                 // Constructors
28                 iterator(const std_list_T_iterator& Q)          : std_list_T_iterator(Q)        {       }
29
30                 // changed operator for pointer
31                 T* operator->() const
32                 {
33                         return *std::list<T*>::iterator::operator->();
34                 }
35
36                 operator T&() const
37                 {
38                         return *operator->();
39                 }
40
41                 operator T*() const
42                 {
43                         return operator->();
44                 }
45
46                 iterator& operator++()
47                 {
48                         std::list<T*>::iterator::operator++();
49                         return *this;
50                 }
51
52                 iterator operator++(int)
53                 {
54                         return std::list<T*>::iterator::operator++(0);
55                 }
56
57                 iterator& operator--()
58                 {
59                         std::list<T*>::iterator::operator--();
60                         return *this;
61                 }
62
63                 iterator operator--(int)
64                 {
65                         return std::list<T*>::iterator::operator--(0);
66                 }
67         };
68
69         class const_iterator;
70         class reverse_iterator;
71         class const_reverse_iterator;
72
73 // Constructors
74         inline ePtrList();
75         inline ePtrList(const ePtrList&);
76         inline ~ePtrList();
77
78 // overwritted sort method
79         inline void sort();
80
81 // changed methods for autodelete and current implementation
82         inline void remove(T* t);
83         inline void clear();
84         inline void pop_back();
85         inline void pop_front();
86         inline void push_back(T*);
87         inline void push_front(T*);
88
89 // added methods for current implementation
90         inline T* take();
91         inline void take(T* t);
92         inline T* current();
93         inline T* next();
94         inline T* prev();
95         inline T* first();
96         inline T* last();
97         inline T* setCurrent(const T*);
98         inline const T* current() const;
99         inline const T* next() const;
100         inline const T* prev() const;
101         inline const T* first() const;
102         inline const T* last() const;
103
104 // added operator methods
105         inline operator bool() const;
106         inline bool operator!() const;
107
108 // added compare struct ... to sort
109         struct less;
110 private:
111         iterator cur;
112 public:
113         iterator begin()
114         {                               
115         //      makes implicit type conversion form std::list::iterator to ePtrList::iterator
116                 return std::list<T*>::begin();          
117         }
118
119         iterator end()
120         {                               
121         //      makes implicit type conversion form std::list::iterator to ePtrList::iterator
122                 return std::list<T*>::end();            
123         }
124
125         const_iterator begin() const
126         {                               
127         //      makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator
128                 return std::list<T*>::begin();          
129         }
130
131         const_iterator end() const
132         {                               
133         //      makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator
134                 return std::list<T*>::end();            
135         }
136
137         reverse_iterator rbegin()
138         {                               
139         //      makes implicit type conversion form std::list::reverse:_iterator to ePtrList::reverse_iterator
140                 return std::list<T*>::rbegin();         
141         }
142
143         reverse_iterator rend()
144         {                               
145         //      makes implicit type conversion form std::list::reverse_iterator to ePtrList::reverse_iterator
146                 return std::list<T*>::rend();           
147         }
148
149         const_reverse_iterator rbegin() const
150         {                               
151         //      makes implicit type conversion form std::list::const_reverse_iterator to ePtrList::const_reverse_iterator
152                 return std::list<T*>::rbegin();         
153         }
154
155         const_reverse_iterator rend() const
156         {                               
157         //      makes implicit type conversion form std::list::const_reverse_iterator to ePtrList::const_reverse_iterator
158                 return std::list<T*>::rend();           
159         }
160
161         iterator erase(iterator it)
162         {
163         //      Remove the item it, if auto-deletion is enabled, than the list call delete for this item
164         //  If current is equal to the item that was removed, current is set to the next item in the list
165                 if (cur == it)
166                         return cur = std::list<T*>::erase(it);
167                 else
168                         return std::list<T*>::erase(it);
169         }
170
171         iterator erase(iterator from, iterator to)
172         {
173         //      Remove all items between the to iterators from and to
174         //      If auto-deletion is enabled, than the list call delete for all removed items
175                 while (from != to)
176                         from = erase(from);
177         
178                 return from;
179         }
180
181         operator iterator()     
182         {
183         //      Returns a iterator that equal to begin() of the list
184                 return begin(); 
185         }
186
187         operator const_iterator() const
188         {
189         //      Returns a const_iterator that equal to begin() of the list
190                 return begin(); 
191         }
192
193         operator reverse_iterator()
194         {
195         //      Returns a reverse_iterator that equal to rbegin() of the list
196                 return rbegin();        
197         }
198
199         operator const_reverse_iterator() const 
200         {
201         //      Returns a const_reverse_iterator that equal to rbegin() of the list
202                 return rbegin();        
203         }
204
205         std::vector<T>* getVector()
206         {
207                 // Creates an vector and copys all elements to this vector
208                 // returns a pointer to this new vector ( the reserved memory must deletet from the receiver !! )
209                 std::vector<T>* v=new std::vector<T>();
210                 v->reserve( std::list<T>::size() );
211                 for ( std_list_T_iterator it( std::list<T*>::begin() ); it != std::list<T*>::end(); it++)
212                         v->push_back( **it );
213
214                 return v;
215         }
216
217         inline iterator insert_in_order( T* e )
218         {
219                 // added a new item to the list... in order
220                 // returns a iterator to the new item
221                 // this-> is required as of gcc 4.7, which would raise the following without it
222                 // "declarations in dependent base ‘std::list<eTimer*, std::allocator<eTimer*> >’ are not found by unqualified lookup"
223                 return this->insert( std::lower_bound( std::list<T*>::begin(), std::list<T*>::end(), e, less()), e );
224         }
225
226 };
227
228 /////////////////// const_iterator class /////////////////////////////
229 template <class T>
230 class ePtrList<T>::const_iterator : public std::list<T*>::const_iterator
231 {
232 public:
233         // Constructors
234         const_iterator(const std_list_T_const_iterator& Q)              :std_list_T_const_iterator(Q)   {       }
235
236         // changed operator for pointer
237         T* operator->() const
238         {
239                 return *std::list<T*>::const_iterator::operator->();
240         }
241
242         operator T&() const
243         {
244                 return *operator->();
245         }
246
247         operator T*() const
248         {
249                 return operator->();
250         }
251
252         const_iterator& operator++()
253         {
254                 std::list<T*>::const_iterator::operator++();
255                 return *this;
256         }
257
258         const_iterator operator++(int)
259         {
260                 return std::list<T*>::const_iterator::operator++(0);
261         }
262
263         const_iterator& operator--()
264         {
265                 std::list<T*>::const_iterator::operator--();
266                 return *this;
267         }
268
269         const_iterator operator--(int)
270         {
271                 return std::list<T*>::const_iterator::operator--(0);
272         }
273 };
274
275 /////////////////// reverse_iterator class /////////////////////////////
276 template <class T>
277 class ePtrList<T>::reverse_iterator : public std::list<T*>::reverse_iterator
278 {
279 public:
280         // Constructors
281         reverse_iterator(const std_list_T_reverse_iterator& Q)          :std_list_T_reverse_iterator(Q) {       }
282
283         // changed operators for pointer
284         T* operator->() const
285         {
286                 return *std::list<T*>::reverse_iterator::operator->();
287         }
288
289         operator T&() const
290         {
291                 return *operator->();
292         }
293
294         operator T*() const
295         {
296                 return operator->();
297         }
298
299         reverse_iterator& operator++()
300         {
301                 std::list<T*>::reverse_iterator::operator++();
302                 return *this;
303         }
304
305         reverse_iterator operator++(int)
306         {
307                 return std::list<T*>::reverse_iterator::operator++(0);
308         }
309
310         reverse_iterator& operator--()
311         {
312                 std::list<T*>::reverse_iterator::operator--();
313                 return *this;
314         }
315
316         reverse_iterator operator--(int)
317         {
318                 return std::list<T*>::reverse_iterator::operator--(0);
319         }
320 };
321
322 /////////////////// const_reverse_iterator class /////////////////////////////
323 template <class T>
324 class ePtrList<T>::const_reverse_iterator : public std::list<T*>::const_reverse_iterator
325 {
326 public:
327         // Constructors
328         const_reverse_iterator(const std_list_T_const_reverse_iterator& Q)              :std_list_T_const_reverse_iterator(Q)   {       }
329
330         // changed operators for pointer
331         T* operator->() const
332         {
333                 return *std::list<T*>::const_reverse_iterator::operator->();
334         }
335
336         operator T&() const
337         {
338                 return *operator->();
339         }
340
341         operator T*() const
342         {
343                 return operator->();
344         }
345
346         const_reverse_iterator& operator++()
347         {
348                 std::list<T*>::const_reverse_iterator::operator++();
349                 return *this;
350         }
351
352         const_reverse_iterator operator++(int)
353         {
354                 return std::list<T*>::const_reverse_iterator::operator++(0);
355         }
356
357         const_reverse_iterator& operator--()
358         {
359                 std::list<T*>::const_reverse_iterator::operator--();
360                 return *this;
361         }
362
363         const_reverse_iterator operator--(int)
364         {
365                 return std::list<T*>::const_reverse_iterator::operator--(0);
366         }
367 };
368
369 /////////////////// Default Constructor /////////////////////////////
370 template <class T>
371 ePtrList<T>::ePtrList()
372     :cur(std::list<T*>::begin())
373 {               
374
375 }
376
377 /////////////////// Copy Constructor /////////////////////////////
378 template <class T>
379 ePtrList<T>::ePtrList(const ePtrList& e)
380         :std::list<T*>(e), cur(e.cur)
381 {               
382 }
383
384 /////////////////// ePtrList Destructor /////////////////////////////
385 template <class T>
386 inline ePtrList<T>::~ePtrList()
387 {
388 }
389
390 /////////////////// ePtrList sort() /////////////////////////
391 template <class T>
392 inline void ePtrList<T>::sort()
393 {               
394 //      Sorts all items in the list.
395 //      The type T must have a operator <.
396         std::list<T*>::sort(typename ePtrList<T>::less());
397 }       
398
399 /////////////////// ePtrList remove(T*) /////////////////////////