Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: base/values.h

Issue 441008: Many changes to DictionaryValues:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 ~DictionaryValue(); 200 ~DictionaryValue();
201 201
202 // Subclassed methods 202 // Subclassed methods
203 Value* DeepCopy() const; 203 Value* DeepCopy() const;
204 virtual bool Equals(const Value* other) const; 204 virtual bool Equals(const Value* other) const;
205 205
206 // Returns true if the current dictionary has a value for the given key. 206 // Returns true if the current dictionary has a value for the given key.
207 bool HasKey(const std::wstring& key) const; 207 bool HasKey(const std::wstring& key) const;
208 208
209 // Returns the number of Values in this dictionary. 209 // Returns the number of Values in this dictionary.
210 size_t GetSize() const { return dictionary_.size(); } 210 size_t size() const { return dictionary_.size(); }
211
212 // Returns whether the dictionary is empty.
213 bool empty() const { return dictionary_.empty(); }
211 214
212 // Clears any current contents of this dictionary. 215 // Clears any current contents of this dictionary.
213 void Clear(); 216 void Clear();
214 217
215 // Sets the Value associated with the given path starting from this object. 218 // Sets the Value associated with the given path starting from this object.
216 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes 219 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
217 // into the next DictionaryValue down. Obviously, "." can't be used 220 // into the next DictionaryValue down. Obviously, "." can't be used
218 // within a key, but there are no other restrictions on keys. 221 // within a key, but there are no other restrictions on keys.
219 // If the key at any step of the way doesn't exist, or exists but isn't 222 // If the key at any step of the way doesn't exist, or exists but isn't
220 // a DictionaryValue, a new DictionaryValue will be created and attached 223 // a DictionaryValue, a new DictionaryValue will be created and attached
221 // to the path in that location. 224 // to the path in that location.
222 // Note that the dictionary takes ownership of the value referenced by 225 // Note that the dictionary takes ownership of the value referenced by
223 // |in_value|. 226 // |in_value|, and therefore |in_value| must be non-NULL.
224 bool Set(const std::wstring& path, Value* in_value); 227 void Set(const std::wstring& path, Value* in_value);
225 228
226 // Convenience forms of Set(). These methods will replace any existing 229 // Convenience forms of Set(). These methods will replace any existing
227 // value at that path, even if it has a different type. 230 // value at that path, even if it has a different type.
228 bool SetBoolean(const std::wstring& path, bool in_value); 231 void SetBoolean(const std::wstring& path, bool in_value);
229 bool SetInteger(const std::wstring& path, int in_value); 232 void SetInteger(const std::wstring& path, int in_value);
230 bool SetReal(const std::wstring& path, double in_value); 233 void SetReal(const std::wstring& path, double in_value);
231 bool SetString(const std::wstring& path, const std::string& in_value); 234 void SetString(const std::wstring& path, const std::string& in_value);
232 bool SetString(const std::wstring& path, const std::wstring& in_value); 235 void SetString(const std::wstring& path, const std::wstring& in_value);
236
237 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
238 // be used as paths.
239 void SetWithoutPathExpansion(const std::wstring& key, Value* in_value);
233 240
234 // Gets the Value associated with the given path starting from this object. 241 // Gets the Value associated with the given path starting from this object.
235 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes 242 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
236 // into the next DictionaryValue down. If the path can be resolved 243 // into the next DictionaryValue down. If the path can be resolved
237 // successfully, the value for the last key in the path will be returned 244 // successfully, the value for the last key in the path will be returned
238 // through the "value" parameter, and the function will return true. 245 // through the "value" parameter, and the function will return true.
239 // Otherwise, it will return false and "value" will be untouched. 246 // Otherwise, it will return false and "value" will be untouched.
240 // Note that the dictionary always owns the value that's returned. 247 // Note that the dictionary always owns the value that's returned.
241 bool Get(const std::wstring& path, Value** out_value) const; 248 bool Get(const std::wstring& path, Value** out_value) const;
242 249
243 // These are convenience forms of Get(). The value will be retrieved 250 // These are convenience forms of Get(). The value will be retrieved
244 // and the return value will be true if the path is valid and the value at 251 // and the return value will be true if the path is valid and the value at
245 // the end of the path can be returned in the form specified. 252 // the end of the path can be returned in the form specified.
246 bool GetBoolean(const std::wstring& path, bool* out_value) const; 253 bool GetBoolean(const std::wstring& path, bool* out_value) const;
247 bool GetInteger(const std::wstring& path, int* out_value) const; 254 bool GetInteger(const std::wstring& path, int* out_value) const;
248 bool GetReal(const std::wstring& path, double* out_value) const; 255 bool GetReal(const std::wstring& path, double* out_value) const;
249 bool GetString(const std::wstring& path, std::string* out_value) const; 256 bool GetString(const std::wstring& path, std::string* out_value) const;
250 bool GetString(const std::wstring& path, std::wstring* out_value) const; 257 bool GetString(const std::wstring& path, std::wstring* out_value) const;
251 bool GetBinary(const std::wstring& path, BinaryValue** out_value) const; 258 bool GetBinary(const std::wstring& path, BinaryValue** out_value) const;
252 bool GetDictionary(const std::wstring& path, 259 bool GetDictionary(const std::wstring& path,
253 DictionaryValue** out_value) const; 260 DictionaryValue** out_value) const;
254 bool GetList(const std::wstring& path, ListValue** out_value) const; 261 bool GetList(const std::wstring& path, ListValue** out_value) const;
255 262
263 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
264 // be used as paths.
265 bool GetWithoutPathExpansion(const std::wstring& key,
266 Value** out_value) const;
267 bool GetIntegerWithoutPathExpansion(const std::wstring& path,
268 int* out_value) const;
269 bool GetStringWithoutPathExpansion(const std::wstring& path,
270 std::string* out_value) const;
271 bool GetStringWithoutPathExpansion(const std::wstring& path,
272 std::wstring* out_value) const;
273 bool GetDictionaryWithoutPathExpansion(const std::wstring& path,
274 DictionaryValue** out_value) const;
275 bool GetListWithoutPathExpansion(const std::wstring& path,
276 ListValue** out_value) const;
277
256 // Removes the Value with the specified path from this dictionary (or one 278 // Removes the Value with the specified path from this dictionary (or one
257 // of its child dictionaries, if the path is more than just a local key). 279 // of its child dictionaries, if the path is more than just a local key).
258 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be 280 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
259 // passed out via out_value. If |out_value| is NULL, the removed value will 281 // passed out via out_value. If |out_value| is NULL, the removed value will
260 // be deleted. This method returns true if |path| is a valid path; otherwise 282 // be deleted. This method returns true if |path| is a valid path; otherwise
261 // it will return false and the DictionaryValue object will be unchanged. 283 // it will return false and the DictionaryValue object will be unchanged.
262 bool Remove(const std::wstring& path, Value** out_value); 284 bool Remove(const std::wstring& path, Value** out_value);
263 285
286 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
287 // to be used as paths.
288 bool RemoveWithoutPathExpansion(const std::wstring& key, Value** out_value);
289
264 // This class provides an iterator for the keys in the dictionary. 290 // This class provides an iterator for the keys in the dictionary.
265 // It can't be used to modify the dictionary. 291 // It can't be used to modify the dictionary.
292 //
293 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT
294 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any
295 // keys have '.'s in them.
266 class key_iterator 296 class key_iterator
267 : private std::iterator<std::input_iterator_tag, const std::wstring> { 297 : private std::iterator<std::input_iterator_tag, const std::wstring> {
268 public: 298 public:
269 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } 299 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; }
270 key_iterator operator++() { ++itr_; return *this; } 300 key_iterator operator++() { ++itr_; return *this; }
271 const std::wstring& operator*() { return itr_->first; } 301 const std::wstring& operator*() { return itr_->first; }
272 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } 302 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
273 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } 303 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
274 304
275 private: 305 private:
276 ValueMap::const_iterator itr_; 306 ValueMap::const_iterator itr_;
277 }; 307 };
278 308
279 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } 309 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
280 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } 310 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
281 311
282 private: 312 private:
283 // Associates the value |in_value| with the |key|. This method should be
284 // used instead of "dictionary_[key] = foo" so that any previous value can
285 // be properly deleted.
286 void SetInCurrentNode(const std::wstring& key, Value* in_value);
287
288 ValueMap dictionary_; 313 ValueMap dictionary_;
289 314
290 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); 315 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
291 }; 316 };
292 317
293 // This type of Value represents a list of other Value values. 318 // This type of Value represents a list of other Value values.
294 class ListValue : public Value { 319 class ListValue : public Value {
295 public: 320 public:
296 ListValue() : Value(TYPE_LIST) {} 321 ListValue() : Value(TYPE_LIST) {}
297 ~ListValue(); 322 ~ListValue();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 virtual bool Serialize(const Value& root) = 0; 397 virtual bool Serialize(const Value& root) = 0;
373 398
374 // This method deserializes the subclass-specific format into a Value object. 399 // This method deserializes the subclass-specific format into a Value object.
375 // If the return value is non-NULL, the caller takes ownership of returned 400 // If the return value is non-NULL, the caller takes ownership of returned
376 // Value. If the return value is NULL, and if error_message is non-NULL, 401 // Value. If the return value is NULL, and if error_message is non-NULL,
377 // error_message should be filled with a message describing the error. 402 // error_message should be filled with a message describing the error.
378 virtual Value* Deserialize(std::string* error_message) = 0; 403 virtual Value* Deserialize(std::string* error_message) = 0;
379 }; 404 };
380 405
381 #endif // BASE_VALUES_H_ 406 #endif // BASE_VALUES_H_
OLDNEW
« no previous file with comments | « base/json/json_writer_unittest.cc ('k') | base/values.cc » ('j') | base/values.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698