OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/values.h" | 5 #include "base/values.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
528 case Type::DICTIONARY: | 528 case Type::DICTIONARY: |
529 dict_ptr_.InitFromMove(std::move(that.dict_ptr_)); | 529 dict_ptr_.InitFromMove(std::move(that.dict_ptr_)); |
530 return; | 530 return; |
531 case Type::LIST: | 531 case Type::LIST: |
532 list_.InitFromMove(std::move(that.list_)); | 532 list_.InitFromMove(std::move(that.list_)); |
533 return; | 533 return; |
534 } | 534 } |
535 } | 535 } |
536 | 536 |
537 void Value::InternalCopyAssignFrom(const Value& that) { | 537 void Value::InternalCopyAssignFrom(const Value& that) { |
538 type_ = that.type_; | 538 CHECK_EQ(type_, that.type_); |
jdoerrie
2017/03/06 12:34:43
You could add a comment here that this does not pr
vabr (Chromium)
2017/03/06 17:09:32
Yes, it is confusing. In fact, as I studied the cr
| |
539 | 539 |
540 switch (type_) { | 540 switch (type_) { |
541 case Type::NONE: | 541 case Type::NONE: |
542 case Type::BOOLEAN: | 542 case Type::BOOLEAN: |
543 case Type::INTEGER: | 543 case Type::INTEGER: |
544 case Type::DOUBLE: | 544 case Type::DOUBLE: |
545 InternalCopyFundamentalValue(that); | 545 InternalCopyFundamentalValue(that); |
546 return; | 546 return; |
547 | 547 |
548 case Type::STRING: | 548 case Type::STRING: |
549 *string_value_ = *that.string_value_; | 549 *string_value_ = *that.string_value_; |
550 return; | 550 return; |
551 case Type::BINARY: | 551 case Type::BINARY: |
552 *binary_value_ = *that.binary_value_; | 552 *binary_value_ = *that.binary_value_; |
553 return; | 553 return; |
554 // DictStorage and ListStorage are move-only types due to the presence of | 554 // DictStorage and ListStorage are move-only types due to the presence of |
555 // unique_ptrs. This is why the call to |CreateDeepCopy| is necessary here. | 555 // unique_ptrs. This is why the call to |CreateDeepCopy| is necessary here. |
556 // TODO(crbug.com/646113): Clean this up when DictStorage and ListStorage | 556 // TODO(crbug.com/646113): Clean this up when DictStorage and ListStorage |
557 // can be copied directly. | 557 // can be copied directly. |
558 case Type::DICTIONARY: | 558 case Type::DICTIONARY: |
559 *dict_ptr_ = std::move(*that.CreateDeepCopy()->dict_ptr_); | 559 *dict_ptr_ = std::move(*that.CreateDeepCopy()->dict_ptr_); |
560 return; | 560 return; |
561 case Type::LIST: | 561 case Type::LIST: |
562 *list_ = std::move(*that.CreateDeepCopy()->list_); | 562 *list_ = std::move(*that.CreateDeepCopy()->list_); |
563 return; | 563 return; |
564 } | 564 } |
565 } | 565 } |
566 | 566 |
567 void Value::InternalMoveAssignFrom(Value&& that) { | 567 void Value::InternalMoveAssignFrom(Value&& that) { |
568 type_ = that.type_; | 568 CHECK_EQ(type_, that.type_); |
jdoerrie
2017/03/06 12:34:43
See above (s/copy/move).
vabr (Chromium)
2017/03/06 17:09:32
Done.
| |
569 | 569 |
570 switch (type_) { | 570 switch (type_) { |
571 case Type::NONE: | 571 case Type::NONE: |
572 case Type::BOOLEAN: | 572 case Type::BOOLEAN: |
573 case Type::INTEGER: | 573 case Type::INTEGER: |
574 case Type::DOUBLE: | 574 case Type::DOUBLE: |
575 InternalCopyFundamentalValue(that); | 575 InternalCopyFundamentalValue(that); |
576 return; | 576 return; |
577 | 577 |
578 case Type::STRING: | 578 case Type::STRING: |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1031 std::unique_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren() | 1031 std::unique_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren() |
1032 const { | 1032 const { |
1033 std::unique_ptr<DictionaryValue> copy = | 1033 std::unique_ptr<DictionaryValue> copy = |
1034 CopyDictionaryWithoutEmptyChildren(*this); | 1034 CopyDictionaryWithoutEmptyChildren(*this); |
1035 if (!copy) | 1035 if (!copy) |
1036 copy.reset(new DictionaryValue); | 1036 copy.reset(new DictionaryValue); |
1037 return copy; | 1037 return copy; |
1038 } | 1038 } |
1039 | 1039 |
1040 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { | 1040 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
1041 CHECK(dictionary->is_dict()); | |
1041 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { | 1042 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { |
1042 const Value* merge_value = &it.value(); | 1043 const Value* merge_value = &it.value(); |
1043 // Check whether we have to merge dictionaries. | 1044 // Check whether we have to merge dictionaries. |
1044 if (merge_value->IsType(Value::Type::DICTIONARY)) { | 1045 if (merge_value->IsType(Value::Type::DICTIONARY)) { |
1045 DictionaryValue* sub_dict; | 1046 DictionaryValue* sub_dict; |
1046 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) { | 1047 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) { |
1047 sub_dict->MergeDictionary( | 1048 sub_dict->MergeDictionary( |
1048 static_cast<const DictionaryValue*>(merge_value)); | 1049 static_cast<const DictionaryValue*>(merge_value)); |
1049 continue; | 1050 continue; |
1050 } | 1051 } |
1051 } | 1052 } |
1052 // All other cases: Make a copy and hook it up. | 1053 // All other cases: Make a copy and hook it up. |
1053 SetWithoutPathExpansion(it.key(), | 1054 SetWithoutPathExpansion(it.key(), |
1054 base::WrapUnique(merge_value->DeepCopy())); | 1055 base::WrapUnique(merge_value->DeepCopy())); |
1055 } | 1056 } |
1056 } | 1057 } |
1057 | 1058 |
1058 void DictionaryValue::Swap(DictionaryValue* other) { | 1059 void DictionaryValue::Swap(DictionaryValue* other) { |
1060 CHECK(other->is_dict()); | |
1059 dict_ptr_->swap(*(other->dict_ptr_)); | 1061 dict_ptr_->swap(*(other->dict_ptr_)); |
1060 } | 1062 } |
1061 | 1063 |
1062 DictionaryValue::Iterator::Iterator(const DictionaryValue& target) | 1064 DictionaryValue::Iterator::Iterator(const DictionaryValue& target) |
1063 : target_(target), it_((*target.dict_ptr_)->begin()) {} | 1065 : target_(target), it_((*target.dict_ptr_)->begin()) {} |
1064 | 1066 |
1065 DictionaryValue::Iterator::Iterator(const Iterator& other) = default; | 1067 DictionaryValue::Iterator::Iterator(const Iterator& other) = default; |
1066 | 1068 |
1067 DictionaryValue::Iterator::~Iterator() {} | 1069 DictionaryValue::Iterator::~Iterator() {} |
1068 | 1070 |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1323 } | 1325 } |
1324 | 1326 |
1325 ListValue::const_iterator ListValue::Find(const Value& value) const { | 1327 ListValue::const_iterator ListValue::Find(const Value& value) const { |
1326 return std::find_if(list_->begin(), list_->end(), | 1328 return std::find_if(list_->begin(), list_->end(), |
1327 [&value](const std::unique_ptr<Value>& entry) { | 1329 [&value](const std::unique_ptr<Value>& entry) { |
1328 return entry->Equals(&value); | 1330 return entry->Equals(&value); |
1329 }); | 1331 }); |
1330 } | 1332 } |
1331 | 1333 |
1332 void ListValue::Swap(ListValue* other) { | 1334 void ListValue::Swap(ListValue* other) { |
1335 CHECK(other->is_list()); | |
1333 list_->swap(*(other->list_)); | 1336 list_->swap(*(other->list_)); |
1334 } | 1337 } |
1335 | 1338 |
1336 ListValue* ListValue::DeepCopy() const { | 1339 ListValue* ListValue::DeepCopy() const { |
1337 return static_cast<ListValue*>(Value::DeepCopy()); | 1340 return static_cast<ListValue*>(Value::DeepCopy()); |
1338 } | 1341 } |
1339 | 1342 |
1340 std::unique_ptr<ListValue> ListValue::CreateDeepCopy() const { | 1343 std::unique_ptr<ListValue> ListValue::CreateDeepCopy() const { |
1341 return WrapUnique(DeepCopy()); | 1344 return WrapUnique(DeepCopy()); |
1342 } | 1345 } |
(...skipping 11 matching lines...) Expand all Loading... | |
1354 } | 1357 } |
1355 | 1358 |
1356 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { | 1359 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { |
1357 if (static_cast<int>(type) < 0 || | 1360 if (static_cast<int>(type) < 0 || |
1358 static_cast<size_t>(type) >= arraysize(kTypeNames)) | 1361 static_cast<size_t>(type) >= arraysize(kTypeNames)) |
1359 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; | 1362 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; |
1360 return out << Value::GetTypeName(type); | 1363 return out << Value::GetTypeName(type); |
1361 } | 1364 } |
1362 | 1365 |
1363 } // namespace base | 1366 } // namespace base |
OLD | NEW |