OLD | NEW |
1 // Copyright (c) 2011 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: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 { | 49 enum ValueType { |
50 TYPE_NULL = 0, | 50 TYPE_NULL = 0, |
51 TYPE_BOOLEAN, | 51 TYPE_BOOLEAN, |
52 TYPE_INTEGER, | 52 TYPE_INTEGER, |
53 TYPE_REAL, | 53 TYPE_DOUBLE, |
54 TYPE_STRING, | 54 TYPE_STRING, |
55 TYPE_BINARY, | 55 TYPE_BINARY, |
56 TYPE_DICTIONARY, | 56 TYPE_DICTIONARY, |
57 TYPE_LIST | 57 TYPE_LIST |
58 }; | 58 }; |
59 | 59 |
60 virtual ~Value(); | 60 virtual ~Value(); |
61 | 61 |
62 // Convenience methods for creating Value objects for various | 62 // Convenience methods for creating Value objects for various |
63 // kinds of values without thinking about which class implements them. | 63 // kinds of values without thinking about which class implements them. |
64 // These can always be expected to return a valid Value*. | 64 // These can always be expected to return a valid Value*. |
65 static Value* CreateNullValue(); | 65 static Value* CreateNullValue(); |
66 static FundamentalValue* CreateBooleanValue(bool in_value); | 66 static FundamentalValue* CreateBooleanValue(bool in_value); |
67 static FundamentalValue* CreateIntegerValue(int in_value); | 67 static FundamentalValue* CreateIntegerValue(int in_value); |
68 static FundamentalValue* CreateRealValue(double in_value); | 68 static FundamentalValue* CreateDoubleValue(double in_value); |
69 static StringValue* CreateStringValue(const std::string& in_value); | 69 static StringValue* CreateStringValue(const std::string& in_value); |
70 static StringValue* CreateStringValue(const string16& in_value); | 70 static StringValue* CreateStringValue(const string16& in_value); |
71 | 71 |
72 // 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 |
73 // 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. |
74 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); | 74 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); |
75 | 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 |
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 |out_value| parameter and true is | 88 // the value is returned through the |out_value| parameter and true is |
89 // returned; otherwise, false is returned and |out_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 GetAsDouble(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(string16* out_value) const; | 94 virtual bool GetAsString(string16* out_value) const; |
95 virtual bool GetAsList(ListValue** out_value); | 95 virtual bool GetAsList(ListValue** out_value); |
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 // | 99 // |
100 // Subclasses return their own type directly in their overrides; | 100 // Subclasses return their own type directly in their overrides; |
101 // this works because C++ supports covariant return types. | 101 // this works because C++ supports covariant return types. |
102 virtual Value* DeepCopy() const; | 102 virtual Value* DeepCopy() const; |
(...skipping 22 matching lines...) Expand all Loading... |
125 class FundamentalValue : public Value { | 125 class FundamentalValue : public Value { |
126 public: | 126 public: |
127 explicit FundamentalValue(bool in_value); | 127 explicit FundamentalValue(bool in_value); |
128 explicit FundamentalValue(int in_value); | 128 explicit FundamentalValue(int in_value); |
129 explicit FundamentalValue(double in_value); | 129 explicit FundamentalValue(double in_value); |
130 virtual ~FundamentalValue(); | 130 virtual ~FundamentalValue(); |
131 | 131 |
132 // Subclassed methods | 132 // Subclassed methods |
133 virtual bool GetAsBoolean(bool* out_value) const; | 133 virtual bool GetAsBoolean(bool* out_value) const; |
134 virtual bool GetAsInteger(int* out_value) const; | 134 virtual bool GetAsInteger(int* out_value) const; |
135 virtual bool GetAsReal(double* out_value) const; | 135 virtual bool GetAsDouble(double* out_value) const; |
136 virtual FundamentalValue* DeepCopy() const; | 136 virtual FundamentalValue* DeepCopy() const; |
137 virtual bool Equals(const Value* other) const; | 137 virtual bool Equals(const Value* other) const; |
138 | 138 |
139 private: | 139 private: |
140 union { | 140 union { |
141 bool boolean_value_; | 141 bool boolean_value_; |
142 int integer_value_; | 142 int integer_value_; |
143 double real_value_; | 143 double double_value_; |
144 }; | 144 }; |
145 | 145 |
146 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); | 146 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
147 }; | 147 }; |
148 | 148 |
149 class StringValue : public Value { | 149 class StringValue : public Value { |
150 public: | 150 public: |
151 // Initializes a StringValue with a UTF-8 narrow character string. | 151 // Initializes a StringValue with a UTF-8 narrow character string. |
152 explicit StringValue(const std::string& in_value); | 152 explicit StringValue(const std::string& in_value); |
153 | 153 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 // a DictionaryValue, a new DictionaryValue will be created and attached | 230 // a DictionaryValue, a new DictionaryValue will be created and attached |
231 // to the path in that location. | 231 // to the path in that location. |
232 // Note that the dictionary takes ownership of the value referenced by | 232 // Note that the dictionary takes ownership of the value referenced by |
233 // |in_value|, and therefore |in_value| must be non-NULL. | 233 // |in_value|, and therefore |in_value| must be non-NULL. |
234 void Set(const std::string& path, Value* in_value); | 234 void Set(const std::string& path, Value* in_value); |
235 | 235 |
236 // Convenience forms of Set(). These methods will replace any existing | 236 // Convenience forms of Set(). These methods will replace any existing |
237 // value at that path, even if it has a different type. | 237 // value at that path, even if it has a different type. |
238 void SetBoolean(const std::string& path, bool in_value); | 238 void SetBoolean(const std::string& path, bool in_value); |
239 void SetInteger(const std::string& path, int in_value); | 239 void SetInteger(const std::string& path, int in_value); |
240 void SetReal(const std::string& path, double in_value); | 240 void SetDouble(const std::string& path, double in_value); |
241 void SetString(const std::string& path, const std::string& in_value); | 241 void SetString(const std::string& path, const std::string& in_value); |
242 void SetString(const std::string& path, const string16& in_value); | 242 void SetString(const std::string& path, const string16& in_value); |
243 | 243 |
244 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to | 244 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
245 // be used as paths. | 245 // be used as paths. |
246 void SetWithoutPathExpansion(const std::string& key, Value* in_value); | 246 void SetWithoutPathExpansion(const std::string& key, Value* in_value); |
247 | 247 |
248 // Gets the Value associated with the given path starting from this object. | 248 // Gets the Value associated with the given path starting from this object. |
249 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 249 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
250 // into the next DictionaryValue down. If the path can be resolved | 250 // into the next DictionaryValue down. If the path can be resolved |
251 // successfully, the value for the last key in the path will be returned | 251 // successfully, the value for the last key in the path will be returned |
252 // through the |out_value| parameter, and the function will return true. | 252 // through the |out_value| parameter, and the function will return true. |
253 // Otherwise, it will return false and |out_value| will be untouched. | 253 // Otherwise, it will return false and |out_value| will be untouched. |
254 // Note that the dictionary always owns the value that's returned. | 254 // Note that the dictionary always owns the value that's returned. |
255 bool Get(const std::string& path, Value** out_value) const; | 255 bool Get(const std::string& path, Value** out_value) const; |
256 | 256 |
257 // These are convenience forms of Get(). The value will be retrieved | 257 // These are convenience forms of Get(). The value will be retrieved |
258 // and the return value will be true if the path is valid and the value at | 258 // and the return value will be true if the path is valid and the value at |
259 // the end of the path can be returned in the form specified. | 259 // the end of the path can be returned in the form specified. |
260 bool GetBoolean(const std::string& path, bool* out_value) const; | 260 bool GetBoolean(const std::string& path, bool* out_value) const; |
261 bool GetInteger(const std::string& path, int* out_value) const; | 261 bool GetInteger(const std::string& path, int* out_value) const; |
262 bool GetReal(const std::string& path, double* out_value) const; | 262 bool GetDouble(const std::string& path, double* out_value) const; |
263 bool GetString(const std::string& path, std::string* out_value) const; | 263 bool GetString(const std::string& path, std::string* out_value) const; |
264 bool GetString(const std::string& path, string16* out_value) const; | 264 bool GetString(const std::string& path, string16* out_value) const; |
265 bool GetStringASCII(const std::string& path, std::string* out_value) const; | 265 bool GetStringASCII(const std::string& path, std::string* out_value) const; |
266 bool GetBinary(const std::string& path, BinaryValue** out_value) const; | 266 bool GetBinary(const std::string& path, BinaryValue** out_value) const; |
267 bool GetDictionary(const std::string& path, | 267 bool GetDictionary(const std::string& path, |
268 DictionaryValue** out_value) const; | 268 DictionaryValue** out_value) const; |
269 bool GetList(const std::string& path, ListValue** out_value) const; | 269 bool GetList(const std::string& path, ListValue** out_value) const; |
270 | 270 |
271 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to | 271 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
272 // be used as paths. | 272 // be used as paths. |
273 bool GetWithoutPathExpansion(const std::string& key, | 273 bool GetWithoutPathExpansion(const std::string& key, |
274 Value** out_value) const; | 274 Value** out_value) const; |
275 bool GetIntegerWithoutPathExpansion(const std::string& key, | 275 bool GetIntegerWithoutPathExpansion(const std::string& key, |
276 int* out_value) const; | 276 int* out_value) const; |
277 bool GetRealWithoutPathExpansion(const std::string& key, | 277 bool GetDoubleWithoutPathExpansion(const std::string& key, |
278 double* out_value) const; | 278 double* out_value) const; |
279 bool GetStringWithoutPathExpansion(const std::string& key, | 279 bool GetStringWithoutPathExpansion(const std::string& key, |
280 std::string* out_value) const; | 280 std::string* out_value) const; |
281 bool GetStringWithoutPathExpansion(const std::string& key, | 281 bool GetStringWithoutPathExpansion(const std::string& key, |
282 string16* out_value) const; | 282 string16* out_value) const; |
283 bool GetDictionaryWithoutPathExpansion(const std::string& key, | 283 bool GetDictionaryWithoutPathExpansion(const std::string& key, |
284 DictionaryValue** out_value) const; | 284 DictionaryValue** out_value) const; |
285 bool GetListWithoutPathExpansion(const std::string& key, | 285 bool GetListWithoutPathExpansion(const std::string& key, |
286 ListValue** out_value) const; | 286 ListValue** out_value) const; |
287 | 287 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 // Gets the Value at the given index. Modifies |out_value| (and returns true) | 370 // Gets the Value at the given index. Modifies |out_value| (and returns true) |
371 // only if the index falls within the current list range. | 371 // only if the index falls within the current list range. |
372 // Note that the list always owns the Value passed out via |out_value|. | 372 // Note that the list always owns the Value passed out via |out_value|. |
373 bool Get(size_t index, Value** out_value) const; | 373 bool Get(size_t index, Value** out_value) const; |
374 | 374 |
375 // Convenience forms of Get(). Modifies |out_value| (and returns true) | 375 // Convenience forms of Get(). Modifies |out_value| (and returns true) |
376 // only if the index is valid and the Value at that index can be returned | 376 // only if the index is valid and the Value at that index can be returned |
377 // in the specified form. | 377 // in the specified form. |
378 bool GetBoolean(size_t index, bool* out_value) const; | 378 bool GetBoolean(size_t index, bool* out_value) const; |
379 bool GetInteger(size_t index, int* out_value) const; | 379 bool GetInteger(size_t index, int* out_value) const; |
380 bool GetReal(size_t index, double* out_value) const; | 380 bool GetDouble(size_t index, double* out_value) const; |
381 bool GetString(size_t index, std::string* out_value) const; | 381 bool GetString(size_t index, std::string* out_value) const; |
382 bool GetString(size_t index, string16* out_value) const; | 382 bool GetString(size_t index, string16* out_value) const; |
383 bool GetBinary(size_t index, BinaryValue** out_value) const; | 383 bool GetBinary(size_t index, BinaryValue** out_value) const; |
384 bool GetDictionary(size_t index, DictionaryValue** out_value) const; | 384 bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
385 bool GetList(size_t index, ListValue** out_value) const; | 385 bool GetList(size_t index, ListValue** out_value) const; |
386 | 386 |
387 // Removes the Value with the specified index from this list. | 387 // Removes the Value with the specified index from this list. |
388 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be | 388 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
389 // passed out via |out_value|. If |out_value| is NULL, the removed value will | 389 // passed out via |out_value|. If |out_value| is NULL, the removed value will |
390 // be deleted. This method returns true if |index| is valid; otherwise | 390 // be deleted. This method returns true if |index| is valid; otherwise |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 // This method deserializes the subclass-specific format into a Value object. | 440 // This method deserializes the subclass-specific format into a Value object. |
441 // If the return value is non-NULL, the caller takes ownership of returned | 441 // If the return value is non-NULL, the caller takes ownership of returned |
442 // Value. If the return value is NULL, and if error_code is non-NULL, | 442 // Value. If the return value is NULL, and if error_code is non-NULL, |
443 // error_code will be set with the underlying error. | 443 // error_code will be set with the underlying error. |
444 // If |error_message| is non-null, it will be filled in with a formatted | 444 // If |error_message| is non-null, it will be filled in with a formatted |
445 // error message including the location of the error if appropriate. | 445 // error message including the location of the error if appropriate. |
446 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 446 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
447 }; | 447 }; |
448 | 448 |
449 #endif // BASE_VALUES_H_ | 449 #endif // BASE_VALUES_H_ |
OLD | NEW |