| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 setting and other persistable data. It includes the ability to | 6 // storing setting and other persistable data. It includes the ability to |
| 7 // specify (recursive) lists and dictionaries, so it's fairly expressive. | 7 // specify (recursive) lists and dictionaries, so it's fairly expressive. |
| 8 // However, the API is optimized for the common case, namely storing a | 8 // However, the API is optimized for the common case, namely storing a |
| 9 // hierarchical tree of simple values. Given a DictionaryValue root, you can | 9 // hierarchical tree of simple values. Given a DictionaryValue root, you can |
| 10 // easily do things like: | 10 // easily do things like: |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 // Makes a copy of |this| but doesn't include empty dictionaries and lists in | 360 // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 361 // the copy. This never returns NULL, even if |this| itself is empty. | 361 // the copy. This never returns NULL, even if |this| itself is empty. |
| 362 DictionaryValue* DeepCopyWithoutEmptyChildren(); | 362 DictionaryValue* DeepCopyWithoutEmptyChildren(); |
| 363 | 363 |
| 364 // Merge a given dictionary into this dictionary. This is done recursively, | 364 // Merge a given dictionary into this dictionary. This is done recursively, |
| 365 // i.e. any subdictionaries will be merged as well. In case of key collisions, | 365 // i.e. any subdictionaries will be merged as well. In case of key collisions, |
| 366 // the passed in dictionary takes precedence and data already present will be | 366 // the passed in dictionary takes precedence and data already present will be |
| 367 // replaced. | 367 // replaced. |
| 368 void MergeDictionary(const DictionaryValue* dictionary); | 368 void MergeDictionary(const DictionaryValue* dictionary); |
| 369 | 369 |
| 370 // Builds a vector containing all of the paths that are different between |
| 371 // the dictionary and a second specified dictionary. These are paths of |
| 372 // values that are either in one dictionary or the other but not both, OR |
| 373 // paths that are present in both dictionaries but differ in value. |
| 374 // Path strings are in ascending lexicographical order in the generated |
| 375 // vector. |different_paths| is cleared before added any paths. |
| 376 void GetDifferingPaths( |
| 377 const DictionaryValue* other, |
| 378 std::vector<std::string>* different_paths) const; |
| 379 |
| 370 // This class provides an iterator for the keys in the dictionary. | 380 // This class provides an iterator for the keys in the dictionary. |
| 371 // It can't be used to modify the dictionary. | 381 // It can't be used to modify the dictionary. |
| 372 // | 382 // |
| 373 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT | 383 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT |
| 374 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any | 384 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any |
| 375 // keys have '.'s in them. | 385 // keys have '.'s in them. |
| 376 class key_iterator | 386 class key_iterator |
| 377 : private std::iterator<std::input_iterator_tag, const std::string> { | 387 : private std::iterator<std::input_iterator_tag, const std::string> { |
| 378 public: | 388 public: |
| 379 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } | 389 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
| 380 key_iterator operator++() { | 390 key_iterator operator++() { |
| 381 ++itr_; | 391 ++itr_; |
| 382 return *this; | 392 return *this; |
| 383 } | 393 } |
| 384 const std::string& operator*() { return itr_->first; } | 394 const std::string& operator*() { return itr_->first; } |
| 385 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } | 395 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
| 386 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } | 396 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
| 387 | 397 |
| 388 private: | 398 private: |
| 389 ValueMap::const_iterator itr_; | 399 ValueMap::const_iterator itr_; |
| 390 }; | 400 }; |
| 391 | 401 |
| 392 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } | 402 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
| 393 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } | 403 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
| 394 | 404 |
| 395 private: | 405 private: |
| 406 // Does the actual heavy lifting for GetDifferingPaths. |
| 407 // Returns true if a path is added to different_paths, otherwise false. |
| 408 // The difference compuation is calculated recursively. The keys for |
| 409 // dictionaries that are handled by recursive calls more shallow than |
| 410 // the current one are concatenated and passed through to deeper calls in |
| 411 // |path_prefix|. |
| 412 bool GetDifferingPathsHelper( |
| 413 const std::string& path_prefix, |
| 414 const DictionaryValue* other, |
| 415 std::vector<std::string>* different_paths) const; |
| 416 |
| 396 ValueMap dictionary_; | 417 ValueMap dictionary_; |
| 397 | 418 |
| 398 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); | 419 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
| 399 }; | 420 }; |
| 400 | 421 |
| 401 // This type of Value represents a list of other Value values. | 422 // This type of Value represents a list of other Value values. |
| 402 class ListValue : public Value { | 423 class ListValue : public Value { |
| 403 public: | 424 public: |
| 404 ListValue(); | 425 ListValue(); |
| 405 ~ListValue(); | 426 ~ListValue(); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 // This method deserializes the subclass-specific format into a Value object. | 512 // This method deserializes the subclass-specific format into a Value object. |
| 492 // If the return value is non-NULL, the caller takes ownership of returned | 513 // If the return value is non-NULL, the caller takes ownership of returned |
| 493 // Value. If the return value is NULL, and if error_code is non-NULL, | 514 // Value. If the return value is NULL, and if error_code is non-NULL, |
| 494 // error_code will be set with the underlying error. | 515 // error_code will be set with the underlying error. |
| 495 // If |error_message| is non-null, it will be filled in with a formatted | 516 // If |error_message| is non-null, it will be filled in with a formatted |
| 496 // error message including the location of the error if appropriate. | 517 // error message including the location of the error if appropriate. |
| 497 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 518 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
| 498 }; | 519 }; |
| 499 | 520 |
| 500 #endif // BASE_VALUES_H_ | 521 #endif // BASE_VALUES_H_ |
| OLD | NEW |