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 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 // Makes a copy of |this| but doesn't include empty dictionaries and lists in | 301 // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
302 // the copy. This never returns NULL, even if |this| itself is empty. | 302 // the copy. This never returns NULL, even if |this| itself is empty. |
303 DictionaryValue* DeepCopyWithoutEmptyChildren(); | 303 DictionaryValue* DeepCopyWithoutEmptyChildren(); |
304 | 304 |
305 // Merge a given dictionary into this dictionary. This is done recursively, | 305 // Merge a given dictionary into this dictionary. This is done recursively, |
306 // i.e. any subdictionaries will be merged as well. In case of key collisions, | 306 // i.e. any subdictionaries will be merged as well. In case of key collisions, |
307 // the passed in dictionary takes precedence and data already present will be | 307 // the passed in dictionary takes precedence and data already present will be |
308 // replaced. | 308 // replaced. |
309 void MergeDictionary(const DictionaryValue* dictionary); | 309 void MergeDictionary(const DictionaryValue* dictionary); |
310 | 310 |
311 // Builds a vector containing all of the paths that are different between | |
312 // the dictionary and a second specified dictionary. These are paths of | |
313 // values that are either in one dictionary or the other but not both, OR | |
314 // paths that are present in both dictionaries but differ in value. | |
315 // Path strings are in ascending lexicographical order in the generated | |
316 // vector. |different_paths| is cleared before added any paths. | |
317 void GetDifferingPaths( | |
318 const DictionaryValue* other, | |
319 std::vector<std::string>* different_paths) const; | |
320 | |
321 // This class provides an iterator for the keys in the dictionary. | 311 // This class provides an iterator for the keys in the dictionary. |
322 // It can't be used to modify the dictionary. | 312 // It can't be used to modify the dictionary. |
323 // | 313 // |
324 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT | 314 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT |
325 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any | 315 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any |
326 // keys have '.'s in them. | 316 // keys have '.'s in them. |
327 class key_iterator | 317 class key_iterator |
328 : private std::iterator<std::input_iterator_tag, const std::string> { | 318 : private std::iterator<std::input_iterator_tag, const std::string> { |
329 public: | 319 public: |
330 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } | 320 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
331 key_iterator operator++() { | 321 key_iterator operator++() { |
332 ++itr_; | 322 ++itr_; |
333 return *this; | 323 return *this; |
334 } | 324 } |
335 const std::string& operator*() { return itr_->first; } | 325 const std::string& operator*() { return itr_->first; } |
336 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } | 326 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
337 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } | 327 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
338 | 328 |
339 private: | 329 private: |
340 ValueMap::const_iterator itr_; | 330 ValueMap::const_iterator itr_; |
341 }; | 331 }; |
342 | 332 |
343 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } | 333 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
344 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } | 334 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
345 | 335 |
346 private: | 336 private: |
347 // Does the actual heavy lifting for GetDifferingPaths. | |
348 // Returns true if a path is added to different_paths, otherwise false. | |
349 // The difference compuation is calculated recursively. The keys for | |
350 // dictionaries that are handled by recursive calls more shallow than | |
351 // the current one are concatenated and passed through to deeper calls in | |
352 // |path_prefix|. | |
353 bool GetDifferingPathsHelper( | |
354 const std::string& path_prefix, | |
355 const DictionaryValue* other, | |
356 std::vector<std::string>* different_paths) const; | |
357 | |
358 ValueMap dictionary_; | 337 ValueMap dictionary_; |
359 | 338 |
360 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); | 339 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
361 }; | 340 }; |
362 | 341 |
363 // This type of Value represents a list of other Value values. | 342 // This type of Value represents a list of other Value values. |
364 class ListValue : public Value { | 343 class ListValue : public Value { |
365 public: | 344 public: |
366 ListValue(); | 345 ListValue(); |
367 ~ListValue(); | 346 ~ListValue(); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 // This method deserializes the subclass-specific format into a Value object. | 437 // This method deserializes the subclass-specific format into a Value object. |
459 // If the return value is non-NULL, the caller takes ownership of returned | 438 // If the return value is non-NULL, the caller takes ownership of returned |
460 // Value. If the return value is NULL, and if error_code is non-NULL, | 439 // Value. If the return value is NULL, and if error_code is non-NULL, |
461 // error_code will be set with the underlying error. | 440 // error_code will be set with the underlying error. |
462 // If |error_message| is non-null, it will be filled in with a formatted | 441 // If |error_message| is non-null, it will be filled in with a formatted |
463 // error message including the location of the error if appropriate. | 442 // error message including the location of the error if appropriate. |
464 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 443 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
465 }; | 444 }; |
466 | 445 |
467 #endif // BASE_VALUES_H_ | 446 #endif // BASE_VALUES_H_ |
OLD | NEW |