FreeNOS
ListIterator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __LIBSTD_LISTITERATOR_H
19#define __LIBSTD_LISTITERATOR_H
20
21#include "Macros.h"
22#include "Iterator.h"
23#include "List.h"
24#include "Assert.h"
25
37template <class T> class ListIterator : public Iterator<T>
38{
39 public:
40
47 : m_list(*list)
48 {
49
51 reset();
52 }
53
60 : m_list(list)
61 {
62
64 reset();
65 }
66
72 ListIterator(const List<T> & list)
73 : m_list((List<T> &) list)
74 {
75
77 reset();
78 }
79
83 virtual void reset()
84 {
85 m_current = m_list.head();
87 }
88
94 virtual bool hasNext() const
95 {
96 return m_next != ZERO;
97 }
98
104 virtual bool hasCurrent() const
105 {
106 return m_current != ZERO;
107 }
108
114 virtual const T & current() const
115 {
116 return m_current->data;
117 }
118
124 virtual T & current()
125 {
126 return m_current->data;
127 }
128
140 virtual T & next()
141 {
142
144 m_next = m_current->next;
145 return m_current->data;
146 }
147
153 virtual bool remove()
154 {
155 // Do we have a current item?
156 if (!m_current)
157 return false;
158
159 // Update iterator administration
160 class List<T>::Node *node = m_current;
163
164 // Delete the node on the List
165 m_list.remove(node);
166 return true;
167 }
168
175 virtual void operator++(int num)
176 {
177 if (m_current)
178 {
179 m_current = m_current->next;
180
181 if (m_current)
182 m_next = m_current->next;
183 else
184 m_next = ZERO;
185 }
186 }
187
188 private:
189
192
194 class List<T>::Node *m_current;
195
197 class List<T>::Node *m_next;
198};
199
205#endif /* __LIBSTD_LISTITERATOR_H */
Abstracts an iteration process.
Definition Iterator.h:35
Iterate through a List.
virtual bool remove()
Remove the current item from the List.
class List< T >::Node * m_next
Next node.
ListIterator(List< T > &list)
Class constructor.
virtual T & next()
Fetch the next item.
virtual bool hasCurrent() const
Check if there is a current item on the List.
virtual T & current()
Get current item in the List.
virtual void reset()
Reset the iterator.
virtual const T & current() const
Get current item in the List.
class List< T >::Node * m_current
Current node.
ListIterator(const List< T > &list)
Constant class constructor.
List< T > & m_list
Points to the List to iterate.
ListIterator(List< T > *list)
Class constructor.
virtual bool hasNext() const
Check if there is more on the List to iterate.
Simple linked list template class.
Definition List.h:37
#define ZERO
Zero value.
Definition Macros.h:43