Added an operator[] to Buffer, to make reading data easier.

Review URL: https://codereview.webrtc.org/1745033002

Cr-Commit-Position: refs/heads/master@{#11819}
This commit is contained in:
ossu 2016-02-29 09:36:37 -08:00 committed by Commit bot
parent 012f8c0e73
commit b9338ac62b
2 changed files with 49 additions and 0 deletions

View File

@ -124,6 +124,16 @@ class Buffer {
bool operator!=(const Buffer& buf) const { return !(*this == buf); }
uint8_t& operator[](size_t index) {
RTC_DCHECK_LT(index, size_);
return data()[index];
}
uint8_t operator[](size_t index) const {
RTC_DCHECK_LT(index, size_);
return data()[index];
}
// The SetData functions replace the contents of the buffer. They accept the
// same input types as the constructors.
template <typename T, typename internal::ByteType<T>::t = 0>

View File

@ -279,4 +279,43 @@ TEST(BufferTest, TestMutableLambdaSetAppend) {
}
}
TEST(BufferTest, TestBracketRead) {
Buffer buf(kTestData, 7);
EXPECT_EQ(buf.size(), 7u);
EXPECT_EQ(buf.capacity(), 7u);
EXPECT_NE(buf.data(), nullptr);
for (size_t i = 0; i != 7u; ++i) {
EXPECT_EQ(buf[i], kTestData[i]);
}
}
TEST(BufferTest, TestBracketReadConst) {
Buffer buf(kTestData, 7);
EXPECT_EQ(buf.size(), 7u);
EXPECT_EQ(buf.capacity(), 7u);
EXPECT_NE(buf.data(), nullptr);
const Buffer& cbuf = buf;
for (size_t i = 0; i != 7u; ++i) {
EXPECT_EQ(cbuf[i], kTestData[i]);
}
}
TEST(BufferTest, TestBracketWrite) {
Buffer buf(7);
EXPECT_EQ(buf.size(), 7u);
EXPECT_EQ(buf.capacity(), 7u);
EXPECT_NE(buf.data(), nullptr);
for (size_t i = 0; i != 7u; ++i) {
buf[i] = kTestData[i];
}
for (size_t i = 0; i != 7u; ++i) {
EXPECT_EQ(buf[i], kTestData[i]);
}
}
} // namespace rtc