- eConnections holds reference to object
[enigma2.git] / lib / base / eptrlist.h
1 #ifndef _E_PTRLIST_
2 #define _E_PTRLIST_
3
4 #include <list>
5 #include <vector>
6 #include <lib/base/smartptr.h>
7
8 template <class T>
9 class ePtrList : public std::list<T*>
10 {
11 public:
12         typedef typename std::list<T*, std::allocator<T*> >::iterator std_list_T_iterator;  // to remove compiler warnings
13         typedef typename std::list<T*, std::allocator<T*> >::const_iterator std_list_T_const_iterator;
14         typedef typename std::list<T*, std::allocator<T*> >::reverse_iterator std_list_T_reverse_iterator;
15         typedef typename std::list<T*, std::allocator<T*> >::const_reverse_iterator std_list_T_const_reverse_iterator;
16         typedef typename ePtrList<T>::iterator T_iterator;
17         typedef typename ePtrList<T>::const_iterator T_const_iterator;
18         typedef typename ePtrList<T>::reverse_iterator T_reverse_iterator;
19         typedef typename ePtrList<T>::const_reverse_iterator T_const_reverse_iterator;
20
21 // Iterator classes
22         class iterator;
23         class const_iterator;
24         class reverse_iterator;
25         class const_reverse_iterator;
26
27 // Constructors
28         inline ePtrList();
29         inline ePtrList(const ePtrList&);
30         inline ~ePtrList();
31
32 // overwritted sort method
33         inline void sort();
34
35 // changed methods for autodelete and current implementation
36         inline void remove(T* t);
37         inline void clear();
38         inline void pop_back();
39         inline void pop_front();
40         inline void push_back(T*);
41         inline void push_front(T*);
42
43 // added methods for current implementation
44         inline T* take();
45         inline void take(T* t);
46         inline T* current();
47         inline T* next();
48         inline T* prev();
49         inline T* first();
50         inline T* last();
51         inline T* setCurrent(const T*);
52         inline const T* current() const;
53         inline const T* next() const;
54         inline const T* prev() const;
55         inline const T* first() const;
56         inline const T* last() const;
57
58 // added operator methods
59         inline operator bool();
60         inline bool operator!();
61
62 // added methods for autodelete implementation
63         inline void setAutoDelete(bool b);
64         inline bool isAutoDelete();
65
66 // added compare struct ... to sort
67         struct less;
68 private:
69         iterator cur;
70         bool autoDelete;
71
72 public:
73         iterator ePtrList<T>::begin()
74         {                               
75         //      makes implicit type conversion form std::list::iterator to ePtrList::iterator
76                 return std::list<T*>::begin();          
77         }
78
79         iterator ePtrList<T>::end()
80         {                               
81         //      makes implicit type conversion form std::list::iterator to ePtrList::iterator
82                 return std::list<T*>::end();            
83         }
84
85         const_iterator ePtrList<T>::begin() const
86         {                               
87         //      makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator
88                 return std::list<T*>::begin();          
89         }
90
91         const_iterator ePtrList<T>::end() const
92         {                               
93         //      makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator
94                 return std::list<T*>::end();            
95         }
96
97         reverse_iterator ePtrList<T>::rbegin()
98         {                               
99         //      makes implicit type conversion form std::list::reverse:_iterator to ePtrList::reverse_iterator
100                 return std::list<T*>::rbegin();         
101         }
102
103         reverse_iterator ePtrList<T>::rend()
104         {                               
105         //      makes implicit type conversion form std::list::reverse_iterator to ePtrList::reverse_iterator
106                 return std::list<T*>::rend();           
107         }
108
109         const_reverse_iterator ePtrList<T>::rbegin() const
110         {                               
111         //      makes implicit type conversion form std::list::const_reverse_iterator to ePtrList::const_reverse_iterator
112                 return std::list<T*>::rbegin();         
113         }
114
115         const_reverse_iterator ePtrList<T>::rend() const
116         {                               
117         //      makes implicit type conversion form std::list::const_reverse_iterator to ePtrList::const_reverse_iterator
118                 return std::list<T*>::rend();           
119         }
120
121         iterator ePtrList<T>::erase(iterator it)
122         {
123         //      Remove the item it, if auto-deletion is enabled, than the list call delete for this item
124         //  If current is equal to the item that was removed, current is set to the next item in the list
125                 if (autoDelete && *it)
126                         delete *it;                                     
127
128                 if (cur == it)
129                         return cur = std::list<T*>::erase(it);
130                 else
131                         return std::list<T*>::erase(it);
132         }
133
134         iterator ePtrList<T>::erase(iterator from, iterator to)
135         {
136         //      Remove all items between the to iterators from and to
137         //      If auto-deletion is enabled, than the list call delete for all removed items
138                 while (from != to)
139                         from = erase(from);
140         
141                 return from;
142         }
143
144         operator iterator()     
145         {
146         //      Returns a iterator that equal to begin() of the list
147                 return begin(); 
148         }
149
150         operator const_iterator() const
151         {
152         //      Returns a const_iterator that equal to begin() of the list
153                 return begin(); 
154         }
155
156         operator reverse_iterator()
157         {
158         //      Returns a reverse_iterator that equal to rbegin() of the list
159                 return rbegin();        
160         }
161
162         operator const_reverse_iterator() const 
163         {
164         //      Returns a const_reverse_iterator that equal to rbegin() of the list
165                 return rbegin();        
166         }
167
168         std::vector<T>* getVector()
169         {
170                 // Creates an vector and copys all elements to this vector
171                 // returns a pointer to this new vector ( the reserved memory must deletet from the receiver !! )
172                 std::vector<T>* v=new std::vector<T>();
173                 v->reserve( size() );
174     for ( std_list_T_iterator it( std::list<T*>::begin() ); it != std::list<T*>::end(); it++)
175                         v->push_back( **it );
176
177                 return v;
178         }
179
180         inline iterator insert_in_order( T* e )
181         {
182                 // added a new item to the list... in order
183                 // returns a iterator to the new item
184                 return insert( std::lower_bound( std::list<T*>::begin(), std::list<T*>::end(), e ), e );
185         }
186
187 };
188
189 /////////////////// iterator class /////////////////////////////
190 template <class T>
191 class ePtrList<T>::iterator : public std::list<T*>::iterator
192 {
193 public:
194         // Constructors
195         iterator(const std_list_T_iterator& Q)          : std_list_T_iterator(Q)        {       }
196
197         // changed operator for pointer
198         T* operator->() const
199         {
200                 return *std::list<T*>::iterator::operator->();
201         }
202
203         operator T&() const
204         {
205                 return *operator->();
206         }
207         
208         operator T*() const
209         {
210                 return operator->();
211         }
212
213         iterator& operator++()
214         {
215                 std::list<T*>::iterator::operator++();
216                 return *this;
217         }
218
219         iterator operator++(int)
220         {
221                 return std::list<T*>::iterator::operator++(0);
222         }
223
224         iterator& operator--()
225         {
226                 std::list<T*>::iterator::operator--();
227                 return *this;
228         }
229
230         iterator operator--(int)
231         {
232                 return std::list<T*>::iterator::operator--(0);
233         }
234 };
235
236 /////////////////// const_iterator class /////////////////////////////
237 template <class T>
238 class ePtrList<T>::const_iterator : public std::list<T*>::const_iterator
239 {
240 public:
241         // Constructors
242         const_iterator(const std_list_T_const_iterator& Q)              :std_list_T_const_iterator(Q)   {       }
243
244         // changed operator for pointer
245         T* operator->() const
246         {
247                 return *std::list<T*>::const_iterator::operator->();
248         }
249
250         operator T&() const
251         {
252                 return *operator->();
253         }
254
255         operator T*() const
256         {
257                 return operator->();
258         }
259
260         const_iterator& operator++()
261         {
262                 std::list<T*>::const_iterator::operator++();
263                 return *this;
264         }
265
266         const_iterator operator++(int)
267         {
268                 return std::list<T*>::const_iterator::operator++(0);
269         }
270
271         const_iterator& operator--()
272         {
273                 std::list<T*>::const_iterator::operator--();
274                 return *this;
275         }
276
277         const_iterator operator--(int)
278         {
279                 return std::list<T*>::const_iterator::operator--(0);
280         }
281 };
282
283 /////////////////// reverse_iterator class /////////////////////////////
284 template <class T>
285 class ePtrList<T>::reverse_iterator : public std::list<T*>::reverse_iterator
286 {
287 public:
288         // Constructors
289         reverse_iterator(const std_list_T_reverse_iterator& Q)          :std_list_T_reverse_iterator(Q) {       }
290
291         // changed operators for pointer
292         T* operator->() const
293         {
294                 return *std::list<T*>::reverse_iterator::operator->();
295         }
296
297         operator T&() const
298         {
299                 return *operator->();
300         }
301
302         operator T*() const
303         {
304                 return operator->();
305         }
306
307         reverse_iterator& operator++()
308         {
309                 std::list<T*>::reverse_iterator::operator++();
310                 return *this;
311         }
312
313         reverse_iterator operator++(int)
314         {
315                 return std::list<T*>::reverse_iterator::operator++(0);
316         }
317
318         reverse_iterator& operator--()
319         {
320                 std::list<T*>::reverse_iterator::operator--();
321                 return *this;
322         }
323
324         reverse_iterator operator--(int)
325         {
326                 return std::list<T*>::reverse_iterator::operator--(0);
327         }
328 };
329
330 /////////////////// const_reverse_iterator class /////////////////////////////
331 template <class T>
332 class ePtrList<T>::const_reverse_iterator : public std::list<T*>::const_reverse_iterator
333 {
334 public:
335         // Constructors
336         const_reverse_iterator(const std_list_T_const_reverse_iterator& Q)              :std_list_T_const_reverse_iterator(Q)   {       }
337
338         // changed operators for pointer
339         T* operator->() const
340         {
341                 return *std::list<T*>::const_reverse_iterator::operator->();
342         }
343
344         operator T&() const
345         {
346                 return *operator->();
347         }
348
349         operator T*() const
350         {
351                 return operator->();
352         }
353
354         const_reverse_iterator& operator++()
355         {
356                 std::list<T*>::const_reverse_iterator::operator++();
357                 return *this;
358         }
359
360         const_reverse_iterator operator++(int)
361         {
362                 return std::list<T*>::const_reverse_iterator::operator++(0);
363         }
364
365         const_reverse_iterator& operator--()
366         {
367                 std::list<T*>::const_reverse_iterator::operator--();
368                 return *this;
369         }
370
371         const_reverse_iterator operator--(int)
372         {
373                 return std::list<T*>::const_reverse_iterator::operator--(0);
374         }
375 };
376
377 /////////////////// Default Constructor /////////////////////////////
378 template <class T>
379 ePtrList<T>::ePtrList()
380     :cur(std::list<T*>::begin()), autoDelete(false)             
381 {               
382
383 }
384
385 /////////////////// Copy Constructor /////////////////////////////
386 template <class T>
387 ePtrList<T>::ePtrList(const ePtrList& e)
388         :std::list<T*>(e), cur(e.cur), autoDelete( false )
389 {               
390         if ( e.autoDelete )     
391                 if ( e.size() )
392                         eDebug("Warning !! We make a Copy of a non empty ePtrList, with autoDelete enabled"
393                                                  "We disable autoDelete in the new ePtrList !!");
394                 else
395                         autoDelete=true;
396 }
397
398 /////////////////// ePtrList Destructor /////////////////////////////
399 template <class T>
400 inline ePtrList<T>::~ePtrList()
401 {
402 // if autoDelete is enabled, delete is called for all elements in the list
403         if (autoDelete)
404                 for (std_list_T_iterator it(std::list<T*>::begin()); it != std::list<T*>::end(); it++)
405                         delete *it;
406 }
407
408
409 /////////////////// ePtrList sort() /////////////////////////
410 template <class T>
411 inline void ePtrList<T>::sort()
412 {               
413 //      Sorts all items in the list.
414 //      The type T must have a operator <.
415         std::list<T*>::sort(ePtrList<T>::less());
416 }       
417
418 /////////////////// ePtrList remove(T*) /////////////////////////
419 template <class T>
420 inline void ePtrList<T>::remove(T* t)
421 {
422 //      Remove all items that, equals to t, if auto-deletion is enabled, than the list call delete for the removed items
423 //  If current is equal to one of the removed items, current is set to the next valid item
424         T_iterator it(std::list<T*>::begin());
425
426         while (it != std::list<T*>::end())
427                 if (*it == t)
428                 {
429                         it=erase(it);
430                         break;  // one item is complete removed an deleted
431                 }
432                 else
433                         it++;
434         
435         while (it != std::list<T*>::end())
436                 if (*it == t)
437                         it = std::list<T*>::erase(it);  // remove all other items that equals to t (no delete is called..)
438                 else
439                         it++;
440                         
441 }
442
443 /////////////////// ePtrList clear() //////////////////
444 template <class T>
445 inline void ePtrList<T>::clear()        
446 {               
447 //      Remove all items from the list
448 //      If auto-deletion is enabled, than the list call delete for all items in the list
449         erase(std::list<T*>::begin(), std::list<T*>::end());    
450 }
451
452 /////////////////// ePtrList pop_back() ////////////////////
453 template <class T>
454 inline void ePtrList<T>::pop_back()
455 {
456 //      Removes the last item from the list. If the current item ist the last, than the current is set to the new
457 //      last item in the list;
458 //      The removed item is deleted if auto-deletion is enabled.
459         erase(--end());
460 }
461
462 /////////////////// ePtrList pop_front() ////////////////////
463 template <class T>
464 inline void ePtrList<T>::pop_front()
465 {
466 //      Removes the first item from the list. If the current item ist the first, than the current is set to the new
467 //      first item in the list;
468 //      The removed item is deleted if auto-deletion is enabled.
469         erase(begin());
470 }
471
472 /////////////////// ePtrList push_back(T*) ////////////////////
473 template <class T>
474 inline void ePtrList<T>::push_back(T* x)        
475 {               
476 // Add a new item at the end of the list.
477 // The current item is set to the last item;
478         std::list<T*>::push_back(x);
479         last(); 
480 }
481
482 /////////////////// ePtrList push_front(T*) ////////////////////
483 template <class T>
484 inline void ePtrList<T>::push_front(T* x)       
485 {               
486 // Add a new item at the begin of the list.
487 // The current item is set to the first item;
488         std::list<T*>::push_front(x);
489         first();        
490 }
491
492 /////////////////// ePtrList take() ////////////////////
493 template <class T>
494 inline T* ePtrList<T>::take()
495 {
496 // Takes the current item out of the list without deleting it (even if auto-deletion is enabled).
497 // Returns a pointer to the item taken out of the list, or null if the index is out of range.
498 // The item after the taken item becomes the new current list item if the taken item is not the last item in the list. If the last item is taken, the new last item becomes the current item.
499 // The current item is set to null if the list becomes empty.
500         T* tmp = *cur;
501         cur = std::list<T*>::erase(cur);
502         return tmp;
503 }
504
505 /////////////////// ePtrList take(T*) ////////////////////
506 template <class T>
507 inline void ePtrList<T>::take(T* t)
508 {
509 // Takes all item with T* out of the list without deleting it (even if auto-deletion is enabled).
510         std::list<T*>::remove(t);
511 }
512
513 /////////////////// ePtrList setCurrent(T*) ////////////////////
514 template <class T>
515 inline T* ePtrList<T>::setCurrent(const T* t)
516 {
517         // Sets the internal current iterator to the first element that equals to t, and returns t when a item is found,
518         // otherwise it returns 0 !
519         for (T_iterator it(std::list<T*>::begin()); it != std::list<T*>::end(); ++it)
520                 if (*it == t)
521                 {
522                         cur = it;
523                         return *it;
524                 }
525
526         return 0;
527 }
528
529 /////////////////// ePtrList current() ////////////////////
530 template <class T>
531 inline T* ePtrList<T>::current()
532 {
533 //      Returns a pointer to the current list item. The current item may be null (implies that the current index is -1).
534         return cur==end() ? 0 : *cur;   
535 }
536
537 /////////////////// ePtrList next() ////////////////////
538 template <class T>
539 inline T* ePtrList<T>::next()
540 {
541 //      Returns a pointer to the item succeeding the current item. Returns null if the current items is null or equal to the last item.
542 //      Makes the succeeding item current. If the current item before this function call was the last item, the current item will be set to null. If the current item was null, this function does nothing.
543         if (cur == end())
544                 return 0;
545         else
546                 if (++cur == end())
547                         return 0;
548                 else
549                         return *cur;
550 }
551
552 /////////////////// ePtrList prev() ////////////////////
553 template <class T>
554 inline T* ePtrList<T>::prev()
555 {
556 //      Returns a pointer to the item preceding the current item. Returns null if the current items is null or equal to the first item.
557 //      Makes the preceding item current. If the current item before this function call was the first item, the current item will be set to null. If the current item was null, this function does nothing.
558         if (cur == begin())
559                 return 0;
560         else
561                 return *--cur;
562 }
563
564 /////////////////// ePtrList first() ////////////////////
565 template <class T>
566 inline T* ePtrList<T>::first()
567 {
568 // Returns a pointer to the first item in the list and makes this the current list item, or null if the list is empty.
569         return *(cur = begin());        
570 }
571
572 /////////////////// ePtrList last() ////////////////////
573 template <class T>
574 inline T* ePtrList<T>::last()
575 {
576 //      Returns a pointer to the last item in the list and makes this the current list item, or null if the list is empty.
577         return *(cur = --end());
578 }
579
580 /////////////////// const ePtrList current() ////////////////////
581 template <class T>
582 inline const T* ePtrList<T>::current() const
583 {
584 //      Returns a pointer to the current list item. The current item may be null (implies that the current index is not valid)
585         return cur==end() ? 0 : *cur;   
586 }
587
588 /////////////////// const ePtrList next() ////////////////////
589 template <class T>
590 inline const T* ePtrList<T>::next() const
591 {
592 //      Returns a pointer to the item succeeding the current item. Returns null if the current items is null or equal to the last item.
593 //      Makes the succeeding item current. If the current item before this function call was the last item, the current item will be set to null. If the current item was null, this function does nothing.
594         if (cur == end())
595                 return 0;
596         else
597                 if (++cur == end())
598                         return 0;
599                 else
600                         return *cur;
601 }
602
603 /////////////////// const ePtrList prev() ////////////////////
604 template <class T>
605 inline const T* ePtrList<T>::prev() const
606 {
607 //      Returns a pointer to the item preceding the current item. Returns null if the current items is null or equal to the first item.
608 //      Makes the preceding item current. If the current item before this function call was the first item, the current item will be set to null. If the current item was null, this function does nothing.
609         if (cur == begin())
610                 return 0;
611         else
612                 return *--cur;
613 }
614
615 /////////////////// const ePtrList first() ////////////////////
616 template <class T>
617 inline const T* ePtrList<T>::first() const
618 {
619 // Returns a pointer to the first item in the list and makes this the current list item, or null if the list is empty.
620         return *(cur = begin());        
621 }
622
623 /////////////////// const ePtrList last() ////////////////////
624 template <class T>
625 inline const T* ePtrList<T>::last() const
626 {
627 //      Returns a pointer to the last item in the list and makes this the current list item, or null if the list is empty.
628         return *(cur = --end());
629 }
630
631 ////////////////// struct less //////////////////////////////
632 template <class T>
633 struct ePtrList<T>::less
634 {
635 //      operator() is used internal from the list to sort them
636         bool operator() (const T* t1, const T* t2)
637         {
638                 return (*t1 < *t2);
639         }
640 };
641
642 /////////////////// ePtrList operator bool ////////////////////
643 template <class T>
644 ePtrList<T>::operator bool()    
645 {
646 //      Returns a bool that contains true, when the list is NOT empty otherwise false
647         return !empty();        
648 }
649
650 template <class T>
651 bool ePtrList<T>::operator!()   
652 {
653 //      Returns a bool that contains true, when the list is empty otherwise false
654         return empty(); 
655 }
656
657 template <class T>
658 void ePtrList<T>::setAutoDelete(bool b)
659 {
660 //      switched autoDelete on or off
661 //      if autoDelete is true, than the pointer list controls the heap memory behind the pointer itself
662 //      the list calls delete for the item before it removed from the list
663         autoDelete=b;   
664 }
665
666 template <class T>
667 bool ePtrList<T>::isAutoDelete()
668 {
669 // returns a bool that contains the state of autoDelete
670         return autoDelete;
671 }
672
673 template <class T>
674 class eSmartPtrList : public std::list<ePtr<T> >
675 {
676 public:
677         typedef typename std::list<ePtr<T>, std::allocator<ePtr<T> > >::iterator std_list_T_iterator;  // to remove compiler warnings
678         typedef typename std::list<ePtr<T>, std::allocator<ePtr<T> > >::const_iterator std_list_T_const_iterator;
679         typedef typename std::list<ePtr<T>, std::allocator<ePtr<T> > >::reverse_iterator std_list_T_reverse_iterator;
680         typedef typename std::list<ePtr<T>, std::allocator<ePtr<T> > >::const_reverse_iterator std_list_T_const_reverse_iterator;
681         typedef typename eSmartPtrList<T>::iterator T_iterator;
682         typedef typename eSmartPtrList<T>::const_iterator T_const_iterator;
683         typedef typename eSmartPtrList<T>::reverse_iterator T_reverse_iterator;
684         typedef typename eSmartPtrList<T>::const_reverse_iterator T_const_reverse_iterator;
685
686 // Iterator classes
687         class iterator;
688         class const_iterator;
689         class reverse_iterator;
690         class const_reverse_iterator;
691
692 // Constructors
693         inline eSmartPtrList();
694         inline eSmartPtrList(const eSmartPtrList&);
695         inline ~eSmartPtrList();
696
697 // overwritted sort method
698         inline void sort();
699
700 // changed methods for autodelete and current implementation
701         inline void remove(T* t);
702         inline void clear();
703         inline void pop_back();
704         inline void pop_front();
705         inline void push_back(T*);
706         inline void push_front(T*);
707
708 // added methods for current implementation
709 //      inline T* take();
710 //      inline void take(T* t);
711         inline T* current();
712         inline T* next();
713         inline T* prev();
714         inline T* first();
715         inline T* last();
716         inline T* setCurrent(const T*);
717         inline const T* current() const;
718         inline const T* next() const;
719         inline const T* prev() const;
720         inline const T* first() const;
721         inline const T* last() const;
722
723 // added operator methods
724         inline operator bool();
725         inline bool operator!();
726
727 // added methods for autodelete implementation
728         inline void setAutoDelete(bool b);
729         inline bool isAutoDelete();
730
731 // added compare struct ... to sort
732         struct less;
733 private:
734         iterator cur;
735         bool autoDelete;
736
737 public:
738         iterator eSmartPtrList<T>::begin()
739         {                               
740         //      makes implicit type conversion form std::list::iterator to eSmartPtrList::iterator
741                 return std::list<ePtr<T> >::begin();            
742         }
743
744         iterator eSmartPtrList<T>::end()
745         {                               
746         //      makes implicit type conversion form std::list::iterator to eSmartPtrList::iterator
747                 return std::list<ePtr<T> >::end();              
748         }
749
750         const_iterator eSmartPtrList<T>::begin() const
751         {                               
752         //      makes implicit type conversion form std::list::const_iterator to eSmartPtrList::const_iterator
753                 return std::list<ePtr<T> >::begin();            
754         }
755
756         const_iterator eSmartPtrList<T>::end() const
757         {                               
758         //      makes implicit type conversion form std::list::const_iterator to eSmartPtrList::const_iterator
759                 return std::list<ePtr<T> >::end();              
760         }
761
762         reverse_iterator eSmartPtrList<T>::rbegin()
763         {                               
764         //      makes implicit type conversion form std::list::reverse:_iterator to eSmartPtrList::reverse_iterator
765                 return std::list<ePtr<T> >::rbegin();           
766         }
767
768         reverse_iterator eSmartPtrList<T>::rend()
769         {                               
770         //      makes implicit type conversion form std::list::reverse_iterator to eSmartPtrList::reverse_iterator
771                 return std::list<ePtr<T> >::rend();             
772         }
773
774         const_reverse_iterator eSmartPtrList<T>::rbegin() const
775         {                               
776         //      makes implicit type conversion form std::list::const_reverse_iterator to eSmartPtrList::const_reverse_iterator
777                 return std::list<ePtr<T> >::rbegin();           
778         }
779
780         const_reverse_iterator eSmartPtrList<T>::rend() const
781         {                               
782         //      makes implicit type conversion form std::list::const_reverse_iterator to eSmartPtrList::const_reverse_iterator
783                 return std::list<ePtr<T> >::rend();             
784         }
785
786         iterator eSmartPtrList<T>::erase(iterator it)
787         {
788         //      Remove the item it, if auto-deletion is enabled, than the list call delete for this item
789         //  If current is equal to the item that was removed, current is set to the next item in the list
790                 if (autoDelete && *it)
791                         delete *it;                                     
792
793                 if (cur == it)
794                         return cur = std::list<ePtr<T> >::erase(it);
795                 else
796                         return std::list<ePtr<T> >::erase(it);
797         }
798
799         iterator eSmartPtrList<T>::erase(iterator from, iterator to)
800         {
801         //      Remove all items between the to iterators from and to
802         //      If auto-deletion is enabled, than the list call delete for all removed items
803                 while (from != to)
804                         from = erase(from);
805         
806                 return from;
807         }
808
809         operator iterator()     
810         {
811         //      Returns a iterator that equal to begin() of the list
812                 return begin(); 
813         }
814
815         operator const_iterator() const
816         {
817         //      Returns a const_iterator that equal to begin() of the list
818                 return begin(); 
819         }
820
821         operator reverse_iterator()
822         {
823         //      Returns a reverse_iterator that equal to rbegin() of the list
824                 return rbegin();        
825         }
826
827         operator const_reverse_iterator() const 
828         {
829         //      Returns a const_reverse_iterator that equal to rbegin() of the list
830                 return rbegin();        
831         }
832
833         std::vector<T>* getVector()
834         {
835                 // Creates an vector and copys all elements to this vector
836                 // returns a pointer to this new vector ( the reserved memory must deletet from the receiver !! )
837                 std::vector<T>* v=new std::vector<T>();
838                 v->reserve( size() );
839     for ( std_list_T_iterator it( std::list<ePtr<T> >::begin() ); it != std::list<ePtr<T> >::end(); it++)
840                         v->push_back( **it );
841
842                 return v;
843         }
844
845         inline iterator insert_in_order( T* e )
846         {
847                 // added a new item to the list... in order
848                 // returns a iterator to the new item
849                 return insert( std::lower_bound( std::list<ePtr<T> >::begin(), std::list<ePtr<T> >::end(), e ), e );
850         }
851
852 };
853
854 /////////////////// iterator class /////////////////////////////
855 template <class T>
856 class eSmartPtrList<T>::iterator : public std::list<ePtr<T> >::iterator
857 {
858 public:
859         // Constructors
860         iterator(const std_list_T_iterator& Q)          : std_list_T_iterator(Q)        {       }
861
862         // changed operator for pointer
863         T* operator->() const
864         {
865                 return *std::list<ePtr<T> >::iterator::operator->();
866         }
867
868         operator T&() const
869         {
870                 return *operator->();
871         }
872         
873         operator T*() const
874         {
875                 return operator->();
876         }
877
878         iterator& operator++()
879         {
880                 std::list<ePtr<T> >::iterator::operator++();
881                 return *this;
882         }
883
884         iterator operator++(int)
885         {
886                 return std::list<ePtr<T> >::iterator::operator++(0);
887         }
888
889         iterator& operator--()
890         {
891                 std::list<ePtr<T> >::iterator::operator--();
892                 return *this;
893         }
894
895         iterator operator--(int)
896         {
897                 return std::list<ePtr<T> >::iterator::operator--(0);
898         }
899 };
900
901 /////////////////// const_iterator class /////////////////////////////
902 template <class T>
903 class eSmartPtrList<T>::const_iterator : public std::list<ePtr<T> >::const_iterator
904 {
905 public:
906         // Constructors
907         const_iterator(const std_list_T_const_iterator& Q)              :std_list_T_const_iterator(Q)   {       }
908
909         // changed operator for pointer
910         T* operator->() const
911         {
912                 return *std::list<ePtr<T> >::const_iterator::operator->();
913         }
914
915         operator T&() const
916         {
917                 return *operator->();
918         }
919
920         operator T*() const
921         {
922                 return operator->();
923         }
924
925         const_iterator& operator++()
926         {
927                 std::list<ePtr<T> >::const_iterator::operator++();
928                 return *this;
929         }
930
931         const_iterator operator++(int)
932         {
933                 return std::list<ePtr<T> >::const_iterator::operator++(0);
934         }
935
936         const_iterator& operator--()
937         {
938                 std::list<ePtr<T> >::const_iterator::operator--();
939                 return *this;
940         }
941
942         const_iterator operator--(int)
943         {
944                 return std::list<ePtr<T> >::const_iterator::operator--(0);
945         }
946 };
947
948 /////////////////// reverse_iterator class /////////////////////////////
949 template <class T>
950 class eSmartPtrList<T>::reverse_iterator : public std::list<ePtr<T> >::reverse_iterator
951 {
952 public:
953         // Constructors
954         reverse_iterator(const std_list_T_reverse_iterator& Q)          :std_list_T_reverse_iterator(Q) {       }
955
956         // changed operators for pointer
957         T* operator->() const
958         {
959                 return *std::list<ePtr<T> >::reverse_iterator::operator->();
960         }
961
962         operator T&() const
963         {
964                 return *operator->();
965         }
966
967         operator T*() const
968         {
969                 return operator->();
970         }
971
972         reverse_iterator& operator++()
973         {
974                 std::list<ePtr<T> >::reverse_iterator::operator++();
975                 return *this;
976         }
977
978         reverse_iterator operator++(int)
979         {
980                 return std::list<ePtr<T> >::reverse_iterator::operator++(0);
981         }
982
983         reverse_iterator& operator--()
984         {
985                 std::list<ePtr<T> >::reverse_iterator::operator--();
986                 return *this;
987         }
988
989         reverse_iterator operator--(int)
990         {
991                 return std::list<ePtr<T> >::reverse_iterator::operator--(0);
992         }
993 };
994
995 /////////////////// const_reverse_iterator class /////////////////////////////
996 template <class T>
997 class eSmartPtrList<T>::const_reverse_iterator : public std::list<ePtr<T> >::const_reverse_iterator
998 {
999 public:
1000         // Constructors
1001         const_reverse_iterator(const std_list_T_const_reverse_iterator& Q)              :std_list_T_const_reverse_iterator(Q)   {       }
1002
1003         // changed operators for pointer
1004         T* operator->() const
1005         {
1006                 return *std::list<ePtr<T> >::const_reverse_iterator::operator->();
1007         }
1008
1009         operator T&() const
1010         {
1011                 return *operator->();
1012         }
1013
1014         operator T*() const
1015         {
1016                 return operator->();
1017         }
1018
1019         const_reverse_iterator& operator++()
1020         {
1021                 std::list<ePtr<T> >::const_reverse_iterator::operator++();
1022                 return *this;
1023         }
1024
1025         const_reverse_iterator operator++(int)
1026         {
1027                 return std::list<ePtr<T> >::const_reverse_iterator::operator++(0);
1028         }
1029
1030         const_reverse_iterator& operator--()
1031         {
1032                 std::list<ePtr<T> >::const_reverse_iterator::operator--();
1033                 return *this;
1034         }
1035
1036         const_reverse_iterator operator--(int)
1037         {
1038                 return std::list<ePtr<T> >::const_reverse_iterator::operator--(0);
1039         }
1040 };
1041
1042 /////////////////// Default Constructor /////////////////////////////
1043 template <class T>
1044 eSmartPtrList<T>::eSmartPtrList()
1045     :cur(std::list<ePtr<T> >::begin()), autoDelete(false)               
1046 {               
1047
1048 }
1049
1050 /////////////////// Copy Constructor /////////////////////////////
1051 template <class T>
1052 eSmartPtrList<T>::eSmartPtrList(const eSmartPtrList& e)
1053         :std::list<ePtr<T> >(e), cur(e.cur), autoDelete( false )
1054 {               
1055         if ( e.autoDelete )     
1056                 if ( e.size() )
1057                         eDebug("Warning !! We make a Copy of a non empty eSmartPtrList, with autoDelete enabled"
1058                                                  "We disable autoDelete in the new eSmartPtrList !!");
1059                 else
1060                         autoDelete=true;
1061 }
1062
1063 /////////////////// eSmartPtrList Destructor /////////////////////////////
1064 template <class T>
1065 inline eSmartPtrList<T>::~eSmartPtrList()
1066 {
1067 // if autoDelete is enabled, delete is called for all elements in the list
1068         if (autoDelete)
1069                 for (std_list_T_iterator it(std::list<ePtr<T> >::begin()); it != std::list<ePtr<T> >::end(); it++)
1070                         delete *it;
1071 }
1072
1073
1074 /////////////////// eSmartPtrList sort() /////////////////////////
1075 template <class T>
1076 inline void eSmartPtrList<T>::sort()
1077 {               
1078 //      Sorts all items in the list.
1079 //      The type T must have a operator <.
1080         std::list<ePtr<T> >::sort(eSmartPtrList<T>::less());
1081 }       
1082
1083 /////////////////// eSmartPtrList remove(T*) /////////////////////////
1084 template <class T>
1085 inline void eSmartPtrList<T>::remove(T* t)
1086 {
1087 //      Remove all items that, equals to t, if auto-deletion is enabled, than the list call delete for the removed items
1088 //  If current is equal to one of the removed items, current is set to the next valid item
1089         T_iterator it(std::list<ePtr<T> >::begin());
1090
1091         while (it != std::list<ePtr<T> >::end())
1092                 if (*it == t)
1093                 {
1094                         it=erase(it);
1095                         break;  // one item is complete removed an deleted
1096                 }
1097                 else
1098                         it++;
1099         
1100         while (it != std::list<ePtr<T> >::end())
1101                 if (*it == t)
1102                         it = std::list<ePtr<T> >::erase(it);  // remove all other items that equals to t (no delete is called..)
1103                 else
1104                         it++;
1105                         
1106 }
1107
1108 /////////////////// eSmartPtrList clear() //////////////////
1109 template <class T>
1110 inline void eSmartPtrList<T>::clear()   
1111 {               
1112 //      Remove all items from the list
1113 //      If auto-deletion is enabled, than the list call delete for all items in the list
1114         erase(std::list<ePtr<T> >::begin(), std::list<ePtr<T> >::end());        
1115 }
1116
1117 /////////////////// eSmartPtrList pop_back() ////////////////////
1118 template <class T>
1119 inline void eSmartPtrList<T>::pop_back()
1120 {
1121 //      Removes the last item from the list. If the current item ist the last, than the current is set to the new
1122 //      last item in the list;
1123 //      The removed item is deleted if auto-deletion is enabled.
1124         erase(--end());
1125 }
1126
1127 /////////////////// eSmartPtrList pop_front() ////////////////////
1128 template <class T>
1129 inline void eSmartPtrList<T>::pop_front()
1130 {
1131 //      Removes the first item from the list. If the current item ist the first, than the current is set to the new
1132 //      first item in the list;
1133 //      The removed item is deleted if auto-deletion is enabled.
1134         erase(begin());
1135 }
1136
1137 /////////////////// eSmartPtrList push_back(T*) ////////////////////
1138 template <class T>
1139 inline void eSmartPtrList<T>::push_back(T* x)   
1140 {               
1141 // Add a new item at the end of the list.
1142 // The current item is set to the last item;
1143         std::list<ePtr<T> >::push_back(x);
1144         last(); 
1145 }
1146
1147 /////////////////// eSmartPtrList push_front(T*) ////////////////////
1148 template <class T>
1149 inline void eSmartPtrList<T>::push_front(T* x)  
1150 {               
1151 // Add a new item at the begin of the list.
1152 // The current item is set to the first item;
1153         std::list<ePtr<T> >::push_front(x);
1154         first();        
1155 }
1156
1157 /////////////////// eSmartPtrList take() ////////////////////
1158 //template <class T>
1159 //inline T* eSmartPtrList<T>::take()
1160 //{
1161 //// Takes the current item out of the list without deleting it (even if auto-deletion is enabled).
1162 //// Returns a pointer to the item taken out of the list, or null if the index is out of range.
1163 //// The item after the taken item becomes the new current list item if the taken item is not the last item in the list. If the last item is taken, the new last item becomes the current item.
1164 //// The current item is set to null if the list becomes empty.
1165 //      T* tmp = *cur;
1166 //      cur = std::list<T*>::erase(cur);
1167 //      return tmp;
1168 //}
1169
1170 /////////////////// eSmartPtrList take(T*) ////////////////////
1171 //template <class T>
1172 //inline void eSmartPtrList<T>::take(T* t)
1173 //{
1174 //// Takes all item with T* out of the list without deleting it (even if auto-deletion is enabled).
1175 //      std::list<T*>::remove(t);
1176 //}
1177
1178 /////////////////// eSmartPtrList setCurrent(T*) ////////////////////
1179 template <class T>
1180 inline T* eSmartPtrList<T>::setCurrent(const T* t)
1181 {
1182         // Sets the internal current iterator to the first element that equals to t, and returns t when a item is found,
1183         // otherwise it returns 0 !
1184         for (T_iterator it(std::list<ePtr<T> >::begin()); it != std::list<ePtr<T> >::end(); ++it)
1185                 if (*it == t)
1186                 {
1187                         cur = it;
1188                         return *it;
1189                 }
1190
1191         return 0;
1192 }
1193
1194 /////////////////// eSmartPtrList current() ////////////////////
1195 template <class T>
1196 inline T* eSmartPtrList<T>::current()
1197 {
1198 //      Returns a pointer to the current list item. The current item may be null (implies that the current index is -1).
1199         return cur==end() ? 0 : *cur;   
1200 }
1201
1202 /////////////////// eSmartPtrList next() ////////////////////
1203 template <class T>
1204 inline T* eSmartPtrList<T>::next()
1205 {
1206 //      Returns a pointer to the item succeeding the current item. Returns null if the current items is null or equal to the last item.
1207 //      Makes the succeeding item current. If the current item before this function call was the last item, the current item will be set to null. If the current item was null, this function does nothing.
1208         if (cur == end())
1209                 return 0;
1210         else
1211                 if (++cur == end())
1212                         return 0;
1213                 else
1214                         return *cur;
1215 }
1216
1217 /////////////////// eSmartPtrList prev() ////////////////////
1218 template <class T>
1219 inline T* eSmartPtrList<T>::prev()
1220 {
1221 //      Returns a pointer to the item preceding the current item. Returns null if the current items is null or equal to the first item.
1222 //      Makes the preceding item current. If the current item before this function call was the first item, the current item will be set to null. If the current item was null, this function does nothing.
1223         if (cur == begin())
1224                 return 0;
1225         else
1226                 return *--cur;
1227 }
1228
1229 /////////////////// eSmartPtrList first() ////////////////////
1230 template <class T>
1231 inline T* eSmartPtrList<T>::first()
1232 {
1233 // Returns a pointer to the first item in the list and makes this the current list item, or null if the list is empty.
1234         return *(cur = begin());        
1235 }
1236
1237 /////////////////// eSmartPtrList last() ////////////////////
1238 template <class T>
1239 inline T* eSmartPtrList<T>::last()
1240 {
1241 //      Returns a pointer to the last item in the list and makes this the current list item, or null if the list is empty.
1242         return *(cur = --end());
1243 }
1244
1245 /////////////////// const eSmartPtrList current() ////////////////////
1246 template <class T>
1247 inline const T* eSmartPtrList<T>::current() const
1248 {
1249 //      Returns a pointer to the current list item. The current item may be null (implies that the current index is not valid)
1250         return cur==end() ? 0 : *cur;   
1251 }
1252
1253 /////////////////// const eSmartPtrList next() ////////////////////
1254 template <class T>
1255 inline const T* eSmartPtrList<T>::next() const
1256 {
1257 //      Returns a pointer to the item succeeding the current item. Returns null if the current items is null or equal to the last item.
1258 //      Makes the succeeding item current. If the current item before this function call was the last item, the current item will be set to null. If the current item was null, this function does nothing.
1259         if (cur == end())
1260                 return 0;
1261         else
1262                 if (++cur == end())
1263                         return 0;
1264                 else
1265                         return *cur;
1266 }
1267
1268 /////////////////// const eSmartPtrList prev() ////////////////////
1269 template <class T>
1270 inline const T* eSmartPtrList<T>::prev() const
1271 {
1272 //      Returns a pointer to the item preceding the current item. Returns null if the current items is null or equal to the first item.
1273 //      Makes the preceding item current. If the current item before this function call was the first item, the current item will be set to null. If the current item was null, this function does nothing.
1274         if (cur == begin())
1275                 return 0;
1276         else
1277                 return *--cur;
1278 }
1279
1280 /////////////////// const eSmartPtrList first() ////////////////////
1281 template <class T>
1282 inline const T* eSmartPtrList<T>::first() const
1283 {
1284 // Returns a pointer to the first item in the list and makes this the current list item, or null if the list is empty.
1285         return *(cur = begin());        
1286 }
1287
1288 /////////////////// const eSmartPtrList last() ////////////////////
1289 template <class T>
1290 inline const T* eSmartPtrList<T>::last() const
1291 {
1292 //      Returns a pointer to the last item in the list and makes this the current list item, or null if the list is empty.
1293         return *(cur = --end());
1294 }
1295
1296 ////////////////// struct less //////////////////////////////
1297 template <class T>
1298 struct eSmartPtrList<T>::less
1299 {
1300 //      operator() is used internal from the list to sort them
1301         bool operator() (const T* t1, const T* t2)
1302         {
1303                 return (*t1 < *t2);
1304         }
1305 };
1306
1307 /////////////////// eSmartPtrList operator bool ////////////////////
1308 template <class T>
1309 eSmartPtrList<T>::operator bool()       
1310 {
1311 //      Returns a bool that contains true, when the list is NOT empty otherwise false
1312         return !empty();        
1313 }
1314
1315 template <class T>
1316 bool eSmartPtrList<T>::operator!()      
1317 {
1318 //      Returns a bool that contains true, when the list is empty otherwise false
1319         return empty(); 
1320 }
1321
1322 template <class T>
1323 void eSmartPtrList<T>::setAutoDelete(bool b)
1324 {
1325 //      switched autoDelete on or off
1326 //      if autoDelete is true, than the pointer list controls the heap memory behind the pointer itself
1327 //      the list calls delete for the item before it removed from the list
1328         autoDelete=b;   
1329 }
1330
1331 template <class T>
1332 bool eSmartPtrList<T>::isAutoDelete()
1333 {
1334 // returns a bool that contains the state of autoDelete
1335         return autoDelete;
1336 }
1337
1338 #endif // _E_PTRLIST