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: |
11 // | 11 // |
12 // root->SetString(L"global.pages.homepage", L"http://goateleporter.com"); | 12 // root->SetString(L"global.pages.homepage", L"http://goateleporter.com"); |
13 // std::wstring homepage = L"http://google.com"; // default/fallback value | 13 // std::wstring homepage = L"http://google.com"; // default/fallback value |
14 // root->GetString(L"global.pages.homepage", &homepage); | 14 // root->GetString(L"global.pages.homepage", &homepage); |
15 // | 15 // |
16 // where "global" and "pages" are also DictionaryValues, and "homepage" | 16 // where "global" and "pages" are also DictionaryValues, and "homepage" |
17 // is a string setting. If some elements of the path didn't exist yet, | 17 // is a string setting. If some elements of the path didn't exist yet, |
18 // the SetString() method would create the missing elements and attach them | 18 // the SetString() method would create the missing elements and attach them |
19 // to root before attaching the homepage value. | 19 // to root before attaching the homepage value. |
20 | 20 |
21 #ifndef BASE_VALUES_H_ | 21 #ifndef BASE_VALUES_H_ |
22 #define BASE_VALUES_H_ | 22 #define BASE_VALUES_H_ |
23 | 23 |
24 #include <iterator> | 24 #include <iterator> |
25 #include <map> | 25 #include <map> |
26 #include <string> | |
27 #include <vector> | 26 #include <vector> |
28 | 27 |
29 #include "base/basictypes.h" | 28 #include "base/basictypes.h" |
| 29 #include "base/string16.h" |
30 | 30 |
31 class Value; | 31 class Value; |
32 class FundamentalValue; | 32 class FundamentalValue; |
33 class StringValue; | 33 class StringValue; |
34 class BinaryValue; | 34 class BinaryValue; |
35 class DictionaryValue; | 35 class DictionaryValue; |
36 class ListValue; | 36 class ListValue; |
37 | 37 |
38 typedef std::vector<Value*> ValueVector; | 38 typedef std::vector<Value*> ValueVector; |
39 typedef std::map<std::wstring, Value*> ValueMap; | 39 typedef std::map<string16, Value*> ValueMap; |
40 | 40 |
41 // The Value class is the base class for Values. A Value can be | 41 // The Value class is the base class for Values. A Value can be |
42 // instantiated via the Create*Value() factory methods, or by directly | 42 // instantiated via the Create*Value() factory methods, or by directly |
43 // creating instances of the subclasses. | 43 // creating instances of the subclasses. |
44 class Value { | 44 class Value { |
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. |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 class DictionaryValue : public Value { | 195 class DictionaryValue : public Value { |
196 public: | 196 public: |
197 DictionaryValue() : Value(TYPE_DICTIONARY) {} | 197 DictionaryValue() : Value(TYPE_DICTIONARY) {} |
198 ~DictionaryValue(); | 198 ~DictionaryValue(); |
199 | 199 |
200 // Subclassed methods | 200 // Subclassed methods |
201 Value* DeepCopy() const; | 201 Value* DeepCopy() const; |
202 virtual bool Equals(const Value* other) const; | 202 virtual bool Equals(const Value* other) const; |
203 | 203 |
204 // Returns true if the current dictionary has a value for the given key. | 204 // Returns true if the current dictionary has a value for the given key. |
205 bool HasKey(const std::wstring& key) const; | 205 bool HasKey(const string16& key) const; |
206 | 206 |
207 // Clears any current contents of this dictionary. | 207 // Clears any current contents of this dictionary. |
208 void Clear(); | 208 void Clear(); |
209 | 209 |
210 // Sets the Value associated with the given path starting from this object. | 210 // Sets the Value associated with the given path starting from this object. |
211 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 211 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
212 // into the next DictionaryValue down. Obviously, "." can't be used | 212 // into the next DictionaryValue down. Obviously, "." can't be used |
213 // within a key, but there are no other restrictions on keys. | 213 // within a key, but there are no other restrictions on keys. |
214 // If the key at any step of the way doesn't exist, or exists but isn't | 214 // If the key at any step of the way doesn't exist, or exists but isn't |
215 // a DictionaryValue, a new DictionaryValue will be created and attached | 215 // a DictionaryValue, a new DictionaryValue will be created and attached |
216 // to the path in that location. | 216 // to the path in that location. |
217 // Note that the dictionary takes ownership of the value | 217 // Note that the dictionary takes ownership of the value |
218 // referenced by in_value. | 218 // referenced by in_value. |
219 bool Set(const std::wstring& path, Value* in_value); | 219 bool Set(const string16& path, Value* in_value); |
220 | 220 |
221 // Convenience forms of Set(). These methods will replace any existing | 221 // Convenience forms of Set(). These methods will replace any existing |
222 // value at that path, even if it has a different type. | 222 // value at that path, even if it has a different type. |
223 bool SetBoolean(const std::wstring& path, bool in_value); | 223 bool SetBoolean(const string16& path, bool in_value); |
224 bool SetInteger(const std::wstring& path, int in_value); | 224 bool SetInteger(const string16& path, int in_value); |
225 bool SetReal(const std::wstring& path, double in_value); | 225 bool SetReal(const string16& path, double in_value); |
226 bool SetString(const std::wstring& path, const std::string& in_value); | 226 bool SetString(const string16& path, const std::string& in_value); |
227 bool SetString(const std::wstring& path, const std::wstring& in_value); | 227 bool SetString(const string16& path, const string16& in_value); |
228 | 228 |
229 // 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. |
230 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 230 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
231 // into the next DictionaryValue down. If the path can be resolved | 231 // into the next DictionaryValue down. If the path can be resolved |
232 // 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 |
233 // through the "value" parameter, and the function will return true. | 233 // through the "value" parameter, and the function will return true. |
234 // Otherwise, it will return false and "value" will be untouched. | 234 // Otherwise, it will return false and "value" will be untouched. |
235 // Note that the dictionary always owns the value that's returned. | 235 // Note that the dictionary always owns the value that's returned. |
236 bool Get(const std::wstring& path, Value** out_value) const; | 236 bool Get(const string16& path, Value** out_value) const; |
237 | 237 |
238 // These are convenience forms of Get(). The value will be retrieved | 238 // These are convenience forms of Get(). The value will be retrieved |
239 // 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 |
240 // 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. |
241 bool GetBoolean(const std::wstring& path, bool* out_value) const; | 241 bool GetBoolean(const string16& path, bool* out_value) const; |
242 bool GetInteger(const std::wstring& path, int* out_value) const; | 242 bool GetInteger(const string16& path, int* out_value) const; |
243 bool GetReal(const std::wstring& path, double* out_value) const; | 243 bool GetReal(const string16& path, double* out_value) const; |
244 bool GetString(const std::wstring& path, std::string* out_value) const; | 244 bool GetString(const string16& path, std::string* out_value) const; |
245 bool GetString(const std::wstring& path, std::wstring* out_value) const; | 245 bool GetString(const string16& path, string16* out_value) const; |
246 bool GetBinary(const std::wstring& path, BinaryValue** out_value) const; | 246 bool GetBinary(const string16& path, BinaryValue** out_value) const; |
247 bool GetDictionary(const std::wstring& path, | 247 bool GetDictionary(const string16& path, |
248 DictionaryValue** out_value) const; | 248 DictionaryValue** out_value) const; |
249 bool GetList(const std::wstring& path, ListValue** out_value) const; | 249 bool GetList(const string16& path, ListValue** out_value) const; |
250 | 250 |
251 // 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 |
252 // 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). |
253 // 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 |
254 // 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 |
255 // be deleted. This method returns true if |path| is a valid path; otherwise | 255 // be deleted. This method returns true if |path| is a valid path; otherwise |
256 // it will return false and the DictionaryValue object will be unchanged. | 256 // it will return false and the DictionaryValue object will be unchanged. |
257 bool Remove(const std::wstring& path, Value** out_value); | 257 bool Remove(const string16& path, Value** out_value); |
258 | 258 |
259 // This class provides an iterator for the keys in the dictionary. | 259 // This class provides an iterator for the keys in the dictionary. |
260 // It can't be used to modify the dictionary. | 260 // It can't be used to modify the dictionary. |
261 class key_iterator | 261 class key_iterator |
262 : private std::iterator<std::input_iterator_tag, const std::wstring> { | 262 : private std::iterator<std::input_iterator_tag, const string16> { |
263 public: | 263 public: |
264 key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } | 264 key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
265 key_iterator operator++() { ++itr_; return *this; } | 265 key_iterator operator++() { ++itr_; return *this; } |
266 const std::wstring& operator*() { return itr_->first; } | 266 const string16& operator*() { return itr_->first; } |
267 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } | 267 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
268 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } | 268 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
269 | 269 |
270 private: | 270 private: |
271 ValueMap::const_iterator itr_; | 271 ValueMap::const_iterator itr_; |
272 }; | 272 }; |
273 | 273 |
274 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } | 274 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
275 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } | 275 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
276 | 276 |
277 private: | 277 private: |
278 DISALLOW_EVIL_CONSTRUCTORS(DictionaryValue); | 278 DISALLOW_EVIL_CONSTRUCTORS(DictionaryValue); |
279 | 279 |
280 // Associates the value |in_value| with the |key|. This method should be | 280 // Associates the value |in_value| with the |key|. This method should be |
281 // used instead of "dictionary_[key] = foo" so that any previous value can | 281 // used instead of "dictionary_[key] = foo" so that any previous value can |
282 // be properly deleted. | 282 // be properly deleted. |
283 void SetInCurrentNode(const std::wstring& key, Value* in_value); | 283 void SetInCurrentNode(const string16& key, Value* in_value); |
284 | 284 |
285 ValueMap dictionary_; | 285 ValueMap dictionary_; |
286 }; | 286 }; |
287 | 287 |
288 // This type of Value represents a list of other Value values. | 288 // This type of Value represents a list of other Value values. |
289 class ListValue : public Value { | 289 class ListValue : public Value { |
290 public: | 290 public: |
291 ListValue() : Value(TYPE_LIST) {} | 291 ListValue() : Value(TYPE_LIST) {} |
292 ~ListValue(); | 292 ~ListValue(); |
293 | 293 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 virtual bool Serialize(const Value& root) = 0; | 363 virtual bool Serialize(const Value& root) = 0; |
364 | 364 |
365 // This method deserializes the subclass-specific format into a Value object. | 365 // This method deserializes the subclass-specific format into a Value object. |
366 // If the return value is non-NULL, the caller takes ownership of returned | 366 // If the return value is non-NULL, the caller takes ownership of returned |
367 // Value. If the return value is NULL, and if error_message is non-NULL, | 367 // Value. If the return value is NULL, and if error_message is non-NULL, |
368 // error_message should be filled with a message describing the error. | 368 // error_message should be filled with a message describing the error. |
369 virtual Value* Deserialize(std::string* error_message) = 0; | 369 virtual Value* Deserialize(std::string* error_message) = 0; |
370 }; | 370 }; |
371 | 371 |
372 #endif // BASE_VALUES_H_ | 372 #endif // BASE_VALUES_H_ |
OLD | NEW |