OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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: |
11 // | 11 // |
(...skipping 27 matching lines...) Expand all Loading... |
39 class ListValue; | 39 class ListValue; |
40 | 40 |
41 typedef std::vector<Value*> ValueVector; | 41 typedef std::vector<Value*> ValueVector; |
42 typedef std::map<std::string, Value*> ValueMap; | 42 typedef std::map<std::string, Value*> ValueMap; |
43 | 43 |
44 // The Value class is the base class for Values. A Value can be | 44 // The Value class is the base class for Values. A Value can be |
45 // instantiated via the Create*Value() factory methods, or by directly | 45 // instantiated via the Create*Value() factory methods, or by directly |
46 // creating instances of the subclasses. | 46 // creating instances of the subclasses. |
47 class Value { | 47 class Value { |
48 public: | 48 public: |
| 49 enum ValueType { |
| 50 TYPE_NULL = 0, |
| 51 TYPE_BOOLEAN, |
| 52 TYPE_INTEGER, |
| 53 TYPE_REAL, |
| 54 TYPE_STRING, |
| 55 TYPE_BINARY, |
| 56 TYPE_DICTIONARY, |
| 57 TYPE_LIST |
| 58 }; |
| 59 |
49 virtual ~Value(); | 60 virtual ~Value(); |
50 | 61 |
51 // Convenience methods for creating Value objects for various | 62 // Convenience methods for creating Value objects for various |
52 // kinds of values without thinking about which class implements them. | 63 // kinds of values without thinking about which class implements them. |
53 // These can always be expected to return a valid Value*. | 64 // These can always be expected to return a valid Value*. |
54 static Value* CreateNullValue(); | 65 static Value* CreateNullValue(); |
55 static Value* CreateBooleanValue(bool in_value); | 66 static Value* CreateBooleanValue(bool in_value); |
56 static Value* CreateIntegerValue(int in_value); | 67 static Value* CreateIntegerValue(int in_value); |
57 static Value* CreateRealValue(double in_value); | 68 static Value* CreateRealValue(double in_value); |
58 static Value* CreateStringValue(const std::string& in_value); | 69 static Value* CreateStringValue(const std::string& in_value); |
59 static Value* CreateStringValue(const string16& in_value); | 70 static Value* CreateStringValue(const string16& in_value); |
60 | 71 |
61 // This one can return NULL if the input isn't valid. If the return value | 72 // This one can return NULL if the input isn't valid. If the return value |
62 // is non-null, the new object has taken ownership of the buffer pointer. | 73 // is non-null, the new object has taken ownership of the buffer pointer. |
63 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); | 74 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); |
64 | 75 |
65 typedef enum { | |
66 TYPE_NULL = 0, | |
67 TYPE_BOOLEAN, | |
68 TYPE_INTEGER, | |
69 TYPE_REAL, | |
70 TYPE_STRING, | |
71 TYPE_BINARY, | |
72 TYPE_DICTIONARY, | |
73 TYPE_LIST | |
74 } ValueType; | |
75 | |
76 // Returns the type of the value stored by the current Value object. | 76 // Returns the type of the value stored by the current Value object. |
77 // Each type will be implemented by only one subclass of Value, so it's | 77 // Each type will be implemented by only one subclass of Value, so it's |
78 // safe to use the ValueType to determine whether you can cast from | 78 // safe to use the ValueType to determine whether you can cast from |
79 // Value* to (Implementing Class)*. Also, a Value object never changes | 79 // Value* to (Implementing Class)*. Also, a Value object never changes |
80 // its type after construction. | 80 // its type after construction. |
81 ValueType GetType() const { return type_; } | 81 ValueType GetType() const { return type_; } |
82 | 82 |
83 // Returns true if the current object represents a given type. | 83 // Returns true if the current object represents a given type. |
84 bool IsType(ValueType type) const { return type == type_; } | 84 bool IsType(ValueType type) const { return type == type_; } |
85 | 85 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 virtual bool Equals(const Value* other) const; | 160 virtual bool Equals(const Value* other) const; |
161 | 161 |
162 private: | 162 private: |
163 std::string value_; | 163 std::string value_; |
164 | 164 |
165 DISALLOW_COPY_AND_ASSIGN(StringValue); | 165 DISALLOW_COPY_AND_ASSIGN(StringValue); |
166 }; | 166 }; |
167 | 167 |
168 class BinaryValue: public Value { | 168 class BinaryValue: public Value { |
169 public: | 169 public: |
| 170 virtual ~BinaryValue(); |
| 171 |
170 // Creates a Value to represent a binary buffer. The new object takes | 172 // Creates a Value to represent a binary buffer. The new object takes |
171 // ownership of the pointer passed in, if successful. | 173 // ownership of the pointer passed in, if successful. |
172 // Returns NULL if buffer is NULL. | 174 // Returns NULL if buffer is NULL. |
173 static BinaryValue* Create(char* buffer, size_t size); | 175 static BinaryValue* Create(char* buffer, size_t size); |
174 | 176 |
175 // For situations where you want to keep ownership of your buffer, this | 177 // For situations where you want to keep ownership of your buffer, this |
176 // factory method creates a new BinaryValue by copying the contents of the | 178 // factory method creates a new BinaryValue by copying the contents of the |
177 // buffer that's passed in. | 179 // buffer that's passed in. |
178 // Returns NULL if buffer is NULL. | 180 // Returns NULL if buffer is NULL. |
179 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); | 181 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); |
180 | 182 |
181 virtual ~BinaryValue(); | |
182 | |
183 // Subclassed methods | |
184 virtual Value* DeepCopy() const; | |
185 virtual bool Equals(const Value* other) const; | |
186 | |
187 size_t GetSize() const { return size_; } | 183 size_t GetSize() const { return size_; } |
188 char* GetBuffer() { return buffer_; } | 184 char* GetBuffer() { return buffer_; } |
189 const char* GetBuffer() const { return buffer_; } | 185 const char* GetBuffer() const { return buffer_; } |
190 | 186 |
| 187 // Overridden from Value: |
| 188 virtual Value* DeepCopy() const; |
| 189 virtual bool Equals(const Value* other) const; |
| 190 |
191 private: | 191 private: |
192 // Constructor is private so that only objects with valid buffer pointers | 192 // Constructor is private so that only objects with valid buffer pointers |
193 // and size values can be created. | 193 // and size values can be created. |
194 BinaryValue(char* buffer, size_t size); | 194 BinaryValue(char* buffer, size_t size); |
195 | 195 |
196 char* buffer_; | 196 char* buffer_; |
197 size_t size_; | 197 size_t size_; |
198 | 198 |
199 DISALLOW_COPY_AND_ASSIGN(BinaryValue); | 199 DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
200 }; | 200 }; |
201 | 201 |
202 // DictionaryValue provides a key-value dictionary with (optional) "path" | 202 // DictionaryValue provides a key-value dictionary with (optional) "path" |
203 // parsing for recursive access; see the comment at the top of the file. Keys | 203 // parsing for recursive access; see the comment at the top of the file. Keys |
204 // are |std::string|s and should be UTF-8 encoded. | 204 // are |std::string|s and should be UTF-8 encoded. |
205 class DictionaryValue : public Value { | 205 class DictionaryValue : public Value { |
206 public: | 206 public: |
207 DictionaryValue(); | 207 DictionaryValue(); |
208 virtual ~DictionaryValue(); | 208 virtual ~DictionaryValue(); |
209 | 209 |
210 // Subclassed methods | |
211 virtual Value* DeepCopy() const; | |
212 virtual bool Equals(const Value* other) const; | |
213 | |
214 // Returns true if the current dictionary has a value for the given key. | 210 // Returns true if the current dictionary has a value for the given key. |
215 bool HasKey(const std::string& key) const; | 211 bool HasKey(const std::string& key) const; |
216 | 212 |
217 // Returns the number of Values in this dictionary. | 213 // Returns the number of Values in this dictionary. |
218 size_t size() const { return dictionary_.size(); } | 214 size_t size() const { return dictionary_.size(); } |
219 | 215 |
220 // Returns whether the dictionary is empty. | 216 // Returns whether the dictionary is empty. |
221 bool empty() const { return dictionary_.empty(); } | 217 bool empty() const { return dictionary_.empty(); } |
222 | 218 |
223 // Clears any current contents of this dictionary. | 219 // Clears any current contents of this dictionary. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } | 322 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
327 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } | 323 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
328 | 324 |
329 private: | 325 private: |
330 ValueMap::const_iterator itr_; | 326 ValueMap::const_iterator itr_; |
331 }; | 327 }; |
332 | 328 |
333 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } | 329 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
334 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } | 330 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
335 | 331 |
| 332 // Overridden from Value: |
| 333 virtual Value* DeepCopy() const; |
| 334 virtual bool Equals(const Value* other) const; |
| 335 |
336 private: | 336 private: |
337 ValueMap dictionary_; | 337 ValueMap dictionary_; |
338 | 338 |
339 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); | 339 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
340 }; | 340 }; |
341 | 341 |
342 // This type of Value represents a list of other Value values. | 342 // This type of Value represents a list of other Value values. |
343 class ListValue : public Value { | 343 class ListValue : public Value { |
344 public: | 344 public: |
| 345 typedef ValueVector::iterator iterator; |
| 346 typedef ValueVector::const_iterator const_iterator; |
| 347 |
345 ListValue(); | 348 ListValue(); |
346 ~ListValue(); | 349 ~ListValue(); |
347 | 350 |
348 // Subclassed methods | |
349 virtual bool GetAsList(ListValue** out_value); | |
350 virtual Value* DeepCopy() const; | |
351 virtual bool Equals(const Value* other) const; | |
352 | |
353 // Clears the contents of this ListValue | 351 // Clears the contents of this ListValue |
354 void Clear(); | 352 void Clear(); |
355 | 353 |
356 // Returns the number of Values in this list. | 354 // Returns the number of Values in this list. |
357 size_t GetSize() const { return list_.size(); } | 355 size_t GetSize() const { return list_.size(); } |
358 | 356 |
359 // Returns whether the list is empty. | 357 // Returns whether the list is empty. |
360 bool empty() const { return list_.empty(); } | 358 bool empty() const { return list_.empty(); } |
361 | 359 |
362 // Sets the list item at the given index to be the Value specified by | 360 // Sets the list item at the given index to be the Value specified by |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 // Insert a Value at index. | 402 // Insert a Value at index. |
405 // Returns true if successful, or false if the index was out of range. | 403 // Returns true if successful, or false if the index was out of range. |
406 bool Insert(size_t index, Value* in_value); | 404 bool Insert(size_t index, Value* in_value); |
407 | 405 |
408 // Swaps contents with the |other| list. | 406 // Swaps contents with the |other| list. |
409 void Swap(ListValue* other) { | 407 void Swap(ListValue* other) { |
410 list_.swap(other->list_); | 408 list_.swap(other->list_); |
411 } | 409 } |
412 | 410 |
413 // Iteration | 411 // Iteration |
414 typedef ValueVector::iterator iterator; | |
415 typedef ValueVector::const_iterator const_iterator; | |
416 | |
417 ListValue::iterator begin() { return list_.begin(); } | 412 ListValue::iterator begin() { return list_.begin(); } |
418 ListValue::iterator end() { return list_.end(); } | 413 ListValue::iterator end() { return list_.end(); } |
419 | 414 |
420 ListValue::const_iterator begin() const { return list_.begin(); } | 415 ListValue::const_iterator begin() const { return list_.begin(); } |
421 ListValue::const_iterator end() const { return list_.end(); } | 416 ListValue::const_iterator end() const { return list_.end(); } |
422 | 417 |
| 418 // Overridden from Value: |
| 419 virtual bool GetAsList(ListValue** out_value); |
| 420 virtual Value* DeepCopy() const; |
| 421 virtual bool Equals(const Value* other) const; |
| 422 |
423 private: | 423 private: |
424 ValueVector list_; | 424 ValueVector list_; |
425 | 425 |
426 DISALLOW_COPY_AND_ASSIGN(ListValue); | 426 DISALLOW_COPY_AND_ASSIGN(ListValue); |
427 }; | 427 }; |
428 | 428 |
429 // This interface is implemented by classes that know how to serialize and | 429 // This interface is implemented by classes that know how to serialize and |
430 // deserialize Value objects. | 430 // deserialize Value objects. |
431 class ValueSerializer { | 431 class ValueSerializer { |
432 public: | 432 public: |
433 virtual ~ValueSerializer(); | 433 virtual ~ValueSerializer(); |
434 | 434 |
435 virtual bool Serialize(const Value& root) = 0; | 435 virtual bool Serialize(const Value& root) = 0; |
436 | 436 |
437 // This method deserializes the subclass-specific format into a Value object. | 437 // This method deserializes the subclass-specific format into a Value object. |
438 // 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 |
439 // 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, |
440 // error_code will be set with the underlying error. | 440 // error_code will be set with the underlying error. |
441 // 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 |
442 // error message including the location of the error if appropriate. | 442 // error message including the location of the error if appropriate. |
443 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 443 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
444 }; | 444 }; |
445 | 445 |
446 #endif // BASE_VALUES_H_ | 446 #endif // BASE_VALUES_H_ |
OLD | NEW |