mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
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:
@@ -14,9 +14,21 @@
|
||||
|
||||
#include "connections/implementation/mediums/ble_v2/ble_utils.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "connections/implementation/mediums/ble_v2/ble_advertisement.h"
|
||||
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
|
||||
#include "connections/implementation/mediums/ble_v2/ble_packet.h"
|
||||
#include "connections/implementation/mediums/utils.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/prng.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
@@ -14,14 +14,19 @@
|
||||
|
||||
#include "connections/implementation/mediums/wifi_lan.h"
|
||||
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "connections/implementation/mediums/utils.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/cancellation_flag.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/mutex_lock.h"
|
||||
#include "internal/platform/nsd_service_info.h"
|
||||
#include "internal/platform/wifi_lan.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
@@ -392,6 +392,7 @@ cc_test(
|
||||
"//internal/platform/implementation/g3", # build_cleaner: keep
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_absl//absl/hash:hash_testing",
|
||||
"@com_google_absl//absl/strings:string_view",
|
||||
"@com_google_absl//absl/synchronization",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -14,11 +14,17 @@
|
||||
|
||||
#include "internal/platform/byte_array.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/hash/hash_testing.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -97,6 +103,81 @@ TEST(ByteArrayTest, CreateFromAbslStringReturnsTheSame) {
|
||||
EXPECT_EQ(bytes.AsStringView(), kTestString);
|
||||
}
|
||||
|
||||
TEST(ByteArrayTest, IteratorTypes) {
|
||||
static_assert(std::same_as<decltype(std::declval<ByteArray>().begin()),
|
||||
ByteArray::iterator>);
|
||||
static_assert(std::same_as<decltype(std::declval<ByteArray>().cbegin()),
|
||||
ByteArray::const_iterator>);
|
||||
static_assert(std::same_as<decltype(std::declval<ByteArray>().end()),
|
||||
ByteArray::iterator>);
|
||||
static_assert(std::same_as<decltype(std::declval<ByteArray>().cend()),
|
||||
ByteArray::const_iterator>);
|
||||
|
||||
static_assert(std::same_as<decltype(std::declval<const ByteArray>().begin()),
|
||||
ByteArray::const_iterator>);
|
||||
static_assert(std::same_as<decltype(std::declval<const ByteArray>().end()),
|
||||
ByteArray::const_iterator>);
|
||||
}
|
||||
|
||||
TEST(ByteArrayTest, Iterators) {
|
||||
ByteArray bytes("12345");
|
||||
const ByteArray const_bytes("12345");
|
||||
static constexpr auto kExpected = std::to_array({'1', '2', '3', '4', '5'});
|
||||
|
||||
// Check manual iteration.
|
||||
{
|
||||
size_t i = 0;
|
||||
for (auto it = bytes.begin(); it != bytes.end(); ++it) {
|
||||
ASSERT_LT(i, kExpected.size());
|
||||
EXPECT_EQ(*it, kExpected[i++]);
|
||||
}
|
||||
EXPECT_EQ(i, kExpected.size());
|
||||
}
|
||||
{
|
||||
size_t i = 0;
|
||||
for (auto it = bytes.cbegin(); it != bytes.cend(); ++it) {
|
||||
ASSERT_LT(i, kExpected.size());
|
||||
EXPECT_EQ(*it, kExpected[i++]);
|
||||
}
|
||||
EXPECT_EQ(i, kExpected.size());
|
||||
}
|
||||
{
|
||||
size_t i = 0;
|
||||
for (auto it = const_bytes.begin(); it != const_bytes.end(); ++it) {
|
||||
ASSERT_LT(i, kExpected.size());
|
||||
EXPECT_EQ(*it, kExpected[i++]);
|
||||
}
|
||||
EXPECT_EQ(i, kExpected.size());
|
||||
}
|
||||
|
||||
// Check range-for loops.
|
||||
{
|
||||
size_t i = 0;
|
||||
for (auto c : bytes) {
|
||||
ASSERT_LT(i, kExpected.size());
|
||||
EXPECT_EQ(c, kExpected[i++]);
|
||||
}
|
||||
EXPECT_EQ(i, kExpected.size());
|
||||
}
|
||||
{
|
||||
size_t i = 0;
|
||||
for (auto c : const_bytes) {
|
||||
ASSERT_LT(i, kExpected.size());
|
||||
EXPECT_EQ(c, kExpected[i++]);
|
||||
}
|
||||
EXPECT_EQ(i, kExpected.size());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ByteArrayTest, EmptyArrayIterators) {
|
||||
// It should be legal to call begin()/end() etc. on empty arrays.
|
||||
ByteArray bytes;
|
||||
const ByteArray const_bytes;
|
||||
EXPECT_EQ(bytes.begin(), bytes.end());
|
||||
EXPECT_EQ(bytes.cbegin(), bytes.cend());
|
||||
EXPECT_EQ(const_bytes.begin(), const_bytes.end());
|
||||
}
|
||||
|
||||
TEST(ByteArrayTest, Hash) {
|
||||
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly({
|
||||
ByteArray(),
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
#include <utility>
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/mutex_lock.h"
|
||||
#include "internal/weave/packet.h"
|
||||
|
||||
Reference in New Issue
Block a user