Gorgon Game Engine
Iterator.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 
7 #include <stdexcept>
8 #include <iterator>
9 #include <iostream>
10 
11 
12 namespace Gorgon {
15  namespace Containers {
16 
30  template <class I_, class T_, class D_=long>
31  class Iterator : public std::iterator<std::random_access_iterator_tag, T_, D_> {
32  private:
33  I_ &iterator() { return *static_cast<I_*>(this); }
34  const I_ &iterator() const { return *static_cast<const I_*>(this); }
35 
36  protected:
38  Iterator() { }
39 
40  public:
41 
43  T_ &Current() const {
44  return iterator().current();
45  }
46 
48  T_ *CurrentPtr() const {
49  return &iterator().current();
50  }
51 
53  bool MoveBy(int amount) {
54  return iterator().moveby(amount);
55  }
56 
58  bool Next() {
59  return iterator().moveby(1);
60  }
61 
63  bool Previous() {
64  return iterator().moveby(-1);
65  }
66 
68  bool IsValid() const {
69  return iterator().isvalid();
70  }
71 
73  explicit operator bool() const {
74  return IsValid();
75  }
76 
78  T_ &operator [](D_ ind) const {
79  return *this+ind;
80  }
81 
83  bool Compare(const I_ &iterator) const {
84  return this->iterator().compare(iterator);
85  }
86 
88  D_ Distance(const I_ &iterator) const {
89  return this->iterator().distance(iterator);
90  }
91 
93  bool operator ==(const I_ &iterator) const {
94  return this->iterator().compare(iterator);
95  }
96 
98  bool operator >(const I_ &iterator) const {
99  return !(this->iterator() <= iterator);
100  }
101 
103  bool operator <(const I_ &iterator) const {
104  return this->iterator().isbefore(iterator);
105  }
106 
108  bool operator >=(const I_ &iterator) const {
109  return !(this->iterator() < iterator);
110  }
111 
113  bool operator <=(const I_ &iterator) const {
114  return this->iterator().isbefore(iterator) || this->iterator().compare(iterator);
115  }
116 
118  bool operator !=(const I_ &it) const {
119  return !iterator().compare(it);
120  }
121 
123  I_ &operator =(const I_ &iterator) {
124  this->iterator().set(iterator);
125 
126  return this->iterator();
127  }
128 
130  D_ operator -(const I_ &iterator) const {
131  return iterator.distance(this->iterator());
132  }
133 
135  I_ operator +(D_ offset) const {
136  I_ i(iterator());
137  i.MoveBy(offset);
138 
139  return i;
140  }
141 
143  I_ operator -(D_ offset) const {
144  I_ i(iterator());
145  i.MoveBy(-offset);
146 
147  return i;
148  }
149 
151  I_ &operator +=(D_ offset) {
152  iterator().moveby(offset);
153 
154  return iterator();
155  }
156 
158  I_ &operator -=(D_ offset) {
159  iterator().moveby(-offset);
160 
161  return iterator();
162  }
163 
165  I_ &operator ++() {
166  iterator().moveby(1);
167 
168  return iterator();
169  }
170 
172  I_ &operator --() {
173  iterator().moveby(-1);
174 
175  return iterator();
176  }
177 
179  I_ operator ++(int) {
180  I_ it=iterator();
181  iterator().moveby(1);
182 
183  return it;
184  }
185 
187  I_ operator --(int) {
188  I_ it=iterator();
189  iterator().moveby(-1);
190 
191  return it;
192  }
193 
195  T_ &operator *() const {
196  return iterator().current();
197  }
198 
200  T_ *operator ->() const {
201  return &iterator().current();
202  }
203  };
204 
218  template <class I_, class T_, class D_=long>
219  class ValueIterator : std::iterator<std::random_access_iterator_tag, T_, D_> {
220  private:
221  I_ &iterator() { return *static_cast<I_*>(this); }
222  const I_ &iterator() const { return *static_cast<const I_*>(this); }
223 
224  protected:
227 
228  public:
229 
231  T_ Current() const {
232  return iterator().current();
233  }
234 
235 
237  bool MoveBy(int amount) {
238  return iterator().moveby(amount);
239  }
240 
242  bool Next() {
243  return iterator().moveby(1);
244  }
245 
247  bool Previous() {
248  return iterator().moveby(-1);
249  }
250 
252  bool IsValid() const {
253  return iterator().isvalid();
254  }
255 
257  explicit operator bool() const {
258  return IsValid();
259  }
260 
262  T_ operator [](D_ ind) const {
263  return *this+ind;
264  }
265 
267  bool Compare(const I_ &iterator) const {
268  return this->iterator().compare(iterator);
269  }
270 
272  D_ Distance(const I_ &iterator) const {
273  return this->iterator().distance(iterator);
274  }
275 
277  bool operator ==(const I_ &iterator) const {
278  return this->iterator().compare(iterator);
279  }
280 
282  bool operator >(const I_ &iterator) const {
283  return !(this->iterator() <= iterator);
284  }
285 
287  bool operator <(const I_ &iterator) const {
288  return this->iterator().isbefore(iterator);
289  }
290 
292  bool operator >=(const I_ &iterator) const {
293  return !(this->iterator() < iterator);
294  }
295 
297  bool operator <=(const I_ &iterator) const {
298  return this->iterator().isbefore(iterator) || this->iterator().compare(iterator);
299  }
300 
302  bool operator !=(const I_ &it) const {
303  return !iterator().compare(it);
304  }
305 
307  I_ &operator =(const I_ &iterator) {
308  this->iterator().set(iterator);
309 
310  return this->iterator();
311  }
312 
314  D_ operator -(const I_ &iterator) const {
315  return iterator.distance(this->iterator());
316  }
317 
319  I_ operator +(D_ offset) const {
320  I_ i(iterator());
321  i.MoveBy(offset);
322 
323  return i;
324  }
325 
327  I_ operator -(D_ offset) const {
328  I_ i(iterator());
329  i.MoveBy(-offset);
330 
331  return i;
332  }
333 
335  I_ &operator +=(D_ offset) {
336  iterator().moveby(offset);
337 
338  return iterator();
339  }
340 
342  I_ &operator -=(D_ offset) {
343  iterator().moveby(-offset);
344 
345  return iterator();
346  }
347 
349  I_ &operator ++() {
350  iterator().moveby(1);
351 
352  return iterator();
353  }
354 
356  I_ &operator --() {
357  iterator().moveby(-1);
358 
359  return iterator();
360  }
361 
363  I_ operator ++(int) {
364  I_ it=iterator();
365  iterator().moveby(1);
366 
367  return it;
368  }
369 
371  I_ operator --(int) {
372  I_ it=iterator();
373  iterator().moveby(-1);
374 
375  return it;
376  }
377 
379  T_ operator *() const {
380  return iterator().current();
381  }
382  };
383 
385  template<class I_>
386  void Remove(const I_ &first, const I_ &end) {
387  for(I_ i=first; i!=end; ++i) {
388  i.Remove();
389  }
390  }
391 
393  template<class I_>
394  void Delete(const I_ &first, const I_ &end) {
395  for(I_ i=first; i!=end; ++i) {
396  i.Delete();
397  }
398  }
399 
400 
402  template<class I_, class T_>
403  I_ Find(const I_ &first, const I_ &end, const T_ &item) {
404  for(I_ i=first; i!=end; ++i) {
405  if(*i==item)
406  return i;
407  }
408 
409  return I_();
410  }
411 
414  template<class C_, class I_>
415  void AddCopy(C_ &target, const I_ &it) {
416  for(I_ i=it; i.IsValid(); i.Next()) {
417  target.Add(*i);
418  }
419  }
420 
423  template<class C_, class I_>
424  void AddCopy(C_ &target, const I_ &begin, const I_ &end) {
425  for(I_ i=begin; i!=end; ++i) {
426  target.Add(*i);
427  }
428  }
429 
432  template<class T_, class I_, class A_>
433  void AddCopy(std::vector<T_, A_> &target, const I_ &it) {
434  for(I_ i=it; i.IsValid(); i.Next()) {
435  target.push_back(*i);
436  }
437  }
438 
441  template<class T_, class I_, class A_>
442  void AddCopy(std::vector<T_, A_> &target, const I_ &begin, const I_ &end) {
443  for(I_ i=begin; i!=end; i++) {
444  target.push_back(*i);
445  }
446  }
447 
448  }
449 }
450 
451 //forbidden juju
452 namespace std {
453 
455  template<class T_>
456  std::ostream &operator <<(std::ostream &out, const std::vector<T_> &vec) {
457  for(auto &e : vec) {
458  out<<e<<std::endl;
459  }
460 
461  return out;
462  }
463 
464 }
Gorgon::Containers::ValueIterator::operator*
T_ operator*() const
Dereferences the operator to get its value.
Definition: Iterator.h:379
Gorgon::Containers::ValueIterator::Distance
D_ Distance(const I_ &iterator) const
Returns the distance to the given iterator.
Definition: Iterator.h:272
Gorgon::Containers::Remove
void Remove(const I_ &first, const I_ &end)
This function works with collection iterators.
Definition: Iterator.h:386
Gorgon::Containers::Iterator::MoveBy
bool MoveBy(int amount)
Moves the iterator by the given amount.
Definition: Iterator.h:53
Gorgon::Containers::ValueIterator::operator-=
I_ & operator-=(D_ offset)
Moves the iterator by the given offset to backwards.
Definition: Iterator.h:342
Gorgon::Containers::ValueIterator::Previous
bool Previous()
Moves to the previous item.
Definition: Iterator.h:247
Gorgon::Containers::ValueIterator::operator++
I_ & operator++()
Moves the iterator to forwards.
Definition: Iterator.h:349
Gorgon::Containers::Iterator::Current
T_ & Current() const
Returns current item.
Definition: Iterator.h:43
Gorgon::Containers::ValueIterator::operator-
D_ operator-(const I_ &iterator) const
Returns the distance to the given iterator.
Definition: Iterator.h:314
Gorgon::Containers::ValueIterator::MoveBy
bool MoveBy(int amount)
Moves the iterator by the given amount.
Definition: Iterator.h:237
Gorgon::Containers::ValueIterator::operator<
bool operator<(const I_ &iterator) const
Checks if the current operator is before the given.
Definition: Iterator.h:287
Gorgon::Containers::Iterator::operator->
T_ * operator->() const
Dereferences the operator to access its values.
Definition: Iterator.h:200
Gorgon::Containers::Iterator::operator+
I_ operator+(D_ offset) const
Creates a new iterator adding the given offset.
Definition: Iterator.h:135
Gorgon::Containers::ValueIterator::operator--
I_ & operator--()
Moves the iterator to backwards.
Definition: Iterator.h:356
Gorgon::begin
std::vector< T_ >::const_iterator begin(enum_type_id< T_ >)
Definition: Enum.h:283
Gorgon::Containers::Iterator::CurrentPtr
T_ * CurrentPtr() const
Returns current item.
Definition: Iterator.h:48
Gorgon::Containers::Iterator::operator[]
T_ & operator[](D_ ind) const
Index notation.
Definition: Iterator.h:78
Gorgon::Containers::Delete
void Delete(const I_ &first, const I_ &end)
This function works with collection iterators.
Definition: Iterator.h:394
Gorgon::Containers::Iterator::Next
bool Next()
Advances the iterator to the next item.
Definition: Iterator.h:58
Gorgon::Containers::ValueIterator::operator[]
T_ operator[](D_ ind) const
Index notation.
Definition: Iterator.h:262
Gorgon::Containers::ValueIterator
Generic iterator interface.
Definition: Iterator.h:219
Gorgon::Containers::Iterator::Compare
bool Compare(const I_ &iterator) const
Compares two iterators if they point to the same item.
Definition: Iterator.h:83
Gorgon::Containers::ValueIterator::operator+=
I_ & operator+=(D_ offset)
Moves the D_ by the given offset to forwards.
Definition: Iterator.h:335
Gorgon::Containers::Iterator::operator<=
bool operator<=(const I_ &iterator) const
Checks whether current operator is before or at the same point.
Definition: Iterator.h:113
Gorgon::Containers::Iterator::operator>
bool operator>(const I_ &iterator) const
Checks whether current operator is after the given.
Definition: Iterator.h:98
operator<<
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::ostream & >::type operator<<(std::ostream &out, const T_ &e)
Stream writer for upgraded enums.
Definition: Enum.h:326
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Containers::ValueIterator::operator<=
bool operator<=(const I_ &iterator) const
Checks whether current operator is before or at the same point.
Definition: Iterator.h:297
Gorgon::Containers::AddCopy
void AddCopy(C_ &target, const I_ &it)
This function copies the contents of the given iterator as long as it can be dereferenced to the give...
Definition: Iterator.h:415
Gorgon::Containers::Iterator::IsValid
bool IsValid() const
Checks if the iterator is pointing to a valid item.
Definition: Iterator.h:68
Gorgon::Containers::Find
I_ Find(const I_ &first, const I_ &end, const T_ &item)
This function works with collection iterators.
Definition: Iterator.h:403
Gorgon::Containers::ValueIterator::operator>=
bool operator>=(const I_ &iterator) const
Checks whether current operator is after or at the same point.
Definition: Iterator.h:292
Gorgon::Containers::ValueIterator::Compare
bool Compare(const I_ &iterator) const
Compares two iterators if they point to the same item.
Definition: Iterator.h:267
Gorgon::Containers::Iterator::operator<
bool operator<(const I_ &iterator) const
Checks if the current operator is before the given.
Definition: Iterator.h:103
Gorgon::Containers::Iterator::operator++
I_ & operator++()
Moves the iterator to forwards.
Definition: Iterator.h:165
Gorgon::Containers::Iterator::operator*
T_ & operator*() const
Dereferences the operator to get its value.
Definition: Iterator.h:195
Gorgon::Containers::ValueIterator::IsValid
bool IsValid() const
Checks if the iterator is pointing to a valid item.
Definition: Iterator.h:252
Gorgon::Containers::ValueIterator::ValueIterator
ValueIterator()
Cannot be constructed unless overridden.
Definition: Iterator.h:226
Gorgon::Containers::ValueIterator::operator>
bool operator>(const I_ &iterator) const
Checks whether current operator is after the given.
Definition: Iterator.h:282
Gorgon::Containers::Iterator::Previous
bool Previous()
Moves to the previous item.
Definition: Iterator.h:63
Gorgon::Containers::ValueIterator::operator==
bool operator==(const I_ &iterator) const
Compares two iterators if they point to the same item.
Definition: Iterator.h:277
Gorgon::Containers::Iterator::operator-
D_ operator-(const I_ &iterator) const
Returns the distance to the given iterator.
Definition: Iterator.h:130
Gorgon::Containers::ValueIterator::operator!=
bool operator!=(const I_ &it) const
Compares this iterator with another.
Definition: Iterator.h:302
Gorgon::Containers::Iterator::operator--
I_ & operator--()
Moves the iterator to backwards.
Definition: Iterator.h:172
Gorgon::end
std::vector< T_ >::const_iterator end(enum_type_id< T_ >)
Definition: Enum.h:288
Gorgon::Containers::Iterator::Iterator
Iterator()
Cannot be constructed unless overridden.
Definition: Iterator.h:38
Gorgon::Containers::ValueIterator::operator+
I_ operator+(D_ offset) const
Creates a new iterator adding the given offset.
Definition: Iterator.h:319
Gorgon::Containers::ValueIterator::Next
bool Next()
Advances the iterator to the next item.
Definition: Iterator.h:242
Gorgon::Containers::ValueIterator::Current
T_ Current() const
Returns current item.
Definition: Iterator.h:231
Gorgon::Containers::Iterator
Generic iterator interface.
Definition: Iterator.h:31
Gorgon::Containers::ValueIterator::operator=
I_ & operator=(const I_ &iterator)
Moves this iterator to the item pointed by the given.
Definition: Iterator.h:307
Gorgon::Containers::Iterator::operator+=
I_ & operator+=(D_ offset)
Moves the D_ by the given offset to forwards.
Definition: Iterator.h:151
Gorgon::Containers::Iterator::operator!=
bool operator!=(const I_ &it) const
Compares this iterator with another.
Definition: Iterator.h:118
Gorgon::Containers::Iterator::Distance
D_ Distance(const I_ &iterator) const
Returns the distance to the given iterator.
Definition: Iterator.h:88
Gorgon::Containers::Iterator::operator>=
bool operator>=(const I_ &iterator) const
Checks whether current operator is after or at the same point.
Definition: Iterator.h:108
Gorgon::Containers::Iterator::operator=
I_ & operator=(const I_ &iterator)
Moves this iterator to the item pointed by the given.
Definition: Iterator.h:123
Gorgon::Containers::Iterator::operator==
bool operator==(const I_ &iterator) const
Compares two iterators if they point to the same item.
Definition: Iterator.h:93
Gorgon::Containers::Iterator::operator-=
I_ & operator-=(D_ offset)
Moves the iterator by the given offset to backwards.
Definition: Iterator.h:158