| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 return copy.GetList().empty() ? nullptr : MakeUnique<Value>(std::move(copy)); | 42 return copy.GetList().empty() ? nullptr : MakeUnique<Value>(std::move(copy)); |
| 43 } | 43 } |
| 44 | 44 |
| 45 std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( | 45 std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( |
| 46 const DictionaryValue& dict) { | 46 const DictionaryValue& dict) { |
| 47 std::unique_ptr<DictionaryValue> copy; | 47 std::unique_ptr<DictionaryValue> copy; |
| 48 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 48 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| 49 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value()); | 49 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value()); |
| 50 if (child_copy) { | 50 if (child_copy) { |
| 51 if (!copy) | 51 if (!copy) |
| 52 copy.reset(new DictionaryValue); | 52 copy = MakeUnique<DictionaryValue>(); |
| 53 copy->SetWithoutPathExpansion(it.key(), std::move(child_copy)); | 53 copy->SetWithoutPathExpansion(it.key(), std::move(child_copy)); |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 return copy; | 56 return copy; |
| 57 } | 57 } |
| 58 | 58 |
| 59 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { | 59 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { |
| 60 switch (node.GetType()) { | 60 switch (node.GetType()) { |
| 61 case Value::Type::LIST: | 61 case Value::Type::LIST: |
| 62 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); | 62 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); |
| (...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 | 617 |
| 618 StringPiece current_path(path); | 618 StringPiece current_path(path); |
| 619 DictionaryValue* current_dictionary = this; | 619 DictionaryValue* current_dictionary = this; |
| 620 for (size_t delimiter_position = current_path.find('.'); | 620 for (size_t delimiter_position = current_path.find('.'); |
| 621 delimiter_position != StringPiece::npos; | 621 delimiter_position != StringPiece::npos; |
| 622 delimiter_position = current_path.find('.')) { | 622 delimiter_position = current_path.find('.')) { |
| 623 // Assume that we're indexing into a dictionary. | 623 // Assume that we're indexing into a dictionary. |
| 624 StringPiece key = current_path.substr(0, delimiter_position); | 624 StringPiece key = current_path.substr(0, delimiter_position); |
| 625 DictionaryValue* child_dictionary = nullptr; | 625 DictionaryValue* child_dictionary = nullptr; |
| 626 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { | 626 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { |
| 627 child_dictionary = new DictionaryValue; | 627 child_dictionary = current_dictionary->SetDictionaryWithoutPathExpansion( |
| 628 current_dictionary->SetWithoutPathExpansion( | 628 key, MakeUnique<DictionaryValue>()); |
| 629 key, base::WrapUnique(child_dictionary)); | |
| 630 } | 629 } |
| 631 | 630 |
| 632 current_dictionary = child_dictionary; | 631 current_dictionary = child_dictionary; |
| 633 current_path = current_path.substr(delimiter_position + 1); | 632 current_path = current_path.substr(delimiter_position + 1); |
| 634 } | 633 } |
| 635 | 634 |
| 636 return current_dictionary->SetWithoutPathExpansion(current_path, | 635 return current_dictionary->SetWithoutPathExpansion(current_path, |
| 637 std::move(in_value)); | 636 std::move(in_value)); |
| 638 } | 637 } |
| 639 | 638 |
| 640 Value* DictionaryValue::Set(StringPiece path, Value* in_value) { | 639 Value* DictionaryValue::Set(StringPiece path, Value* in_value) { |
| 641 return Set(path, WrapUnique(in_value)); | 640 return Set(path, WrapUnique(in_value)); |
| 642 } | 641 } |
| 643 | 642 |
| 644 Value* DictionaryValue::SetBoolean(StringPiece path, bool in_value) { | 643 Value* DictionaryValue::SetBoolean(StringPiece path, bool in_value) { |
| 645 return Set(path, new Value(in_value)); | 644 return Set(path, MakeUnique<Value>(in_value)); |
| 646 } | 645 } |
| 647 | 646 |
| 648 Value* DictionaryValue::SetInteger(StringPiece path, int in_value) { | 647 Value* DictionaryValue::SetInteger(StringPiece path, int in_value) { |
| 649 return Set(path, new Value(in_value)); | 648 return Set(path, MakeUnique<Value>(in_value)); |
| 650 } | 649 } |
| 651 | 650 |
| 652 Value* DictionaryValue::SetDouble(StringPiece path, double in_value) { | 651 Value* DictionaryValue::SetDouble(StringPiece path, double in_value) { |
| 653 return Set(path, new Value(in_value)); | 652 return Set(path, MakeUnique<Value>(in_value)); |
| 654 } | 653 } |
| 655 | 654 |
| 656 Value* DictionaryValue::SetString(StringPiece path, StringPiece in_value) { | 655 Value* DictionaryValue::SetString(StringPiece path, StringPiece in_value) { |
| 657 return Set(path, new Value(in_value)); | 656 return Set(path, MakeUnique<Value>(in_value)); |
| 658 } | 657 } |
| 659 | 658 |
| 660 Value* DictionaryValue::SetString(StringPiece path, const string16& in_value) { | 659 Value* DictionaryValue::SetString(StringPiece path, const string16& in_value) { |
| 661 return Set(path, new Value(in_value)); | 660 return Set(path, MakeUnique<Value>(in_value)); |
| 662 } | 661 } |
| 663 | 662 |
| 664 DictionaryValue* DictionaryValue::SetDictionary( | 663 DictionaryValue* DictionaryValue::SetDictionary( |
| 665 StringPiece path, | 664 StringPiece path, |
| 666 std::unique_ptr<DictionaryValue> in_value) { | 665 std::unique_ptr<DictionaryValue> in_value) { |
| 667 return static_cast<DictionaryValue*>(Set(path, std::move(in_value))); | 666 return static_cast<DictionaryValue*>(Set(path, std::move(in_value))); |
| 668 } | 667 } |
| 669 | 668 |
| 670 ListValue* DictionaryValue::SetList(StringPiece path, | 669 ListValue* DictionaryValue::SetList(StringPiece path, |
| 671 std::unique_ptr<ListValue> in_value) { | 670 std::unique_ptr<ListValue> in_value) { |
| 672 return static_cast<ListValue*>(Set(path, std::move(in_value))); | 671 return static_cast<ListValue*>(Set(path, std::move(in_value))); |
| 673 } | 672 } |
| 674 | 673 |
| 675 Value* DictionaryValue::SetWithoutPathExpansion( | 674 Value* DictionaryValue::SetWithoutPathExpansion( |
| 676 StringPiece key, | 675 StringPiece key, |
| 677 std::unique_ptr<Value> in_value) { | 676 std::unique_ptr<Value> in_value) { |
| 678 return ((*dict_)[key.as_string()] = std::move(in_value)).get(); | 677 return ((*dict_)[key.as_string()] = std::move(in_value)).get(); |
| 679 } | 678 } |
| 680 | 679 |
| 681 Value* DictionaryValue::SetWithoutPathExpansion(StringPiece key, | 680 Value* DictionaryValue::SetWithoutPathExpansion(StringPiece key, |
| 682 Value* in_value) { | 681 Value* in_value) { |
| 683 return SetWithoutPathExpansion(key, WrapUnique(in_value)); | 682 return SetWithoutPathExpansion(key, WrapUnique(in_value)); |
| 684 } | 683 } |
| 685 | 684 |
| 686 Value* DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path, | 685 Value* DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path, |
| 687 bool in_value) { | 686 bool in_value) { |
| 688 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 687 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value)); |
| 689 } | 688 } |
| 690 | 689 |
| 691 Value* DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path, | 690 Value* DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path, |
| 692 int in_value) { | 691 int in_value) { |
| 693 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 692 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value)); |
| 694 } | 693 } |
| 695 | 694 |
| 696 Value* DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, | 695 Value* DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, |
| 697 double in_value) { | 696 double in_value) { |
| 698 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 697 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value)); |
| 699 } | 698 } |
| 700 | 699 |
| 701 Value* DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, | 700 Value* DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, |
| 702 StringPiece in_value) { | 701 StringPiece in_value) { |
| 703 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 702 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value)); |
| 704 } | 703 } |
| 705 | 704 |
| 706 Value* DictionaryValue::SetStringWithoutPathExpansion( | 705 Value* DictionaryValue::SetStringWithoutPathExpansion( |
| 707 StringPiece path, | 706 StringPiece path, |
| 708 const string16& in_value) { | 707 const string16& in_value) { |
| 709 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 708 return SetWithoutPathExpansion(path, MakeUnique<Value>(in_value)); |
| 710 } | 709 } |
| 711 | 710 |
| 712 DictionaryValue* DictionaryValue::SetDictionaryWithoutPathExpansion( | 711 DictionaryValue* DictionaryValue::SetDictionaryWithoutPathExpansion( |
| 713 StringPiece path, | 712 StringPiece path, |
| 714 std::unique_ptr<DictionaryValue> in_value) { | 713 std::unique_ptr<DictionaryValue> in_value) { |
| 715 return static_cast<DictionaryValue*>( | 714 return static_cast<DictionaryValue*>( |
| 716 SetWithoutPathExpansion(path, std::move(in_value))); | 715 SetWithoutPathExpansion(path, std::move(in_value))); |
| 717 } | 716 } |
| 718 | 717 |
| 719 ListValue* DictionaryValue::SetListWithoutPathExpansion( | 718 ListValue* DictionaryValue::SetListWithoutPathExpansion( |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1023 RemoveWithoutPathExpansion(subdict_path, NULL); | 1022 RemoveWithoutPathExpansion(subdict_path, NULL); |
| 1024 | 1023 |
| 1025 return result; | 1024 return result; |
| 1026 } | 1025 } |
| 1027 | 1026 |
| 1028 std::unique_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren() | 1027 std::unique_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren() |
| 1029 const { | 1028 const { |
| 1030 std::unique_ptr<DictionaryValue> copy = | 1029 std::unique_ptr<DictionaryValue> copy = |
| 1031 CopyDictionaryWithoutEmptyChildren(*this); | 1030 CopyDictionaryWithoutEmptyChildren(*this); |
| 1032 if (!copy) | 1031 if (!copy) |
| 1033 copy.reset(new DictionaryValue); | 1032 copy = MakeUnique<DictionaryValue>(); |
| 1034 return copy; | 1033 return copy; |
| 1035 } | 1034 } |
| 1036 | 1035 |
| 1037 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { | 1036 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
| 1038 CHECK(dictionary->is_dict()); | 1037 CHECK(dictionary->is_dict()); |
| 1039 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { | 1038 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { |
| 1040 const Value* merge_value = &it.value(); | 1039 const Value* merge_value = &it.value(); |
| 1041 // Check whether we have to merge dictionaries. | 1040 // Check whether we have to merge dictionaries. |
| 1042 if (merge_value->IsType(Value::Type::DICTIONARY)) { | 1041 if (merge_value->IsType(Value::Type::DICTIONARY)) { |
| 1043 DictionaryValue* sub_dict; | 1042 DictionaryValue* sub_dict; |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1336 } | 1335 } |
| 1337 | 1336 |
| 1338 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { | 1337 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { |
| 1339 if (static_cast<int>(type) < 0 || | 1338 if (static_cast<int>(type) < 0 || |
| 1340 static_cast<size_t>(type) >= arraysize(kTypeNames)) | 1339 static_cast<size_t>(type) >= arraysize(kTypeNames)) |
| 1341 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; | 1340 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; |
| 1342 return out << Value::GetTypeName(type); | 1341 return out << Value::GetTypeName(type); |
| 1343 } | 1342 } |
| 1344 | 1343 |
| 1345 } // namespace base | 1344 } // namespace base |
| OLD | NEW |