tbx  0.7.3
safelist.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef TBX_SAFELIST_H
26 #define TBX_SAFELIST_H
27 
28 namespace tbx
29 {
30 
44 template<class T> class SafeList
45 {
46  private:
47  struct Node
48  {
49  Node(T *ptr) : _ptr(ptr), _next(0) {};
50  T *_ptr;
51  Node *_next;
52  };
53 
54  public:
62  class Iterator
63  {
64  friend class SafeList;
65 
66  private:
67  SafeList _list;
68  Node *_next;
69 
70  public:
74  Iterator(SafeList &list) : _next(list._head)
75  {
76  _list._iter = this;
77  }
78 
79  ~Iterator()
80  {
81  _list._iter = 0;
82  }
83 
87  T *next()
88  {
89  T *ptr;
90 
91  if (_next != 0)
92  {
93  ptr = _next->_ptr;
94  _next = _next->_next;
95  } else
96  {
97  ptr = 0;
98  }
99 
100  return ptr;
101  }
102  };
103 
104  private:
105  Node *_head;
106  Iterator *_iter;
107 
108  friend class Iterator;
109 
110  public:
114  SafeList() : _head(0), _iter(0) {};
115 
120 
126  void push_back(T *ptr)
127  {
128  if (_head == 0) _head = new Node(ptr);
129  else
130  {
131  Node *last = _head;
132  while (last->_next != 0) last = last->_next;
133  last->_next = new Node(ptr);
134  }
135  }
136 
142  void remove(T *ptr)
143  {
144  Node *prev = 0;
145  Node *check = _head;
146  while (check && check->_ptr != ptr)
147  {
148  prev = check;
149  check = check->_next;
150  }
151  if (check)
152  {
153  if (prev) prev->_next = check->_next;
154  else _head = check->_next;
155 
156  if (_iter && _iter->_next == check)
157  {
158  _iter->_next = check->_next;
159  }
160  delete check;
161  }
162  }
163 
167  void clear()
168  {
169  if (_iter) _iter->_next = 0;
170  Node *check = _head;
171  Node *next;
172  while (check)
173  {
174  next = check->_next;
175  delete check;
176  check = next;
177  }
178  _head = 0;
179  }
180 
184  bool empty() const
185  {
186  return (_head == 0);
187  }
188 };
189 
190 }
191 
192 #endif
193 
void clear()
Empty list.
Definition: safelist.h:167
void push_back(T *ptr)
Add pointer to end of list.
Definition: safelist.h:126
Simple one way linked list of pointers that provides a single iterator that can be used safely if ite...
Definition: safelist.h:44
Simple class to iterate through the list.
Definition: safelist.h:62
SafeList()
Construct an empty SafeList.
Definition: safelist.h:114
Iterator(SafeList &list)
Construct the iterator for the list.
Definition: safelist.h:74
T * next()
Get next pointer from the list.
Definition: safelist.h:87
bool empty() const
Check if list is empty.
Definition: safelist.h:184
~SafeList()
Destructor will clear the list.
Definition: safelist.h:119