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 10 matching lines...) Expand all Loading... |
21 #ifndef BASE_VALUES_H_ | 21 #ifndef BASE_VALUES_H_ |
22 #define BASE_VALUES_H_ | 22 #define BASE_VALUES_H_ |
23 #pragma once | 23 #pragma once |
24 | 24 |
25 #include <iterator> | 25 #include <iterator> |
26 #include <map> | 26 #include <map> |
27 #include <string> | 27 #include <string> |
28 #include <vector> | 28 #include <vector> |
29 | 29 |
30 #include "base/basictypes.h" | 30 #include "base/basictypes.h" |
| 31 #include "base/file_path.h" |
31 #include "base/string16.h" | 32 #include "base/string16.h" |
32 #include "build/build_config.h" | 33 #include "build/build_config.h" |
33 | 34 |
34 class Value; | 35 class Value; |
35 class FundamentalValue; | 36 class FundamentalValue; |
36 class StringValue; | 37 class StringValue; |
37 class BinaryValue; | 38 class BinaryValue; |
38 class DictionaryValue; | 39 class DictionaryValue; |
39 class ListValue; | 40 class ListValue; |
40 | 41 |
41 typedef std::vector<Value*> ValueVector; | 42 typedef std::vector<Value*> ValueVector; |
42 typedef std::map<std::string, Value*> ValueMap; | 43 typedef std::map<std::string, Value*> ValueMap; |
43 | 44 |
44 // The Value class is the base class for Values. A Value can be | 45 // The Value class is the base class for Values. A Value can be |
45 // instantiated via the Create*Value() factory methods, or by directly | 46 // instantiated via the Create*Value() factory methods, or by directly |
46 // creating instances of the subclasses. | 47 // creating instances of the subclasses. |
47 class Value { | 48 class Value { |
48 public: | 49 public: |
49 enum ValueType { | 50 enum ValueType { |
50 TYPE_NULL = 0, | 51 TYPE_NULL = 0, |
51 TYPE_BOOLEAN, | 52 TYPE_BOOLEAN, |
52 TYPE_INTEGER, | 53 TYPE_INTEGER, |
53 TYPE_REAL, | 54 TYPE_REAL, |
54 TYPE_STRING, | 55 TYPE_STRING, |
55 TYPE_BINARY, | 56 TYPE_BINARY, |
56 TYPE_DICTIONARY, | 57 TYPE_DICTIONARY, |
57 TYPE_LIST | 58 TYPE_LIST, |
| 59 TYPE_PATH, |
58 }; | 60 }; |
59 | 61 |
60 virtual ~Value(); | 62 virtual ~Value(); |
61 | 63 |
62 // Convenience methods for creating Value objects for various | 64 // Convenience methods for creating Value objects for various |
63 // kinds of values without thinking about which class implements them. | 65 // kinds of values without thinking about which class implements them. |
64 // These can always be expected to return a valid Value*. | 66 // These can always be expected to return a valid Value*. |
65 static Value* CreateNullValue(); | 67 static Value* CreateNullValue(); |
66 static Value* CreateBooleanValue(bool in_value); | 68 static Value* CreateBooleanValue(bool in_value); |
67 static Value* CreateIntegerValue(int in_value); | 69 static Value* CreateIntegerValue(int in_value); |
(...skipping 17 matching lines...) Expand all Loading... |
85 | 87 |
86 // These methods allow the convenient retrieval of settings. | 88 // These methods allow the convenient retrieval of settings. |
87 // If the current setting object can be converted into the given type, | 89 // 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 | 90 // the value is returned through the |out_value| parameter and true is |
89 // returned; otherwise, false is returned and |out_value| is unchanged. | 91 // returned; otherwise, false is returned and |out_value| is unchanged. |
90 virtual bool GetAsBoolean(bool* out_value) const; | 92 virtual bool GetAsBoolean(bool* out_value) const; |
91 virtual bool GetAsInteger(int* out_value) const; | 93 virtual bool GetAsInteger(int* out_value) const; |
92 virtual bool GetAsReal(double* out_value) const; | 94 virtual bool GetAsReal(double* out_value) const; |
93 virtual bool GetAsString(std::string* out_value) const; | 95 virtual bool GetAsString(std::string* out_value) const; |
94 virtual bool GetAsString(string16* out_value) const; | 96 virtual bool GetAsString(string16* out_value) const; |
| 97 virtual bool GetAsFilePath(FilePath* out_value) const; |
95 virtual bool GetAsList(ListValue** out_value); | 98 virtual bool GetAsList(ListValue** out_value); |
96 | 99 |
97 // This creates a deep copy of the entire Value tree, and returns a pointer | 100 // 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. | 101 // to the copy. The caller gets ownership of the copy, of course. |
99 virtual Value* DeepCopy() const; | 102 virtual Value* DeepCopy() const; |
100 | 103 |
101 // Compares if two Value objects have equal contents. | 104 // Compares if two Value objects have equal contents. |
102 virtual bool Equals(const Value* other) const; | 105 virtual bool Equals(const Value* other) const; |
103 | 106 |
104 // Compares if two Value objects have equal contents. Can handle NULLs. | 107 // Compares if two Value objects have equal contents. Can handle NULLs. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 virtual bool GetAsString(string16* out_value) const; | 161 virtual bool GetAsString(string16* out_value) const; |
159 virtual Value* DeepCopy() const; | 162 virtual Value* DeepCopy() const; |
160 virtual bool Equals(const Value* other) const; | 163 virtual bool Equals(const Value* other) const; |
161 | 164 |
162 private: | 165 private: |
163 std::string value_; | 166 std::string value_; |
164 | 167 |
165 DISALLOW_COPY_AND_ASSIGN(StringValue); | 168 DISALLOW_COPY_AND_ASSIGN(StringValue); |
166 }; | 169 }; |
167 | 170 |
| 171 class FilePathValue : public Value { |
| 172 public: |
| 173 explicit FilePathValue(const FilePath& in_value); |
| 174 virtual ~FilePathValue(); |
| 175 |
| 176 // Overridden from Value: |
| 177 virtual bool GetAsFilePath(FilePath* out_value) const; |
| 178 virtual bool Equals(const Value* other) const; |
| 179 |
| 180 private: |
| 181 FilePath value_; |
| 182 |
| 183 DISALLOW_COPY_AND_ASSIGN(FilePathValue); |
| 184 }; |
| 185 |
168 class BinaryValue: public Value { | 186 class BinaryValue: public Value { |
169 public: | 187 public: |
170 virtual ~BinaryValue(); | 188 virtual ~BinaryValue(); |
171 | 189 |
172 // Creates a Value to represent a binary buffer. The new object takes | 190 // Creates a Value to represent a binary buffer. The new object takes |
173 // ownership of the pointer passed in, if successful. | 191 // ownership of the pointer passed in, if successful. |
174 // Returns NULL if buffer is NULL. | 192 // Returns NULL if buffer is NULL. |
175 static BinaryValue* Create(char* buffer, size_t size); | 193 static BinaryValue* Create(char* buffer, size_t size); |
176 | 194 |
177 // For situations where you want to keep ownership of your buffer, this | 195 // For situations where you want to keep ownership of your buffer, this |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 bool GetBoolean(const std::string& path, bool* out_value) const; | 275 bool GetBoolean(const std::string& path, bool* out_value) const; |
258 bool GetInteger(const std::string& path, int* out_value) const; | 276 bool GetInteger(const std::string& path, int* out_value) const; |
259 bool GetReal(const std::string& path, double* out_value) const; | 277 bool GetReal(const std::string& path, double* out_value) const; |
260 bool GetString(const std::string& path, std::string* out_value) const; | 278 bool GetString(const std::string& path, std::string* out_value) const; |
261 bool GetString(const std::string& path, string16* out_value) const; | 279 bool GetString(const std::string& path, string16* out_value) const; |
262 bool GetStringASCII(const std::string& path, std::string* out_value) const; | 280 bool GetStringASCII(const std::string& path, std::string* out_value) const; |
263 bool GetBinary(const std::string& path, BinaryValue** out_value) const; | 281 bool GetBinary(const std::string& path, BinaryValue** out_value) const; |
264 bool GetDictionary(const std::string& path, | 282 bool GetDictionary(const std::string& path, |
265 DictionaryValue** out_value) const; | 283 DictionaryValue** out_value) const; |
266 bool GetList(const std::string& path, ListValue** out_value) const; | 284 bool GetList(const std::string& path, ListValue** out_value) const; |
| 285 bool GetFilePath(const std::string& path, FilePath* out_value) const; |
267 | 286 |
268 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to | 287 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
269 // be used as paths. | 288 // be used as paths. |
270 bool GetWithoutPathExpansion(const std::string& key, | 289 bool GetWithoutPathExpansion(const std::string& key, |
271 Value** out_value) const; | 290 Value** out_value) const; |
272 bool GetIntegerWithoutPathExpansion(const std::string& key, | 291 bool GetIntegerWithoutPathExpansion(const std::string& key, |
273 int* out_value) const; | 292 int* out_value) const; |
274 bool GetRealWithoutPathExpansion(const std::string& key, | 293 bool GetRealWithoutPathExpansion(const std::string& key, |
275 double* out_value) const; | 294 double* out_value) const; |
276 bool GetStringWithoutPathExpansion(const std::string& key, | 295 bool GetStringWithoutPathExpansion(const std::string& key, |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 // only if the index is valid and the Value at that index can be returned | 392 // only if the index is valid and the Value at that index can be returned |
374 // in the specified form. | 393 // in the specified form. |
375 bool GetBoolean(size_t index, bool* out_value) const; | 394 bool GetBoolean(size_t index, bool* out_value) const; |
376 bool GetInteger(size_t index, int* out_value) const; | 395 bool GetInteger(size_t index, int* out_value) const; |
377 bool GetReal(size_t index, double* out_value) const; | 396 bool GetReal(size_t index, double* out_value) const; |
378 bool GetString(size_t index, std::string* out_value) const; | 397 bool GetString(size_t index, std::string* out_value) const; |
379 bool GetString(size_t index, string16* out_value) const; | 398 bool GetString(size_t index, string16* out_value) const; |
380 bool GetBinary(size_t index, BinaryValue** out_value) const; | 399 bool GetBinary(size_t index, BinaryValue** out_value) const; |
381 bool GetDictionary(size_t index, DictionaryValue** out_value) const; | 400 bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
382 bool GetList(size_t index, ListValue** out_value) const; | 401 bool GetList(size_t index, ListValue** out_value) const; |
| 402 bool GetFilePath(size_t index, FilePath* out_value) const; |
383 | 403 |
384 // Removes the Value with the specified index from this list. | 404 // Removes the Value with the specified index from this list. |
385 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be | 405 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
386 // passed out via |out_value|. If |out_value| is NULL, the removed value will | 406 // passed out via |out_value|. If |out_value| is NULL, the removed value will |
387 // be deleted. This method returns true if |index| is valid; otherwise | 407 // be deleted. This method returns true if |index| is valid; otherwise |
388 // it will return false and the ListValue object will be unchanged. | 408 // it will return false and the ListValue object will be unchanged. |
389 bool Remove(size_t index, Value** out_value); | 409 bool Remove(size_t index, Value** out_value); |
390 | 410 |
391 // Removes the first instance of |value| found in the list, if any, and | 411 // Removes the first instance of |value| found in the list, if any, and |
392 // deletes it. Returns the index that it was located at (-1 for not present). | 412 // deletes it. Returns the index that it was located at (-1 for not present). |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 // This method deserializes the subclass-specific format into a Value object. | 457 // 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 | 458 // 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, | 459 // Value. If the return value is NULL, and if error_code is non-NULL, |
440 // error_code will be set with the underlying error. | 460 // error_code will be set with the underlying error. |
441 // If |error_message| is non-null, it will be filled in with a formatted | 461 // 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. | 462 // error message including the location of the error if appropriate. |
443 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 463 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
444 }; | 464 }; |
445 | 465 |
446 #endif // BASE_VALUES_H_ | 466 #endif // BASE_VALUES_H_ |
OLD | NEW |