VR-Forces 4.6.1 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mapIteratorBase.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2018 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 #pragma once
6 
9 
10 #include <vrfutil/iteratorBase.h>
11 #include <vlutil/vlPrint.h>
12 #include <map>
13 
18 template<class K, class T> class DtMapIteratorBase
19 {
20 private:
21 
22  //default constructor is not allowed
24 
26  const DtMapIteratorBase& operator=(const DtMapIteratorBase& orig);
27 
28 public:
29 
31  DtMapIteratorBase(const std::map<K, T*>& map) : myMap(map), myCursor(), myDirection(DtIteratorForward) { first(); };
32 
35  : myMap(orig.myMap), myCursor(orig.myCursor), myDirection(orig.myDirection) {};
36 
37  virtual ~DtMapIteratorBase() {};
38 
40  virtual unsigned int size() const { return myMap.size(); }
41 
44  virtual bool empty() const { return myMap.empty(); }
45 
46  virtual const std::map<K, T*>& map() { return myMap; };
47 
52  virtual bool reset(bool reverse = false)
53  {
54  return reset(
57  };
58 
60  virtual bool reset(DtIteratorLocation location,
62  {
63  switch (location)
64  {
66  myCursor = myMap.begin();
67  break;
68 
69  case DtIteratorEnd:
70  myCursor = myMap.end();
71  if (!myMap.empty())
72  {
73  --myCursor;
74  }
75  break;
76 
77  default:
78  DtWarn("DtIteratorBase::reset() unexpected location specified.\n");
79  }
80 
82 
83  return (myCursor != myMap.end());
84  };
85 
87  virtual void reverse()
88  {
89  myDirection =
92  };
93 
96  {
98  };
99 
102 
103 
104 public:
105 
111  virtual T* internalFirst(bool move = true)
112  {
114  {
115  if (move)
116  {
117  reset();
118  }
119 
120  if (myMap.empty())
121  {
122  return 0;
123  }
124  else
125  {
126  return myMap.begin()->second;
127  }
128  }
129  else
130  {
131  if (move)
132  {
133  reset(true);
134  }
135 
136  if (myMap.empty())
137  {
138  return 0;
139  }
140  else
141  {
142  return myMap.rbegin()->second;
143  }
144  }
145  };
146 
147  T* operator->() { return current(); };
148  T* operator*() { return current(); };
149 
151  virtual T* internalCurrent() const
152  {
153  return (myCursor != myMap.end() ? myCursor->second : 0);
154  };
155 
156  virtual T* internalNext(bool move = true)
157  {
159  {
160  if (myCursor != myMap.end())
161  {
162  typename std::map<K, T*>::const_iterator nextItem = myCursor;
163  ++nextItem;
164 
165  if (move)
166  {
167  myCursor = nextItem;
168  }
169 
170  if (nextItem != myMap.end())
171  {
172  return nextItem->second;
173  }
174  }
175  }
176  else
177  {
178  if (myCursor != myMap.end())
179  {
180  typename std::map<K, T*>::const_iterator nextItem = myCursor;
181 
182  if (myCursor == myMap.begin())
183  {
184  nextItem = myMap.end();
185  }
186  else
187  {
188  --nextItem;
189  }
190 
191  if (move)
192  {
193  myCursor = nextItem;
194  }
195 
196  if (nextItem != myMap.end())
197  {
198  return nextItem->second;
199  }
200  }
201  }
202 
203  return 0;
204  };
205 
211  virtual T* internalLast(bool move = true)
212  {
214  {
215  if (move)
216  {
217  reset(true);
218  }
219 
220  if (myMap.empty())
221  {
222  return 0;
223  }
224  else
225  {
226  return myMap.rbegin()->second;
227  }
228  }
229  else
230  {
231  if (move)
232  {
233  reset();
234  }
235 
236  if (myMap.empty())
237  {
238  return 0;
239  }
240  else
241  {
242  return myMap.begin()->second;
243  }
244  }
245  };
246 
247 
251  virtual T* internalWrapNext(bool move = true)
252  {
253  if (myCursor == myMap.end() || myMap.empty())
254  {
255  return 0;
256  }
257 
258  typename std::map<K, T*>::const_iterator tmpCursor = myCursor;
259 
261  {
262  ++tmpCursor;
263  }
264  else
265  {
266  if (myCursor == myMap.begin())
267  {
268  tmpCursor = myMap.end();
269  }
270  else
271  {
272  --tmpCursor;
273  }
274  }
275 
276  if (tmpCursor == myMap.end())
277  {
279  {
280  tmpCursor = myMap.begin();
281  }
282  else
283  {
284  tmpCursor = myMap.end();
285  --tmpCursor;
286  }
287  }
288 
289  if (move)
290  {
291  myCursor = tmpCursor;
292  }
293 
294  if (tmpCursor != myMap.end())
295  {
296  return tmpCursor->second;
297  }
298 
299  return 0;
300  };
301 
305  virtual T* internalPrev(bool move = true)
306  {
308  {
309  if (myCursor != myMap.end())
310  {
311  typename std::map<K, T*>::const_iterator prevItem = myCursor;
312 
313  if (myCursor == myMap.begin())
314  {
315  prevItem = myMap.end();
316  }
317  else
318  {
319  --prevItem;
320  }
321 
322  if (move)
323  {
324  myCursor = prevItem;
325  }
326 
327  if (prevItem != myMap.end())
328  {
329  return prevItem->second;
330  }
331  }
332  }
333  else
334  {
335  if (myCursor != myMap.end())
336  {
337  typename std::map<K, T*>::const_iterator prevItem = myCursor;
338  ++prevItem;
339 
340  if (move)
341  {
342  myCursor = prevItem;
343  }
344 
345  if (prevItem != myMap.end())
346  {
347  return prevItem->second;
348  }
349  }
350  }
351 
352  return 0;
353  };
354 
355  virtual T* internalWrapPrev(bool move = true)
356  {
357  if (myCursor == myMap.end() || myMap.empty())
358  {
359  return 0;
360  }
361 
362  typename std::map<K, T*>::const_iterator tmpCursor = myCursor;
363 
365  {
366  if (myCursor == myMap.begin())
367  {
368  tmpCursor = myMap.end();
369  }
370  else
371  {
372  --tmpCursor;
373  }
374  }
375  else
376  {
377  ++tmpCursor;
378  }
379 
380  if (tmpCursor == myMap.end())
381  {
383  {
384  tmpCursor = myMap.end();
385  --tmpCursor;
386  }
387  else
388  {
389  tmpCursor = myMap.begin();
390  }
391  }
392 
393  if (move)
394  {
395  myCursor = tmpCursor;
396  }
397 
398  if (tmpCursor != myMap.end())
399  {
400  return tmpCursor->second;
401  }
402 
403  return 0;
404  };
405 
406  virtual T* current() { return internalCurrent(); };
407  virtual T* first(bool move = true) { return internalFirst(move); };
408  virtual T* next(bool move = true) { return internalNext(move); };
409  virtual T* prev(bool move = true) { return internalPrev(move); };
410  virtual T* last(bool move = true) { return internalLast(move); };
411  virtual T* wrapNext(bool move = true) { return internalWrapNext(move); };
412  virtual T* wrapPrev(bool move = true) { return internalWrapPrev(move); };
413 
414  T* operator++() { return next(); };
415  T* operator--() { return prev(); };
416 
417 protected:
418 
419  const std::map<K, T*>& myMap;
420  typename std::map<K, T*>::const_iterator myCursor;
422 
423 };
424 

Document ID: Generated on Wed Jul 25 16:57:45 EDT 2018 from SVN revision 190790
Copyright © 2005-2018 VT MÄK. All Rights Reserved (www.mak.com)