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 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 } | 712 } |
713 | 713 |
714 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, | 714 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, |
715 ListValue** out_value) { | 715 ListValue** out_value) { |
716 return | 716 return |
717 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( | 717 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( |
718 key, | 718 key, |
719 const_cast<const ListValue**>(out_value)); | 719 const_cast<const ListValue**>(out_value)); |
720 } | 720 } |
721 | 721 |
722 bool DictionaryValue::Remove(const std::string& path, Value** out_value) { | 722 bool DictionaryValue::Remove(const std::string& path, |
| 723 scoped_ptr<Value>* out_value) { |
723 DCHECK(IsStringUTF8(path)); | 724 DCHECK(IsStringUTF8(path)); |
724 std::string current_path(path); | 725 std::string current_path(path); |
725 DictionaryValue* current_dictionary = this; | 726 DictionaryValue* current_dictionary = this; |
726 size_t delimiter_position = current_path.rfind('.'); | 727 size_t delimiter_position = current_path.rfind('.'); |
727 if (delimiter_position != std::string::npos) { | 728 if (delimiter_position != std::string::npos) { |
728 if (!GetDictionary(current_path.substr(0, delimiter_position), | 729 if (!GetDictionary(current_path.substr(0, delimiter_position), |
729 ¤t_dictionary)) | 730 ¤t_dictionary)) |
730 return false; | 731 return false; |
731 current_path.erase(0, delimiter_position + 1); | 732 current_path.erase(0, delimiter_position + 1); |
732 } | 733 } |
733 | 734 |
734 return current_dictionary->RemoveWithoutPathExpansion(current_path, | 735 return current_dictionary->RemoveWithoutPathExpansion(current_path, |
735 out_value); | 736 out_value); |
736 } | 737 } |
737 | 738 |
738 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key, | 739 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key, |
739 Value** out_value) { | 740 scoped_ptr<Value>* out_value) { |
740 DCHECK(IsStringUTF8(key)); | 741 DCHECK(IsStringUTF8(key)); |
741 ValueMap::iterator entry_iterator = dictionary_.find(key); | 742 ValueMap::iterator entry_iterator = dictionary_.find(key); |
742 if (entry_iterator == dictionary_.end()) | 743 if (entry_iterator == dictionary_.end()) |
743 return false; | 744 return false; |
744 | 745 |
745 Value* entry = entry_iterator->second; | 746 Value* entry = entry_iterator->second; |
746 if (out_value) | 747 if (out_value) |
747 *out_value = entry; | 748 out_value->reset(entry); |
748 else | 749 else |
749 delete entry; | 750 delete entry; |
750 dictionary_.erase(entry_iterator); | 751 dictionary_.erase(entry_iterator); |
751 return true; | 752 return true; |
752 } | 753 } |
753 | 754 |
754 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() { | 755 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() { |
755 Value* copy = CopyWithoutEmptyChildren(this); | 756 Value* copy = CopyWithoutEmptyChildren(this); |
756 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; | 757 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; |
757 } | 758 } |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 | 952 |
952 return true; | 953 return true; |
953 } | 954 } |
954 | 955 |
955 bool ListValue::GetList(size_t index, ListValue** out_value) { | 956 bool ListValue::GetList(size_t index, ListValue** out_value) { |
956 return static_cast<const ListValue&>(*this).GetList( | 957 return static_cast<const ListValue&>(*this).GetList( |
957 index, | 958 index, |
958 const_cast<const ListValue**>(out_value)); | 959 const_cast<const ListValue**>(out_value)); |
959 } | 960 } |
960 | 961 |
961 bool ListValue::Remove(size_t index, Value** out_value) { | 962 bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) { |
962 if (index >= list_.size()) | 963 if (index >= list_.size()) |
963 return false; | 964 return false; |
964 | 965 |
965 if (out_value) | 966 if (out_value) |
966 *out_value = list_[index]; | 967 out_value->reset(list_[index]); |
967 else | 968 else |
968 delete list_[index]; | 969 delete list_[index]; |
969 | 970 |
970 list_.erase(list_.begin() + index); | 971 list_.erase(list_.begin() + index); |
971 return true; | 972 return true; |
972 } | 973 } |
973 | 974 |
974 bool ListValue::Remove(const Value& value, size_t* index) { | 975 bool ListValue::Remove(const Value& value, size_t* index) { |
975 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) { | 976 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) { |
976 if ((*i)->Equals(&value)) { | 977 if ((*i)->Equals(&value)) { |
977 size_t previous_index = i - list_.begin(); | 978 size_t previous_index = i - list_.begin(); |
978 delete *i; | 979 delete *i; |
979 list_.erase(i); | 980 list_.erase(i); |
980 | 981 |
981 if (index) | 982 if (index) |
982 *index = previous_index; | 983 *index = previous_index; |
983 return true; | 984 return true; |
984 } | 985 } |
985 } | 986 } |
986 return false; | 987 return false; |
987 } | 988 } |
988 | 989 |
989 ListValue::iterator ListValue::Erase(iterator iter, Value** out_value) { | 990 ListValue::iterator ListValue::Erase(iterator iter, |
| 991 scoped_ptr<Value>* out_value) { |
990 if (out_value) | 992 if (out_value) |
991 *out_value = *iter; | 993 out_value->reset(*iter); |
992 else | 994 else |
993 delete *iter; | 995 delete *iter; |
994 | 996 |
995 return list_.erase(iter); | 997 return list_.erase(iter); |
996 } | 998 } |
997 | 999 |
998 void ListValue::Append(Value* in_value) { | 1000 void ListValue::Append(Value* in_value) { |
999 DCHECK(in_value); | 1001 DCHECK(in_value); |
1000 list_.push_back(in_value); | 1002 list_.push_back(in_value); |
1001 } | 1003 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1108 | 1110 |
1109 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1111 std::ostream& operator<<(std::ostream& out, const Value& value) { |
1110 std::string json; | 1112 std::string json; |
1111 JSONWriter::WriteWithOptions(&value, | 1113 JSONWriter::WriteWithOptions(&value, |
1112 JSONWriter::OPTIONS_PRETTY_PRINT, | 1114 JSONWriter::OPTIONS_PRETTY_PRINT, |
1113 &json); | 1115 &json); |
1114 return out << json; | 1116 return out << json; |
1115 } | 1117 } |
1116 | 1118 |
1117 } // namespace base | 1119 } // namespace base |
OLD | NEW |