blob: 02a3566ec9cbf1949f122c9a8799efb3f83160e2 [file] [log] [blame]
jhawkins@chromium.orgcbb22392012-05-10 06:54:27 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +09005// 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.commit3f4a7322008-07-27 06:49:38 +090011//
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +090012// 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.commit3f4a7322008-07-27 06:49:38 +090015//
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +090016// 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.commit3f4a7322008-07-27 06:49:38 +090020
darin@chromium.org01f10702008-09-27 05:22:42 +090021#ifndef BASE_VALUES_H_
22#define BASE_VALUES_H_
initial.commit3f4a7322008-07-27 06:49:38 +090023
24#include <iterator>
25#include <map>
nsylvain@chromium.org12426672009-03-04 07:59:43 +090026#include <string>
initial.commit3f4a7322008-07-27 06:49:38 +090027#include <vector>
28
darin@chromium.orge585bed2011-08-06 00:34:00 +090029#include "base/base_export.h"
initial.commit3f4a7322008-07-27 06:49:38 +090030#include "base/basictypes.h"
tfarina@chromium.orgcc177402011-08-12 05:56:32 +090031#include "base/compiler_specific.h"
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +090032#include "base/string16.h"
initial.commit3f4a7322008-07-27 06:49:38 +090033
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +090034// 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.commit3f4a7322008-07-27 06:49:38 +090041class BinaryValue;
42class DictionaryValue;
bauerb@chromium.org6fca9582011-02-22 01:35:04 +090043class FundamentalValue;
initial.commit3f4a7322008-07-27 06:49:38 +090044class ListValue;
bauerb@chromium.org6fca9582011-02-22 01:35:04 +090045class StringValue;
46class Value;
initial.commit3f4a7322008-07-27 06:49:38 +090047
48typedef std::vector<Value*> ValueVector;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +090049typedef std::map<std::string, Value*> ValueMap;
initial.commit3f4a7322008-07-27 06:49:38 +090050
tfarina@chromium.org2e2a9022011-08-06 03:20:05 +090051// 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.
darin@chromium.orge585bed2011-08-06 00:34:00 +090054class BASE_EXPORT Value {
initial.commit3f4a7322008-07-27 06:49:38 +090055 public:
tfarina@chromium.org254780a2011-08-13 05:59:02 +090056 enum Type {
erg@google.comd5fffd42011-01-08 03:06:45 +090057 TYPE_NULL = 0,
58 TYPE_BOOLEAN,
59 TYPE_INTEGER,
arv@chromium.org13413eb2011-02-01 10:02:07 +090060 TYPE_DOUBLE,
erg@google.comd5fffd42011-01-08 03:06:45 +090061 TYPE_STRING,
62 TYPE_BINARY,
63 TYPE_DICTIONARY,
64 TYPE_LIST
65 };
66
initial.commit3f4a7322008-07-27 06:49:38 +090067 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();
akalin@chromium.org810f92f2011-01-18 11:16:59 +090073 static FundamentalValue* CreateBooleanValue(bool in_value);
74 static FundamentalValue* CreateIntegerValue(int in_value);
arv@chromium.org13413eb2011-02-01 10:02:07 +090075 static FundamentalValue* CreateDoubleValue(double in_value);
akalin@chromium.org810f92f2011-01-18 11:16:59 +090076 static StringValue* CreateStringValue(const std::string& in_value);
77 static StringValue* CreateStringValue(const string16& in_value);
initial.commit3f4a7322008-07-27 06:49:38 +090078
initial.commit3f4a7322008-07-27 06:49:38 +090079 // 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
tfarina@chromium.org254780a2011-08-13 05:59:02 +090081 // safe to use the Type to determine whether you can cast from
initial.commit3f4a7322008-07-27 06:49:38 +090082 // Value* to (Implementing Class)*. Also, a Value object never changes
83 // its type after construction.
tfarina@chromium.org254780a2011-08-13 05:59:02 +090084 Type GetType() const { return type_; }
initial.commit3f4a7322008-07-27 06:49:38 +090085
86 // Returns true if the current object represents a given type.
tfarina@chromium.org254780a2011-08-13 05:59:02 +090087 bool IsType(Type type) const { return type == type_; }
initial.commit3f4a7322008-07-27 06:49:38 +090088
89 // These methods allow the convenient retrieval of settings.
90 // If the current setting object can be converted into the given type,
skerner@chromium.org53f71f02010-04-09 04:06:15 +090091 // the value is returned through the |out_value| parameter and true is
92 // returned; otherwise, false is returned and |out_value| is unchanged.
initial.commit3f4a7322008-07-27 06:49:38 +090093 virtual bool GetAsBoolean(bool* out_value) const;
94 virtual bool GetAsInteger(int* out_value) const;
arv@chromium.org13413eb2011-02-01 10:02:07 +090095 virtual bool GetAsDouble(double* out_value) const;
scherkus@chromium.org2b923e72008-12-11 10:23:17 +090096 virtual bool GetAsString(std::string* out_value) const;
viettrungluu@chromium.org31b80ba2010-08-04 00:42:58 +090097 virtual bool GetAsString(string16* out_value) const;
scottbyer@google.com673b8762010-12-07 09:35:29 +090098 virtual bool GetAsList(ListValue** out_value);
bauerb@chromium.org6878e502011-07-12 18:04:38 +090099 virtual bool GetAsList(const ListValue** out_value) const;
battre@chromium.org29eaa252011-11-26 10:11:44 +0900100 virtual bool GetAsDictionary(DictionaryValue** out_value);
101 virtual bool GetAsDictionary(const DictionaryValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900102
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.
akalin@chromium.org810f92f2011-01-18 11:16:59 +0900105 //
106 // Subclasses return their own type directly in their overrides;
107 // this works because C++ supports covariant return types.
initial.commit3f4a7322008-07-27 06:49:38 +0900108 virtual Value* DeepCopy() const;
109
110 // Compares if two Value objects have equal contents.
111 virtual bool Equals(const Value* other) const;
112
bauerb@chromium.orgfbd55e12010-12-07 03:13:43 +0900113 // 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.commit3f4a7322008-07-27 06:49:38 +0900117 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.
tfarina@chromium.org254780a2011-08-13 05:59:02 +0900120 explicit Value(Type type);
initial.commit3f4a7322008-07-27 06:49:38 +0900121
122 private:
sky@chromium.org8d103c72011-08-25 06:17:59 +0900123 Value();
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900124
sky@chromium.org8d103c72011-08-25 06:17:59 +0900125 Type type_;
sky@chromium.org142c71c2011-08-23 12:40:57 +0900126
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900127 DISALLOW_COPY_AND_ASSIGN(Value);
initial.commit3f4a7322008-07-27 06:49:38 +0900128};
129
130// FundamentalValue represents the simple fundamental types of values.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900131class BASE_EXPORT FundamentalValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900132 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900133 explicit FundamentalValue(bool in_value);
134 explicit FundamentalValue(int in_value);
135 explicit FundamentalValue(double in_value);
erg@google.com2d9b43e2010-12-09 03:06:44 +0900136 virtual ~FundamentalValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900137
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900138 // 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.commit3f4a7322008-07-27 06:49:38 +0900144
145 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900146 union {
147 bool boolean_value_;
148 int integer_value_;
arv@chromium.org13413eb2011-02-01 10:02:07 +0900149 double double_value_;
initial.commit3f4a7322008-07-27 06:49:38 +0900150 };
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900151
152 DISALLOW_COPY_AND_ASSIGN(FundamentalValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900153};
154
darin@chromium.orge585bed2011-08-06 00:34:00 +0900155class BASE_EXPORT StringValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900156 public:
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900157 // Initializes a StringValue with a UTF-8 narrow character string.
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900158 explicit StringValue(const std::string& in_value);
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900159
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900160 // Initializes a StringValue with a string16.
161 explicit StringValue(const string16& in_value);
viettrungluu@chromium.org31b80ba2010-08-04 00:42:58 +0900162
erg@google.com2d9b43e2010-12-09 03:06:44 +0900163 virtual ~StringValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900164
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900165 // 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.commit3f4a7322008-07-27 06:49:38 +0900170
171 private:
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900172 std::string value_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900173
174 DISALLOW_COPY_AND_ASSIGN(StringValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900175};
176
darin@chromium.orge585bed2011-08-06 00:34:00 +0900177class BASE_EXPORT BinaryValue: public Value {
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900178 public:
mitchellwrosen@chromium.org3897f9b2012-05-20 04:31:16 +0900179 virtual ~BinaryValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900180
jhawkins@chromium.org592520e2012-05-20 11:34:57 +0900181 // 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.commit3f4a7322008-07-27 06:49:38 +0900186 // 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.
jhawkins@chromium.org592520e2012-05-20 11:34:57 +0900189 // Returns NULL if buffer is NULL.
mpcomplete@chromium.org554d4312009-10-07 03:15:58 +0900190 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
initial.commit3f4a7322008-07-27 06:49:38 +0900191
initial.commit3f4a7322008-07-27 06:49:38 +0900192 size_t GetSize() const { return size_; }
jhawkins@chromium.org592520e2012-05-20 11:34:57 +0900193 char* GetBuffer() { return buffer_; }
194 const char* GetBuffer() const { return buffer_; }
initial.commit3f4a7322008-07-27 06:49:38 +0900195
erg@google.comd5fffd42011-01-08 03:06:45 +0900196 // Overridden from Value:
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900197 virtual BinaryValue* DeepCopy() const OVERRIDE;
198 virtual bool Equals(const Value* other) const OVERRIDE;
erg@google.comd5fffd42011-01-08 03:06:45 +0900199
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900200 private:
jhawkins@chromium.org592520e2012-05-20 11:34:57 +0900201 // 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.commit3f4a7322008-07-27 06:49:38 +0900206 size_t size_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900207
208 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900209};
210
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +0900211// 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.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900214class BASE_EXPORT DictionaryValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900215 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900216 DictionaryValue();
erg@google.com2d9b43e2010-12-09 03:06:44 +0900217 virtual ~DictionaryValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900218
battre@chromium.org29eaa252011-11-26 10:11:44 +0900219 // Overridden from Value:
220 virtual bool GetAsDictionary(DictionaryValue** out_value) OVERRIDE;
221 virtual bool GetAsDictionary(
222 const DictionaryValue** out_value) const OVERRIDE;
223
initial.commit3f4a7322008-07-27 06:49:38 +0900224 // Returns true if the current dictionary has a value for the given key.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900225 bool HasKey(const std::string& key) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900226
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900227 // Returns the number of Values in this dictionary.
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900228 size_t size() const { return dictionary_.size(); }
229
230 // Returns whether the dictionary is empty.
231 bool empty() const { return dictionary_.empty(); }
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900232
initial.commit3f4a7322008-07-27 06:49:38 +0900233 // Clears any current contents of this dictionary.
deanm@google.com522c3832008-08-04 22:56:25 +0900234 void Clear();
initial.commit3f4a7322008-07-27 06:49:38 +0900235
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.
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900243 // Note that the dictionary takes ownership of the value referenced by
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900244 // |in_value|, and therefore |in_value| must be non-NULL.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900245 void Set(const std::string& path, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900246
247 // Convenience forms of Set(). These methods will replace any existing
248 // value at that path, even if it has a different type.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900249 void SetBoolean(const std::string& path, bool in_value);
250 void SetInteger(const std::string& path, int in_value);
arv@chromium.org13413eb2011-02-01 10:02:07 +0900251 void SetDouble(const std::string& path, double in_value);
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900252 void SetString(const std::string& path, const std::string& in_value);
viettrungluu@chromium.orgb7b6aa02010-08-05 01:58:12 +0900253 void SetString(const std::string& path, const string16& in_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900254
255 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
256 // be used as paths.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900257 void SetWithoutPathExpansion(const std::string& key, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900258
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
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900263 // through the |out_value| parameter, and the function will return true.
264 // Otherwise, it will return false and |out_value| will be untouched.
initial.commit3f4a7322008-07-27 06:49:38 +0900265 // Note that the dictionary always owns the value that's returned.
vabr@chromium.org74562432012-07-28 07:27:11 +0900266 bool Get(const std::string& path, const Value** out_value) const;
267 bool Get(const std::string& path, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900268
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.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900272 bool GetBoolean(const std::string& path, bool* out_value) const;
273 bool GetInteger(const std::string& path, int* out_value) const;
arv@chromium.org13413eb2011-02-01 10:02:07 +0900274 bool GetDouble(const std::string& path, double* out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900275 bool GetString(const std::string& path, std::string* out_value) const;
viettrungluu@chromium.org58767ef2010-08-05 04:35:33 +0900276 bool GetString(const std::string& path, string16* out_value) const;
evan@chromium.orgd9ab8de2010-02-19 22:32:16 +0900277 bool GetStringASCII(const std::string& path, std::string* out_value) const;
vabr@chromium.org74562432012-07-28 07:27:11 +0900278 bool GetBinary(const std::string& path, const BinaryValue** out_value) const;
279 bool GetBinary(const std::string& path, BinaryValue** out_value);
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900280 bool GetDictionary(const std::string& path,
vabr@chromium.org74562432012-07-28 07:27:11 +0900281 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.commit3f4a7322008-07-27 06:49:38 +0900285
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900286 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
287 // be used as paths.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900288 bool GetWithoutPathExpansion(const std::string& key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900289 const Value** out_value) const;
290 bool GetWithoutPathExpansion(const std::string& key, Value** out_value);
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900291 bool GetIntegerWithoutPathExpansion(const std::string& key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900292 int* out_value) const;
arv@chromium.org13413eb2011-02-01 10:02:07 +0900293 bool GetDoubleWithoutPathExpansion(const std::string& key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900294 double* out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900295 bool GetStringWithoutPathExpansion(const std::string& key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900296 std::string* out_value) const;
viettrungluu@chromium.org58767ef2010-08-05 04:35:33 +0900297 bool GetStringWithoutPathExpansion(const std::string& key,
298 string16* out_value) const;
vabr@chromium.org74562432012-07-28 07:27:11 +0900299 bool GetDictionaryWithoutPathExpansion(
300 const std::string& key,
301 const DictionaryValue** out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900302 bool GetDictionaryWithoutPathExpansion(const std::string& key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900303 DictionaryValue** out_value);
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900304 bool GetListWithoutPathExpansion(const std::string& key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900305 const ListValue** out_value) const;
306 bool GetListWithoutPathExpansion(const std::string& key,
307 ListValue** out_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900308
initial.commit3f4a7322008-07-27 06:49:38 +0900309 // 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.
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900315 virtual bool Remove(const std::string& path, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900316
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900317 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
318 // to be used as paths.
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900319 virtual bool RemoveWithoutPathExpansion(const std::string& key,
320 Value** out_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900321
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900322 // 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
jhawkins@chromium.orgcbb22392012-05-10 06:54:27 +0900326 // 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.
mnissler@chromium.org17cee0e2010-05-14 22:17:40 +0900331 void MergeDictionary(const DictionaryValue* dictionary);
332
akalin@chromium.org71cd1302011-05-10 18:23:39 +0900333 // Swaps contents with the |other| dictionary.
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900334 virtual void Swap(DictionaryValue* other);
akalin@chromium.org71cd1302011-05-10 18:23:39 +0900335
initial.commit3f4a7322008-07-27 06:49:38 +0900336 // This class provides an iterator for the keys in the dictionary.
337 // It can't be used to modify the dictionary.
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900338 //
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.
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900342 class BASE_EXPORT key_iterator
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900343 : private std::iterator<std::input_iterator_tag, const std::string> {
jar@chromium.orgaf39a372009-02-18 07:50:14 +0900344 public:
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900345 explicit key_iterator(ValueMap::const_iterator itr);
346 // Not explicit, because this is a copy constructor.
347 key_iterator(const key_iterator& rhs);
erg@google.combf6ce9f2010-01-27 08:08:02 +0900348 key_iterator operator++() {
349 ++itr_;
350 return *this;
351 }
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900352 const std::string& operator*() { return itr_->first; }
initial.commit3f4a7322008-07-27 06:49:38 +0900353 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
354 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
355
jar@chromium.orgaf39a372009-02-18 07:50:14 +0900356 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900357 ValueMap::const_iterator itr_;
358 };
359
360 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
361 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
362
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900363 // This class provides an iterator over both keys and values in the
364 // dictionary. It can't be used to modify the dictionary.
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900365 class BASE_EXPORT Iterator {
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900366 public:
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900367 explicit Iterator(const DictionaryValue& target);
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900368
369 bool HasNext() const { return it_ != target_.dictionary_.end(); }
370 void Advance() { ++it_; }
371
372 const std::string& key() const { return it_->first; }
373 const Value& value() const { return *it_->second; }
374
375 private:
376 const DictionaryValue& target_;
377 ValueMap::const_iterator it_;
378 };
379
erg@google.comd5fffd42011-01-08 03:06:45 +0900380 // Overridden from Value:
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900381 virtual DictionaryValue* DeepCopy() const OVERRIDE;
382 virtual bool Equals(const Value* other) const OVERRIDE;
erg@google.comd5fffd42011-01-08 03:06:45 +0900383
initial.commit3f4a7322008-07-27 06:49:38 +0900384 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900385 ValueMap dictionary_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900386
387 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900388};
389
390// This type of Value represents a list of other Value values.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900391class BASE_EXPORT ListValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900392 public:
erg@google.comd5fffd42011-01-08 03:06:45 +0900393 typedef ValueVector::iterator iterator;
394 typedef ValueVector::const_iterator const_iterator;
395
erg@chromium.org493f5f62010-07-16 06:03:54 +0900396 ListValue();
hans@chromium.org78b75932011-05-25 18:08:19 +0900397 virtual ~ListValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900398
initial.commit3f4a7322008-07-27 06:49:38 +0900399 // Clears the contents of this ListValue
400 void Clear();
401
402 // Returns the number of Values in this list.
403 size_t GetSize() const { return list_.size(); }
404
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900405 // Returns whether the list is empty.
406 bool empty() const { return list_.empty(); }
407
initial.commit3f4a7322008-07-27 06:49:38 +0900408 // Sets the list item at the given index to be the Value specified by
409 // the value given. If the index beyond the current end of the list, null
410 // Values will be used to pad out the list.
411 // Returns true if successful, or false if the index was negative or
412 // the value is a null pointer.
413 bool Set(size_t index, Value* in_value);
414
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900415 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commit3f4a7322008-07-27 06:49:38 +0900416 // only if the index falls within the current list range.
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900417 // Note that the list always owns the Value passed out via |out_value|.
vabr@chromium.orga8ca4fd2012-08-03 17:43:37 +0900418 bool Get(size_t index, const Value** out_value) const;
419 bool Get(size_t index, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900420
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900421 // Convenience forms of Get(). Modifies |out_value| (and returns true)
422 // only if the index is valid and the Value at that index can be returned
423 // in the specified form.
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900424 bool GetBoolean(size_t index, bool* out_value) const;
425 bool GetInteger(size_t index, int* out_value) const;
arv@chromium.org13413eb2011-02-01 10:02:07 +0900426 bool GetDouble(size_t index, double* out_value) const;
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900427 bool GetString(size_t index, std::string* out_value) const;
viettrungluu@chromium.org58767ef2010-08-05 04:35:33 +0900428 bool GetString(size_t index, string16* out_value) const;
vabr@chromium.orga8ca4fd2012-08-03 17:43:37 +0900429 bool GetBinary(size_t index, const BinaryValue** out_value) const;
430 bool GetBinary(size_t index, BinaryValue** out_value);
431 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
432 bool GetDictionary(size_t index, DictionaryValue** out_value);
433 bool GetList(size_t index, const ListValue** out_value) const;
434 bool GetList(size_t index, ListValue** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900435
436 // Removes the Value with the specified index from this list.
437 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
ericroman@google.com3d19ee52009-01-06 03:37:51 +0900438 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commit3f4a7322008-07-27 06:49:38 +0900439 // be deleted. This method returns true if |index| is valid; otherwise
440 // it will return false and the ListValue object will be unchanged.
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900441 virtual bool Remove(size_t index, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900442
pkasting@chromium.orgdfe6a692009-12-01 04:59:11 +0900443 // Removes the first instance of |value| found in the list, if any, and
tfarina@chromium.org09cf4342011-08-14 02:34:31 +0900444 // deletes it. |index| is the location where |value| was found. Returns false
445 // if not found.
446 bool Remove(const Value& value, size_t* index);
pkasting@chromium.org727139c2009-05-09 09:33:04 +0900447
estade@chromium.org52852d52012-07-03 11:51:43 +0900448 // Removes the element at |iter|. If |out_value| is NULL, the value will be
449 // deleted, otherwise ownership of the value is passed back to the caller.
450 void Erase(iterator iter, Value** out_value);
451
initial.commit3f4a7322008-07-27 06:49:38 +0900452 // Appends a Value to the end of the list.
453 void Append(Value* in_value);
454
markusheintz@chromium.org78621872011-03-18 22:56:38 +0900455 // Appends a Value if it's not already present. Takes ownership of the
456 // |in_value|. Returns true if successful, or false if the value was already
457 // present. If the value was already present the |in_value| is deleted.
zork@chromium.orgb5f742b2010-04-13 06:48:10 +0900458 bool AppendIfNotPresent(Value* in_value);
459
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900460 // Insert a Value at index.
461 // Returns true if successful, or false if the index was out of range.
462 bool Insert(size_t index, Value* in_value);
463
pastarmovj@chromium.org1602a472011-09-20 00:23:10 +0900464 // Searches for the first instance of |value| in the list using the Equals
465 // method of the Value type.
466 // Returns a const_iterator to the found item or to end() if none exists.
467 const_iterator Find(const Value& value) const;
468
brettw@chromium.org2031cd82010-08-20 03:05:56 +0900469 // Swaps contents with the |other| list.
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900470 virtual void Swap(ListValue* other);
brettw@chromium.org2031cd82010-08-20 03:05:56 +0900471
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900472 // Iteration.
473 iterator begin() { return list_.begin(); }
474 iterator end() { return list_.end(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900475
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900476 const_iterator begin() const { return list_.begin(); }
477 const_iterator end() const { return list_.end(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900478
erg@google.comd5fffd42011-01-08 03:06:45 +0900479 // Overridden from Value:
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900480 virtual bool GetAsList(ListValue** out_value) OVERRIDE;
481 virtual bool GetAsList(const ListValue** out_value) const OVERRIDE;
482 virtual ListValue* DeepCopy() const OVERRIDE;
483 virtual bool Equals(const Value* other) const OVERRIDE;
erg@google.comd5fffd42011-01-08 03:06:45 +0900484
initial.commit3f4a7322008-07-27 06:49:38 +0900485 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900486 ValueVector list_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900487
488 DISALLOW_COPY_AND_ASSIGN(ListValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900489};
490
491// This interface is implemented by classes that know how to serialize and
492// deserialize Value objects.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900493class BASE_EXPORT ValueSerializer {
initial.commit3f4a7322008-07-27 06:49:38 +0900494 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900495 virtual ~ValueSerializer();
mmentovai@google.com4a5b6272008-08-07 00:46:59 +0900496
initial.commit3f4a7322008-07-27 06:49:38 +0900497 virtual bool Serialize(const Value& root) = 0;
498
499 // This method deserializes the subclass-specific format into a Value object.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900500 // If the return value is non-NULL, the caller takes ownership of returned
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +0900501 // Value. If the return value is NULL, and if error_code is non-NULL,
502 // error_code will be set with the underlying error.
503 // If |error_message| is non-null, it will be filled in with a formatted
504 // error message including the location of the error if appropriate.
505 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900506};
507
kalman@chromium.org829cf752012-09-12 12:39:35 +0900508// Stream operator so Values can be used in assertion statements.
509BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
510
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +0900511} // namespace base
512
513// http://crbug.com/88666
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +0900514using base::DictionaryValue;
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +0900515using base::ListValue;
516using base::StringValue;
517using base::Value;
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +0900518
darin@chromium.org01f10702008-09-27 05:22:42 +0900519#endif // BASE_VALUES_H_