VR-Forces 4.7 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
iteratorBase.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2001 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 /*******************************************************************************
6 ** $RCSfile: iteratorBase.h,v $ $Revision: 1.13 $ $State: Exp $
7 *******************************************************************************/
8 #ifndef iteratorBase_H_
9 #define iteratorBase_H_
10 
13 
14 #include <vlutil/vlList.h>
15 #include <vlutil/vlPrint.h>
16 
18 {
21 };
22 
24 {
27 };
28 
32 template<class T> class DtIteratorBase
33 {
34 private:
35 
36  //default constructor is not allowed
38 
40  const DtIteratorBase& operator=(const DtIteratorBase& orig);
41 
42 public:
43 
45  DtIteratorBase(const DtList& list) : myList(list), myCursor(0), myDirection(DtIteratorForward) { first(); };
46 
49 
50  virtual ~DtIteratorBase() {};
51 
53  virtual unsigned int size() const { return myList.count(); }
54 
57  virtual bool empty() const { return myList.count() == 0; }
58 
59  virtual const DtList& list() { return myList; };
60 
65  virtual bool reset(bool reverse = false)
66  {
67  return reset(
70  };
71 
73  virtual bool reset(DtIteratorLocation location,
75  {
76  switch (location)
77  {
79  myCursor = myList.first();
80  break;
81 
82  case DtIteratorEnd:
83  myCursor = myList.last();
84  break;
85 
86  default:
87  DtWarn("DtIteratorBase::reset() unexpected location specified.\n");
88  }
89 
91 
92  return (myCursor != 0);
93  };
94 
96  virtual void reverse()
97  {
98  myDirection =
102  };
103 
106  { myDirection = direction; };
107 
110 
112  virtual int cursorIndex() const
113  {
114  DtListItem* item = myList.first();
115  int i = 0;
116 
117  for (;item; item = item->next(), i++)
118  {
119  if (item == myCursor)
120  {
121  return i;
122  }
123  }
124 
125  return -1;
126  }
127 
128  virtual void setCursorIndex(int i)
129  {
130  myCursor = myList.first();
131 
132  if (i != -1)
133  {
134  while (i && myCursor)
135  {
136  myCursor = myCursor->next();
137  i--;
138  }
139  }
140  }
141 
142 public:
143 
149  virtual T* internalFirst(bool move = true)
150  {
152  {
153  if (move)
154  {
155  reset();
156  }
157 
158  if (myList.first())
159  {
160  return (T*)(myList.first()->data());
161  }
162  else
163  {
164  return 0;
165  }
166  }
167  else
168  {
169  if (move)
170  {
171  reset(true);
172  }
173 
174  if (myList.last())
175  {
176  return (T*)(myList.last()->data());
177  }
178  else
179  {
180  return 0;
181  }
182  }
183  };
184 
185  T* operator->() { return current(); };
186  T* operator*() { return current(); };
187 
189  virtual T* internalCurrent() const {return (myCursor ? (T*)myCursor->data() : 0); };
190 
192  virtual DtListItem* cursor() const { return myCursor; };
193 
194  virtual T* itemToObject(DtListItem* item, bool move)
195  {
196  if (move)
197  {
198  myCursor = item;
199  }
200 
201  if (item)
202  {
203  return (T*)item->data();
204  }
205 
206  return 0;
207  };
208 
209  virtual T* internalNext(bool move = true)
210  {
211  if (myCursor)
212  {
213 
214  DtListItem* tmpCursor = 0;
215 
216  tmpCursor = ((myDirection == DtIteratorForward) ?
217  myCursor->next() : myCursor->prev());
218 
219  return itemToObject(tmpCursor, move);
220  }
221 
222  return 0;
223  };
224 
230  virtual T* internalLast(bool move = true)
231  {
233  {
234  if (move)
235  {
236  reset(true);
237  }
238 
239  if (myList.last())
240  {
241  return (T*)(myList.last()->data());
242  }
243  else
244  {
245  return 0;
246  }
247  }
248  else
249  {
250  if (move)
251  {
252  reset();
253  }
254 
255  if (myList.first())
256  {
257  return (T*)(myList.first()->data());
258  }
259  else
260  {
261  return 0;
262  }
263  }
264  };
265 
266 
270  virtual T* internalWrapNext(bool move = true)
271  {
272  if (!myCursor)
273  {
274  return 0;
275  }
276 
277  DtListItem* tmpCursor = 0;
278 
279  tmpCursor = ((myDirection == DtIteratorForward) ?
280  myCursor->next() : myCursor->prev());
281 
282  if (! tmpCursor)
283  {
284  tmpCursor = ((myDirection == DtIteratorForward) ?
285  myList.first() : myList.last());
286  }
287 
288  return itemToObject(tmpCursor, move);
289  };
290 
294  virtual T* internalPrev(bool move = true)
295  {
296  if (myCursor)
297  {
298  DtListItem* tmpCursor = 0;
299 
300  tmpCursor = ((myDirection == DtIteratorForward) ?
301  myCursor->prev() : myCursor->next());
302 
303  return itemToObject(tmpCursor, move);
304  }
305 
306  return 0;
307  };
308 
309  virtual T* internalWrapPrev(bool move = true)
310  {
311  if (!myCursor)
312  {
313  return 0;
314  }
315 
316  DtListItem* tmpCursor = 0;
317 
318  tmpCursor = ((myDirection == DtIteratorForward) ?
319  myCursor->prev() : myCursor->next());
320 
321  if (! tmpCursor)
322  {
323  tmpCursor = ((myDirection == DtIteratorForward) ?
324  myList.last() : myList.first());
325  }
326 
327  return itemToObject(tmpCursor, move);
328  };
329 
330  virtual T* current() { return internalCurrent(); };
331  virtual T* first(bool move = true) { return internalFirst(move); };
332  virtual T* next(bool move = true) { return internalNext(move); };
333  virtual T* prev(bool move = true) { return internalPrev(move); };
334  virtual T* last(bool move = true) { return internalLast(move); };
335  virtual T* wrapNext(bool move = true) { return internalWrapNext(move); };
336  virtual T* wrapPrev(bool move = true) { return internalWrapPrev(move); };
337 
338  T* operator++() { return next(); };
339  T* operator--() { return prev(); };
340 
341 protected:
342 
343  const DtList& myList;
344  DtListItem* myCursor;
346 
347 };
348 
349 
350 #endif

Document ID: Generated on Fri Apr 26 21:53:14 EDT 2019 from SVN revision 197883
Copyright © 2005-2019 VT MAK. All Rights Reserved (www.mak.com)