Commit ea877bea authored by Daniel Cheng's avatar Daniel Cheng Committed by Commit Bot

Add base::PickleIterator::ReachedEnd()

This can be useful if a bit of data serialized at the end is optional;
instead of having to encode an explicit boolean flag, the deserializer
can simply consult ReachedEnd().

Bug: 1055911
Change-Id: Id92bfa5ffbdd0c309367417ed12444eff2d94cb4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2091715
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747909}
parent 4e6416eb
...@@ -75,6 +75,8 @@ class BASE_EXPORT PickleIterator { ...@@ -75,6 +75,8 @@ class BASE_EXPORT PickleIterator {
return !!GetReadPointerAndAdvance(num_bytes); return !!GetReadPointerAndAdvance(num_bytes);
} }
bool ReachedEnd() const { return read_index_ == end_index_; }
private: private:
// Read Type from Pickle. // Read Type from Pickle.
template <typename Type> template <typename Type>
......
...@@ -570,4 +570,30 @@ TEST(PickleTest, ClaimBytes) { ...@@ -570,4 +570,30 @@ TEST(PickleTest, ClaimBytes) {
EXPECT_EQ(42, out_value); EXPECT_EQ(42, out_value);
} }
TEST(PickleTest, ReachedEnd) {
Pickle pickle;
pickle.WriteInt(1);
pickle.WriteInt(2);
pickle.WriteInt(3);
PickleIterator iter(pickle);
int out;
EXPECT_FALSE(iter.ReachedEnd());
EXPECT_TRUE(iter.ReadInt(&out));
EXPECT_EQ(1, out);
EXPECT_FALSE(iter.ReachedEnd());
EXPECT_TRUE(iter.ReadInt(&out));
EXPECT_EQ(2, out);
EXPECT_FALSE(iter.ReachedEnd());
EXPECT_TRUE(iter.ReadInt(&out));
EXPECT_EQ(3, out);
EXPECT_TRUE(iter.ReachedEnd());
EXPECT_FALSE(iter.ReadInt(&out));
EXPECT_TRUE(iter.ReachedEnd());
}
} // namespace base } // namespace base
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment