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 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 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 DictionaryValue** out_value) const; | 296 DictionaryValue** out_value) const; |
297 bool GetListWithoutPathExpansion(const std::string& key, | 297 bool GetListWithoutPathExpansion(const std::string& key, |
298 ListValue** out_value) const; | 298 ListValue** out_value) const; |
299 | 299 |
300 // Removes the Value with the specified path from this dictionary (or one | 300 // Removes the Value with the specified path from this dictionary (or one |
301 // of its child dictionaries, if the path is more than just a local key). | 301 // of its child dictionaries, if the path is more than just a local key). |
302 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be | 302 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
303 // passed out via out_value. If |out_value| is NULL, the removed value will | 303 // passed out via out_value. If |out_value| is NULL, the removed value will |
304 // be deleted. This method returns true if |path| is a valid path; otherwise | 304 // be deleted. This method returns true if |path| is a valid path; otherwise |
305 // it will return false and the DictionaryValue object will be unchanged. | 305 // it will return false and the DictionaryValue object will be unchanged. |
306 bool Remove(const std::string& path, Value** out_value); | 306 virtual bool Remove(const std::string& path, Value** out_value); |
307 | 307 |
308 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs | 308 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
309 // to be used as paths. | 309 // to be used as paths. |
310 bool RemoveWithoutPathExpansion(const std::string& key, Value** out_value); | 310 virtual bool RemoveWithoutPathExpansion(const std::string& key, |
| 311 Value** out_value); |
311 | 312 |
312 // Makes a copy of |this| but doesn't include empty dictionaries and lists in | 313 // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
313 // the copy. This never returns NULL, even if |this| itself is empty. | 314 // the copy. This never returns NULL, even if |this| itself is empty. |
314 DictionaryValue* DeepCopyWithoutEmptyChildren(); | 315 DictionaryValue* DeepCopyWithoutEmptyChildren(); |
315 | 316 |
316 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any | 317 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any |
317 // sub-dictionaries will be merged as well. In case of key collisions, the | 318 // sub-dictionaries will be merged as well. In case of key collisions, the |
318 // passed in dictionary takes precedence and data already present will be | 319 // passed in dictionary takes precedence and data already present will be |
319 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may | 320 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may |
320 // be freed any time after this call. | 321 // be freed any time after this call. |
321 void MergeDictionary(const DictionaryValue* dictionary); | 322 void MergeDictionary(const DictionaryValue* dictionary); |
322 | 323 |
323 // Swaps contents with the |other| dictionary. | 324 // Swaps contents with the |other| dictionary. |
324 void Swap(DictionaryValue* other) { | 325 virtual void Swap(DictionaryValue* other); |
325 dictionary_.swap(other->dictionary_); | |
326 } | |
327 | 326 |
328 // This class provides an iterator for the keys in the dictionary. | 327 // This class provides an iterator for the keys in the dictionary. |
329 // It can't be used to modify the dictionary. | 328 // It can't be used to modify the dictionary. |
330 // | 329 // |
331 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT | 330 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT |
332 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any | 331 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any |
333 // keys have '.'s in them. | 332 // keys have '.'s in them. |
334 class key_iterator | 333 class key_iterator |
335 : private std::iterator<std::input_iterator_tag, const std::string> { | 334 : private std::iterator<std::input_iterator_tag, const std::string> { |
336 public: | 335 public: |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 bool GetString(size_t index, string16* out_value) const; | 417 bool GetString(size_t index, string16* out_value) const; |
419 bool GetBinary(size_t index, BinaryValue** out_value) const; | 418 bool GetBinary(size_t index, BinaryValue** out_value) const; |
420 bool GetDictionary(size_t index, DictionaryValue** out_value) const; | 419 bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
421 bool GetList(size_t index, ListValue** out_value) const; | 420 bool GetList(size_t index, ListValue** out_value) const; |
422 | 421 |
423 // Removes the Value with the specified index from this list. | 422 // Removes the Value with the specified index from this list. |
424 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be | 423 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
425 // passed out via |out_value|. If |out_value| is NULL, the removed value will | 424 // passed out via |out_value|. If |out_value| is NULL, the removed value will |
426 // be deleted. This method returns true if |index| is valid; otherwise | 425 // be deleted. This method returns true if |index| is valid; otherwise |
427 // it will return false and the ListValue object will be unchanged. | 426 // it will return false and the ListValue object will be unchanged. |
428 bool Remove(size_t index, Value** out_value); | 427 virtual bool Remove(size_t index, Value** out_value); |
429 | 428 |
430 // Removes the first instance of |value| found in the list, if any, and | 429 // Removes the first instance of |value| found in the list, if any, and |
431 // deletes it. |index| is the location where |value| was found. Returns false | 430 // deletes it. |index| is the location where |value| was found. Returns false |
432 // if not found. | 431 // if not found. |
433 bool Remove(const Value& value, size_t* index); | 432 bool Remove(const Value& value, size_t* index); |
434 | 433 |
435 // Appends a Value to the end of the list. | 434 // Appends a Value to the end of the list. |
436 void Append(Value* in_value); | 435 void Append(Value* in_value); |
437 | 436 |
438 // Appends a Value if it's not already present. Takes ownership of the | 437 // Appends a Value if it's not already present. Takes ownership of the |
439 // |in_value|. Returns true if successful, or false if the value was already | 438 // |in_value|. Returns true if successful, or false if the value was already |
440 // present. If the value was already present the |in_value| is deleted. | 439 // present. If the value was already present the |in_value| is deleted. |
441 bool AppendIfNotPresent(Value* in_value); | 440 bool AppendIfNotPresent(Value* in_value); |
442 | 441 |
443 // Insert a Value at index. | 442 // Insert a Value at index. |
444 // Returns true if successful, or false if the index was out of range. | 443 // Returns true if successful, or false if the index was out of range. |
445 bool Insert(size_t index, Value* in_value); | 444 bool Insert(size_t index, Value* in_value); |
446 | 445 |
447 // Searches for the first instance of |value| in the list using the Equals | 446 // Searches for the first instance of |value| in the list using the Equals |
448 // method of the Value type. | 447 // method of the Value type. |
449 // Returns a const_iterator to the found item or to end() if none exists. | 448 // Returns a const_iterator to the found item or to end() if none exists. |
450 const_iterator Find(const Value& value) const; | 449 const_iterator Find(const Value& value) const; |
451 | 450 |
452 // Swaps contents with the |other| list. | 451 // Swaps contents with the |other| list. |
453 void Swap(ListValue* other) { | 452 virtual void Swap(ListValue* other); |
454 list_.swap(other->list_); | |
455 } | |
456 | 453 |
457 // Iteration. | 454 // Iteration. |
458 iterator begin() { return list_.begin(); } | 455 iterator begin() { return list_.begin(); } |
459 iterator end() { return list_.end(); } | 456 iterator end() { return list_.end(); } |
460 | 457 |
461 const_iterator begin() const { return list_.begin(); } | 458 const_iterator begin() const { return list_.begin(); } |
462 const_iterator end() const { return list_.end(); } | 459 const_iterator end() const { return list_.end(); } |
463 | 460 |
464 // Overridden from Value: | 461 // Overridden from Value: |
465 virtual bool GetAsList(ListValue** out_value) OVERRIDE; | 462 virtual bool GetAsList(ListValue** out_value) OVERRIDE; |
(...skipping 26 matching lines...) Expand all Loading... |
492 | 489 |
493 } // namespace base | 490 } // namespace base |
494 | 491 |
495 // http://crbug.com/88666 | 492 // http://crbug.com/88666 |
496 using base::DictionaryValue; | 493 using base::DictionaryValue; |
497 using base::ListValue; | 494 using base::ListValue; |
498 using base::StringValue; | 495 using base::StringValue; |
499 using base::Value; | 496 using base::Value; |
500 | 497 |
501 #endif // BASE_VALUES_H_ | 498 #endif // BASE_VALUES_H_ |
OLD | NEW |