| 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 | 5 // This file specifies a recursive data storage class called Value |
| 6 // intended for storing setting and other persistable data. | 6 // intended for storing setting and other persistable data. |
| 7 // It includes the ability to specify (recursive) lists and dictionaries, so | 7 // It includes the ability to specify (recursive) lists and dictionaries, so |
| 8 // it's fairly expressive. However, the API is optimized for the common case, | 8 // it's fairly expressive. However, the API is optimized for the common case, |
| 9 // namely storing a hierarchical tree of simple values. Given a | 9 // namely storing a hierarchical tree of simple values. Given a |
| 10 // DictionaryValue root, you can easily do things like: | 10 // DictionaryValue root, you can easily do things like: |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // This creates a deep copy of the entire Value tree, and returns a pointer | 97 // This creates a deep copy of the entire Value tree, and returns a pointer |
| 98 // to the copy. The caller gets ownership of the copy, of course. | 98 // to the copy. The caller gets ownership of the copy, of course. |
| 99 virtual Value* DeepCopy() const; | 99 virtual Value* DeepCopy() const; |
| 100 | 100 |
| 101 // Compares if two Value objects have equal contents. | 101 // Compares if two Value objects have equal contents. |
| 102 virtual bool Equals(const Value* other) const; | 102 virtual bool Equals(const Value* other) const; |
| 103 | 103 |
| 104 protected: | 104 protected: |
| 105 // This isn't safe for end-users (they should use the Create*Value() | 105 // This isn't safe for end-users (they should use the Create*Value() |
| 106 // static methods above), but it's useful for subclasses. | 106 // static methods above), but it's useful for subclasses. |
| 107 explicit Value(ValueType type) : type_(type) {} | 107 explicit Value(ValueType type); |
| 108 | 108 |
| 109 private: | 109 private: |
| 110 Value(); | 110 Value(); |
| 111 | 111 |
| 112 ValueType type_; | 112 ValueType type_; |
| 113 | 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(Value); | 114 DISALLOW_COPY_AND_ASSIGN(Value); |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 // FundamentalValue represents the simple fundamental types of values. | 117 // FundamentalValue represents the simple fundamental types of values. |
| 118 class FundamentalValue : public Value { | 118 class FundamentalValue : public Value { |
| 119 public: | 119 public: |
| 120 explicit FundamentalValue(bool in_value) | 120 explicit FundamentalValue(bool in_value); |
| 121 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {} | 121 explicit FundamentalValue(int in_value); |
| 122 explicit FundamentalValue(int in_value) | 122 explicit FundamentalValue(double in_value); |
| 123 : Value(TYPE_INTEGER), integer_value_(in_value) {} | |
| 124 explicit FundamentalValue(double in_value) | |
| 125 : Value(TYPE_REAL), real_value_(in_value) {} | |
| 126 ~FundamentalValue(); | 123 ~FundamentalValue(); |
| 127 | 124 |
| 128 // Subclassed methods | 125 // Subclassed methods |
| 129 virtual bool GetAsBoolean(bool* out_value) const; | 126 virtual bool GetAsBoolean(bool* out_value) const; |
| 130 virtual bool GetAsInteger(int* out_value) const; | 127 virtual bool GetAsInteger(int* out_value) const; |
| 131 virtual bool GetAsReal(double* out_value) const; | 128 virtual bool GetAsReal(double* out_value) const; |
| 132 virtual Value* DeepCopy() const; | 129 virtual Value* DeepCopy() const; |
| 133 virtual bool Equals(const Value* other) const; | 130 virtual bool Equals(const Value* other) const; |
| 134 | 131 |
| 135 private: | 132 private: |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 BinaryValue(char* buffer, size_t size); | 196 BinaryValue(char* buffer, size_t size); |
| 200 | 197 |
| 201 char* buffer_; | 198 char* buffer_; |
| 202 size_t size_; | 199 size_t size_; |
| 203 | 200 |
| 204 DISALLOW_COPY_AND_ASSIGN(BinaryValue); | 201 DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
| 205 }; | 202 }; |
| 206 | 203 |
| 207 class DictionaryValue : public Value { | 204 class DictionaryValue : public Value { |
| 208 public: | 205 public: |
| 209 DictionaryValue() : Value(TYPE_DICTIONARY) {} | 206 DictionaryValue(); |
| 210 ~DictionaryValue(); | 207 ~DictionaryValue(); |
| 211 | 208 |
| 212 // Subclassed methods | 209 // Subclassed methods |
| 213 Value* DeepCopy() const; | 210 Value* DeepCopy() const; |
| 214 virtual bool Equals(const Value* other) const; | 211 virtual bool Equals(const Value* other) const; |
| 215 | 212 |
| 216 // Returns true if the current dictionary has a value for the given key. | 213 // Returns true if the current dictionary has a value for the given key. |
| 217 bool HasKeyASCII(const std::string& key) const; | 214 bool HasKeyASCII(const std::string& key) const; |
| 218 // Deprecated version of the above. TODO: add a string16 version for Unicode. | 215 // Deprecated version of the above. TODO: add a string16 version for Unicode. |
| 219 // http://code.google.com/p/chromium/issues/detail?id=23581 | 216 // http://code.google.com/p/chromium/issues/detail?id=23581 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 | 342 |
| 346 private: | 343 private: |
| 347 ValueMap dictionary_; | 344 ValueMap dictionary_; |
| 348 | 345 |
| 349 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); | 346 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
| 350 }; | 347 }; |
| 351 | 348 |
| 352 // This type of Value represents a list of other Value values. | 349 // This type of Value represents a list of other Value values. |
| 353 class ListValue : public Value { | 350 class ListValue : public Value { |
| 354 public: | 351 public: |
| 355 ListValue() : Value(TYPE_LIST) {} | 352 ListValue(); |
| 356 ~ListValue(); | 353 ~ListValue(); |
| 357 | 354 |
| 358 // Subclassed methods | 355 // Subclassed methods |
| 359 Value* DeepCopy() const; | 356 Value* DeepCopy() const; |
| 360 virtual bool Equals(const Value* other) const; | 357 virtual bool Equals(const Value* other) const; |
| 361 | 358 |
| 362 // Clears the contents of this ListValue | 359 // Clears the contents of this ListValue |
| 363 void Clear(); | 360 void Clear(); |
| 364 | 361 |
| 365 // Returns the number of Values in this list. | 362 // Returns the number of Values in this list. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 private: | 425 private: |
| 429 ValueVector list_; | 426 ValueVector list_; |
| 430 | 427 |
| 431 DISALLOW_COPY_AND_ASSIGN(ListValue); | 428 DISALLOW_COPY_AND_ASSIGN(ListValue); |
| 432 }; | 429 }; |
| 433 | 430 |
| 434 // This interface is implemented by classes that know how to serialize and | 431 // This interface is implemented by classes that know how to serialize and |
| 435 // deserialize Value objects. | 432 // deserialize Value objects. |
| 436 class ValueSerializer { | 433 class ValueSerializer { |
| 437 public: | 434 public: |
| 438 virtual ~ValueSerializer() {} | 435 virtual ~ValueSerializer(); |
| 439 | 436 |
| 440 virtual bool Serialize(const Value& root) = 0; | 437 virtual bool Serialize(const Value& root) = 0; |
| 441 | 438 |
| 442 // This method deserializes the subclass-specific format into a Value object. | 439 // This method deserializes the subclass-specific format into a Value object. |
| 443 // If the return value is non-NULL, the caller takes ownership of returned | 440 // If the return value is non-NULL, the caller takes ownership of returned |
| 444 // Value. If the return value is NULL, and if error_code is non-NULL, | 441 // Value. If the return value is NULL, and if error_code is non-NULL, |
| 445 // error_code will be set with the underlying error. | 442 // error_code will be set with the underlying error. |
| 446 // If |error_message| is non-null, it will be filled in with a formatted | 443 // If |error_message| is non-null, it will be filled in with a formatted |
| 447 // error message including the location of the error if appropriate. | 444 // error message including the location of the error if appropriate. |
| 448 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 445 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
| 449 }; | 446 }; |
| 450 | 447 |
| 451 #endif // BASE_VALUES_H_ | 448 #endif // BASE_VALUES_H_ |
| OLD | NEW |