| 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 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 DCHECK(IsStringUTF8(key)); | 604 DCHECK(IsStringUTF8(key)); |
| 605 auto current_entry = dict_->find(key.as_string()); | 605 auto current_entry = dict_->find(key.as_string()); |
| 606 DCHECK((current_entry == dict_->end()) || current_entry->second); | 606 DCHECK((current_entry == dict_->end()) || current_entry->second); |
| 607 return current_entry != dict_->end(); | 607 return current_entry != dict_->end(); |
| 608 } | 608 } |
| 609 | 609 |
| 610 void DictionaryValue::Clear() { | 610 void DictionaryValue::Clear() { |
| 611 dict_->clear(); | 611 dict_->clear(); |
| 612 } | 612 } |
| 613 | 613 |
| 614 void DictionaryValue::Set(StringPiece path, std::unique_ptr<Value> in_value) { | 614 Value* DictionaryValue::Set(StringPiece path, std::unique_ptr<Value> in_value) { |
| 615 DCHECK(IsStringUTF8(path)); | 615 DCHECK(IsStringUTF8(path)); |
| 616 DCHECK(in_value); | 616 DCHECK(in_value); |
| 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 = new DictionaryValue; |
| 628 current_dictionary->SetWithoutPathExpansion( | 628 current_dictionary->SetWithoutPathExpansion( |
| 629 key, base::WrapUnique(child_dictionary)); | 629 key, base::WrapUnique(child_dictionary)); |
| 630 } | 630 } |
| 631 | 631 |
| 632 current_dictionary = child_dictionary; | 632 current_dictionary = child_dictionary; |
| 633 current_path = current_path.substr(delimiter_position + 1); | 633 current_path = current_path.substr(delimiter_position + 1); |
| 634 } | 634 } |
| 635 | 635 |
| 636 current_dictionary->SetWithoutPathExpansion(current_path, | 636 return current_dictionary->SetWithoutPathExpansion(current_path, |
| 637 std::move(in_value)); | 637 std::move(in_value)); |
| 638 } | 638 } |
| 639 | 639 |
| 640 void DictionaryValue::Set(StringPiece path, Value* in_value) { | 640 Value* DictionaryValue::Set(StringPiece path, Value* in_value) { |
| 641 Set(path, WrapUnique(in_value)); | 641 return Set(path, WrapUnique(in_value)); |
| 642 } | 642 } |
| 643 | 643 |
| 644 void DictionaryValue::SetBoolean(StringPiece path, bool in_value) { | 644 Value* DictionaryValue::SetBoolean(StringPiece path, bool in_value) { |
| 645 Set(path, new Value(in_value)); | 645 return Set(path, new Value(in_value)); |
| 646 } | 646 } |
| 647 | 647 |
| 648 void DictionaryValue::SetInteger(StringPiece path, int in_value) { | 648 Value* DictionaryValue::SetInteger(StringPiece path, int in_value) { |
| 649 Set(path, new Value(in_value)); | 649 return Set(path, new Value(in_value)); |
| 650 } | 650 } |
| 651 | 651 |
| 652 void DictionaryValue::SetDouble(StringPiece path, double in_value) { | 652 Value* DictionaryValue::SetDouble(StringPiece path, double in_value) { |
| 653 Set(path, new Value(in_value)); | 653 return Set(path, new Value(in_value)); |
| 654 } | 654 } |
| 655 | 655 |
| 656 void DictionaryValue::SetString(StringPiece path, StringPiece in_value) { | 656 Value* DictionaryValue::SetString(StringPiece path, StringPiece in_value) { |
| 657 Set(path, new Value(in_value)); | 657 return Set(path, new Value(in_value)); |
| 658 } | 658 } |
| 659 | 659 |
| 660 void DictionaryValue::SetString(StringPiece path, const string16& in_value) { | 660 Value* DictionaryValue::SetString(StringPiece path, const string16& in_value) { |
| 661 Set(path, new Value(in_value)); | 661 return Set(path, new Value(in_value)); |
| 662 } | 662 } |
| 663 | 663 |
| 664 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, | 664 DictionaryValue* DictionaryValue::SetDictionary( |
| 665 std::unique_ptr<Value> in_value) { | 665 StringPiece path, |
| 666 (*dict_)[key.as_string()] = std::move(in_value); | 666 std::unique_ptr<DictionaryValue> in_value) { |
| 667 return static_cast<DictionaryValue*>(Set(path, std::move(in_value))); |
| 667 } | 668 } |
| 668 | 669 |
| 669 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, | 670 ListValue* DictionaryValue::SetList(StringPiece path, |
| 670 Value* in_value) { | 671 std::unique_ptr<ListValue> in_value) { |
| 671 SetWithoutPathExpansion(key, WrapUnique(in_value)); | 672 return static_cast<ListValue*>(Set(path, std::move(in_value))); |
| 672 } | 673 } |
| 673 | 674 |
| 674 void DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path, | 675 Value* DictionaryValue::SetWithoutPathExpansion( |
| 675 bool in_value) { | 676 StringPiece key, |
| 676 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 677 std::unique_ptr<Value> in_value) { |
| 678 return ((*dict_)[key.as_string()] = std::move(in_value)).get(); |
| 677 } | 679 } |
| 678 | 680 |
| 679 void DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path, | 681 Value* DictionaryValue::SetWithoutPathExpansion(StringPiece key, |
| 680 int in_value) { | 682 Value* in_value) { |
| 681 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 683 return SetWithoutPathExpansion(key, WrapUnique(in_value)); |
| 682 } | 684 } |
| 683 | 685 |
| 684 void DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, | 686 Value* DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path, |
| 685 double in_value) { | 687 bool in_value) { |
| 686 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 688 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
| 687 } | 689 } |
| 688 | 690 |
| 689 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, | 691 Value* DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path, |
| 690 StringPiece in_value) { | 692 int in_value) { |
| 691 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 693 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
| 692 } | 694 } |
| 693 | 695 |
| 694 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, | 696 Value* DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, |
| 695 const string16& in_value) { | 697 double in_value) { |
| 696 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); | 698 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
| 699 } |
| 700 |
| 701 Value* DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, |
| 702 StringPiece in_value) { |
| 703 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
| 704 } |
| 705 |
| 706 Value* DictionaryValue::SetStringWithoutPathExpansion( |
| 707 StringPiece path, |
| 708 const string16& in_value) { |
| 709 return SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); |
| 710 } |
| 711 |
| 712 DictionaryValue* DictionaryValue::SetDictionaryWithoutPathExpansion( |
| 713 StringPiece path, |
| 714 std::unique_ptr<DictionaryValue> in_value) { |
| 715 return static_cast<DictionaryValue*>( |
| 716 SetWithoutPathExpansion(path, std::move(in_value))); |
| 717 } |
| 718 |
| 719 ListValue* DictionaryValue::SetListWithoutPathExpansion( |
| 720 StringPiece path, |
| 721 std::unique_ptr<ListValue> in_value) { |
| 722 return static_cast<ListValue*>( |
| 723 SetWithoutPathExpansion(path, std::move(in_value))); |
| 697 } | 724 } |
| 698 | 725 |
| 699 bool DictionaryValue::Get(StringPiece path, | 726 bool DictionaryValue::Get(StringPiece path, |
| 700 const Value** out_value) const { | 727 const Value** out_value) const { |
| 701 DCHECK(IsStringUTF8(path)); | 728 DCHECK(IsStringUTF8(path)); |
| 702 StringPiece current_path(path); | 729 StringPiece current_path(path); |
| 703 const DictionaryValue* current_dictionary = this; | 730 const DictionaryValue* current_dictionary = this; |
| 704 for (size_t delimiter_position = current_path.find('.'); | 731 for (size_t delimiter_position = current_path.find('.'); |
| 705 delimiter_position != std::string::npos; | 732 delimiter_position != std::string::npos; |
| 706 delimiter_position = current_path.find('.')) { | 733 delimiter_position = current_path.find('.')) { |
| (...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1309 } | 1336 } |
| 1310 | 1337 |
| 1311 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { | 1338 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { |
| 1312 if (static_cast<int>(type) < 0 || | 1339 if (static_cast<int>(type) < 0 || |
| 1313 static_cast<size_t>(type) >= arraysize(kTypeNames)) | 1340 static_cast<size_t>(type) >= arraysize(kTypeNames)) |
| 1314 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; | 1341 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; |
| 1315 return out << Value::GetTypeName(type); | 1342 return out << Value::GetTypeName(type); |
| 1316 } | 1343 } |
| 1317 | 1344 |
| 1318 } // namespace base | 1345 } // namespace base |
| OLD | NEW |