Add begin()/end() to ByteArray.

This allows this type to meet the requirements of e.g.
std::ranges::range, which is necessary for it to work with the
std::span range constructor, or the "non-legacy" constructor for
Chromium's base::span.

Fixes a number of IWYU problems along the way, to silence checks.

Bug: chromium:364987728
PiperOrigin-RevId: 689841844
This commit is contained in:
hai007
2024-10-25 10:50:00 -07:00
committed by Copybara-Service
parent 9aa2510055
commit 7c8071e847
6 changed files with 115 additions and 4 deletions
+13 -3
View File
@@ -17,19 +17,19 @@
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
namespace nearby {
class ByteArray {
public:
using iterator = std::string::iterator;
using const_iterator = std::string::const_iterator;
// Create an empty ByteArray
ByteArray() = default;
template <size_t N>
@@ -84,6 +84,16 @@ class ByteArray {
size_t size() const { return data_.size(); }
bool Empty() const { return data_.empty(); }
// Iterators. These allow `ByteArray` to meet the requirements of
// `std::ranges::contiguous_range`, which in turn make it implicitly
// convertible to e.g. `std::span`.
iterator begin() { return data_.begin(); }
const_iterator begin() const { return data_.begin(); }
const_iterator cbegin() const { return data_.cbegin(); }
iterator end() { return data_.end(); }
const_iterator end() const { return data_.end(); }
const_iterator cend() const { return data_.cend(); }
friend bool operator==(const ByteArray& lhs, const ByteArray& rhs);
friend bool operator!=(const ByteArray& lhs, const ByteArray& rhs);
friend bool operator<(const ByteArray& lhs, const ByteArray& rhs);