123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #ifndef BASE_CONTAINERS_CHECKED_ITERATORS_H_
- #define BASE_CONTAINERS_CHECKED_ITERATORS_H_
- #include <iterator>
- #include <memory>
- #include <type_traits>
- #include "base/containers/util.h"
- #include "base/logging.h"
- namespace base {
- template <typename T>
- class CheckedContiguousIterator {
- public:
- using difference_type = std::ptrdiff_t;
- using value_type = std::remove_cv_t<T>;
- using pointer = T*;
- using reference = T&;
- using iterator_category = std::random_access_iterator_tag;
-
- template <typename U>
- friend class CheckedContiguousIterator;
- constexpr CheckedContiguousIterator() = default;
- #if defined(_LIBCPP_VERSION)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename U>
- using PtrIfSafeToMemmove = std::enable_if_t<
- std::is_trivially_copy_assignable<std::remove_const_t<U>>::value,
- U*>;
- template <int&... ExplicitArgumentBarrier, typename U = T>
- constexpr CheckedContiguousIterator(PtrIfSafeToMemmove<U> ptr)
- : start_(ptr), current_(ptr), end_(ptr) {}
- template <int&... ExplicitArgumentBarrier, typename U = T>
- friend constexpr PtrIfSafeToMemmove<U> __unwrap_iter(
- CheckedContiguousIterator iter) {
- return iter.current_;
- }
- #endif
- constexpr CheckedContiguousIterator(T* start, const T* end)
- : CheckedContiguousIterator(start, start, end) {}
- constexpr CheckedContiguousIterator(const T* start, T* current, const T* end)
- : start_(start), current_(current), end_(end) {
- CHECK_LE(start, current);
- CHECK_LE(current, end);
- }
- constexpr CheckedContiguousIterator(const CheckedContiguousIterator& other) =
- default;
-
-
-
-
-
- template <
- typename U,
- std::enable_if_t<std::is_convertible<U (*)[], T (*)[]>::value>* = nullptr>
- constexpr CheckedContiguousIterator(const CheckedContiguousIterator<U>& other)
- : start_(other.start_), current_(other.current_), end_(other.end_) {
-
-
-
- DCHECK_LE(other.start_, other.current_);
- DCHECK_LE(other.current_, other.end_);
- }
- ~CheckedContiguousIterator() = default;
- constexpr CheckedContiguousIterator& operator=(
- const CheckedContiguousIterator& other) = default;
- friend constexpr bool operator==(const CheckedContiguousIterator& lhs,
- const CheckedContiguousIterator& rhs) {
- lhs.CheckComparable(rhs);
- return lhs.current_ == rhs.current_;
- }
- friend constexpr bool operator!=(const CheckedContiguousIterator& lhs,
- const CheckedContiguousIterator& rhs) {
- lhs.CheckComparable(rhs);
- return lhs.current_ != rhs.current_;
- }
- friend constexpr bool operator<(const CheckedContiguousIterator& lhs,
- const CheckedContiguousIterator& rhs) {
- lhs.CheckComparable(rhs);
- return lhs.current_ < rhs.current_;
- }
- friend constexpr bool operator<=(const CheckedContiguousIterator& lhs,
- const CheckedContiguousIterator& rhs) {
- lhs.CheckComparable(rhs);
- return lhs.current_ <= rhs.current_;
- }
- friend constexpr bool operator>(const CheckedContiguousIterator& lhs,
- const CheckedContiguousIterator& rhs) {
- lhs.CheckComparable(rhs);
- return lhs.current_ > rhs.current_;
- }
- friend constexpr bool operator>=(const CheckedContiguousIterator& lhs,
- const CheckedContiguousIterator& rhs) {
- lhs.CheckComparable(rhs);
- return lhs.current_ >= rhs.current_;
- }
- constexpr CheckedContiguousIterator& operator++() {
- CHECK_NE(current_, end_);
- ++current_;
- return *this;
- }
- constexpr CheckedContiguousIterator operator++(int) {
- CheckedContiguousIterator old = *this;
- ++*this;
- return old;
- }
- constexpr CheckedContiguousIterator& operator--() {
- CHECK_NE(current_, start_);
- --current_;
- return *this;
- }
- constexpr CheckedContiguousIterator operator--(int) {
- CheckedContiguousIterator old = *this;
- --*this;
- return old;
- }
- constexpr CheckedContiguousIterator& operator+=(difference_type rhs) {
- if (rhs > 0) {
- CHECK_LE(rhs, end_ - current_);
- } else {
- CHECK_LE(-rhs, current_ - start_);
- }
- current_ += rhs;
- return *this;
- }
- constexpr CheckedContiguousIterator operator+(difference_type rhs) const {
- CheckedContiguousIterator it = *this;
- it += rhs;
- return it;
- }
- constexpr CheckedContiguousIterator& operator-=(difference_type rhs) {
- if (rhs < 0) {
- CHECK_LE(-rhs, end_ - current_);
- } else {
- CHECK_LE(rhs, current_ - start_);
- }
- current_ -= rhs;
- return *this;
- }
- constexpr CheckedContiguousIterator operator-(difference_type rhs) const {
- CheckedContiguousIterator it = *this;
- it -= rhs;
- return it;
- }
- constexpr friend difference_type operator-(
- const CheckedContiguousIterator& lhs,
- const CheckedContiguousIterator& rhs) {
- lhs.CheckComparable(rhs);
- return lhs.current_ - rhs.current_;
- }
- constexpr reference operator*() const {
- CHECK_NE(current_, end_);
- return *current_;
- }
- constexpr pointer operator->() const {
- CHECK_NE(current_, end_);
- return current_;
- }
- constexpr reference operator[](difference_type rhs) const {
- CHECK_GE(rhs, 0);
- CHECK_LT(rhs, end_ - current_);
- return current_[rhs];
- }
- static bool IsRangeMoveSafe(const CheckedContiguousIterator& from_begin,
- const CheckedContiguousIterator& from_end,
- const CheckedContiguousIterator& to)
- WARN_UNUSED_RESULT {
- if (from_end < from_begin)
- return false;
- const auto from_begin_uintptr = get_uintptr(from_begin.current_);
- const auto from_end_uintptr = get_uintptr(from_end.current_);
- const auto to_begin_uintptr = get_uintptr(to.current_);
- const auto to_end_uintptr =
- get_uintptr((to + std::distance(from_begin, from_end)).current_);
- return to_begin_uintptr >= from_end_uintptr ||
- to_end_uintptr <= from_begin_uintptr;
- }
- private:
- constexpr void CheckComparable(const CheckedContiguousIterator& other) const {
- CHECK_EQ(start_, other.start_);
- CHECK_EQ(end_, other.end_);
- }
- const T* start_ = nullptr;
- T* current_ = nullptr;
- const T* end_ = nullptr;
- };
- template <typename T>
- using CheckedContiguousConstIterator = CheckedContiguousIterator<const T>;
- }
- #endif
|