0

WTF: Add CheckedBegin() and CheckedEnd() to WTF::Vector

This CL has no user-visible behavior changes.

Bug: 355003172
Change-Id: Ie3666c77dc35547ae845c5203a99ceb4fa971ecb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5856439
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Kent Tamura <tkent@chromium.org>
Auto-Submit: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1354933}
This commit is contained in:
Kent Tamura
2024-09-13 01:01:34 +00:00
committed by Chromium LUCI CQ
parent b5ef432521
commit bee21103a3

@ -987,6 +987,10 @@ class UncheckedIterator {
constexpr UncheckedIterator() = default;
explicit UncheckedIterator(T* cur) : current_(cur) {}
UncheckedIterator(const UncheckedIterator& other) = default;
// Allow implicit conversion from a base::CheckedContiguousIterator<T>.
// NOLINTNEXTLINE(google-explicit-constructor)
UncheckedIterator(const base::CheckedContiguousIterator<T>& other)
: current_(base::to_address(other)) {}
~UncheckedIterator() = default;
UncheckedIterator& operator=(const UncheckedIterator& other) = default;
@ -1316,6 +1320,11 @@ class Vector : private VectorBuffer<T, INLINE_CAPACITY, Allocator> {
const T* data() const { return Base::Buffer(); }
// Iterators and reverse iterators. They are invalidated on a reallocation.
//
// If you get a compiler warning about code adding or subtracting values
// from the iterators returned by these functions, you can either use the
// standard library or base::span::subspan to avoid the addition or
// subtraction, or use CheckedBegin() and CheckedEnd().
iterator begin() { return iterator(data()); }
iterator end() { return iterator(DataEnd()); }
const_iterator begin() const { return const_iterator(data()); }
@ -1330,6 +1339,25 @@ class Vector : private VectorBuffer<T, INLINE_CAPACITY, Allocator> {
return const_reverse_iterator(begin());
}
// Checked iterators.
// These iterators have runtime CHECK()s for incorrect operations. So
// they are safer and slower than begin() and end().
base::CheckedContiguousIterator<T> CheckedBegin() {
return base::CheckedContiguousIterator<T>(data(), DataEnd());
}
base::CheckedContiguousIterator<T> CheckedEnd() {
auto* e = DataEnd();
return base::CheckedContiguousIterator<T>(data(), e, e);
}
base::CheckedContiguousIterator<const T> CheckedBegin() const {
return base::CheckedContiguousIterator<const T>(data(), DataEnd());
}
base::CheckedContiguousIterator<const T> CheckedEnd() const {
auto* e = DataEnd();
return base::CheckedContiguousIterator<const T>(data(), e, e);
}
// Quick access to the first and the last element. It is invalid to call
// these functions when the vector is empty.
T& front() { return at(0); }