| 6 | #define BASE_CONTAINERS_CHECKED_ITERATORS_H_ |
| 7 |
| 8 | #include <iterator> |
| 9 | #include <memory> |
| 10 | #include <type_traits> |
| 11 |
| 12 | #include "base/containers/util.h" |
| 13 | #include "base/logging.h" |
| 14 |
| 15 | namespace base { |
| 16 |
| 17 | template <typename T> |
| 18 | class CheckedContiguousIterator { |
| 19 | public: |
| 20 | using difference_type = std::ptrdiff_t; |
| 21 | using value_type = std::remove_cv_t<T>; |
| 22 | using pointer = T*; |
| 23 | using reference = T&; |
| 24 | using iterator_category = std::random_access_iterator_tag; |
| 25 |
| 26 | // Required for converting constructor below. |
| 27 | template <typename U> |
| 28 | friend class CheckedContiguousIterator; |
| 29 |
| 30 | constexpr CheckedContiguousIterator() = default; |
| 31 | constexpr CheckedContiguousIterator(T* start, const T* end) |
| 32 | : CheckedContiguousIterator(start, start, end) {} |
| 33 | constexpr CheckedContiguousIterator(const T* start, T* current, const T* end) |
| 34 | : start_(start), current_(current), end_(end) { |
| 35 | CHECK_LE(start, current); |
| 36 | CHECK_LE(current, end); |
| 37 | } |
| 38 | constexpr CheckedContiguousIterator(const CheckedContiguousIterator& other) = |
| 39 | default; |
| 40 |
| 41 | // Converting constructor allowing conversions like CCI<T> to CCI<const T>, |
| 42 | // but disallowing CCI<const T> to CCI<T> or CCI<Derived> to CCI<Base>, which |
| 43 | // are unsafe. Furthermore, this is the same condition as used by the |
| 44 | // converting constructors of std::span<T> and std::unique_ptr<T[]>. |
| 45 | // See https://wg21.link/n4042 for details. |
| 46 | template < |
| 47 | typename U, |
| 48 | std::enable_if_t<std::is_convertible<U (*)[], T (*)[]>::value>* = nullptr> |
| 49 | constexpr CheckedContiguousIterator(const CheckedContiguousIterator<U>& other) |
| 50 | : start_(other.start_), current_(other.current_), end_(other.end_) { |
| 51 | // We explicitly don't delegate to the 3-argument constructor here. Its |
| 52 | // CHECKs would be redundant, since we expect |other| to maintain its own |
| 53 | // invariant. However, DCHECKs never hurt anybody. Presumably. |
| 54 | DCHECK_LE(other.start_, other.current_); |
| 55 | DCHECK_LE(other.current_, other.end_); |
| 56 | } |
| 57 |
| 58 | ~CheckedContiguousIterator() = default; |
| 59 |
| 60 | constexpr CheckedContiguousIterator& operator=( |
| 61 | const CheckedContiguousIterator& other) = default; |
| 62 |
| 63 | friend constexpr bool operator==(const CheckedContiguousIterator& lhs, |
| 64 | const CheckedContiguousIterator& rhs) { |
| 65 | lhs.CheckComparable(rhs); |
| 66 | return lhs.current_ == rhs.current_; |
| 67 | } |
| 68 |
| 69 | friend constexpr bool operator!=(const CheckedContiguousIterator& lhs, |
| 70 | const CheckedContiguousIterator& rhs) { |
| 71 | lhs.CheckComparable(rhs); |
| 72 | return lhs.current_ != rhs.current_; |
| 73 | } |
| 74 |
| 75 | friend constexpr bool operator<(const CheckedContiguousIterator& lhs, |
| 76 | const CheckedContiguousIterator& rhs) { |
| 77 | lhs.CheckComparable(rhs); |
| 78 | return lhs.current_ < rhs.current_; |
| 79 | } |
| 80 |
| 81 | friend constexpr bool operator<=(const CheckedContiguousIterator& lhs, |
| 82 | const CheckedContiguousIterator& rhs) { |
| 83 | lhs.CheckComparable(rhs); |
| 84 | return lhs.current_ <= rhs.current_; |
| 85 | } |
| 86 | friend constexpr bool operator>(const CheckedContiguousIterator& lhs, |
| 87 | const CheckedContiguousIterator& rhs) { |
| 88 | lhs.CheckComparable(rhs); |
| 89 | return lhs.current_ > rhs.current_; |
| 90 | } |
| 91 |
| 92 | friend constexpr bool operator>=(const CheckedContiguousIterator& lhs, |
| 93 | const CheckedContiguousIterator& rhs) { |
| 94 | lhs.CheckComparable(rhs); |
| 95 | return lhs.current_ >= rhs.current_; |
| 96 | } |
| 97 |
| 98 | constexpr CheckedContiguousIterator& operator++() { |
| 99 | CHECK_NE(current_, end_); |
| 100 | ++current_; |
| 101 | return *this; |
| 102 | } |
| 103 |
| 104 | constexpr CheckedContiguousIterator operator++(int) { |
| 105 | CheckedContiguousIterator old = *this; |
| 106 | ++*this; |
| 107 | return old; |
| 108 | } |
| 109 |
| 110 | constexpr CheckedContiguousIterator& operator--() { |
| 111 | CHECK_NE(current_, start_); |
| 112 | --current_; |
| 113 | return *this; |
| 114 | } |
| 115 |
| 116 | constexpr CheckedContiguousIterator operator--(int) { |
| 117 | CheckedContiguousIterator old = *this; |
| 118 | --*this; |
| 119 | return old; |
| 120 | } |
| 121 |
| 122 | constexpr CheckedContiguousIterator& operator+=(difference_type rhs) { |
| 123 | if (rhs > 0) { |
| 124 | CHECK_LE(rhs, end_ - current_); |
| 125 | } else { |
| 126 | CHECK_LE(-rhs, current_ - start_); |
| 127 | } |
| 128 | current_ += rhs; |
| 129 | return *this; |
| 130 | } |
| 131 |
| 132 | constexpr CheckedContiguousIterator operator+(difference_type rhs) const { |
| 133 | CheckedContiguousIterator it = *this; |
| 134 | it += rhs; |
| 135 | return it; |
| 136 | } |
| 137 |
| 138 | constexpr CheckedContiguousIterator& operator-=(difference_type rhs) { |
| 139 | if (rhs < 0) { |
| 140 | CHECK_LE(-rhs, end_ - current_); |
| 141 | } else { |
| 142 | CHECK_LE(rhs, current_ - start_); |
| 143 | } |
| 144 | current_ -= rhs; |
| 145 | return *this; |
| 146 | } |
| 147 |
| 148 | constexpr CheckedContiguousIterator operator-(difference_type rhs) const { |
| 149 | CheckedContiguousIterator it = *this; |
| 150 | it -= rhs; |
| 151 | return it; |
| 152 | } |
| 153 |
| 154 | constexpr friend difference_type operator-( |
| 155 | const CheckedContiguousIterator& lhs, |
| 156 | const CheckedContiguousIterator& rhs) { |
| 157 | lhs.CheckComparable(rhs); |
| 158 | return lhs.current_ - rhs.current_; |
| 159 | } |
| 160 |
| 161 | constexpr reference operator*() const { |
| 162 | CHECK_NE(current_, end_); |
| 163 | return *current_; |
| 164 | } |
| 165 |
| 166 | constexpr pointer operator->() const { |
| 167 | CHECK_NE(current_, end_); |
| 168 | return current_; |
| 169 | } |
| 170 |
| 171 | constexpr reference operator[](difference_type rhs) const { |
| 172 | CHECK_GE(rhs, 0); |
| 173 | CHECK_LT(rhs, end_ - current_); |
| 174 | return current_[rhs]; |
| 175 | } |
| 176 |
| 177 | static bool IsRangeMoveSafe(const CheckedContiguousIterator& from_begin, |
| 178 | const CheckedContiguousIterator& from_end, |
| 179 | const CheckedContiguousIterator& to) |
| 180 | WARN_UNUSED_RESULT { |
| 181 | if (from_end < from_begin) |
| 182 | return false; |
| 183 | const auto from_begin_uintptr = get_uintptr(from_begin.current_); |
| 184 | const auto from_end_uintptr = get_uintptr(from_end.current_); |
| 185 | const auto to_begin_uintptr = get_uintptr(to.current_); |
| 186 | const auto to_end_uintptr = |
| 187 | get_uintptr((to + std::distance(from_begin, from_end)).current_); |
| 188 |
| 189 | return to_begin_uintptr >= from_end_uintptr || |
| 190 | to_end_uintptr <= from_begin_uintptr; |
| 191 | } |
| 192 |
| 193 | private: |
| 194 | constexpr void CheckComparable(const CheckedContiguousIterator& other) const { |
| 195 | CHECK_EQ(start_, other.start_); |
| 196 | CHECK_EQ(end_, other.end_); |
| 197 | } |
| 198 |
| 199 | const T* start_ = nullptr; |
| 200 | T* current_ = nullptr; |
| 201 | const T* end_ = nullptr; |
| 202 | }; |
| 203 |
| 204 | template <typename T> |
| 205 | using CheckedContiguousConstIterator = CheckedContiguousIterator<const T>; |
| 206 |
| 207 | } // namespace base |
| 208 |
| 209 | #endif // BASE_CONTAINERS_CHECKED_ITERATORS_H_ |
| 210 |