PandaTree
Iterator.h
1 #ifndef PandaTree_Framework_Iterator_h
2 #define PandaTree_Framework_Iterator_h
3 
4 #include <type_traits>
5 
6 namespace panda {
7  namespace utils {
8 
10  template<class C, bool is_const>
11  class Iterator {
12  typedef C container_type;
15 
16  friend container_type;
17  friend const_type;
18 
19  public:
20  typedef int difference_type;
22  typedef typename C::value_type value_type;
23  typedef typename std::conditional<is_const, typename C::const_reference, typename C::reference>::type reference;
24  typedef typename std::conditional<is_const, typename C::const_pointer, typename C::pointer>::type pointer;
25  typedef std::random_access_iterator_tag iterator_category;
26 
27  Iterator() {}
28  Iterator(mutable_type const& it) : unitSize_(it.unitSize_) { ptr_.obj = it.ptr_.obj; }
29 
30  // implicitly assumes unitSize_ is the same
31  self_type& operator=(const_type const& rhs) { ptr_.obj = rhs.ptr_.obj; return *this; }
32  self_type& operator=(mutable_type const& rhs) { ptr_.obj = rhs.ptr_.obj; return *this; }
33 
34  bool operator==(mutable_type const& rhs) const { return ptr_.obj == rhs.ptr_.obj; }
35  bool operator==(const_type const& rhs) const { return ptr_.obj == rhs.ptr_.obj; }
36  bool operator!=(mutable_type const& rhs) const { return ptr_.obj != rhs.ptr_.obj; }
37  bool operator!=(const_type const& rhs) const { return ptr_.obj != rhs.ptr_.obj; }
38 
39  self_type& operator++() { shiftPtr_(1); return *this; }
40  self_type operator++(int) { auto itr(*this); shiftPtr_(1); return itr; }
41  self_type& operator--() { shiftPtr_(-1); return *this; }
42  self_type operator--(int) { auto itr(*this); shiftPtr_(-1); return itr; }
43 
44  self_type& operator+=(int n) { shiftPtr_(n); return *this; }
45  self_type& operator-=(int n) { shiftPtr_(-n); return *this; }
46 
47  self_type operator+(int n) const { auto itr(*this); return (itr += n); }
48  self_type operator-(int n) const { auto itr(*this); return (itr -= n); }
49 
50  reference operator*() const { return *ptr_.obj; }
51  pointer operator->() const { return ptr_.obj; }
52 
53  int operator-(self_type const& rhs) const { return ptr_.obj - rhs.operator->(); }
54  reference operator[](int n) const { auto itr(*this); return *(itr += n); }
55 
56  bool operator<(self_type const& rhs) const { return this->operator-(rhs) < 0; }
57  bool operator>(self_type const& rhs) const { return this->operator-(rhs) > 0; }
58  bool operator<=(self_type const& rhs) const { return !(this->operator>(rhs)); }
59  bool operator>=(self_type const& rhs) const { return !(this->operator<(rhs)); }
60 
61  private:
62  Iterator(pointer p, unsigned unitSize) : unitSize_(unitSize) { ptr_.obj = p; }
63 
64  void shiftPtr_(int shift) { ptr_.ch += unitSize_ * shift; }
65 
66  typedef typename std::conditional<is_const, char const*, char*>::type char_pointer;
67 
68  union Ptr {
69  pointer obj;
70  char_pointer ch;
71  } ptr_;
72  unsigned const unitSize_{0};
73  };
74 
75  }
76 }
77 
78 #endif
Iterator class for containers.
Definition: Iterator.h:11
Definition: Array.h:11