| 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 // This file specifies a recursive data storage class called Value intended for | 5 // This file specifies a recursive data storage class called Value intended for |
| 6 // storing settings and other persistable data. | 6 // storing settings and other persistable data. |
| 7 // | 7 // |
| 8 // A Value represents something that can be stored in JSON or passed to/from | 8 // A Value represents something that can be stored in JSON or passed to/from |
| 9 // JavaScript. As such, it is NOT a generalized variant type, since only the | 9 // JavaScript. As such, it is NOT a generalized variant type, since only the |
| 10 // types supported by JavaScript/JSON are supported. | 10 // types supported by JavaScript/JSON are supported. |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 // Note: Do not add more types. See the file-level comment above for why. | 154 // Note: Do not add more types. See the file-level comment above for why. |
| 155 | 155 |
| 156 // This creates a deep copy of the entire Value tree, and returns a pointer | 156 // This creates a deep copy of the entire Value tree, and returns a pointer |
| 157 // to the copy. The caller gets ownership of the copy, of course. | 157 // to the copy. The caller gets ownership of the copy, of course. |
| 158 // Subclasses return their own type directly in their overrides; | 158 // Subclasses return their own type directly in their overrides; |
| 159 // this works because C++ supports covariant return types. | 159 // this works because C++ supports covariant return types. |
| 160 Value* DeepCopy() const; | 160 Value* DeepCopy() const; |
| 161 // Preferred version of DeepCopy. TODO(estade): remove the above. | 161 // Preferred version of DeepCopy. TODO(estade): remove the above. |
| 162 std::unique_ptr<Value> CreateDeepCopy() const; | 162 std::unique_ptr<Value> CreateDeepCopy() const; |
| 163 | 163 |
| 164 // Comparison operators. |
| 165 friend bool operator==(const Value& lhs, const Value& rhs) { |
| 166 if (lhs.type_ != rhs.type_) |
| 167 return false; |
| 168 |
| 169 switch (lhs.type_) { |
| 170 case Type::NONE: |
| 171 return true; |
| 172 case Type::BOOLEAN: |
| 173 return lhs.bool_value_ == rhs.bool_value_; |
| 174 case Type::INTEGER: |
| 175 return lhs.int_value_ == rhs.int_value_; |
| 176 case Type::DOUBLE: |
| 177 return lhs.double_value_ == rhs.double_value_; |
| 178 case Type::STRING: |
| 179 return *lhs.string_value_ == *rhs.string_value_; |
| 180 case Type::BINARY: |
| 181 return *lhs.binary_value_ == *rhs.binary_value_; |
| 182 // TODO(crbug.com/646113): Clean this up when DictionaryValue and |
| 183 // ListValue are completely inlined. |
| 184 case Type::DICTIONARY: |
| 185 if ((*lhs.dict_ptr_)->size() != (*rhs.dict_ptr_)->size()) |
| 186 return false; |
| 187 return std::equal(std::begin(**lhs.dict_ptr_), |
| 188 std::end(**lhs.dict_ptr_), |
| 189 std::begin(**rhs.dict_ptr_), |
| 190 [](const DictStorage::value_type& u, |
| 191 const DictStorage::value_type& v) { |
| 192 return std::tie(u.first, *u.second) == |
| 193 std::tie(v.first, *v.second); |
| 194 }); |
| 195 case Type::LIST: |
| 196 if (lhs.list_->size() != rhs.list_->size()) |
| 197 return false; |
| 198 return std::equal( |
| 199 std::begin(*lhs.list_), std::end(*lhs.list_), |
| 200 std::begin(*rhs.list_), |
| 201 [](const ListStorage::value_type& u, |
| 202 const ListStorage::value_type& v) { return *u == *v; }); |
| 203 } |
| 204 |
| 205 NOTREACHED(); |
| 206 return false; |
| 207 } |
| 208 |
| 209 friend bool operator!=(const Value& lhs, const Value& rhs) { |
| 210 return !(lhs == rhs); |
| 211 } |
| 212 |
| 213 friend bool operator<(const Value& lhs, const Value& rhs) { |
| 214 if (lhs.type_ != rhs.type_) |
| 215 return lhs.type_ < rhs.type_; |
| 216 |
| 217 switch (lhs.type_) { |
| 218 case Type::NONE: |
| 219 return false; |
| 220 case Type::BOOLEAN: |
| 221 return lhs.bool_value_ < rhs.bool_value_; |
| 222 case Type::INTEGER: |
| 223 return lhs.int_value_ < rhs.int_value_; |
| 224 case Type::DOUBLE: |
| 225 return lhs.double_value_ < rhs.double_value_; |
| 226 case Type::STRING: |
| 227 return *lhs.string_value_ < *rhs.string_value_; |
| 228 case Type::BINARY: |
| 229 return *lhs.binary_value_ < *rhs.binary_value_; |
| 230 // TODO(crbug.com/646113): Clean this up when DictionaryValue and |
| 231 // ListValue are completely inlined. |
| 232 case Type::DICTIONARY: |
| 233 return std::lexicographical_compare( |
| 234 std::begin(**lhs.dict_ptr_), std::end(**lhs.dict_ptr_), |
| 235 std::begin(**rhs.dict_ptr_), std::end(**rhs.dict_ptr_), |
| 236 [](const DictStorage::value_type& u, |
| 237 const DictStorage::value_type& v) { |
| 238 return std::tie(u.first, *u.second) < |
| 239 std::tie(v.first, *v.second); |
| 240 }); |
| 241 case Type::LIST: |
| 242 return std::lexicographical_compare( |
| 243 std::begin(*lhs.list_), std::end(*lhs.list_), |
| 244 std::begin(*rhs.list_), std::end(*rhs.list_), |
| 245 [](const ListStorage::value_type& u, |
| 246 const ListStorage::value_type& v) { return *u < *v; }); |
| 247 } |
| 248 |
| 249 NOTREACHED(); |
| 250 return false; |
| 251 } |
| 252 |
| 253 friend bool operator>(const Value& lhs, const Value& rhs) { |
| 254 return rhs < lhs; |
| 255 } |
| 256 |
| 257 friend bool operator<=(const Value& lhs, const Value& rhs) { |
| 258 return !(rhs < lhs); |
| 259 } |
| 260 |
| 261 friend bool operator>=(const Value& lhs, const Value& rhs) { |
| 262 return !(lhs < rhs); |
| 263 } |
| 264 |
| 164 // Compares if two Value objects have equal contents. | 265 // Compares if two Value objects have equal contents. |
| 266 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead. |
| 267 // TODO(crbug.com/646113): Delete this and migrate callsites. |
| 165 bool Equals(const Value* other) const; | 268 bool Equals(const Value* other) const; |
| 166 | 269 |
| 167 // Compares if two Value objects have equal contents. Can handle NULLs. | 270 // Compares if two Value objects have equal contents. Can handle NULLs. |
| 168 // NULLs are considered equal but different from Value::CreateNullValue(). | 271 // NULLs are considered equal but different from Value::CreateNullValue(). |
| 272 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead. |
| 273 // TODO(crbug.com/646113): Delete this and migrate callsites. |
| 169 static bool Equals(const Value* a, const Value* b); | 274 static bool Equals(const Value* a, const Value* b); |
| 170 | 275 |
| 171 protected: | 276 protected: |
| 172 // TODO(crbug.com/646113): Make these private once DictionaryValue and | 277 // TODO(crbug.com/646113): Make these private once DictionaryValue and |
| 173 // ListValue are properly inlined. | 278 // ListValue are properly inlined. |
| 174 Type type_; | 279 Type type_; |
| 175 | 280 |
| 176 union { | 281 union { |
| 177 bool bool_value_; | 282 bool bool_value_; |
| 178 int int_value_; | 283 int int_value_; |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 return out << static_cast<const Value&>(value); | 618 return out << static_cast<const Value&>(value); |
| 514 } | 619 } |
| 515 | 620 |
| 516 // Stream operator so that enum class Types can be used in log statements. | 621 // Stream operator so that enum class Types can be used in log statements. |
| 517 BASE_EXPORT std::ostream& operator<<(std::ostream& out, | 622 BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
| 518 const Value::Type& type); | 623 const Value::Type& type); |
| 519 | 624 |
| 520 } // namespace base | 625 } // namespace base |
| 521 | 626 |
| 522 #endif // BASE_VALUES_H_ | 627 #endif // BASE_VALUES_H_ |
| OLD | NEW |