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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 private: | 78 private: |
79 const Value* first_; | 79 const Value* first_; |
80 }; | 80 }; |
81 | 81 |
82 } // namespace | 82 } // namespace |
83 | 83 |
84 Value::~Value() { | 84 Value::~Value() { |
85 } | 85 } |
86 | 86 |
87 // static | 87 // static |
88 Value* Value::CreateNullValue() { | 88 scoped_ptr<Value> Value::CreateNullValue() { |
89 return new Value(TYPE_NULL); | 89 return make_scoped_ptr(new Value(TYPE_NULL)); |
90 } | 90 } |
91 | 91 |
92 bool Value::GetAsBinary(const BinaryValue** out_value) const { | 92 bool Value::GetAsBinary(const BinaryValue** out_value) const { |
93 return false; | 93 return false; |
94 } | 94 } |
95 | 95 |
96 bool Value::GetAsBoolean(bool* out_value) const { | 96 bool Value::GetAsBoolean(bool* out_value) const { |
97 return false; | 97 return false; |
98 } | 98 } |
99 | 99 |
(...skipping 30 matching lines...) Expand all Loading... |
130 } | 130 } |
131 | 131 |
132 bool Value::GetAsDictionary(const DictionaryValue** out_value) const { | 132 bool Value::GetAsDictionary(const DictionaryValue** out_value) const { |
133 return false; | 133 return false; |
134 } | 134 } |
135 | 135 |
136 Value* Value::DeepCopy() const { | 136 Value* Value::DeepCopy() const { |
137 // This method should only be getting called for null Values--all subclasses | 137 // This method should only be getting called for null Values--all subclasses |
138 // need to provide their own implementation;. | 138 // need to provide their own implementation;. |
139 DCHECK(IsType(TYPE_NULL)); | 139 DCHECK(IsType(TYPE_NULL)); |
140 return CreateNullValue(); | 140 return CreateNullValue().release(); |
| 141 } |
| 142 |
| 143 scoped_ptr<Value> Value::CreateDeepCopy() const { |
| 144 return make_scoped_ptr(DeepCopy()); |
141 } | 145 } |
142 | 146 |
143 bool Value::Equals(const Value* other) const { | 147 bool Value::Equals(const Value* other) const { |
144 // This method should only be getting called for null Values--all subclasses | 148 // This method should only be getting called for null Values--all subclasses |
145 // need to provide their own implementation;. | 149 // need to provide their own implementation;. |
146 DCHECK(IsType(TYPE_NULL)); | 150 DCHECK(IsType(TYPE_NULL)); |
147 return other->IsType(TYPE_NULL); | 151 return other->IsType(TYPE_NULL); |
148 } | 152 } |
149 | 153 |
150 // static | 154 // static |
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 | 826 |
823 for (ValueMap::const_iterator current_entry(dictionary_.begin()); | 827 for (ValueMap::const_iterator current_entry(dictionary_.begin()); |
824 current_entry != dictionary_.end(); ++current_entry) { | 828 current_entry != dictionary_.end(); ++current_entry) { |
825 result->SetWithoutPathExpansion(current_entry->first, | 829 result->SetWithoutPathExpansion(current_entry->first, |
826 current_entry->second->DeepCopy()); | 830 current_entry->second->DeepCopy()); |
827 } | 831 } |
828 | 832 |
829 return result; | 833 return result; |
830 } | 834 } |
831 | 835 |
| 836 scoped_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const { |
| 837 return make_scoped_ptr(DeepCopy()); |
| 838 } |
| 839 |
832 bool DictionaryValue::Equals(const Value* other) const { | 840 bool DictionaryValue::Equals(const Value* other) const { |
833 if (other->GetType() != GetType()) | 841 if (other->GetType() != GetType()) |
834 return false; | 842 return false; |
835 | 843 |
836 const DictionaryValue* other_dict = | 844 const DictionaryValue* other_dict = |
837 static_cast<const DictionaryValue*>(other); | 845 static_cast<const DictionaryValue*>(other); |
838 Iterator lhs_it(*this); | 846 Iterator lhs_it(*this); |
839 Iterator rhs_it(*other_dict); | 847 Iterator rhs_it(*other_dict); |
840 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) { | 848 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) { |
841 if (lhs_it.key() != rhs_it.key() || | 849 if (lhs_it.key() != rhs_it.key() || |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
876 Append(CreateNullValue()); | 884 Append(CreateNullValue()); |
877 Append(in_value); | 885 Append(in_value); |
878 } else { | 886 } else { |
879 DCHECK(list_[index] != in_value); | 887 DCHECK(list_[index] != in_value); |
880 delete list_[index]; | 888 delete list_[index]; |
881 list_[index] = in_value; | 889 list_[index] = in_value; |
882 } | 890 } |
883 return true; | 891 return true; |
884 } | 892 } |
885 | 893 |
| 894 bool ListValue::Set(size_t index, scoped_ptr<Value> in_value) { |
| 895 return Set(index, in_value.release()); |
| 896 } |
| 897 |
886 bool ListValue::Get(size_t index, const Value** out_value) const { | 898 bool ListValue::Get(size_t index, const Value** out_value) const { |
887 if (index >= list_.size()) | 899 if (index >= list_.size()) |
888 return false; | 900 return false; |
889 | 901 |
890 if (out_value) | 902 if (out_value) |
891 *out_value = list_[index]; | 903 *out_value = list_[index]; |
892 | 904 |
893 return true; | 905 return true; |
894 } | 906 } |
895 | 907 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 ListValue::iterator ListValue::Erase(iterator iter, | 1037 ListValue::iterator ListValue::Erase(iterator iter, |
1026 scoped_ptr<Value>* out_value) { | 1038 scoped_ptr<Value>* out_value) { |
1027 if (out_value) | 1039 if (out_value) |
1028 out_value->reset(*iter); | 1040 out_value->reset(*iter); |
1029 else | 1041 else |
1030 delete *iter; | 1042 delete *iter; |
1031 | 1043 |
1032 return list_.erase(iter); | 1044 return list_.erase(iter); |
1033 } | 1045 } |
1034 | 1046 |
| 1047 void ListValue::Append(scoped_ptr<Value> in_value) { |
| 1048 Append(in_value.release()); |
| 1049 } |
| 1050 |
1035 void ListValue::Append(Value* in_value) { | 1051 void ListValue::Append(Value* in_value) { |
1036 DCHECK(in_value); | 1052 DCHECK(in_value); |
1037 list_.push_back(in_value); | 1053 list_.push_back(in_value); |
1038 } | 1054 } |
1039 | 1055 |
1040 void ListValue::AppendBoolean(bool in_value) { | 1056 void ListValue::AppendBoolean(bool in_value) { |
1041 Append(new FundamentalValue(in_value)); | 1057 Append(new FundamentalValue(in_value)); |
1042 } | 1058 } |
1043 | 1059 |
1044 void ListValue::AppendInteger(int in_value) { | 1060 void ListValue::AppendInteger(int in_value) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1114 | 1130 |
1115 ListValue* ListValue::DeepCopy() const { | 1131 ListValue* ListValue::DeepCopy() const { |
1116 ListValue* result = new ListValue; | 1132 ListValue* result = new ListValue; |
1117 | 1133 |
1118 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) | 1134 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) |
1119 result->Append((*i)->DeepCopy()); | 1135 result->Append((*i)->DeepCopy()); |
1120 | 1136 |
1121 return result; | 1137 return result; |
1122 } | 1138 } |
1123 | 1139 |
| 1140 scoped_ptr<ListValue> ListValue::CreateDeepCopy() const { |
| 1141 return make_scoped_ptr(DeepCopy()); |
| 1142 } |
| 1143 |
1124 bool ListValue::Equals(const Value* other) const { | 1144 bool ListValue::Equals(const Value* other) const { |
1125 if (other->GetType() != GetType()) | 1145 if (other->GetType() != GetType()) |
1126 return false; | 1146 return false; |
1127 | 1147 |
1128 const ListValue* other_list = | 1148 const ListValue* other_list = |
1129 static_cast<const ListValue*>(other); | 1149 static_cast<const ListValue*>(other); |
1130 const_iterator lhs_it, rhs_it; | 1150 const_iterator lhs_it, rhs_it; |
1131 for (lhs_it = begin(), rhs_it = other_list->begin(); | 1151 for (lhs_it = begin(), rhs_it = other_list->begin(); |
1132 lhs_it != end() && rhs_it != other_list->end(); | 1152 lhs_it != end() && rhs_it != other_list->end(); |
1133 ++lhs_it, ++rhs_it) { | 1153 ++lhs_it, ++rhs_it) { |
(...skipping 14 matching lines...) Expand all Loading... |
1148 | 1168 |
1149 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1169 std::ostream& operator<<(std::ostream& out, const Value& value) { |
1150 std::string json; | 1170 std::string json; |
1151 JSONWriter::WriteWithOptions(&value, | 1171 JSONWriter::WriteWithOptions(&value, |
1152 JSONWriter::OPTIONS_PRETTY_PRINT, | 1172 JSONWriter::OPTIONS_PRETTY_PRINT, |
1153 &json); | 1173 &json); |
1154 return out << json; | 1174 return out << json; |
1155 } | 1175 } |
1156 | 1176 |
1157 } // namespace base | 1177 } // namespace base |
OLD | NEW |