blob: 8756debdbdf76a0dc9658af0ec6c14159885f905 [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
brettw@chromium.org10374832013-07-17 17:43:33 +09006// storing settings and other persistable data.
initial.commit3f4a7322008-07-27 06:49:38 +09007//
brettw@chromium.org10374832013-07-17 17:43:33 +09008// A Value represents something that can be stored in JSON or passed to/from
9// JavaScript. As such, it is NOT a generalized variant type, since only the
10// types supported by JavaScript/JSON are supported.
initial.commit3f4a7322008-07-27 06:49:38 +090011//
brettw@chromium.org10374832013-07-17 17:43:33 +090012// IN PARTICULAR this means that there is no support for int64 or unsigned
13// numbers. Writing JSON with such types would violate the spec. If you need
14// something like this, either use a double or make a string value containing
15// the number you want.
initial.commit3f4a7322008-07-27 06:49:38 +090016
darin@chromium.org01f10702008-09-27 05:22:42 +090017#ifndef BASE_VALUES_H_
18#define BASE_VALUES_H_
initial.commit3f4a7322008-07-27 06:49:38 +090019
mostynb@opera.comf29dd612013-09-04 08:29:12 +090020#include <stddef.h>
21
22#include <iosfwd>
initial.commit3f4a7322008-07-27 06:49:38 +090023#include <map>
nsylvain@chromium.org12426672009-03-04 07:59:43 +090024#include <string>
mostynb@opera.comf29dd612013-09-04 08:29:12 +090025#include <utility>
initial.commit3f4a7322008-07-27 06:49:38 +090026#include <vector>
27
darin@chromium.orge585bed2011-08-06 00:34:00 +090028#include "base/base_export.h"
initial.commit3f4a7322008-07-27 06:49:38 +090029#include "base/basictypes.h"
tfarina@chromium.orgcc177402011-08-12 05:56:32 +090030#include "base/compiler_specific.h"
rpaquay@chromium.org7b1dfba2013-01-08 12:16:22 +090031#include "base/memory/scoped_ptr.h"
avi@chromium.org68a745c2013-06-11 05:11:14 +090032#include "base/strings/string16.h"
asvitkine22632bf2015-06-24 03:22:52 +090033#include "base/strings/string_piece.h"
initial.commit3f4a7322008-07-27 06:49:38 +090034
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +090035namespace base {
36
pneubeckb57f3f52015-01-20 20:26:36 +090037class BinaryValue;
initial.commit3f4a7322008-07-27 06:49:38 +090038class DictionaryValue;
bauerb@chromium.org6fca9582011-02-22 01:35:04 +090039class FundamentalValue;
initial.commit3f4a7322008-07-27 06:49:38 +090040class ListValue;
bauerb@chromium.org6fca9582011-02-22 01:35:04 +090041class StringValue;
42class Value;
initial.commit3f4a7322008-07-27 06:49:38 +090043
44typedef std::vector<Value*> ValueVector;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +090045typedef std::map<std::string, Value*> ValueMap;
initial.commit3f4a7322008-07-27 06:49:38 +090046
tfarina@chromium.org2e2a9022011-08-06 03:20:05 +090047// The Value class is the base class for Values. A Value can be instantiated
48// via the Create*Value() factory methods, or by directly creating instances of
49// the subclasses.
brettw@chromium.org10374832013-07-17 17:43:33 +090050//
51// See the file-level comment above for more information.
darin@chromium.orge585bed2011-08-06 00:34:00 +090052class BASE_EXPORT Value {
initial.commit3f4a7322008-07-27 06:49:38 +090053 public:
tfarina@chromium.org254780a2011-08-13 05:59:02 +090054 enum Type {
erg@google.comd5fffd42011-01-08 03:06:45 +090055 TYPE_NULL = 0,
56 TYPE_BOOLEAN,
57 TYPE_INTEGER,
arv@chromium.org13413eb2011-02-01 10:02:07 +090058 TYPE_DOUBLE,
erg@google.comd5fffd42011-01-08 03:06:45 +090059 TYPE_STRING,
60 TYPE_BINARY,
61 TYPE_DICTIONARY,
62 TYPE_LIST
brettw@chromium.org10374832013-07-17 17:43:33 +090063 // Note: Do not add more types. See the file-level comment above for why.
erg@google.comd5fffd42011-01-08 03:06:45 +090064 };
65
initial.commit3f4a7322008-07-27 06:49:38 +090066 virtual ~Value();
67
estade033e61e2015-05-13 03:11:50 +090068 static scoped_ptr<Value> CreateNullValue();
initial.commit3f4a7322008-07-27 06:49:38 +090069
initial.commit3f4a7322008-07-27 06:49:38 +090070 // Returns the type of the value stored by the current Value object.
71 // Each type will be implemented by only one subclass of Value, so it's
tfarina@chromium.org254780a2011-08-13 05:59:02 +090072 // safe to use the Type to determine whether you can cast from
initial.commit3f4a7322008-07-27 06:49:38 +090073 // Value* to (Implementing Class)*. Also, a Value object never changes
74 // its type after construction.
tfarina@chromium.org254780a2011-08-13 05:59:02 +090075 Type GetType() const { return type_; }
initial.commit3f4a7322008-07-27 06:49:38 +090076
77 // Returns true if the current object represents a given type.
tfarina@chromium.org254780a2011-08-13 05:59:02 +090078 bool IsType(Type type) const { return type == type_; }
initial.commit3f4a7322008-07-27 06:49:38 +090079
brettw@chromium.org10374832013-07-17 17:43:33 +090080 // These methods allow the convenient retrieval of the contents of the Value.
81 // If the current object can be converted into the given type, the value is
82 // returned through the |out_value| parameter and true is returned;
83 // otherwise, false is returned and |out_value| is unchanged.
initial.commit3f4a7322008-07-27 06:49:38 +090084 virtual bool GetAsBoolean(bool* out_value) const;
85 virtual bool GetAsInteger(int* out_value) const;
arv@chromium.org13413eb2011-02-01 10:02:07 +090086 virtual bool GetAsDouble(double* out_value) const;
scherkus@chromium.org2b923e72008-12-11 10:23:17 +090087 virtual bool GetAsString(std::string* out_value) const;
viettrungluu@chromium.org31b80ba2010-08-04 00:42:58 +090088 virtual bool GetAsString(string16* out_value) const;
estade@chromium.org0fd309d2014-02-06 05:42:06 +090089 virtual bool GetAsString(const StringValue** out_value) const;
pneubeckb57f3f52015-01-20 20:26:36 +090090 virtual bool GetAsBinary(const BinaryValue** out_value) const;
scottbyer@google.com673b8762010-12-07 09:35:29 +090091 virtual bool GetAsList(ListValue** out_value);
bauerb@chromium.org6878e502011-07-12 18:04:38 +090092 virtual bool GetAsList(const ListValue** out_value) const;
battre@chromium.org29eaa252011-11-26 10:11:44 +090093 virtual bool GetAsDictionary(DictionaryValue** out_value);
94 virtual bool GetAsDictionary(const DictionaryValue** out_value) const;
brettw@chromium.org10374832013-07-17 17:43:33 +090095 // Note: Do not add more types. See the file-level comment above for why.
initial.commit3f4a7322008-07-27 06:49:38 +090096
97 // This creates a deep copy of the entire Value tree, and returns a pointer
98 // to the copy. The caller gets ownership of the copy, of course.
akalin@chromium.org810f92f2011-01-18 11:16:59 +090099 //
100 // Subclasses return their own type directly in their overrides;
101 // this works because C++ supports covariant return types.
initial.commit3f4a7322008-07-27 06:49:38 +0900102 virtual Value* DeepCopy() const;
estade77a018f2015-05-07 10:53:08 +0900103 // Preferred version of DeepCopy. TODO(estade): remove the above.
104 scoped_ptr<Value> CreateDeepCopy() const;
initial.commit3f4a7322008-07-27 06:49:38 +0900105
106 // Compares if two Value objects have equal contents.
107 virtual bool Equals(const Value* other) const;
108
bauerb@chromium.orgfbd55e12010-12-07 03:13:43 +0900109 // Compares if two Value objects have equal contents. Can handle NULLs.
110 // NULLs are considered equal but different from Value::CreateNullValue().
111 static bool Equals(const Value* a, const Value* b);
112
initial.commit3f4a7322008-07-27 06:49:38 +0900113 protected:
estade@chromium.orge2d2b722012-11-21 05:37:55 +0900114 // These aren't safe for end-users, but they are useful for subclasses.
tfarina@chromium.org254780a2011-08-13 05:59:02 +0900115 explicit Value(Type type);
estade@chromium.orge2d2b722012-11-21 05:37:55 +0900116 Value(const Value& that);
117 Value& operator=(const Value& that);
initial.commit3f4a7322008-07-27 06:49:38 +0900118
119 private:
sky@chromium.org8d103c72011-08-25 06:17:59 +0900120 Type type_;
initial.commit3f4a7322008-07-27 06:49:38 +0900121};
122
123// FundamentalValue represents the simple fundamental types of values.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900124class BASE_EXPORT FundamentalValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900125 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900126 explicit FundamentalValue(bool in_value);
127 explicit FundamentalValue(int in_value);
128 explicit FundamentalValue(double in_value);
dcheng7dc8df52014-10-21 19:54:51 +0900129 ~FundamentalValue() override;
initial.commit3f4a7322008-07-27 06:49:38 +0900130
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900131 // Overridden from Value:
dcheng7dc8df52014-10-21 19:54:51 +0900132 bool GetAsBoolean(bool* out_value) const override;
133 bool GetAsInteger(int* out_value) const override;
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900134 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
135 // doubles.
dcheng7dc8df52014-10-21 19:54:51 +0900136 bool GetAsDouble(double* out_value) const override;
137 FundamentalValue* DeepCopy() const override;
138 bool Equals(const Value* other) const override;
initial.commit3f4a7322008-07-27 06:49:38 +0900139
140 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900141 union {
142 bool boolean_value_;
143 int integer_value_;
arv@chromium.org13413eb2011-02-01 10:02:07 +0900144 double double_value_;
initial.commit3f4a7322008-07-27 06:49:38 +0900145 };
146};
147
darin@chromium.orge585bed2011-08-06 00:34:00 +0900148class BASE_EXPORT StringValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900149 public:
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900150 // Initializes a StringValue with a UTF-8 narrow character string.
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900151 explicit StringValue(const std::string& in_value);
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900152
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900153 // Initializes a StringValue with a string16.
154 explicit StringValue(const string16& in_value);
viettrungluu@chromium.org31b80ba2010-08-04 00:42:58 +0900155
dcheng7dc8df52014-10-21 19:54:51 +0900156 ~StringValue() override;
initial.commit3f4a7322008-07-27 06:49:38 +0900157
estade@chromium.org0fd309d2014-02-06 05:42:06 +0900158 // Returns |value_| as a pointer or reference.
159 std::string* GetString();
160 const std::string& GetString() const;
161
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900162 // Overridden from Value:
dcheng7dc8df52014-10-21 19:54:51 +0900163 bool GetAsString(std::string* out_value) const override;
164 bool GetAsString(string16* out_value) const override;
165 bool GetAsString(const StringValue** out_value) const override;
166 StringValue* DeepCopy() const override;
167 bool Equals(const Value* other) const override;
initial.commit3f4a7322008-07-27 06:49:38 +0900168
169 private:
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900170 std::string value_;
initial.commit3f4a7322008-07-27 06:49:38 +0900171};
172
darin@chromium.orge585bed2011-08-06 00:34:00 +0900173class BASE_EXPORT BinaryValue: public Value {
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900174 public:
rpaquay@chromium.org7b1dfba2013-01-08 12:16:22 +0900175 // Creates a BinaryValue with a null buffer and size of 0.
176 BinaryValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900177
rpaquay@chromium.org7b1dfba2013-01-08 12:16:22 +0900178 // Creates a BinaryValue, taking ownership of the bytes pointed to by
179 // |buffer|.
180 BinaryValue(scoped_ptr<char[]> buffer, size_t size);
181
dcheng7dc8df52014-10-21 19:54:51 +0900182 ~BinaryValue() override;
jhawkins@chromium.org592520e2012-05-20 11:34:57 +0900183
initial.commit3f4a7322008-07-27 06:49:38 +0900184 // For situations where you want to keep ownership of your buffer, this
185 // factory method creates a new BinaryValue by copying the contents of the
186 // buffer that's passed in.
mpcomplete@chromium.org554d4312009-10-07 03:15:58 +0900187 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
initial.commit3f4a7322008-07-27 06:49:38 +0900188
initial.commit3f4a7322008-07-27 06:49:38 +0900189 size_t GetSize() const { return size_; }
rpaquay@chromium.org7b1dfba2013-01-08 12:16:22 +0900190
191 // May return NULL.
192 char* GetBuffer() { return buffer_.get(); }
193 const char* GetBuffer() const { return buffer_.get(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900194
erg@google.comd5fffd42011-01-08 03:06:45 +0900195 // Overridden from Value:
pneubeckb57f3f52015-01-20 20:26:36 +0900196 bool GetAsBinary(const BinaryValue** out_value) const override;
dcheng7dc8df52014-10-21 19:54:51 +0900197 BinaryValue* DeepCopy() const override;
198 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:
rpaquay@chromium.org7b1dfba2013-01-08 12:16:22 +0900201 scoped_ptr<char[]> buffer_;
initial.commit3f4a7322008-07-27 06:49:38 +0900202 size_t size_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900203
204 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900205};
206
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +0900207// DictionaryValue provides a key-value dictionary with (optional) "path"
208// parsing for recursive access; see the comment at the top of the file. Keys
209// are |std::string|s and should be UTF-8 encoded.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900210class BASE_EXPORT DictionaryValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900211 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900212 DictionaryValue();
dcheng7dc8df52014-10-21 19:54:51 +0900213 ~DictionaryValue() override;
initial.commit3f4a7322008-07-27 06:49:38 +0900214
battre@chromium.org29eaa252011-11-26 10:11:44 +0900215 // Overridden from Value:
dcheng7dc8df52014-10-21 19:54:51 +0900216 bool GetAsDictionary(DictionaryValue** out_value) override;
217 bool GetAsDictionary(const DictionaryValue** out_value) const override;
battre@chromium.org29eaa252011-11-26 10:11:44 +0900218
initial.commit3f4a7322008-07-27 06:49:38 +0900219 // Returns true if the current dictionary has a value for the given key.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900220 bool HasKey(const std::string& key) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900221
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900222 // Returns the number of Values in this dictionary.
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900223 size_t size() const { return dictionary_.size(); }
224
225 // Returns whether the dictionary is empty.
226 bool empty() const { return dictionary_.empty(); }
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900227
initial.commit3f4a7322008-07-27 06:49:38 +0900228 // Clears any current contents of this dictionary.
deanm@google.com522c3832008-08-04 22:56:25 +0900229 void Clear();
initial.commit3f4a7322008-07-27 06:49:38 +0900230
231 // Sets the Value associated with the given path starting from this object.
232 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
233 // into the next DictionaryValue down. Obviously, "." can't be used
234 // within a key, but there are no other restrictions on keys.
235 // If the key at any step of the way doesn't exist, or exists but isn't
236 // a DictionaryValue, a new DictionaryValue will be created and attached
estade948395d2015-01-07 05:06:50 +0900237 // to the path in that location. |in_value| must be non-null.
238 void Set(const std::string& path, scoped_ptr<Value> in_value);
239 // Deprecated version of the above. TODO(estade): remove.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900240 void Set(const std::string& path, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900241
242 // Convenience forms of Set(). These methods will replace any existing
243 // value at that path, even if it has a different type.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900244 void SetBoolean(const std::string& path, bool in_value);
245 void SetInteger(const std::string& path, int in_value);
arv@chromium.org13413eb2011-02-01 10:02:07 +0900246 void SetDouble(const std::string& path, double in_value);
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900247 void SetString(const std::string& path, const std::string& in_value);
viettrungluu@chromium.orgb7b6aa02010-08-05 01:58:12 +0900248 void SetString(const std::string& path, const string16& in_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900249
250 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
251 // be used as paths.
estade948395d2015-01-07 05:06:50 +0900252 void SetWithoutPathExpansion(const std::string& key,
253 scoped_ptr<Value> in_value);
254 // Deprecated version of the above. TODO(estade): remove.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900255 void SetWithoutPathExpansion(const std::string& key, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900256
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900257 // Convenience forms of SetWithoutPathExpansion().
258 void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value);
259 void SetIntegerWithoutPathExpansion(const std::string& path, int in_value);
260 void SetDoubleWithoutPathExpansion(const std::string& path, double in_value);
261 void SetStringWithoutPathExpansion(const std::string& path,
262 const std::string& in_value);
263 void SetStringWithoutPathExpansion(const std::string& path,
264 const string16& in_value);
265
initial.commit3f4a7322008-07-27 06:49:38 +0900266 // Gets the Value associated with the given path starting from this object.
267 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
268 // into the next DictionaryValue down. If the path can be resolved
269 // successfully, the value for the last key in the path will be returned
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900270 // through the |out_value| parameter, and the function will return true.
271 // Otherwise, it will return false and |out_value| will be untouched.
initial.commit3f4a7322008-07-27 06:49:38 +0900272 // Note that the dictionary always owns the value that's returned.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900273 // |out_value| is optional and will only be set if non-NULL.
asvitkine22632bf2015-06-24 03:22:52 +0900274 bool Get(StringPiece path, const Value** out_value) const;
275 bool Get(StringPiece path, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900276
277 // These are convenience forms of Get(). The value will be retrieved
278 // and the return value will be true if the path is valid and the value at
279 // the end of the path can be returned in the form specified.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900280 // |out_value| is optional and will only be set if non-NULL.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900281 bool GetBoolean(const std::string& path, bool* out_value) const;
282 bool GetInteger(const std::string& path, int* out_value) const;
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900283 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
284 // doubles.
arv@chromium.org13413eb2011-02-01 10:02:07 +0900285 bool GetDouble(const std::string& path, double* out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900286 bool GetString(const std::string& path, std::string* out_value) const;
viettrungluu@chromium.org58767ef2010-08-05 04:35:33 +0900287 bool GetString(const std::string& path, string16* out_value) const;
evan@chromium.orgd9ab8de2010-02-19 22:32:16 +0900288 bool GetStringASCII(const std::string& path, std::string* out_value) const;
vabr@chromium.org74562432012-07-28 07:27:11 +0900289 bool GetBinary(const std::string& path, const BinaryValue** out_value) const;
290 bool GetBinary(const std::string& path, BinaryValue** out_value);
asvitkine22632bf2015-06-24 03:22:52 +0900291 bool GetDictionary(StringPiece path,
vabr@chromium.org74562432012-07-28 07:27:11 +0900292 const DictionaryValue** out_value) const;
asvitkine22632bf2015-06-24 03:22:52 +0900293 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
vabr@chromium.org74562432012-07-28 07:27:11 +0900294 bool GetList(const std::string& path, const ListValue** out_value) const;
295 bool GetList(const std::string& path, ListValue** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900296
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900297 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
298 // be used as paths.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900299 bool GetWithoutPathExpansion(const std::string& key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900300 const Value** out_value) const;
301 bool GetWithoutPathExpansion(const std::string& key, Value** out_value);
pneubeck@chromium.org3b600542012-11-07 01:46:55 +0900302 bool GetBooleanWithoutPathExpansion(const std::string& key,
303 bool* out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900304 bool GetIntegerWithoutPathExpansion(const std::string& key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900305 int* out_value) const;
arv@chromium.org13413eb2011-02-01 10:02:07 +0900306 bool GetDoubleWithoutPathExpansion(const std::string& key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900307 double* out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900308 bool GetStringWithoutPathExpansion(const std::string& key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900309 std::string* out_value) const;
viettrungluu@chromium.org58767ef2010-08-05 04:35:33 +0900310 bool GetStringWithoutPathExpansion(const std::string& key,
311 string16* out_value) const;
vabr@chromium.org74562432012-07-28 07:27:11 +0900312 bool GetDictionaryWithoutPathExpansion(
313 const std::string& key,
314 const DictionaryValue** out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900315 bool GetDictionaryWithoutPathExpansion(const std::string& key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900316 DictionaryValue** out_value);
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900317 bool GetListWithoutPathExpansion(const std::string& key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900318 const ListValue** out_value) const;
319 bool GetListWithoutPathExpansion(const std::string& key,
320 ListValue** out_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900321
initial.commit3f4a7322008-07-27 06:49:38 +0900322 // Removes the Value with the specified path from this dictionary (or one
323 // of its child dictionaries, if the path is more than just a local key).
bauerb@chromium.orgae43e0a2013-08-06 22:33:04 +0900324 // If |out_value| is non-NULL, the removed Value will be passed out via
325 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
326 // This method returns true if |path| is a valid path; otherwise it will
327 // return false and the DictionaryValue object will be unchanged.
328 virtual bool Remove(const std::string& path, scoped_ptr<Value>* out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900329
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900330 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
331 // to be used as paths.
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900332 virtual bool RemoveWithoutPathExpansion(const std::string& key,
bauerb@chromium.orgae43e0a2013-08-06 22:33:04 +0900333 scoped_ptr<Value>* out_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900334
gab@chromium.org903d5c92013-11-27 10:38:24 +0900335 // Removes a path, clearing out all dictionaries on |path| that remain empty
336 // after removing the value at |path|.
337 virtual bool RemovePath(const std::string& path,
338 scoped_ptr<Value>* out_value);
339
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900340 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
341 // the copy. This never returns NULL, even if |this| itself is empty.
estade51a57ab2015-05-23 11:42:09 +0900342 scoped_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900343
jhawkins@chromium.orgcbb22392012-05-10 06:54:27 +0900344 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
345 // sub-dictionaries will be merged as well. In case of key collisions, the
346 // passed in dictionary takes precedence and data already present will be
347 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
348 // be freed any time after this call.
mnissler@chromium.org17cee0e2010-05-14 22:17:40 +0900349 void MergeDictionary(const DictionaryValue* dictionary);
350
akalin@chromium.org71cd1302011-05-10 18:23:39 +0900351 // Swaps contents with the |other| dictionary.
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900352 virtual void Swap(DictionaryValue* other);
akalin@chromium.org71cd1302011-05-10 18:23:39 +0900353
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900354 // This class provides an iterator over both keys and values in the
355 // dictionary. It can't be used to modify the dictionary.
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900356 class BASE_EXPORT Iterator {
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900357 public:
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900358 explicit Iterator(const DictionaryValue& target);
phajdan.jr@chromium.org8e2b2082013-12-11 03:52:22 +0900359 ~Iterator();
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900360
pneubeck@chromium.orga2fbefc2013-01-18 23:43:27 +0900361 bool IsAtEnd() const { return it_ == target_.dictionary_.end(); }
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900362 void Advance() { ++it_; }
363
364 const std::string& key() const { return it_->first; }
365 const Value& value() const { return *it_->second; }
366
367 private:
368 const DictionaryValue& target_;
369 ValueMap::const_iterator it_;
370 };
371
erg@google.comd5fffd42011-01-08 03:06:45 +0900372 // Overridden from Value:
dcheng7dc8df52014-10-21 19:54:51 +0900373 DictionaryValue* DeepCopy() const override;
estade77a018f2015-05-07 10:53:08 +0900374 // Preferred version of DeepCopy. TODO(estade): remove the above.
375 scoped_ptr<DictionaryValue> CreateDeepCopy() const;
dcheng7dc8df52014-10-21 19:54:51 +0900376 bool Equals(const Value* other) const override;
erg@google.comd5fffd42011-01-08 03:06:45 +0900377
initial.commit3f4a7322008-07-27 06:49:38 +0900378 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900379 ValueMap dictionary_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900380
381 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900382};
383
384// This type of Value represents a list of other Value values.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900385class BASE_EXPORT ListValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900386 public:
erg@google.comd5fffd42011-01-08 03:06:45 +0900387 typedef ValueVector::iterator iterator;
388 typedef ValueVector::const_iterator const_iterator;
389
erg@chromium.org493f5f62010-07-16 06:03:54 +0900390 ListValue();
dcheng7dc8df52014-10-21 19:54:51 +0900391 ~ListValue() override;
initial.commit3f4a7322008-07-27 06:49:38 +0900392
initial.commit3f4a7322008-07-27 06:49:38 +0900393 // Clears the contents of this ListValue
394 void Clear();
395
396 // Returns the number of Values in this list.
397 size_t GetSize() const { return list_.size(); }
398
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900399 // Returns whether the list is empty.
400 bool empty() const { return list_.empty(); }
401
initial.commit3f4a7322008-07-27 06:49:38 +0900402 // Sets the list item at the given index to be the Value specified by
403 // the value given. If the index beyond the current end of the list, null
404 // Values will be used to pad out the list.
405 // Returns true if successful, or false if the index was negative or
406 // the value is a null pointer.
407 bool Set(size_t index, Value* in_value);
estade77a018f2015-05-07 10:53:08 +0900408 // Preferred version of the above. TODO(estade): remove the above.
409 bool Set(size_t index, scoped_ptr<Value> in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900410
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900411 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commit3f4a7322008-07-27 06:49:38 +0900412 // only if the index falls within the current list range.
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900413 // Note that the list always owns the Value passed out via |out_value|.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900414 // |out_value| is optional and will only be set if non-NULL.
vabr@chromium.orga8ca4fd2012-08-03 17:43:37 +0900415 bool Get(size_t index, const Value** out_value) const;
416 bool Get(size_t index, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900417
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900418 // Convenience forms of Get(). Modifies |out_value| (and returns true)
419 // only if the index is valid and the Value at that index can be returned
420 // in the specified form.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900421 // |out_value| is optional and will only be set if non-NULL.
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900422 bool GetBoolean(size_t index, bool* out_value) const;
423 bool GetInteger(size_t index, int* out_value) const;
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900424 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
425 // doubles.
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.
bauerb@chromium.orgae43e0a2013-08-06 22:33:04 +0900441 virtual bool Remove(size_t index, scoped_ptr<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.
dubroy@chromium.orgaf2a3962013-02-19 01:31:45 +0900450 // Returns an iterator pointing to the location of the element that
451 // followed the erased element.
bauerb@chromium.orgae43e0a2013-08-06 22:33:04 +0900452 iterator Erase(iterator iter, scoped_ptr<Value>* out_value);
estade@chromium.org52852d52012-07-03 11:51:43 +0900453
initial.commit3f4a7322008-07-27 06:49:38 +0900454 // Appends a Value to the end of the list.
estade77a018f2015-05-07 10:53:08 +0900455 void Append(scoped_ptr<Value> in_value);
456 // Deprecated version of the above. TODO(estade): remove.
initial.commit3f4a7322008-07-27 06:49:38 +0900457 void Append(Value* in_value);
458
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900459 // Convenience forms of Append.
460 void AppendBoolean(bool in_value);
461 void AppendInteger(int in_value);
462 void AppendDouble(double in_value);
463 void AppendString(const std::string& in_value);
464 void AppendString(const string16& in_value);
465 void AppendStrings(const std::vector<std::string>& in_values);
466 void AppendStrings(const std::vector<string16>& in_values);
467
markusheintz@chromium.org78621872011-03-18 22:56:38 +0900468 // Appends a Value if it's not already present. Takes ownership of the
469 // |in_value|. Returns true if successful, or false if the value was already
470 // present. If the value was already present the |in_value| is deleted.
zork@chromium.orgb5f742b2010-04-13 06:48:10 +0900471 bool AppendIfNotPresent(Value* in_value);
472
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900473 // Insert a Value at index.
474 // Returns true if successful, or false if the index was out of range.
475 bool Insert(size_t index, Value* in_value);
476
pastarmovj@chromium.org1602a472011-09-20 00:23:10 +0900477 // Searches for the first instance of |value| in the list using the Equals
478 // method of the Value type.
479 // Returns a const_iterator to the found item or to end() if none exists.
480 const_iterator Find(const Value& value) const;
481
brettw@chromium.org2031cd82010-08-20 03:05:56 +0900482 // Swaps contents with the |other| list.
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900483 virtual void Swap(ListValue* other);
brettw@chromium.org2031cd82010-08-20 03:05:56 +0900484
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900485 // Iteration.
486 iterator begin() { return list_.begin(); }
487 iterator end() { return list_.end(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900488
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900489 const_iterator begin() const { return list_.begin(); }
490 const_iterator end() const { return list_.end(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900491
erg@google.comd5fffd42011-01-08 03:06:45 +0900492 // Overridden from Value:
dcheng7dc8df52014-10-21 19:54:51 +0900493 bool GetAsList(ListValue** out_value) override;
494 bool GetAsList(const ListValue** out_value) const override;
495 ListValue* DeepCopy() const override;
496 bool Equals(const Value* other) const override;
erg@google.comd5fffd42011-01-08 03:06:45 +0900497
estade033e61e2015-05-13 03:11:50 +0900498 // Preferred version of DeepCopy. TODO(estade): remove DeepCopy.
499 scoped_ptr<ListValue> CreateDeepCopy() const;
500
initial.commit3f4a7322008-07-27 06:49:38 +0900501 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900502 ValueVector list_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900503
504 DISALLOW_COPY_AND_ASSIGN(ListValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900505};
506
prashhir58322f42015-03-05 18:30:57 +0900507// This interface is implemented by classes that know how to serialize
508// Value objects.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900509class BASE_EXPORT ValueSerializer {
initial.commit3f4a7322008-07-27 06:49:38 +0900510 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900511 virtual ~ValueSerializer();
mmentovai@google.com4a5b6272008-08-07 00:46:59 +0900512
initial.commit3f4a7322008-07-27 06:49:38 +0900513 virtual bool Serialize(const Value& root) = 0;
prashhir58322f42015-03-05 18:30:57 +0900514};
515
516// This interface is implemented by classes that know how to deserialize Value
517// objects.
518class BASE_EXPORT ValueDeserializer {
519 public:
520 virtual ~ValueDeserializer();
initial.commit3f4a7322008-07-27 06:49:38 +0900521
522 // This method deserializes the subclass-specific format into a Value object.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900523 // If the return value is non-NULL, the caller takes ownership of returned
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +0900524 // Value. If the return value is NULL, and if error_code is non-NULL,
525 // error_code will be set with the underlying error.
526 // If |error_message| is non-null, it will be filled in with a formatted
527 // error message including the location of the error if appropriate.
528 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900529};
530
pneubeck@google.com6671d3f2013-04-16 18:04:02 +0900531// Stream operator so Values can be used in assertion statements. In order that
532// gtest uses this operator to print readable output on test failures, we must
533// override each specific type. Otherwise, the default template implementation
534// is preferred over an upcast.
kalman@chromium.org829cf752012-09-12 12:39:35 +0900535BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
536
pneubeck@google.com6671d3f2013-04-16 18:04:02 +0900537BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
538 const FundamentalValue& value) {
539 return out << static_cast<const Value&>(value);
540}
541
542BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
543 const StringValue& value) {
544 return out << static_cast<const Value&>(value);
545}
546
547BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
548 const DictionaryValue& value) {
549 return out << static_cast<const Value&>(value);
550}
551
552BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
553 const ListValue& value) {
554 return out << static_cast<const Value&>(value);
555}
556
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +0900557} // namespace base
558
darin@chromium.org01f10702008-09-27 05:22:42 +0900559#endif // BASE_VALUES_H_