| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <ostream> | 8 #include <ostream> |
| 9 | 9 |
| 10 #include "base/float_util.h" | 10 #include "base/float_util.h" |
| (...skipping 738 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 dictionary_.erase(entry_iterator); | 749 dictionary_.erase(entry_iterator); |
| 750 return true; | 750 return true; |
| 751 } | 751 } |
| 752 | 752 |
| 753 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() { | 753 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() { |
| 754 Value* copy = CopyWithoutEmptyChildren(this); | 754 Value* copy = CopyWithoutEmptyChildren(this); |
| 755 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; | 755 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; |
| 756 } | 756 } |
| 757 | 757 |
| 758 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { | 758 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
| 759 for (DictionaryValue::key_iterator key(dictionary->begin_keys()); | 759 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) { |
| 760 key != dictionary->end_keys(); ++key) { | 760 const Value* merge_value = &it.value(); |
| 761 const Value* merge_value; | 761 // Check whether we have to merge dictionaries. |
| 762 if (dictionary->GetWithoutPathExpansion(*key, &merge_value)) { | 762 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { |
| 763 // Check whether we have to merge dictionaries. | 763 DictionaryValue* sub_dict; |
| 764 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { | 764 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) { |
| 765 DictionaryValue* sub_dict; | 765 sub_dict->MergeDictionary( |
| 766 if (GetDictionaryWithoutPathExpansion(*key, &sub_dict)) { | 766 static_cast<const DictionaryValue*>(merge_value)); |
| 767 sub_dict->MergeDictionary( | 767 continue; |
| 768 static_cast<const DictionaryValue*>(merge_value)); | |
| 769 continue; | |
| 770 } | |
| 771 } | 768 } |
| 772 // All other cases: Make a copy and hook it up. | |
| 773 SetWithoutPathExpansion(*key, merge_value->DeepCopy()); | |
| 774 } | 769 } |
| 770 // All other cases: Make a copy and hook it up. |
| 771 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy()); |
| 775 } | 772 } |
| 776 } | 773 } |
| 777 | 774 |
| 778 void DictionaryValue::Swap(DictionaryValue* other) { | 775 void DictionaryValue::Swap(DictionaryValue* other) { |
| 779 dictionary_.swap(other->dictionary_); | 776 dictionary_.swap(other->dictionary_); |
| 780 } | 777 } |
| 781 | 778 |
| 782 DictionaryValue::key_iterator::key_iterator(ValueMap::const_iterator itr) { | |
| 783 itr_ = itr; | |
| 784 } | |
| 785 | |
| 786 DictionaryValue::key_iterator::key_iterator(const key_iterator& rhs) { | |
| 787 itr_ = rhs.itr_; | |
| 788 } | |
| 789 | |
| 790 DictionaryValue::Iterator::Iterator(const DictionaryValue& target) | 779 DictionaryValue::Iterator::Iterator(const DictionaryValue& target) |
| 791 : target_(target), | 780 : target_(target), |
| 792 it_(target.dictionary_.begin()) {} | 781 it_(target.dictionary_.begin()) {} |
| 793 | 782 |
| 794 DictionaryValue* DictionaryValue::DeepCopy() const { | 783 DictionaryValue* DictionaryValue::DeepCopy() const { |
| 795 DictionaryValue* result = new DictionaryValue; | 784 DictionaryValue* result = new DictionaryValue; |
| 796 | 785 |
| 797 for (ValueMap::const_iterator current_entry(dictionary_.begin()); | 786 for (ValueMap::const_iterator current_entry(dictionary_.begin()); |
| 798 current_entry != dictionary_.end(); ++current_entry) { | 787 current_entry != dictionary_.end(); ++current_entry) { |
| 799 result->SetWithoutPathExpansion(current_entry->first, | 788 result->SetWithoutPathExpansion(current_entry->first, |
| 800 current_entry->second->DeepCopy()); | 789 current_entry->second->DeepCopy()); |
| 801 } | 790 } |
| 802 | 791 |
| 803 return result; | 792 return result; |
| 804 } | 793 } |
| 805 | 794 |
| 806 bool DictionaryValue::Equals(const Value* other) const { | 795 bool DictionaryValue::Equals(const Value* other) const { |
| 807 if (other->GetType() != GetType()) | 796 if (other->GetType() != GetType()) |
| 808 return false; | 797 return false; |
| 809 | 798 |
| 810 const DictionaryValue* other_dict = | 799 const DictionaryValue* other_dict = |
| 811 static_cast<const DictionaryValue*>(other); | 800 static_cast<const DictionaryValue*>(other); |
| 812 key_iterator lhs_it(begin_keys()); | 801 Iterator lhs_it(*this); |
| 813 key_iterator rhs_it(other_dict->begin_keys()); | 802 Iterator rhs_it(*other_dict); |
| 814 while (lhs_it != end_keys() && rhs_it != other_dict->end_keys()) { | 803 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) { |
| 815 const Value* lhs; | 804 if (lhs_it.key() != rhs_it.key() || |
| 816 const Value* rhs; | 805 !lhs_it.value().Equals(&rhs_it.value())) { |
| 817 if (*lhs_it != *rhs_it || | |
| 818 !GetWithoutPathExpansion(*lhs_it, &lhs) || | |
| 819 !other_dict->GetWithoutPathExpansion(*rhs_it, &rhs) || | |
| 820 !lhs->Equals(rhs)) { | |
| 821 return false; | 806 return false; |
| 822 } | 807 } |
| 823 ++lhs_it; | 808 lhs_it.Advance(); |
| 824 ++rhs_it; | 809 rhs_it.Advance(); |
| 825 } | 810 } |
| 826 if (lhs_it != end_keys() || rhs_it != other_dict->end_keys()) | 811 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd()) |
| 827 return false; | 812 return false; |
| 828 | 813 |
| 829 return true; | 814 return true; |
| 830 } | 815 } |
| 831 | 816 |
| 832 ///////////////////// ListValue //////////////////// | 817 ///////////////////// ListValue //////////////////// |
| 833 | 818 |
| 834 ListValue::ListValue() : Value(TYPE_LIST) { | 819 ListValue::ListValue() : Value(TYPE_LIST) { |
| 835 } | 820 } |
| 836 | 821 |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 | 1107 |
| 1123 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1108 std::ostream& operator<<(std::ostream& out, const Value& value) { |
| 1124 std::string json; | 1109 std::string json; |
| 1125 JSONWriter::WriteWithOptions(&value, | 1110 JSONWriter::WriteWithOptions(&value, |
| 1126 JSONWriter::OPTIONS_PRETTY_PRINT, | 1111 JSONWriter::OPTIONS_PRETTY_PRINT, |
| 1127 &json); | 1112 &json); |
| 1128 return out << json; | 1113 return out << json; |
| 1129 } | 1114 } |
| 1130 | 1115 |
| 1131 } // namespace base | 1116 } // namespace base |
| OLD | NEW |