Commit 55b0b2b8 authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Introduce base::Value::Append overloads

This change introduces several overloads for base::Value::Append,
supporting boolean, integer, double and string arguments. This
change is in preparation for no longer returning a mutable std::vector
reference from GetList(), making the previous advice to use emplace_back
no longer relevant.

Bug: 646113
Change-Id: I10607d14de336ef026a73689e2cae144cc5fec92
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1793083Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695056}
parent 5725de41
......@@ -340,6 +340,51 @@ span<const Value> Value::GetList() const {
return list_;
}
void Value::Append(bool value) {
CHECK(is_list());
list_.emplace_back(value);
}
void Value::Append(int value) {
CHECK(is_list());
list_.emplace_back(value);
}
void Value::Append(double value) {
CHECK(is_list());
list_.emplace_back(value);
}
void Value::Append(const char* value) {
CHECK(is_list());
list_.emplace_back(value);
}
void Value::Append(StringPiece value) {
CHECK(is_list());
list_.emplace_back(value);
}
void Value::Append(std::string&& value) {
CHECK(is_list());
list_.emplace_back(std::move(value));
}
void Value::Append(const char16* value) {
CHECK(is_list());
list_.emplace_back(value);
}
void Value::Append(StringPiece16 value) {
CHECK(is_list());
list_.emplace_back(value);
}
void Value::Append(Value&& value) {
CHECK(is_list());
list_.emplace_back(std::move(value));
}
Value* Value::FindKey(StringPiece key) {
return const_cast<Value*>(static_cast<const Value*>(this)->FindKey(key));
}
......
......@@ -178,6 +178,18 @@ class BASE_EXPORT Value {
ListStorage& GetList();
span<const Value> GetList() const;
// Appends |value| to the end of the list.
// Note: These CHECK that type() is Type::LIST.
void Append(bool value);
void Append(int value);
void Append(double value);
void Append(const char* value);
void Append(StringPiece value);
void Append(std::string&& value);
void Append(const char16* value);
void Append(StringPiece16 value);
void Append(Value&& value);
// |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
......@@ -803,24 +815,25 @@ class BASE_EXPORT ListValue : public Value {
// DEPRECATED, use GetList()::erase() instead.
iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
using Value::Append;
// Appends a Value to the end of the list.
// DEPRECATED, use GetList()::push_back() instead.
// DEPRECATED, use Value::Append() instead.
void Append(std::unique_ptr<Value> in_value);
// Convenience forms of Append.
// DEPRECATED, use GetList()::emplace_back() instead.
// DEPRECATED, use Value::Append() instead.
void AppendBoolean(bool in_value);
void AppendInteger(int in_value);
void AppendDouble(double in_value);
void AppendString(StringPiece in_value);
void AppendString(const string16& in_value);
// DEPRECATED, use GetList()::emplace_back() in a loop instead.
// DEPRECATED, use Value::Append() in a loop instead.
void AppendStrings(const std::vector<std::string>& in_values);
void AppendStrings(const std::vector<string16>& in_values);
// Appends a Value if it's not already present. Returns true if successful,
// or false if the value was already
// DEPRECATED, use std::find() with GetList()::push_back() instead.
// DEPRECATED, use std::find() with Value::Append() instead.
bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
// Insert a Value at index.
......
......@@ -458,6 +458,44 @@ TEST(ValuesTest, MoveList) {
EXPECT_EQ(123, blank.GetList().back().GetInt());
}
TEST(ValuesTest, Append) {
ListValue value;
value.Append(true);
EXPECT_TRUE(value.GetList().back().is_bool());
value.Append(123);
EXPECT_TRUE(value.GetList().back().is_int());
value.Append(3.14);
EXPECT_TRUE(value.GetList().back().is_double());
std::string str = "foo";
value.Append(str.c_str());
EXPECT_TRUE(value.GetList().back().is_string());
value.Append(StringPiece(str));
EXPECT_TRUE(value.GetList().back().is_string());
value.Append(std::move(str));
EXPECT_TRUE(value.GetList().back().is_string());
string16 str16 = ASCIIToUTF16("bar");
value.Append(str16.c_str());
EXPECT_TRUE(value.GetList().back().is_string());
value.Append(base::StringPiece16(str16));
EXPECT_TRUE(value.GetList().back().is_string());
value.Append(Value());
EXPECT_TRUE(value.GetList().back().is_none());
value.Append(Value(Value::Type::DICTIONARY));
EXPECT_TRUE(value.GetList().back().is_dict());
value.Append(Value(Value::Type::LIST));
EXPECT_TRUE(value.GetList().back().is_list());
}
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