OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 public: | 45 public: |
46 virtual ~Value(); | 46 virtual ~Value(); |
47 | 47 |
48 // Convenience methods for creating Value objects for various | 48 // Convenience methods for creating Value objects for various |
49 // kinds of values without thinking about which class implements them. | 49 // kinds of values without thinking about which class implements them. |
50 // These can always be expected to return a valid Value*. | 50 // These can always be expected to return a valid Value*. |
51 static Value* CreateNullValue(); | 51 static Value* CreateNullValue(); |
52 static Value* CreateBooleanValue(bool in_value); | 52 static Value* CreateBooleanValue(bool in_value); |
53 static Value* CreateIntegerValue(int in_value); | 53 static Value* CreateIntegerValue(int in_value); |
54 static Value* CreateRealValue(double in_value); | 54 static Value* CreateRealValue(double in_value); |
| 55 static Value* CreateStringValue(const std::string& in_value); |
55 static Value* CreateStringValue(const std::wstring& in_value); | 56 static Value* CreateStringValue(const std::wstring& in_value); |
56 | 57 |
57 // This one can return NULL if the input isn't valid. If the return value | 58 // This one can return NULL if the input isn't valid. If the return value |
58 // is non-null, the new object has taken ownership of the buffer pointer. | 59 // is non-null, the new object has taken ownership of the buffer pointer. |
59 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); | 60 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); |
60 | 61 |
61 typedef enum { | 62 typedef enum { |
62 TYPE_NULL = 0, | 63 TYPE_NULL = 0, |
63 TYPE_BOOLEAN, | 64 TYPE_BOOLEAN, |
64 TYPE_INTEGER, | 65 TYPE_INTEGER, |
(...skipping 14 matching lines...) Expand all Loading... |
79 // Returns true if the current object represents a given type. | 80 // Returns true if the current object represents a given type. |
80 bool IsType(ValueType type) const { return type == type_; } | 81 bool IsType(ValueType type) const { return type == type_; } |
81 | 82 |
82 // These methods allow the convenient retrieval of settings. | 83 // These methods allow the convenient retrieval of settings. |
83 // If the current setting object can be converted into the given type, | 84 // If the current setting object can be converted into the given type, |
84 // the value is returned through the "value" parameter and true is returned; | 85 // the value is returned through the "value" parameter and true is returned; |
85 // otherwise, false is returned and "value" is unchanged. | 86 // otherwise, false is returned and "value" is unchanged. |
86 virtual bool GetAsBoolean(bool* out_value) const; | 87 virtual bool GetAsBoolean(bool* out_value) const; |
87 virtual bool GetAsInteger(int* out_value) const; | 88 virtual bool GetAsInteger(int* out_value) const; |
88 virtual bool GetAsReal(double* out_value) const; | 89 virtual bool GetAsReal(double* out_value) const; |
| 90 virtual bool GetAsString(std::string* out_value) const; |
89 virtual bool GetAsString(std::wstring* out_value) const; | 91 virtual bool GetAsString(std::wstring* out_value) const; |
90 | 92 |
91 // This creates a deep copy of the entire Value tree, and returns a pointer | 93 // This creates a deep copy of the entire Value tree, and returns a pointer |
92 // to the copy. The caller gets ownership of the copy, of course. | 94 // to the copy. The caller gets ownership of the copy, of course. |
93 virtual Value* DeepCopy() const; | 95 virtual Value* DeepCopy() const; |
94 | 96 |
95 // Compares if two Value objects have equal contents. | 97 // Compares if two Value objects have equal contents. |
96 virtual bool Equals(const Value* other) const; | 98 virtual bool Equals(const Value* other) const; |
97 | 99 |
98 protected: | 100 protected: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 132 |
131 union { | 133 union { |
132 bool boolean_value_; | 134 bool boolean_value_; |
133 int integer_value_; | 135 int integer_value_; |
134 double real_value_; | 136 double real_value_; |
135 }; | 137 }; |
136 }; | 138 }; |
137 | 139 |
138 class StringValue : public Value { | 140 class StringValue : public Value { |
139 public: | 141 public: |
140 StringValue(const std::wstring& in_value) | 142 // Initializes a StringValue with a UTF-8 narrow character string. |
141 : Value(TYPE_STRING), value_(in_value) {} | 143 StringValue(const std::string& in_value); |
| 144 |
| 145 // Initializes a StringValue with a wide character string. |
| 146 StringValue(const std::wstring& in_value); |
| 147 |
142 ~StringValue(); | 148 ~StringValue(); |
143 | 149 |
144 // Subclassed methods | 150 // Subclassed methods |
| 151 bool GetAsString(std::string* out_value) const; |
145 bool GetAsString(std::wstring* out_value) const; | 152 bool GetAsString(std::wstring* out_value) const; |
146 Value* DeepCopy() const; | 153 Value* DeepCopy() const; |
147 virtual bool Equals(const Value* other) const; | 154 virtual bool Equals(const Value* other) const; |
148 | 155 |
149 private: | 156 private: |
150 DISALLOW_EVIL_CONSTRUCTORS(StringValue); | 157 DISALLOW_EVIL_CONSTRUCTORS(StringValue); |
151 | 158 |
152 std::wstring value_; | 159 std::string value_; |
153 }; | 160 }; |
154 | 161 |
155 class BinaryValue: public Value { | 162 class BinaryValue: public Value { |
156 public: | 163 public: |
157 // Creates a Value to represent a binary buffer. The new object takes | 164 // Creates a Value to represent a binary buffer. The new object takes |
158 // ownership of the pointer passed in, if successful. | 165 // ownership of the pointer passed in, if successful. |
159 // Returns NULL if buffer is NULL. | 166 // Returns NULL if buffer is NULL. |
160 static BinaryValue* Create(char* buffer, size_t size); | 167 static BinaryValue* Create(char* buffer, size_t size); |
161 | 168 |
162 // For situations where you want to keep ownership of your buffer, this | 169 // For situations where you want to keep ownership of your buffer, this |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // to the path in that location. | 216 // to the path in that location. |
210 // Note that the dictionary takes ownership of the value | 217 // Note that the dictionary takes ownership of the value |
211 // referenced by in_value. | 218 // referenced by in_value. |
212 bool Set(const std::wstring& path, Value* in_value); | 219 bool Set(const std::wstring& path, Value* in_value); |
213 | 220 |
214 // Convenience forms of Set(). These methods will replace any existing | 221 // Convenience forms of Set(). These methods will replace any existing |
215 // value at that path, even if it has a different type. | 222 // value at that path, even if it has a different type. |
216 bool SetBoolean(const std::wstring& path, bool in_value); | 223 bool SetBoolean(const std::wstring& path, bool in_value); |
217 bool SetInteger(const std::wstring& path, int in_value); | 224 bool SetInteger(const std::wstring& path, int in_value); |
218 bool SetReal(const std::wstring& path, double in_value); | 225 bool SetReal(const std::wstring& path, double in_value); |
| 226 bool SetString(const std::wstring& path, const std::string& in_value); |
219 bool SetString(const std::wstring& path, const std::wstring& in_value); | 227 bool SetString(const std::wstring& path, const std::wstring& in_value); |
220 | 228 |
221 // Gets the Value associated with the given path starting from this object. | 229 // Gets the Value associated with the given path starting from this object. |
222 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 230 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
223 // into the next DictionaryValue down. If the path can be resolved | 231 // into the next DictionaryValue down. If the path can be resolved |
224 // successfully, the value for the last key in the path will be returned | 232 // successfully, the value for the last key in the path will be returned |
225 // through the "value" parameter, and the function will return true. | 233 // through the "value" parameter, and the function will return true. |
226 // Otherwise, it will return false and "value" will be untouched. | 234 // Otherwise, it will return false and "value" will be untouched. |
227 // Note that the dictionary always owns the value that's returned. | 235 // Note that the dictionary always owns the value that's returned. |
228 bool Get(const std::wstring& path, Value** out_value) const; | 236 bool Get(const std::wstring& path, Value** out_value) const; |
229 | 237 |
230 // These are convenience forms of Get(). The value will be retrieved | 238 // These are convenience forms of Get(). The value will be retrieved |
231 // and the return value will be true if the path is valid and the value at | 239 // and the return value will be true if the path is valid and the value at |
232 // the end of the path can be returned in the form specified. | 240 // the end of the path can be returned in the form specified. |
233 bool GetBoolean(const std::wstring& path, bool* out_value) const; | 241 bool GetBoolean(const std::wstring& path, bool* out_value) const; |
234 bool GetInteger(const std::wstring& path, int* out_value) const; | 242 bool GetInteger(const std::wstring& path, int* out_value) const; |
235 bool GetReal(const std::wstring& path, double* out_value) const; | 243 bool GetReal(const std::wstring& path, double* out_value) const; |
| 244 bool GetString(const std::wstring& path, std::string* out_value) const; |
236 bool GetString(const std::wstring& path, std::wstring* out_value) const; | 245 bool GetString(const std::wstring& path, std::wstring* out_value) const; |
237 bool GetBinary(const std::wstring& path, BinaryValue** out_value) const; | 246 bool GetBinary(const std::wstring& path, BinaryValue** out_value) const; |
238 bool GetDictionary(const std::wstring& path, | 247 bool GetDictionary(const std::wstring& path, |
239 DictionaryValue** out_value) const; | 248 DictionaryValue** out_value) const; |
240 bool GetList(const std::wstring& path, ListValue** out_value) const; | 249 bool GetList(const std::wstring& path, ListValue** out_value) const; |
241 | 250 |
242 // Removes the Value with the specified path from this dictionary (or one | 251 // Removes the Value with the specified path from this dictionary (or one |
243 // of its child dictionaries, if the path is more than just a local key). | 252 // of its child dictionaries, if the path is more than just a local key). |
244 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be | 253 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
245 // passed out via out_value. If |out_value| is NULL, the removed value will | 254 // passed out via out_value. If |out_value| is NULL, the removed value will |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 // The method should return true if and only if the root parameter is set | 361 // The method should return true if and only if the root parameter is set |
353 // to a complete Value representation of the serialized form. If the | 362 // to a complete Value representation of the serialized form. If the |
354 // return value is true, the caller takes ownership of the objects pointed | 363 // return value is true, the caller takes ownership of the objects pointed |
355 // to by root. If the return value is false, root should be unchanged and if | 364 // to by root. If the return value is false, root should be unchanged and if |
356 // error_message is non-null, it should be filled with a message describing | 365 // error_message is non-null, it should be filled with a message describing |
357 // the error. | 366 // the error. |
358 virtual bool Deserialize(Value** root, std::string* error_message) = 0; | 367 virtual bool Deserialize(Value** root, std::string* error_message) = 0; |
359 }; | 368 }; |
360 | 369 |
361 #endif // BASE_VALUES_H_ | 370 #endif // BASE_VALUES_H_ |
OLD | NEW |