summaryrefslogtreecommitdiffstats
path: root/chromium/base/values.cc
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2017-11-20 10:33:36 +0100
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2017-11-22 11:45:12 +0000
commitbe59a35641616a4cf23c4a13fa0632624b021c1b (patch)
tree9da183258bdf9cc413f7562079d25ace6955467f /chromium/base/values.cc
parentd702e4b6a64574e97fc7df8fe3238cde70242080 (diff)
BASELINE: Update Chromium to 62.0.3202.101
Change-Id: I2d5eca8117600df6d331f6166ab24d943d9814ac Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'chromium/base/values.cc')
-rw-r--r--chromium/base/values.cc371
1 files changed, 155 insertions, 216 deletions
diff --git a/chromium/base/values.cc b/chromium/base/values.cc
index 37a4e944363..723ad6f9c28 100644
--- a/chromium/base/values.cc
+++ b/chromium/base/values.cc
@@ -39,7 +39,8 @@ std::unique_ptr<Value> CopyListWithoutEmptyChildren(const Value& list) {
if (child_copy)
copy.GetList().push_back(std::move(*child_copy));
}
- return copy.GetList().empty() ? nullptr : MakeUnique<Value>(std::move(copy));
+ return copy.GetList().empty() ? nullptr
+ : std::make_unique<Value>(std::move(copy));
}
std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren(
@@ -49,7 +50,7 @@ std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren(
std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value());
if (child_copy) {
if (!copy)
- copy = MakeUnique<DictionaryValue>();
+ copy = std::make_unique<DictionaryValue>();
copy->SetWithoutPathExpansion(it.key(), std::move(child_copy));
}
}
@@ -66,7 +67,7 @@ std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) {
static_cast<const DictionaryValue&>(node));
default:
- return MakeUnique<Value>(node);
+ return std::make_unique<Value>(node.Clone());
}
}
@@ -75,11 +76,7 @@ std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) {
// static
std::unique_ptr<Value> Value::CreateWithCopiedBuffer(const char* buffer,
size_t size) {
- return MakeUnique<Value>(BlobStorage(buffer, buffer + size));
-}
-
-Value::Value(const Value& that) {
- InternalCopyConstructFrom(that);
+ return std::make_unique<Value>(BlobStorage(buffer, buffer + size));
}
Value::Value(Value&& that) noexcept {
@@ -163,30 +160,30 @@ Value::Value(BlobStorage&& in_blob) noexcept : type_(Type::BINARY) {
binary_value_.Init(std::move(in_blob));
}
+Value::Value(const DictStorage& in_dict) : type_(Type::DICTIONARY) {
+ dict_.Init();
+ dict_->reserve(in_dict.size());
+ for (const auto& it : in_dict) {
+ dict_->emplace_hint(dict_->end(), it.first,
+ MakeUnique<Value>(it.second->Clone()));
+ }
+}
+
Value::Value(DictStorage&& in_dict) noexcept : type_(Type::DICTIONARY) {
dict_.Init(std::move(in_dict));
}
Value::Value(const ListStorage& in_list) : type_(Type::LIST) {
- list_.Init(in_list);
+ list_.Init();
+ list_->reserve(in_list.size());
+ for (const auto& val : in_list)
+ list_->emplace_back(val.Clone());
}
Value::Value(ListStorage&& in_list) noexcept : type_(Type::LIST) {
list_.Init(std::move(in_list));
}
-Value& Value::operator=(const Value& that) {
- if (type_ == that.type_) {
- InternalCopyAssignFromSameType(that);
- } else {
- // This is not a self assignment because the type_ doesn't match.
- InternalCleanup();
- InternalCopyConstructFrom(that);
- }
-
- return *this;
-}
-
Value& Value::operator=(Value&& that) noexcept {
InternalCleanup();
InternalMoveConstructFrom(std::move(that));
@@ -194,6 +191,30 @@ Value& Value::operator=(Value&& that) noexcept {
return *this;
}
+Value Value::Clone() const {
+ switch (type_) {
+ case Type::NONE:
+ return Value();
+ case Type::BOOLEAN:
+ return Value(bool_value_);
+ case Type::INTEGER:
+ return Value(int_value_);
+ case Type::DOUBLE:
+ return Value(double_value_);
+ case Type::STRING:
+ return Value(*string_value_);
+ case Type::BINARY:
+ return Value(*binary_value_);
+ case Type::DICTIONARY:
+ return Value(*dict_);
+ case Type::LIST:
+ return Value(*list_);
+ }
+
+ NOTREACHED();
+ return Value();
+}
+
Value::~Value() {
InternalCleanup();
}
@@ -244,87 +265,124 @@ const Value::ListStorage& Value::GetList() const {
return *list_;
}
-Value::dict_iterator Value::FindKey(const char* key) {
- return FindKey(std::string(key));
+Value* Value::FindKey(StringPiece key) {
+ return const_cast<Value*>(static_cast<const Value*>(this)->FindKey(key));
}
-Value::dict_iterator Value::FindKey(const std::string& key) {
+const Value* Value::FindKey(StringPiece key) const {
CHECK(is_dict());
- return dict_iterator(dict_->find(key));
+ auto found = dict_->find(key);
+ if (found == dict_->end())
+ return nullptr;
+ return found->second.get();
}
-Value::const_dict_iterator Value::FindKey(const char* key) const {
- return FindKey(std::string(key));
+Value* Value::FindKeyOfType(StringPiece key, Type type) {
+ return const_cast<Value*>(
+ static_cast<const Value*>(this)->FindKeyOfType(key, type));
}
-Value::const_dict_iterator Value::FindKey(const std::string& key) const {
- CHECK(is_dict());
- return const_dict_iterator(dict_->find(key));
+const Value* Value::FindKeyOfType(StringPiece key, Type type) const {
+ const Value* result = FindKey(key);
+ if (!result || result->type() != type)
+ return nullptr;
+ return result;
}
-Value::dict_iterator Value::FindKeyOfType(const char* key, Type type) {
- return FindKeyOfType(std::string(key), type);
+Value* Value::SetKey(StringPiece key, Value value) {
+ CHECK(is_dict());
+ return ((*dict_)[key.as_string()] = std::make_unique<Value>(std::move(value)))
+ .get();
}
-Value::dict_iterator Value::FindKeyOfType(const std::string& key, Type type) {
+Value* Value::SetKey(std::string&& key, Value value) {
CHECK(is_dict());
- auto iter = dict_->find(key);
- return dict_iterator((iter != dict_->end() && iter->second->IsType(type))
- ? iter
- : dict_->end());
+ return ((*dict_)[std::move(key)] = std::make_unique<Value>(std::move(value)))
+ .get();
}
-Value::const_dict_iterator Value::FindKeyOfType(const char* key,
- Type type) const {
- return FindKeyOfType(std::string(key), type);
+Value* Value::SetKey(const char* key, Value value) {
+ return SetKey(StringPiece(key), std::move(value));
}
-Value::const_dict_iterator Value::FindKeyOfType(const std::string& key,
- Type type) const {
- CHECK(is_dict());
- auto iter = dict_->find(key);
- return const_dict_iterator(
- (iter != dict_->end() && iter->second->IsType(type)) ? iter
- : dict_->end());
+Value* Value::FindPath(std::initializer_list<StringPiece> path) {
+ return const_cast<Value*>(const_cast<const Value*>(this)->FindPath(path));
}
-Value::dict_iterator Value::SetKey(const char* key, Value value) {
- return SetKey(std::string(key), std::move(value));
+Value* Value::FindPath(span<const StringPiece> path) {
+ return const_cast<Value*>(const_cast<const Value*>(this)->FindPath(path));
}
-Value::dict_iterator Value::SetKey(const std::string& key, Value value) {
- CHECK(is_dict());
- auto iter = dict_->find(key);
- if (iter != dict_->end()) {
- *iter->second = std::move(value);
- return dict_iterator(iter);
+const Value* Value::FindPath(std::initializer_list<StringPiece> path) const {
+ return FindPath(make_span(path.begin(), path.size()));
+}
+
+const Value* Value::FindPath(span<const StringPiece> path) const {
+ const Value* cur = this;
+ for (const StringPiece component : path) {
+ if (!cur->is_dict() || (cur = cur->FindKey(component)) == nullptr)
+ return nullptr;
}
+ return cur;
+}
- return dict_iterator(
- dict_->emplace(key, MakeUnique<Value>(std::move(value))).first);
+Value* Value::FindPathOfType(std::initializer_list<StringPiece> path,
+ Type type) {
+ return const_cast<Value*>(
+ const_cast<const Value*>(this)->FindPathOfType(path, type));
}
-Value::dict_iterator Value::SetKey(std::string&& key, Value value) {
- CHECK(is_dict());
- auto iter = dict_->find(key);
- if (iter != dict_->end()) {
- *iter->second = std::move(value);
- return dict_iterator(iter);
- }
+Value* Value::FindPathOfType(span<const StringPiece> path, Type type) {
+ return const_cast<Value*>(
+ const_cast<const Value*>(this)->FindPathOfType(path, type));
+}
- return dict_iterator(
- dict_->emplace(std::move(key), MakeUnique<Value>(std::move(value)))
- .first);
+const Value* Value::FindPathOfType(std::initializer_list<StringPiece> path,
+ Type type) const {
+ return FindPathOfType(make_span(path.begin(), path.size()), type);
}
-Value::dict_iterator Value::DictEnd() {
- CHECK(is_dict());
- return dict_iterator(dict_->end());
+const Value* Value::FindPathOfType(span<const StringPiece> path,
+ Type type) const {
+ const Value* result = FindPath(path);
+ if (!result || !result->IsType(type))
+ return nullptr;
+ return result;
}
-Value::const_dict_iterator Value::DictEnd() const {
- CHECK(is_dict());
- return const_dict_iterator(dict_->end());
+Value* Value::SetPath(std::initializer_list<StringPiece> path, Value value) {
+ return SetPath(make_span(path.begin(), path.size()), std::move(value));
+}
+
+Value* Value::SetPath(span<const StringPiece> path, Value value) {
+ DCHECK_NE(path.begin(), path.end()); // Can't be empty path.
+
+ // Walk/construct intermediate dictionaries. The last element requires
+ // special handling so skip it in this loop.
+ Value* cur = this;
+ const StringPiece* cur_path = path.begin();
+ for (; (cur_path + 1) < path.end(); ++cur_path) {
+ if (!cur->is_dict())
+ return nullptr;
+
+ // Use lower_bound to avoid doing the search twice for missing keys.
+ const StringPiece path_component = *cur_path;
+ auto found = cur->dict_->lower_bound(path_component);
+ if (found == cur->dict_->end() || found->first != path_component) {
+ // No key found, insert one.
+ auto inserted =
+ cur->dict_->emplace_hint(found, path_component.as_string(),
+ std::make_unique<Value>(Type::DICTIONARY));
+ cur = inserted->second.get();
+ } else {
+ cur = found->second.get();
+ }
+ }
+
+ // "cur" will now contain the last dictionary to insert or replace into.
+ if (!cur->is_dict())
+ return nullptr;
+ return cur->SetKey(*cur_path, std::move(value));
}
Value::dict_iterator_proxy Value::DictItems() {
@@ -430,11 +488,11 @@ bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
}
Value* Value::DeepCopy() const {
- return new Value(*this);
+ return new Value(Clone());
}
std::unique_ptr<Value> Value::CreateDeepCopy() const {
- return MakeUnique<Value>(*this);
+ return std::make_unique<Value>(Clone());
}
bool operator==(const Value& lhs, const Value& rhs) {
@@ -530,21 +588,12 @@ bool Value::Equals(const Value* other) const {
return *this == *other;
}
-// static
-bool Value::Equals(const Value* a, const Value* b) {
- if ((a == NULL) && (b == NULL))
- return true;
- if ((a == NULL) ^ (b == NULL))
- return false;
- return *a == *b;
-}
+void Value::InternalMoveConstructFrom(Value&& that) {
+ type_ = that.type_;
-void Value::InternalCopyFundamentalValue(const Value& that) {
switch (type_) {
case Type::NONE:
- // Nothing to do.
return;
-
case Type::BOOLEAN:
bool_value_ = that.bool_value_;
return;
@@ -554,57 +603,6 @@ void Value::InternalCopyFundamentalValue(const Value& that) {
case Type::DOUBLE:
double_value_ = that.double_value_;
return;
-
- default:
- NOTREACHED();
- }
-}
-
-void Value::InternalCopyConstructFrom(const Value& that) {
- type_ = that.type_;
-
- switch (type_) {
- case Type::NONE:
- case Type::BOOLEAN:
- case Type::INTEGER:
- case Type::DOUBLE:
- InternalCopyFundamentalValue(that);
- return;
-
- case Type::STRING:
- string_value_.Init(*that.string_value_);
- return;
- case Type::BINARY:
- binary_value_.Init(*that.binary_value_);
- return;
- // DictStorage is a move-only type due to the presence of unique_ptrs. This
- // is why the explicit copy of every element is necessary here.
- // TODO(crbug.com/646113): Clean this up when DictStorage can be copied
- // directly.
- case Type::DICTIONARY:
- dict_.Init();
- for (const auto& it : *that.dict_) {
- dict_->emplace_hint(dict_->end(), it.first,
- MakeUnique<Value>(*it.second));
- }
- return;
- case Type::LIST:
- list_.Init(*that.list_);
- return;
- }
-}
-
-void Value::InternalMoveConstructFrom(Value&& that) {
- type_ = that.type_;
-
- switch (type_) {
- case Type::NONE:
- case Type::BOOLEAN:
- case Type::INTEGER:
- case Type::DOUBLE:
- InternalCopyFundamentalValue(that);
- return;
-
case Type::STRING:
string_value_.InitFromMove(std::move(that.string_value_));
return;
@@ -620,40 +618,6 @@ void Value::InternalMoveConstructFrom(Value&& that) {
}
}
-void Value::InternalCopyAssignFromSameType(const Value& that) {
- // TODO(crbug.com/646113): make this a DCHECK once base::Value does not have
- // subclasses.
- CHECK_EQ(type_, that.type_);
-
- switch (type_) {
- case Type::NONE:
- case Type::BOOLEAN:
- case Type::INTEGER:
- case Type::DOUBLE:
- InternalCopyFundamentalValue(that);
- return;
-
- case Type::STRING:
- *string_value_ = *that.string_value_;
- return;
- case Type::BINARY:
- *binary_value_ = *that.binary_value_;
- return;
- // DictStorage is a move-only type due to the presence of unique_ptrs. This
- // is why the explicit call to the copy constructor is necessary here.
- // TODO(crbug.com/646113): Clean this up when DictStorage can be copied
- // directly.
- case Type::DICTIONARY: {
- Value copy = that;
- *dict_ = std::move(*copy.dict_);
- return;
- }
- case Type::LIST:
- *list_ = *that.list_;
- return;
- }
-}
-
void Value::InternalCleanup() {
switch (type_) {
case Type::NONE:
@@ -692,10 +656,13 @@ std::unique_ptr<DictionaryValue> DictionaryValue::From(
}
DictionaryValue::DictionaryValue() : Value(Type::DICTIONARY) {}
+DictionaryValue::DictionaryValue(const DictStorage& in_dict) : Value(in_dict) {}
+DictionaryValue::DictionaryValue(DictStorage&& in_dict) noexcept
+ : Value(std::move(in_dict)) {}
bool DictionaryValue::HasKey(StringPiece key) const {
DCHECK(IsStringUTF8(key));
- auto current_entry = dict_->find(key.as_string());
+ auto current_entry = dict_->find(key);
DCHECK((current_entry == dict_->end()) || current_entry->second);
return current_entry != dict_->end();
}
@@ -718,7 +685,7 @@ Value* DictionaryValue::Set(StringPiece path, std::unique_ptr<Value> in_value) {
DictionaryValue* child_dictionary = nullptr;
if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
child_dictionary = current_dictionary->SetDictionaryWithoutPathExpansion(
- key, MakeUnique<DictionaryValue>());
+ key, std::make_unique<DictionaryValue>());
}
current_dictionary = child_dictionary;
@@ -730,23 +697,23 @@ Value* DictionaryValue::Set(StringPiece path, std::unique_ptr<Value> in_value) {
}
Value* DictionaryValue::SetBoolean(StringPiece path, bool in_value) {
- return Set(path, MakeUnique<Value>(in_value));
+ return Set(path, std::make_unique<Value>(in_value));
}
Value* DictionaryValue::SetInteger(StringPiece path, int in_value) {
- return Set(path, MakeUnique<Value>(in_value));
+ return Set(path, std::make_unique<Value>(in_value));
}
Value* DictionaryValue::SetDouble(StringPiece path, double in_value) {
- return Set(path, MakeUnique<Value>(in_value));
+ return Set(path, std::make_unique<Value>(in_value));
}
Value* DictionaryValue::SetString(StringPiece path, StringPiece in_value) {
- return Set(path, MakeUnique<Value>(in_value));
+ return Set(path, std::make_unique<Value>(in_value));
}
Value* DictionaryValue::SetString(StringPiece path, const string16& in_value) {
- return Set(path, MakeUnique<Value>(in_value));
+ return Set(path, std::make_unique<Value>(in_value));
}
DictionaryValue* DictionaryValue::SetDictionary(
@@ -766,32 +733,6 @@ Value* DictionaryValue::SetWithoutPathExpansion(
return ((*dict_)[key.as_string()] = std::move(in_value)).get();
}
-Value* DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path,
- bool in_value) {
- return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
-}
-
-Value* DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path,
- int in_value) {
- return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
-}
-
-Value* DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path,
- double in_value) {
- return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
-}
-
-Value* DictionaryValue::SetStringWithoutPathExpansion(StringPiece path,
- StringPiece in_value) {
- return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
-}
-
-Value* DictionaryValue::SetStringWithoutPathExpansion(
- StringPiece path,
- const string16& in_value) {
- return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value));
-}
-
DictionaryValue* DictionaryValue::SetDictionaryWithoutPathExpansion(
StringPiece path,
std::unique_ptr<DictionaryValue> in_value) {
@@ -949,7 +890,7 @@ bool DictionaryValue::GetList(StringPiece path, ListValue** out_value) {
bool DictionaryValue::GetWithoutPathExpansion(StringPiece key,
const Value** out_value) const {
DCHECK(IsStringUTF8(key));
- auto entry_iterator = dict_->find(key.as_string());
+ auto entry_iterator = dict_->find(key);
if (entry_iterator == dict_->end())
return false;
@@ -1078,7 +1019,7 @@ bool DictionaryValue::RemoveWithoutPathExpansion(
StringPiece key,
std::unique_ptr<Value>* out_value) {
DCHECK(IsStringUTF8(key));
- auto entry_iterator = dict_->find(key.as_string());
+ auto entry_iterator = dict_->find(key);
if (entry_iterator == dict_->end())
return false;
@@ -1113,7 +1054,7 @@ std::unique_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren()
std::unique_ptr<DictionaryValue> copy =
CopyDictionaryWithoutEmptyChildren(*this);
if (!copy)
- copy = MakeUnique<DictionaryValue>();
+ copy = std::make_unique<DictionaryValue>();
return copy;
}
@@ -1131,7 +1072,7 @@ void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
}
}
// All other cases: Make a copy and hook it up.
- SetWithoutPathExpansion(it.key(), MakeUnique<Value>(*merge_value));
+ SetKey(it.key(), merge_value->Clone());
}
}
@@ -1148,11 +1089,11 @@ DictionaryValue::Iterator::Iterator(const Iterator& other) = default;
DictionaryValue::Iterator::~Iterator() {}
DictionaryValue* DictionaryValue::DeepCopy() const {
- return new DictionaryValue(*this);
+ return new DictionaryValue(*dict_);
}
std::unique_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const {
- return MakeUnique<DictionaryValue>(*this);
+ return std::make_unique<DictionaryValue>(*dict_);
}
///////////////////// ListValue ////////////////////
@@ -1168,9 +1109,7 @@ std::unique_ptr<ListValue> ListValue::From(std::unique_ptr<Value> value) {
}
ListValue::ListValue() : Value(Type::LIST) {}
-
ListValue::ListValue(const ListStorage& in_list) : Value(in_list) {}
-
ListValue::ListValue(ListStorage&& in_list) noexcept
: Value(std::move(in_list)) {}
@@ -1308,7 +1247,7 @@ bool ListValue::Remove(size_t index, std::unique_ptr<Value>* out_value) {
return false;
if (out_value)
- *out_value = MakeUnique<Value>(std::move((*list_)[index]));
+ *out_value = std::make_unique<Value>(std::move((*list_)[index]));
list_->erase(list_->begin() + index);
return true;
@@ -1330,7 +1269,7 @@ bool ListValue::Remove(const Value& value, size_t* index) {
ListValue::iterator ListValue::Erase(iterator iter,
std::unique_ptr<Value>* out_value) {
if (out_value)
- *out_value = MakeUnique<Value>(std::move(*iter));
+ *out_value = std::make_unique<Value>(std::move(*iter));
return list_->erase(iter);
}
@@ -1399,11 +1338,11 @@ void ListValue::Swap(ListValue* other) {
}
ListValue* ListValue::DeepCopy() const {
- return new ListValue(*this);
+ return new ListValue(*list_);
}
std::unique_ptr<ListValue> ListValue::CreateDeepCopy() const {
- return MakeUnique<ListValue>(*this);
+ return std::make_unique<ListValue>(*list_);
}
ValueSerializer::~ValueSerializer() {