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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 TYPE_DOUBLE, | 57 TYPE_DOUBLE, |
58 TYPE_STRING, | 58 TYPE_STRING, |
59 TYPE_BINARY, | 59 TYPE_BINARY, |
60 TYPE_DICTIONARY, | 60 TYPE_DICTIONARY, |
61 TYPE_LIST | 61 TYPE_LIST |
62 // Note: Do not add more types. See the file-level comment above for why. | 62 // Note: Do not add more types. See the file-level comment above for why. |
63 }; | 63 }; |
64 | 64 |
65 virtual ~Value(); | 65 virtual ~Value(); |
66 | 66 |
67 static Value* CreateNullValue(); | 67 static scoped_ptr<Value> CreateNullValue(); |
68 | 68 |
69 // Returns the type of the value stored by the current Value object. | 69 // Returns the type of the value stored by the current Value object. |
70 // Each type will be implemented by only one subclass of Value, so it's | 70 // Each type will be implemented by only one subclass of Value, so it's |
71 // safe to use the Type to determine whether you can cast from | 71 // safe to use the Type to determine whether you can cast from |
72 // Value* to (Implementing Class)*. Also, a Value object never changes | 72 // Value* to (Implementing Class)*. Also, a Value object never changes |
73 // its type after construction. | 73 // its type after construction. |
74 Type GetType() const { return type_; } | 74 Type GetType() const { return type_; } |
75 | 75 |
76 // Returns true if the current object represents a given type. | 76 // Returns true if the current object represents a given type. |
77 bool IsType(Type type) const { return type == type_; } | 77 bool IsType(Type type) const { return type == type_; } |
(...skipping 14 matching lines...) Expand all Loading... |
92 virtual bool GetAsDictionary(DictionaryValue** out_value); | 92 virtual bool GetAsDictionary(DictionaryValue** out_value); |
93 virtual bool GetAsDictionary(const DictionaryValue** out_value) const; | 93 virtual bool GetAsDictionary(const DictionaryValue** out_value) const; |
94 // Note: Do not add more types. See the file-level comment above for why. | 94 // Note: Do not add more types. See the file-level comment above for why. |
95 | 95 |
96 // This creates a deep copy of the entire Value tree, and returns a pointer | 96 // This creates a deep copy of the entire Value tree, and returns a pointer |
97 // to the copy. The caller gets ownership of the copy, of course. | 97 // to the copy. The caller gets ownership of the copy, of course. |
98 // | 98 // |
99 // Subclasses return their own type directly in their overrides; | 99 // Subclasses return their own type directly in their overrides; |
100 // this works because C++ supports covariant return types. | 100 // this works because C++ supports covariant return types. |
101 virtual Value* DeepCopy() const; | 101 virtual Value* DeepCopy() const; |
| 102 // Preferred version of DeepCopy. TODO(estade): remove the above. |
| 103 scoped_ptr<Value> CreateDeepCopy() const; |
102 | 104 |
103 // Compares if two Value objects have equal contents. | 105 // Compares if two Value objects have equal contents. |
104 virtual bool Equals(const Value* other) const; | 106 virtual bool Equals(const Value* other) const; |
105 | 107 |
106 // Compares if two Value objects have equal contents. Can handle NULLs. | 108 // Compares if two Value objects have equal contents. Can handle NULLs. |
107 // NULLs are considered equal but different from Value::CreateNullValue(). | 109 // NULLs are considered equal but different from Value::CreateNullValue(). |
108 static bool Equals(const Value* a, const Value* b); | 110 static bool Equals(const Value* a, const Value* b); |
109 | 111 |
110 protected: | 112 protected: |
111 // These aren't safe for end-users, but they are useful for subclasses. | 113 // These aren't safe for end-users, but they are useful for subclasses. |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 const std::string& key() const { return it_->first; } | 363 const std::string& key() const { return it_->first; } |
362 const Value& value() const { return *it_->second; } | 364 const Value& value() const { return *it_->second; } |
363 | 365 |
364 private: | 366 private: |
365 const DictionaryValue& target_; | 367 const DictionaryValue& target_; |
366 ValueMap::const_iterator it_; | 368 ValueMap::const_iterator it_; |
367 }; | 369 }; |
368 | 370 |
369 // Overridden from Value: | 371 // Overridden from Value: |
370 DictionaryValue* DeepCopy() const override; | 372 DictionaryValue* DeepCopy() const override; |
| 373 // Preferred version of DeepCopy. TODO(estade): remove the above. |
| 374 scoped_ptr<DictionaryValue> CreateDeepCopy() const; |
371 bool Equals(const Value* other) const override; | 375 bool Equals(const Value* other) const override; |
372 | 376 |
373 private: | 377 private: |
374 ValueMap dictionary_; | 378 ValueMap dictionary_; |
375 | 379 |
376 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); | 380 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
377 }; | 381 }; |
378 | 382 |
379 // This type of Value represents a list of other Value values. | 383 // This type of Value represents a list of other Value values. |
380 class BASE_EXPORT ListValue : public Value { | 384 class BASE_EXPORT ListValue : public Value { |
(...skipping 12 matching lines...) Expand all Loading... |
393 | 397 |
394 // Returns whether the list is empty. | 398 // Returns whether the list is empty. |
395 bool empty() const { return list_.empty(); } | 399 bool empty() const { return list_.empty(); } |
396 | 400 |
397 // Sets the list item at the given index to be the Value specified by | 401 // Sets the list item at the given index to be the Value specified by |
398 // the value given. If the index beyond the current end of the list, null | 402 // the value given. If the index beyond the current end of the list, null |
399 // Values will be used to pad out the list. | 403 // Values will be used to pad out the list. |
400 // Returns true if successful, or false if the index was negative or | 404 // Returns true if successful, or false if the index was negative or |
401 // the value is a null pointer. | 405 // the value is a null pointer. |
402 bool Set(size_t index, Value* in_value); | 406 bool Set(size_t index, Value* in_value); |
| 407 // Preferred version of the above. TODO(estade): remove the above. |
| 408 bool Set(size_t index, scoped_ptr<Value> in_value); |
403 | 409 |
404 // Gets the Value at the given index. Modifies |out_value| (and returns true) | 410 // Gets the Value at the given index. Modifies |out_value| (and returns true) |
405 // only if the index falls within the current list range. | 411 // only if the index falls within the current list range. |
406 // Note that the list always owns the Value passed out via |out_value|. | 412 // Note that the list always owns the Value passed out via |out_value|. |
407 // |out_value| is optional and will only be set if non-NULL. | 413 // |out_value| is optional and will only be set if non-NULL. |
408 bool Get(size_t index, const Value** out_value) const; | 414 bool Get(size_t index, const Value** out_value) const; |
409 bool Get(size_t index, Value** out_value); | 415 bool Get(size_t index, Value** out_value); |
410 | 416 |
411 // Convenience forms of Get(). Modifies |out_value| (and returns true) | 417 // Convenience forms of Get(). Modifies |out_value| (and returns true) |
412 // only if the index is valid and the Value at that index can be returned | 418 // only if the index is valid and the Value at that index can be returned |
(...skipping 25 matching lines...) Expand all Loading... |
438 // if not found. | 444 // if not found. |
439 bool Remove(const Value& value, size_t* index); | 445 bool Remove(const Value& value, size_t* index); |
440 | 446 |
441 // Removes the element at |iter|. If |out_value| is NULL, the value will be | 447 // Removes the element at |iter|. If |out_value| is NULL, the value will be |
442 // deleted, otherwise ownership of the value is passed back to the caller. | 448 // deleted, otherwise ownership of the value is passed back to the caller. |
443 // Returns an iterator pointing to the location of the element that | 449 // Returns an iterator pointing to the location of the element that |
444 // followed the erased element. | 450 // followed the erased element. |
445 iterator Erase(iterator iter, scoped_ptr<Value>* out_value); | 451 iterator Erase(iterator iter, scoped_ptr<Value>* out_value); |
446 | 452 |
447 // Appends a Value to the end of the list. | 453 // Appends a Value to the end of the list. |
| 454 void Append(scoped_ptr<Value> in_value); |
| 455 // Deprecated version of the above. TODO(estade): remove. |
448 void Append(Value* in_value); | 456 void Append(Value* in_value); |
449 | 457 |
450 // Convenience forms of Append. | 458 // Convenience forms of Append. |
451 void AppendBoolean(bool in_value); | 459 void AppendBoolean(bool in_value); |
452 void AppendInteger(int in_value); | 460 void AppendInteger(int in_value); |
453 void AppendDouble(double in_value); | 461 void AppendDouble(double in_value); |
454 void AppendString(const std::string& in_value); | 462 void AppendString(const std::string& in_value); |
455 void AppendString(const string16& in_value); | 463 void AppendString(const string16& in_value); |
456 void AppendStrings(const std::vector<std::string>& in_values); | 464 void AppendStrings(const std::vector<std::string>& in_values); |
457 void AppendStrings(const std::vector<string16>& in_values); | 465 void AppendStrings(const std::vector<string16>& in_values); |
(...skipping 21 matching lines...) Expand all Loading... |
479 | 487 |
480 const_iterator begin() const { return list_.begin(); } | 488 const_iterator begin() const { return list_.begin(); } |
481 const_iterator end() const { return list_.end(); } | 489 const_iterator end() const { return list_.end(); } |
482 | 490 |
483 // Overridden from Value: | 491 // Overridden from Value: |
484 bool GetAsList(ListValue** out_value) override; | 492 bool GetAsList(ListValue** out_value) override; |
485 bool GetAsList(const ListValue** out_value) const override; | 493 bool GetAsList(const ListValue** out_value) const override; |
486 ListValue* DeepCopy() const override; | 494 ListValue* DeepCopy() const override; |
487 bool Equals(const Value* other) const override; | 495 bool Equals(const Value* other) const override; |
488 | 496 |
| 497 // Preferred version of DeepCopy. TODO(estade): remove DeepCopy. |
| 498 scoped_ptr<ListValue> CreateDeepCopy() const; |
| 499 |
489 private: | 500 private: |
490 ValueVector list_; | 501 ValueVector list_; |
491 | 502 |
492 DISALLOW_COPY_AND_ASSIGN(ListValue); | 503 DISALLOW_COPY_AND_ASSIGN(ListValue); |
493 }; | 504 }; |
494 | 505 |
495 // This interface is implemented by classes that know how to serialize | 506 // This interface is implemented by classes that know how to serialize |
496 // Value objects. | 507 // Value objects. |
497 class BASE_EXPORT ValueSerializer { | 508 class BASE_EXPORT ValueSerializer { |
498 public: | 509 public: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 } | 549 } |
539 | 550 |
540 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | 551 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
541 const ListValue& value) { | 552 const ListValue& value) { |
542 return out << static_cast<const Value&>(value); | 553 return out << static_cast<const Value&>(value); |
543 } | 554 } |
544 | 555 |
545 } // namespace base | 556 } // namespace base |
546 | 557 |
547 #endif // BASE_VALUES_H_ | 558 #endif // BASE_VALUES_H_ |
OLD | NEW |