OLD | NEW |
1 // Copyright (c) 2009 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: |
11 // | 11 // |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
86 // These methods allow the convenient retrieval of settings. | 86 // These methods allow the convenient retrieval of settings. |
87 // If the current setting object can be converted into the given type, | 87 // If the current setting object can be converted into the given type, |
88 // the value is returned through the "value" parameter and true is returned; | 88 // the value is returned through the |out_value| parameter and true is |
89 // otherwise, false is returned and "value" is unchanged. | 89 // returned; otherwise, false is returned and |out_value| is unchanged. |
90 virtual bool GetAsBoolean(bool* out_value) const; | 90 virtual bool GetAsBoolean(bool* out_value) const; |
91 virtual bool GetAsInteger(int* out_value) const; | 91 virtual bool GetAsInteger(int* out_value) const; |
92 virtual bool GetAsReal(double* out_value) const; | 92 virtual bool GetAsReal(double* out_value) const; |
93 virtual bool GetAsString(std::string* out_value) const; | 93 virtual bool GetAsString(std::string* out_value) const; |
94 virtual bool GetAsString(std::wstring* out_value) const; | 94 virtual bool GetAsString(std::wstring* out_value) const; |
95 virtual bool GetAsUTF16(string16* out_value) const; | 95 virtual bool GetAsUTF16(string16* out_value) const; |
96 | 96 |
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; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 void SetStringFromUTF16(const std::wstring& path, const string16& in_value); | 249 void SetStringFromUTF16(const std::wstring& path, const string16& in_value); |
250 | 250 |
251 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to | 251 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
252 // be used as paths. | 252 // be used as paths. |
253 void SetWithoutPathExpansion(const std::wstring& key, Value* in_value); | 253 void SetWithoutPathExpansion(const std::wstring& key, Value* in_value); |
254 | 254 |
255 // Gets the Value associated with the given path starting from this object. | 255 // Gets the Value associated with the given path starting from this object. |
256 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 256 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
257 // into the next DictionaryValue down. If the path can be resolved | 257 // into the next DictionaryValue down. If the path can be resolved |
258 // successfully, the value for the last key in the path will be returned | 258 // successfully, the value for the last key in the path will be returned |
259 // through the "value" parameter, and the function will return true. | 259 // through the |out_value| parameter, and the function will return true. |
260 // Otherwise, it will return false and "value" will be untouched. | 260 // Otherwise, it will return false and |out_value| will be untouched. |
261 // Note that the dictionary always owns the value that's returned. | 261 // Note that the dictionary always owns the value that's returned. |
262 bool Get(const std::wstring& path, Value** out_value) const; | 262 bool Get(const std::wstring& path, Value** out_value) const; |
263 | 263 |
264 // These are convenience forms of Get(). The value will be retrieved | 264 // These are convenience forms of Get(). The value will be retrieved |
265 // and the return value will be true if the path is valid and the value at | 265 // and the return value will be true if the path is valid and the value at |
266 // the end of the path can be returned in the form specified. | 266 // the end of the path can be returned in the form specified. |
267 bool GetBoolean(const std::wstring& path, bool* out_value) const; | 267 bool GetBoolean(const std::wstring& path, bool* out_value) const; |
268 bool GetInteger(const std::wstring& path, int* out_value) const; | 268 bool GetInteger(const std::wstring& path, int* out_value) const; |
269 bool GetReal(const std::wstring& path, double* out_value) const; | 269 bool GetReal(const std::wstring& path, double* out_value) const; |
270 bool GetString(const std::string& path, string16* out_value) const; | 270 bool GetString(const std::string& path, string16* out_value) const; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 // Returns whether the list is empty. | 362 // Returns whether the list is empty. |
363 bool empty() const { return list_.empty(); } | 363 bool empty() const { return list_.empty(); } |
364 | 364 |
365 // Sets the list item at the given index to be the Value specified by | 365 // Sets the list item at the given index to be the Value specified by |
366 // the value given. If the index beyond the current end of the list, null | 366 // the value given. If the index beyond the current end of the list, null |
367 // Values will be used to pad out the list. | 367 // Values will be used to pad out the list. |
368 // Returns true if successful, or false if the index was negative or | 368 // Returns true if successful, or false if the index was negative or |
369 // the value is a null pointer. | 369 // the value is a null pointer. |
370 bool Set(size_t index, Value* in_value); | 370 bool Set(size_t index, Value* in_value); |
371 | 371 |
372 // Gets the Value at the given index. Modifies value (and returns true) | 372 // Gets the Value at the given index. Modifies |out_value| (and returns true) |
373 // only if the index falls within the current list range. | 373 // only if the index falls within the current list range. |
374 // Note that the list always owns the Value passed out via out_value. | 374 // Note that the list always owns the Value passed out via |out_value|. |
375 bool Get(size_t index, Value** out_value) const; | 375 bool Get(size_t index, Value** out_value) const; |
376 | 376 |
377 // Convenience forms of Get(). Modifies value (and returns true) only if | 377 // Convenience forms of Get(). Modifies |out_value| (and returns true) |
378 // the index is valid and the Value at that index can be returned in | 378 // only if the index is valid and the Value at that index can be returned |
379 // the specified form. | 379 // in the specified form. |
380 bool GetBoolean(size_t index, bool* out_value) const; | 380 bool GetBoolean(size_t index, bool* out_value) const; |
381 bool GetInteger(size_t index, int* out_value) const; | 381 bool GetInteger(size_t index, int* out_value) const; |
382 bool GetReal(size_t index, double* out_value) const; | 382 bool GetReal(size_t index, double* out_value) const; |
383 bool GetString(size_t index, std::string* out_value) const; | 383 bool GetString(size_t index, std::string* out_value) const; |
384 bool GetString(size_t index, std::wstring* out_value) const; | 384 bool GetString(size_t index, std::wstring* out_value) const; |
385 bool GetStringAsUTF16(size_t index, string16* out_value) const; | 385 bool GetStringAsUTF16(size_t index, string16* out_value) const; |
386 bool GetBinary(size_t index, BinaryValue** out_value) const; | 386 bool GetBinary(size_t index, BinaryValue** out_value) const; |
387 bool GetDictionary(size_t index, DictionaryValue** out_value) const; | 387 bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
388 bool GetList(size_t index, ListValue** out_value) const; | 388 bool GetList(size_t index, ListValue** out_value) const; |
389 | 389 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 // This method deserializes the subclass-specific format into a Value object. | 432 // This method deserializes the subclass-specific format into a Value object. |
433 // If the return value is non-NULL, the caller takes ownership of returned | 433 // If the return value is non-NULL, the caller takes ownership of returned |
434 // Value. If the return value is NULL, and if error_code is non-NULL, | 434 // Value. If the return value is NULL, and if error_code is non-NULL, |
435 // error_code will be set with the underlying error. | 435 // error_code will be set with the underlying error. |
436 // If |error_message| is non-null, it will be filled in with a formatted | 436 // If |error_message| is non-null, it will be filled in with a formatted |
437 // error message including the location of the error if appropriate. | 437 // error message including the location of the error if appropriate. |
438 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 438 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
439 }; | 439 }; |
440 | 440 |
441 #endif // BASE_VALUES_H_ | 441 #endif // BASE_VALUES_H_ |
OLD | NEW |