Commit 02577a2c authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Add Value::ClearList()

This change that adds base::Value::ClearList() as a convenient way to
completely clear a list Value.

Bug: 646113
Change-Id: I83d2a1b8f51595ca0e70b88d66013ea600fd4b98
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1949798Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721466}
parent c1e2e73b
......@@ -436,6 +436,11 @@ size_t Value::EraseListValue(const Value& val) {
return EraseListValueIf([&val](const Value& other) { return val == other; });
}
void Value::ClearList() {
CHECK(is_list());
list_.clear();
}
Value* Value::FindKey(StringPiece key) {
return const_cast<Value*>(as_const(*this).FindKey(key));
}
......
......@@ -236,6 +236,10 @@ class BASE_EXPORT Value {
return old_size - list_.size();
}
// Erases all Values from the list.
// Note: This CHECKs that type() is Type::LIST.
void ClearList();
// |FindKey| looks up |key| in the underlying dictionary. If found, it returns
// a pointer to the element. Otherwise it returns nullptr.
// returned. Callers are expected to perform a check against null before using
......@@ -785,7 +789,7 @@ class BASE_EXPORT ListValue : public Value {
explicit ListValue(ListStorage&& in_list) noexcept;
// Clears the contents of this ListValue
// DEPRECATED, use GetList()::clear() instead.
// DEPRECATED, use ClearList() instead.
void Clear();
// Returns the number of Values in this list.
......
......@@ -599,6 +599,21 @@ TEST(ValuesTest, EraseListValueIf) {
EXPECT_EQ(0u, value.EraseListValueIf([](const auto& val) { return true; }));
}
TEST(ValuesTest, ClearList) {
ListValue value;
value.Append(1);
value.Append(2);
value.Append(3);
EXPECT_EQ(3u, value.GetList().size());
value.ClearList();
EXPECT_TRUE(value.GetList().empty());
// ClearList() should be idempotent.
value.ClearList();
EXPECT_TRUE(value.GetList().empty());
}
TEST(ValuesTest, FindKey) {
Value::DictStorage storage;
storage.emplace("foo", std::make_unique<Value>("bar"));
......
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