1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// Factorisation du code commun dans une classe de base.
// Que ce soit un iterator, const_iterator, reverse_iterator, ...
// on a toujours le même comportement, donc inutile de se
// répéter.
template<typename ChildIter, typename BaseIter>
struct ptr_iterator_base_impl {
BaseIter i;
ptr_iterator_base_impl(const BaseIter& j) : i(j) {}
ChildIter operator ++ (int) {
ChildIter iter = static_cast<ChildIter&>(*this);
++i; return iter;
}
ChildIter& operator ++ () {
++i; return static_cast<ChildIter&>(*this);
}
ChildIter operator -- (int) {
ChildIter iter = static_cast<ChildIter&>(*this);
--i; return iter;
}
ChildIter& operator -- () {
--i; return static_cast<ChildIter&>(*this);
}
template<typename U>
ChildIter operator + (U d) const {
return i + d;
}
auto operator - (const ChildIter& iter) const -> decltype(i - i) {
return i - iter.i;
}
template<typename U, typename enable = typename std::enable_if<std::is_arithmetic<U>::value>::type>
ChildIter operator - (U d) const {
return i - d;
}
template<typename U, typename enable = typename std::enable_if<std::is_arithmetic<U>::value>::type>
ChildIter& operator += (U d) {
i += d; return static_cast<ChildIter&>(*this);
}
template<typename U, typename enable = typename std::enable_if<std::is_arithmetic<U>::value>::type>
ChildIter& operator -= (U d) {
i -= d; return static_cast<ChildIter&>(*this);
}
bool operator == (const ChildIter& iter) const {
return i == iter.i;
}
bool operator != (const ChildIter& iter) const {
return i != iter.i;
}
bool operator < (const ChildIter& iter) const {
return i < iter.i;
}
bool operator <= (const ChildIter& iter) const {
return i <= iter.i;
}
bool operator > (const ChildIter& iter) const {
return i > iter.i;
}
bool operator >= (const ChildIter& iter) const {
return i >= iter.i;
}
auto operator * () const -> decltype(**i) {
return **i;
}
auto operator -> () const -> decltype(*i) {
return *i;
}
BaseIter stdbase() const {
return i;
}
};
template<typename BaseIter>
class ptr_iterator_base;
// Le const_iterator
template<typename BaseIter>
class const_ptr_iterator_base :
private ptr_iterator_base_impl<const_ptr_iterator_base<BaseIter>, BaseIter> {
using impl = ptr_iterator_base_impl<const_ptr_iterator_base, BaseIter>;
friend impl;
public :
const_ptr_iterator_base() = default;
const_ptr_iterator_base(const BaseIter& j) : impl(j) {}
const_ptr_iterator_base(const ptr_iterator_base<BaseIter>& iter) : impl(iter.i) {}
const_ptr_iterator_base(const const_ptr_iterator_base&) = default;
const_ptr_iterator_base(const_ptr_iterator_base&&) = default;
const_ptr_iterator_base& operator = (const const_ptr_iterator_base&) = default;
const_ptr_iterator_base& operator = (const_ptr_iterator_base&&) = default;
using impl::operator*;
using impl::operator->;
using impl::operator+;
using impl::operator-;
using impl::operator++;
using impl::operator--;
using impl::operator+=;
using impl::operator-=;
using impl::operator==;
using impl::operator!=;
using impl::operator<;
using impl::operator<=;
using impl::operator>;
using impl::operator>=;
using impl::stdbase;
};
// L'iterator de base
template<typename BaseIter>
class ptr_iterator_base :
private ptr_iterator_base_impl<ptr_iterator_base<BaseIter>, BaseIter> {
template<typename N>
friend class const_ptr_iterator_base;
using impl = ptr_iterator_base_impl<ptr_iterator_base, BaseIter>;
friend impl;
public :
ptr_iterator_base() = default;
ptr_iterator_base(const BaseIter& j) : impl(j) {}
ptr_iterator_base(const ptr_iterator_base&) = default;
ptr_iterator_base(ptr_iterator_base&&) = default;
ptr_iterator_base& operator = (const ptr_iterator_base&) = default;
ptr_iterator_base& operator = (ptr_iterator_base&&) = default;
using impl::operator*;
using impl::operator->;
using impl::operator+;
using impl::operator-;
using impl::operator++;
using impl::operator--;
using impl::operator+=;
using impl::operator-=;
using impl::operator==;
using impl::operator!=;
using impl::operator<;
using impl::operator<=;
using impl::operator>;
using impl::operator>=;
using impl::stdbase;
};
// NB: tu peux facilement rajouter les reverse_iterator et const_reverse_iterator si besoin
// Cf. le code sur github. |
Partager