blob: 223e0f88134aeae9a32c96d095fc7ffbb568eed8 [file] [log] [blame]
[email protected]13502562012-05-09 21:54:271// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]9e4cda7332010-07-31 04:56:145// This file specifies a recursive data storage class called Value intended for
6// storing setting and other persistable data. It includes the ability to
7// specify (recursive) lists and dictionaries, so it's fairly expressive.
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
10// easily do things like:
initial.commitd7cae122008-07-26 21:49:3811//
[email protected]9e4cda7332010-07-31 04:56:1412// root->SetString("global.pages.homepage", "http://goateleporter.com");
13// std::string homepage = "http://google.com"; // default/fallback value
14// root->GetString("global.pages.homepage", &homepage);
initial.commitd7cae122008-07-26 21:49:3815//
[email protected]9e4cda7332010-07-31 04:56:1416// where "global" and "pages" are also DictionaryValues, and "homepage" is a
17// string setting. If some elements of the path didn't exist yet, the
18// SetString() method would create the missing elements and attach them to root
19// before attaching the homepage value.
initial.commitd7cae122008-07-26 21:49:3820
[email protected]101d5422008-09-26 20:22:4221#ifndef BASE_VALUES_H_
22#define BASE_VALUES_H_
initial.commitd7cae122008-07-26 21:49:3823
24#include <iterator>
25#include <map>
[email protected]8e50b602009-03-03 22:59:4326#include <string>
initial.commitd7cae122008-07-26 21:49:3827#include <vector>
28
[email protected]0bea7252011-08-05 15:34:0029#include "base/base_export.h"
initial.commitd7cae122008-07-26 21:49:3830#include "base/basictypes.h"
[email protected]e8789192011-08-11 20:56:3231#include "base/compiler_specific.h"
[email protected]9101ef1e2010-01-15 20:09:0332#include "base/string16.h"
initial.commitd7cae122008-07-26 21:49:3833
[email protected]f3a1c642011-07-12 19:15:0334// This file declares "using base::Value", etc. at the bottom, so that
35// current code can use these classes without the base namespace. In
36// new code, please always use base::Value, etc. or add your own
37// "using" declaration.
38// http://crbug.com/88666
39namespace base {
40
initial.commitd7cae122008-07-26 21:49:3841class BinaryValue;
42class DictionaryValue;
[email protected]68d9d352011-02-21 16:35:0443class FundamentalValue;
initial.commitd7cae122008-07-26 21:49:3844class ListValue;
[email protected]68d9d352011-02-21 16:35:0445class StringValue;
46class Value;
initial.commitd7cae122008-07-26 21:49:3847
48typedef std::vector<Value*> ValueVector;
[email protected]e7b418b2010-07-30 19:47:4749typedef std::map<std::string, Value*> ValueMap;
initial.commitd7cae122008-07-26 21:49:3850
[email protected]b59ea312011-08-05 18:20:0551// The Value class is the base class for Values. A Value can be instantiated
52// via the Create*Value() factory methods, or by directly creating instances of
53// the subclasses.
[email protected]0bea7252011-08-05 15:34:0054class BASE_EXPORT Value {
initial.commitd7cae122008-07-26 21:49:3855 public:
[email protected]bab1c13f2011-08-12 20:59:0256 enum Type {
[email protected]a502bbe72011-01-07 18:06:4557 TYPE_NULL = 0,
58 TYPE_BOOLEAN,
59 TYPE_INTEGER,
[email protected]fb534c92011-02-01 01:02:0760 TYPE_DOUBLE,
[email protected]a502bbe72011-01-07 18:06:4561 TYPE_STRING,
62 TYPE_BINARY,
63 TYPE_DICTIONARY,
64 TYPE_LIST
65 };
66
initial.commitd7cae122008-07-26 21:49:3867 virtual ~Value();
68
69 // Convenience methods for creating Value objects for various
70 // kinds of values without thinking about which class implements them.
71 // These can always be expected to return a valid Value*.
72 static Value* CreateNullValue();
[email protected]16f47e082011-01-18 02:16:5973 static FundamentalValue* CreateBooleanValue(bool in_value);
74 static FundamentalValue* CreateIntegerValue(int in_value);
[email protected]fb534c92011-02-01 01:02:0775 static FundamentalValue* CreateDoubleValue(double in_value);
[email protected]16f47e082011-01-18 02:16:5976 static StringValue* CreateStringValue(const std::string& in_value);
77 static StringValue* CreateStringValue(const string16& in_value);
initial.commitd7cae122008-07-26 21:49:3878
initial.commitd7cae122008-07-26 21:49:3879 // Returns the type of the value stored by the current Value object.
80 // Each type will be implemented by only one subclass of Value, so it's
[email protected]bab1c13f2011-08-12 20:59:0281 // safe to use the Type to determine whether you can cast from
initial.commitd7cae122008-07-26 21:49:3882 // Value* to (Implementing Class)*. Also, a Value object never changes
83 // its type after construction.
[email protected]bab1c13f2011-08-12 20:59:0284 Type GetType() const { return type_; }
initial.commitd7cae122008-07-26 21:49:3885
86 // Returns true if the current object represents a given type.
[email protected]bab1c13f2011-08-12 20:59:0287 bool IsType(Type type) const { return type == type_; }
initial.commitd7cae122008-07-26 21:49:3888
89 // These methods allow the convenient retrieval of settings.
90 // If the current setting object can be converted into the given type,
[email protected]35213ce92010-04-08 19:06:1591 // the value is returned through the |out_value| parameter and true is
92 // returned; otherwise, false is returned and |out_value| is unchanged.
initial.commitd7cae122008-07-26 21:49:3893 virtual bool GetAsBoolean(bool* out_value) const;
94 virtual bool GetAsInteger(int* out_value) const;
[email protected]fb534c92011-02-01 01:02:0795 virtual bool GetAsDouble(double* out_value) const;
[email protected]4cd5f6a2008-12-11 01:23:1796 virtual bool GetAsString(std::string* out_value) const;
[email protected]e2e593d2010-08-03 15:42:5897 virtual bool GetAsString(string16* out_value) const;
[email protected]81f9fe0b2010-12-07 00:35:2998 virtual bool GetAsList(ListValue** out_value);
[email protected]35552dc52011-07-12 09:04:3899 virtual bool GetAsList(const ListValue** out_value) const;
[email protected]5cf906f82011-11-26 01:11:44100 virtual bool GetAsDictionary(DictionaryValue** out_value);
101 virtual bool GetAsDictionary(const DictionaryValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38102
103 // This creates a deep copy of the entire Value tree, and returns a pointer
104 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:59105 //
106 // Subclasses return their own type directly in their overrides;
107 // this works because C++ supports covariant return types.
initial.commitd7cae122008-07-26 21:49:38108 virtual Value* DeepCopy() const;
109
110 // Compares if two Value objects have equal contents.
111 virtual bool Equals(const Value* other) const;
112
[email protected]73c47932010-12-06 18:13:43113 // Compares if two Value objects have equal contents. Can handle NULLs.
114 // NULLs are considered equal but different from Value::CreateNullValue().
115 static bool Equals(const Value* a, const Value* b);
116
initial.commitd7cae122008-07-26 21:49:38117 protected:
118 // This isn't safe for end-users (they should use the Create*Value()
119 // static methods above), but it's useful for subclasses.
[email protected]bab1c13f2011-08-12 20:59:02120 explicit Value(Type type);
initial.commitd7cae122008-07-26 21:49:38121
122 private:
[email protected]9b5f56b42011-08-24 21:17:59123 Value();
[email protected]7867cd02009-09-14 16:56:12124
[email protected]9b5f56b42011-08-24 21:17:59125 Type type_;
[email protected]2f0a6472011-08-23 03:40:57126
[email protected]7867cd02009-09-14 16:56:12127 DISALLOW_COPY_AND_ASSIGN(Value);
initial.commitd7cae122008-07-26 21:49:38128};
129
130// FundamentalValue represents the simple fundamental types of values.
[email protected]0bea7252011-08-05 15:34:00131class BASE_EXPORT FundamentalValue : public Value {
initial.commitd7cae122008-07-26 21:49:38132 public:
[email protected]3a3d47472010-07-15 21:03:54133 explicit FundamentalValue(bool in_value);
134 explicit FundamentalValue(int in_value);
135 explicit FundamentalValue(double in_value);
[email protected]78994ab02010-12-08 18:06:44136 virtual ~FundamentalValue();
initial.commitd7cae122008-07-26 21:49:38137
[email protected]e8789192011-08-11 20:56:32138 // Overridden from Value:
139 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE;
140 virtual bool GetAsInteger(int* out_value) const OVERRIDE;
141 virtual bool GetAsDouble(double* out_value) const OVERRIDE;
142 virtual FundamentalValue* DeepCopy() const OVERRIDE;
143 virtual bool Equals(const Value* other) const OVERRIDE;
initial.commitd7cae122008-07-26 21:49:38144
145 private:
initial.commitd7cae122008-07-26 21:49:38146 union {
147 bool boolean_value_;
148 int integer_value_;
[email protected]fb534c92011-02-01 01:02:07149 double double_value_;
initial.commitd7cae122008-07-26 21:49:38150 };
[email protected]7867cd02009-09-14 16:56:12151
152 DISALLOW_COPY_AND_ASSIGN(FundamentalValue);
initial.commitd7cae122008-07-26 21:49:38153};
154
[email protected]0bea7252011-08-05 15:34:00155class BASE_EXPORT StringValue : public Value {
initial.commitd7cae122008-07-26 21:49:38156 public:
[email protected]4cd5f6a2008-12-11 01:23:17157 // Initializes a StringValue with a UTF-8 narrow character string.
[email protected]7867cd02009-09-14 16:56:12158 explicit StringValue(const std::string& in_value);
[email protected]4cd5f6a2008-12-11 01:23:17159
[email protected]9101ef1e2010-01-15 20:09:03160 // Initializes a StringValue with a string16.
161 explicit StringValue(const string16& in_value);
[email protected]e2e593d2010-08-03 15:42:58162
[email protected]78994ab02010-12-08 18:06:44163 virtual ~StringValue();
initial.commitd7cae122008-07-26 21:49:38164
[email protected]e8789192011-08-11 20:56:32165 // Overridden from Value:
166 virtual bool GetAsString(std::string* out_value) const OVERRIDE;
167 virtual bool GetAsString(string16* out_value) const OVERRIDE;
168 virtual StringValue* DeepCopy() const OVERRIDE;
169 virtual bool Equals(const Value* other) const OVERRIDE;
initial.commitd7cae122008-07-26 21:49:38170
171 private:
[email protected]4cd5f6a2008-12-11 01:23:17172 std::string value_;
[email protected]7867cd02009-09-14 16:56:12173
174 DISALLOW_COPY_AND_ASSIGN(StringValue);
initial.commitd7cae122008-07-26 21:49:38175};
176
[email protected]0bea7252011-08-05 15:34:00177class BASE_EXPORT BinaryValue: public Value {
[email protected]7867cd02009-09-14 16:56:12178 public:
[email protected]00590b32012-05-19 19:31:16179 virtual ~BinaryValue();
initial.commitd7cae122008-07-26 21:49:38180
[email protected]0d0ed0c2012-05-20 02:34:57181 // Creates a Value to represent a binary buffer. The new object takes
182 // ownership of the pointer passed in, if successful.
183 // Returns NULL if buffer is NULL.
184 static BinaryValue* Create(char* buffer, size_t size);
185
initial.commitd7cae122008-07-26 21:49:38186 // For situations where you want to keep ownership of your buffer, this
187 // factory method creates a new BinaryValue by copying the contents of the
188 // buffer that's passed in.
[email protected]0d0ed0c2012-05-20 02:34:57189 // Returns NULL if buffer is NULL.
[email protected]e4dad9fb2009-10-06 18:15:58190 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
initial.commitd7cae122008-07-26 21:49:38191
initial.commitd7cae122008-07-26 21:49:38192 size_t GetSize() const { return size_; }
[email protected]0d0ed0c2012-05-20 02:34:57193 char* GetBuffer() { return buffer_; }
194 const char* GetBuffer() const { return buffer_; }
initial.commitd7cae122008-07-26 21:49:38195
[email protected]a502bbe72011-01-07 18:06:45196 // Overridden from Value:
[email protected]e8789192011-08-11 20:56:32197 virtual BinaryValue* DeepCopy() const OVERRIDE;
198 virtual bool Equals(const Value* other) const OVERRIDE;
[email protected]a502bbe72011-01-07 18:06:45199
[email protected]7867cd02009-09-14 16:56:12200 private:
[email protected]0d0ed0c2012-05-20 02:34:57201 // Constructor is private so that only objects with valid buffer pointers
202 // and size values can be created.
203 BinaryValue(char* buffer, size_t size);
204
205 char* buffer_;
initial.commitd7cae122008-07-26 21:49:38206 size_t size_;
[email protected]7867cd02009-09-14 16:56:12207
208 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
initial.commitd7cae122008-07-26 21:49:38209};
210
[email protected]9e4cda7332010-07-31 04:56:14211// DictionaryValue provides a key-value dictionary with (optional) "path"
212// parsing for recursive access; see the comment at the top of the file. Keys
213// are |std::string|s and should be UTF-8 encoded.
[email protected]0bea7252011-08-05 15:34:00214class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:38215 public:
[email protected]3a3d47472010-07-15 21:03:54216 DictionaryValue();
[email protected]78994ab02010-12-08 18:06:44217 virtual ~DictionaryValue();
initial.commitd7cae122008-07-26 21:49:38218
[email protected]5cf906f82011-11-26 01:11:44219 // Overridden from Value:
220 virtual bool GetAsDictionary(DictionaryValue** out_value) OVERRIDE;
221 virtual bool GetAsDictionary(
222 const DictionaryValue** out_value) const OVERRIDE;
223
initial.commitd7cae122008-07-26 21:49:38224 // Returns true if the current dictionary has a value for the given key.
[email protected]e7b418b2010-07-30 19:47:47225 bool HasKey(const std::string& key) const;
initial.commitd7cae122008-07-26 21:49:38226
[email protected]fb804132c2009-04-15 00:17:53227 // Returns the number of Values in this dictionary.
[email protected]4dad9ad82009-11-25 20:47:52228 size_t size() const { return dictionary_.size(); }
229
230 // Returns whether the dictionary is empty.
231 bool empty() const { return dictionary_.empty(); }
[email protected]fb804132c2009-04-15 00:17:53232
initial.commitd7cae122008-07-26 21:49:38233 // Clears any current contents of this dictionary.
[email protected]af5ed4a2008-08-04 13:56:25234 void Clear();
initial.commitd7cae122008-07-26 21:49:38235
236 // Sets the Value associated with the given path starting from this object.
237 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
238 // into the next DictionaryValue down. Obviously, "." can't be used
239 // within a key, but there are no other restrictions on keys.
240 // If the key at any step of the way doesn't exist, or exists but isn't
241 // a DictionaryValue, a new DictionaryValue will be created and attached
242 // to the path in that location.
[email protected]fb804132c2009-04-15 00:17:53243 // Note that the dictionary takes ownership of the value referenced by
[email protected]4dad9ad82009-11-25 20:47:52244 // |in_value|, and therefore |in_value| must be non-NULL.
[email protected]e7b418b2010-07-30 19:47:47245 void Set(const std::string& path, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38246
247 // Convenience forms of Set(). These methods will replace any existing
248 // value at that path, even if it has a different type.
[email protected]e7b418b2010-07-30 19:47:47249 void SetBoolean(const std::string& path, bool in_value);
250 void SetInteger(const std::string& path, int in_value);
[email protected]fb534c92011-02-01 01:02:07251 void SetDouble(const std::string& path, double in_value);
[email protected]e7b418b2010-07-30 19:47:47252 void SetString(const std::string& path, const std::string& in_value);
[email protected]ff4c1d82010-08-04 16:58:12253 void SetString(const std::string& path, const string16& in_value);
[email protected]4dad9ad82009-11-25 20:47:52254
255 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
256 // be used as paths.
[email protected]e7b418b2010-07-30 19:47:47257 void SetWithoutPathExpansion(const std::string& key, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38258
259 // Gets the Value associated with the given path starting from this object.
260 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
261 // into the next DictionaryValue down. If the path can be resolved
262 // successfully, the value for the last key in the path will be returned
[email protected]35213ce92010-04-08 19:06:15263 // through the |out_value| parameter, and the function will return true.
264 // Otherwise, it will return false and |out_value| will be untouched.
initial.commitd7cae122008-07-26 21:49:38265 // Note that the dictionary always owns the value that's returned.
[email protected]a61890e2012-07-27 22:27:11266 bool Get(const std::string& path, const Value** out_value) const;
267 bool Get(const std::string& path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38268
269 // These are convenience forms of Get(). The value will be retrieved
270 // and the return value will be true if the path is valid and the value at
271 // the end of the path can be returned in the form specified.
[email protected]e7b418b2010-07-30 19:47:47272 bool GetBoolean(const std::string& path, bool* out_value) const;
273 bool GetInteger(const std::string& path, int* out_value) const;
[email protected]fb534c92011-02-01 01:02:07274 bool GetDouble(const std::string& path, double* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47275 bool GetString(const std::string& path, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33276 bool GetString(const std::string& path, string16* out_value) const;
[email protected]9430e4b2010-02-19 13:32:16277 bool GetStringASCII(const std::string& path, std::string* out_value) const;
[email protected]a61890e2012-07-27 22:27:11278 bool GetBinary(const std::string& path, const BinaryValue** out_value) const;
279 bool GetBinary(const std::string& path, BinaryValue** out_value);
[email protected]e7b418b2010-07-30 19:47:47280 bool GetDictionary(const std::string& path,
[email protected]a61890e2012-07-27 22:27:11281 const DictionaryValue** out_value) const;
282 bool GetDictionary(const std::string& path, DictionaryValue** out_value);
283 bool GetList(const std::string& path, const ListValue** out_value) const;
284 bool GetList(const std::string& path, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38285
[email protected]4dad9ad82009-11-25 20:47:52286 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
287 // be used as paths.
[email protected]e7b418b2010-07-30 19:47:47288 bool GetWithoutPathExpansion(const std::string& key,
[email protected]a61890e2012-07-27 22:27:11289 const Value** out_value) const;
290 bool GetWithoutPathExpansion(const std::string& key, Value** out_value);
[email protected]e7b418b2010-07-30 19:47:47291 bool GetIntegerWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52292 int* out_value) const;
[email protected]fb534c92011-02-01 01:02:07293 bool GetDoubleWithoutPathExpansion(const std::string& key,
[email protected]a61890e2012-07-27 22:27:11294 double* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47295 bool GetStringWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52296 std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33297 bool GetStringWithoutPathExpansion(const std::string& key,
298 string16* out_value) const;
[email protected]a61890e2012-07-27 22:27:11299 bool GetDictionaryWithoutPathExpansion(
300 const std::string& key,
301 const DictionaryValue** out_value) const;
[email protected]e7b418b2010-07-30 19:47:47302 bool GetDictionaryWithoutPathExpansion(const std::string& key,
[email protected]a61890e2012-07-27 22:27:11303 DictionaryValue** out_value);
[email protected]e7b418b2010-07-30 19:47:47304 bool GetListWithoutPathExpansion(const std::string& key,
[email protected]a61890e2012-07-27 22:27:11305 const ListValue** out_value) const;
306 bool GetListWithoutPathExpansion(const std::string& key,
307 ListValue** out_value);
[email protected]4dad9ad82009-11-25 20:47:52308
initial.commitd7cae122008-07-26 21:49:38309 // Removes the Value with the specified path from this dictionary (or one
310 // of its child dictionaries, if the path is more than just a local key).
311 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
312 // passed out via out_value. If |out_value| is NULL, the removed value will
313 // be deleted. This method returns true if |path| is a valid path; otherwise
314 // it will return false and the DictionaryValue object will be unchanged.
[email protected]6e680cf2012-05-16 15:23:30315 virtual bool Remove(const std::string& path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38316
[email protected]4dad9ad82009-11-25 20:47:52317 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
318 // to be used as paths.
[email protected]6e680cf2012-05-16 15:23:30319 virtual bool RemoveWithoutPathExpansion(const std::string& key,
320 Value** out_value);
[email protected]4dad9ad82009-11-25 20:47:52321
[email protected]ec330b52009-12-02 00:20:32322 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
323 // the copy. This never returns NULL, even if |this| itself is empty.
324 DictionaryValue* DeepCopyWithoutEmptyChildren();
325
[email protected]13502562012-05-09 21:54:27326 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
327 // sub-dictionaries will be merged as well. In case of key collisions, the
328 // passed in dictionary takes precedence and data already present will be
329 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
330 // be freed any time after this call.
[email protected]c378cca2010-05-14 13:17:40331 void MergeDictionary(const DictionaryValue* dictionary);
332
[email protected]ec5263a2011-05-10 09:23:39333 // Swaps contents with the |other| dictionary.
[email protected]6e680cf2012-05-16 15:23:30334 virtual void Swap(DictionaryValue* other);
[email protected]ec5263a2011-05-10 09:23:39335
initial.commitd7cae122008-07-26 21:49:38336 // This class provides an iterator for the keys in the dictionary.
337 // It can't be used to modify the dictionary.
[email protected]4dad9ad82009-11-25 20:47:52338 //
339 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT
340 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any
341 // keys have '.'s in them.
[email protected]2cdf8cda2011-04-29 18:08:21342 class key_iterator
[email protected]e7b418b2010-07-30 19:47:47343 : private std::iterator<std::input_iterator_tag, const std::string> {
[email protected]03c5e862009-02-17 22:50:14344 public:
[email protected]7867cd02009-09-14 16:56:12345 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; }
[email protected]2fdc86a2010-01-26 23:08:02346 key_iterator operator++() {
347 ++itr_;
348 return *this;
349 }
[email protected]e7b418b2010-07-30 19:47:47350 const std::string& operator*() { return itr_->first; }
initial.commitd7cae122008-07-26 21:49:38351 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
352 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
353
[email protected]03c5e862009-02-17 22:50:14354 private:
initial.commitd7cae122008-07-26 21:49:38355 ValueMap::const_iterator itr_;
356 };
357
358 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
359 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
360
[email protected]32c0e002011-11-08 21:26:41361 // This class provides an iterator over both keys and values in the
362 // dictionary. It can't be used to modify the dictionary.
363 class Iterator {
364 public:
365 explicit Iterator(const DictionaryValue& target)
366 : target_(target), it_(target.dictionary_.begin()) {}
367
368 bool HasNext() const { return it_ != target_.dictionary_.end(); }
369 void Advance() { ++it_; }
370
371 const std::string& key() const { return it_->first; }
372 const Value& value() const { return *it_->second; }
373
374 private:
375 const DictionaryValue& target_;
376 ValueMap::const_iterator it_;
377 };
378
[email protected]a502bbe72011-01-07 18:06:45379 // Overridden from Value:
[email protected]e8789192011-08-11 20:56:32380 virtual DictionaryValue* DeepCopy() const OVERRIDE;
381 virtual bool Equals(const Value* other) const OVERRIDE;
[email protected]a502bbe72011-01-07 18:06:45382
initial.commitd7cae122008-07-26 21:49:38383 private:
initial.commitd7cae122008-07-26 21:49:38384 ValueMap dictionary_;
[email protected]7867cd02009-09-14 16:56:12385
386 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
initial.commitd7cae122008-07-26 21:49:38387};
388
389// This type of Value represents a list of other Value values.
[email protected]0bea7252011-08-05 15:34:00390class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:38391 public:
[email protected]a502bbe72011-01-07 18:06:45392 typedef ValueVector::iterator iterator;
393 typedef ValueVector::const_iterator const_iterator;
394
[email protected]3a3d47472010-07-15 21:03:54395 ListValue();
[email protected]3690ebe02011-05-25 09:08:19396 virtual ~ListValue();
initial.commitd7cae122008-07-26 21:49:38397
initial.commitd7cae122008-07-26 21:49:38398 // Clears the contents of this ListValue
399 void Clear();
400
401 // Returns the number of Values in this list.
402 size_t GetSize() const { return list_.size(); }
403
[email protected]ec330b52009-12-02 00:20:32404 // Returns whether the list is empty.
405 bool empty() const { return list_.empty(); }
406
initial.commitd7cae122008-07-26 21:49:38407 // Sets the list item at the given index to be the Value specified by
408 // the value given. If the index beyond the current end of the list, null
409 // Values will be used to pad out the list.
410 // Returns true if successful, or false if the index was negative or
411 // the value is a null pointer.
412 bool Set(size_t index, Value* in_value);
413
[email protected]35213ce92010-04-08 19:06:15414 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commitd7cae122008-07-26 21:49:38415 // only if the index falls within the current list range.
[email protected]35213ce92010-04-08 19:06:15416 // Note that the list always owns the Value passed out via |out_value|.
initial.commitd7cae122008-07-26 21:49:38417 bool Get(size_t index, Value** out_value) const;
418
[email protected]35213ce92010-04-08 19:06:15419 // Convenience forms of Get(). Modifies |out_value| (and returns true)
420 // only if the index is valid and the Value at that index can be returned
421 // in the specified form.
[email protected]f82fb4952009-01-20 21:05:32422 bool GetBoolean(size_t index, bool* out_value) const;
423 bool GetInteger(size_t index, int* out_value) const;
[email protected]fb534c92011-02-01 01:02:07424 bool GetDouble(size_t index, double* out_value) const;
[email protected]f82fb4952009-01-20 21:05:32425 bool GetString(size_t index, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33426 bool GetString(size_t index, string16* out_value) const;
[email protected]f82fb4952009-01-20 21:05:32427 bool GetBinary(size_t index, BinaryValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38428 bool GetDictionary(size_t index, DictionaryValue** out_value) const;
[email protected]f82fb4952009-01-20 21:05:32429 bool GetList(size_t index, ListValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38430
431 // Removes the Value with the specified index from this list.
432 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
[email protected]b930d132009-01-05 18:37:51433 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commitd7cae122008-07-26 21:49:38434 // be deleted. This method returns true if |index| is valid; otherwise
435 // it will return false and the ListValue object will be unchanged.
[email protected]6e680cf2012-05-16 15:23:30436 virtual bool Remove(size_t index, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38437
[email protected]6832cbe2009-11-30 19:59:11438 // Removes the first instance of |value| found in the list, if any, and
[email protected]4fc3c5642011-08-13 17:34:31439 // deletes it. |index| is the location where |value| was found. Returns false
440 // if not found.
441 bool Remove(const Value& value, size_t* index);
[email protected]e7f5c6f2009-05-09 00:33:04442
[email protected]3cbe0812012-07-03 02:51:43443 // Removes the element at |iter|. If |out_value| is NULL, the value will be
444 // deleted, otherwise ownership of the value is passed back to the caller.
445 void Erase(iterator iter, Value** out_value);
446
initial.commitd7cae122008-07-26 21:49:38447 // Appends a Value to the end of the list.
448 void Append(Value* in_value);
449
[email protected]e36eaac2011-03-18 13:56:38450 // Appends a Value if it's not already present. Takes ownership of the
451 // |in_value|. Returns true if successful, or false if the value was already
452 // present. If the value was already present the |in_value| is deleted.
[email protected]840642202010-04-12 21:48:10453 bool AppendIfNotPresent(Value* in_value);
454
[email protected]86c008e82009-08-28 20:26:05455 // Insert a Value at index.
456 // Returns true if successful, or false if the index was out of range.
457 bool Insert(size_t index, Value* in_value);
458
[email protected]5fb35372011-09-19 15:23:10459 // Searches for the first instance of |value| in the list using the Equals
460 // method of the Value type.
461 // Returns a const_iterator to the found item or to end() if none exists.
462 const_iterator Find(const Value& value) const;
463
[email protected]8b8e7c92010-08-19 18:05:56464 // Swaps contents with the |other| list.
[email protected]6e680cf2012-05-16 15:23:30465 virtual void Swap(ListValue* other);
[email protected]8b8e7c92010-08-19 18:05:56466
[email protected]e8789192011-08-11 20:56:32467 // Iteration.
468 iterator begin() { return list_.begin(); }
469 iterator end() { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38470
[email protected]e8789192011-08-11 20:56:32471 const_iterator begin() const { return list_.begin(); }
472 const_iterator end() const { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38473
[email protected]a502bbe72011-01-07 18:06:45474 // Overridden from Value:
[email protected]e8789192011-08-11 20:56:32475 virtual bool GetAsList(ListValue** out_value) OVERRIDE;
476 virtual bool GetAsList(const ListValue** out_value) const OVERRIDE;
477 virtual ListValue* DeepCopy() const OVERRIDE;
478 virtual bool Equals(const Value* other) const OVERRIDE;
[email protected]a502bbe72011-01-07 18:06:45479
initial.commitd7cae122008-07-26 21:49:38480 private:
initial.commitd7cae122008-07-26 21:49:38481 ValueVector list_;
[email protected]7867cd02009-09-14 16:56:12482
483 DISALLOW_COPY_AND_ASSIGN(ListValue);
initial.commitd7cae122008-07-26 21:49:38484};
485
486// This interface is implemented by classes that know how to serialize and
487// deserialize Value objects.
[email protected]0bea7252011-08-05 15:34:00488class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:38489 public:
[email protected]3a3d47472010-07-15 21:03:54490 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59491
initial.commitd7cae122008-07-26 21:49:38492 virtual bool Serialize(const Value& root) = 0;
493
494 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08495 // If the return value is non-NULL, the caller takes ownership of returned
[email protected]ba399672010-04-06 15:42:39496 // Value. If the return value is NULL, and if error_code is non-NULL,
497 // error_code will be set with the underlying error.
498 // If |error_message| is non-null, it will be filled in with a formatted
499 // error message including the location of the error if appropriate.
500 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
initial.commitd7cae122008-07-26 21:49:38501};
502
[email protected]f3a1c642011-07-12 19:15:03503} // namespace base
504
505// http://crbug.com/88666
[email protected]f3a1c642011-07-12 19:15:03506using base::DictionaryValue;
[email protected]f3a1c642011-07-12 19:15:03507using base::ListValue;
508using base::StringValue;
509using base::Value;
[email protected]f3a1c642011-07-12 19:15:03510
[email protected]101d5422008-09-26 20:22:42511#endif // BASE_VALUES_H_