blob: 075bc9fbb8fed7cd14095f1de4daa8a3ab9c13ea [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//
avia6a6a682015-12-27 07:15:14 +090012// IN PARTICULAR this means that there is no support for int64_t or unsigned
brettw@chromium.org10374832013-07-17 17:43:33 +090013// 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>
avia6a6a682015-12-27 07:15:14 +090021#include <stdint.h>
mostynb@opera.comf29dd612013-09-04 08:29:12 +090022
23#include <iosfwd>
initial.commit3f4a7322008-07-27 06:49:38 +090024#include <map>
dchengcc8e4d82016-04-05 06:25:51 +090025#include <memory>
nsylvain@chromium.org12426672009-03-04 07:59:43 +090026#include <string>
mostynb@opera.comf29dd612013-09-04 08:29:12 +090027#include <utility>
initial.commit3f4a7322008-07-27 06:49:38 +090028#include <vector>
29
darin@chromium.orge585bed2011-08-06 00:34:00 +090030#include "base/base_export.h"
tfarina@chromium.orgcc177402011-08-12 05:56:32 +090031#include "base/compiler_specific.h"
mkwst62e159c2017-04-11 15:52:21 +090032#include "base/containers/flat_map.h"
avia6a6a682015-12-27 07:15:14 +090033#include "base/macros.h"
jdoerrie31299132017-02-01 23:38:32 +090034#include "base/memory/manual_constructor.h"
avi@chromium.org68a745c2013-06-11 05:11:14 +090035#include "base/strings/string16.h"
asvitkine22632bf2015-06-24 03:22:52 +090036#include "base/strings/string_piece.h"
initial.commit3f4a7322008-07-27 06:49:38 +090037
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +090038namespace base {
39
initial.commit3f4a7322008-07-27 06:49:38 +090040class DictionaryValue;
41class ListValue;
bauerb@chromium.org6fca9582011-02-22 01:35:04 +090042class Value;
initial.commit3f4a7322008-07-27 06:49:38 +090043
tfarina@chromium.org2e2a9022011-08-06 03:20:05 +090044// The Value class is the base class for Values. A Value can be instantiated
45// via the Create*Value() factory methods, or by directly creating instances of
46// the subclasses.
brettw@chromium.org10374832013-07-17 17:43:33 +090047//
48// See the file-level comment above for more information.
darin@chromium.orge585bed2011-08-06 00:34:00 +090049class BASE_EXPORT Value {
initial.commit3f4a7322008-07-27 06:49:38 +090050 public:
mkwst62e159c2017-04-11 15:52:21 +090051 using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>;
jdoerrie45184002017-04-12 03:09:14 +090052 using ListStorage = std::vector<Value>;
jdoerrie1d5b3482017-02-17 22:54:49 +090053
jdoerrie89ee31a2016-12-08 00:43:28 +090054 enum class Type {
55 NONE = 0,
56 BOOLEAN,
57 INTEGER,
58 DOUBLE,
59 STRING,
60 BINARY,
61 DICTIONARY,
62 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
jdoerrie58325a42017-02-15 17:42:14 +090066 // For situations where you want to keep ownership of your buffer, this
67 // factory method creates a new BinaryValue by copying the contents of the
68 // buffer that's passed in.
69 // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead.
70 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerriec56cc7f2017-04-11 16:45:50 +090071 static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer,
72 size_t size);
jdoerrie58325a42017-02-15 17:42:14 +090073
jdoerrie93a0cf32017-02-01 19:36:56 +090074 Value(const Value& that);
brettwce38c192017-03-25 01:36:42 +090075 Value(Value&& that) noexcept;
jdoerrie74f76802017-03-30 15:40:29 +090076 Value() noexcept; // A null value.
jdoerrie93a0cf32017-02-01 19:36:56 +090077 explicit Value(Type type);
78 explicit Value(bool in_bool);
79 explicit Value(int in_int);
80 explicit Value(double in_double);
81
jdoerrie31299132017-02-01 23:38:32 +090082 // Value(const char*) and Value(const char16*) are required despite
83 // Value(const std::string&) and Value(const string16&) because otherwise the
84 // compiler will choose the Value(bool) constructor for these arguments.
85 // Value(std::string&&) allow for efficient move construction.
86 // Value(StringPiece) exists due to many callsites passing StringPieces as
87 // arguments.
88 explicit Value(const char* in_string);
89 explicit Value(const std::string& in_string);
jdoerrie74f76802017-03-30 15:40:29 +090090 explicit Value(std::string&& in_string) noexcept;
jdoerrie31299132017-02-01 23:38:32 +090091 explicit Value(const char16* in_string);
92 explicit Value(const string16& in_string);
93 explicit Value(StringPiece in_string);
94
jdoerrie58325a42017-02-15 17:42:14 +090095 explicit Value(const std::vector<char>& in_blob);
jdoerrie74f76802017-03-30 15:40:29 +090096 explicit Value(std::vector<char>&& in_blob) noexcept;
jdoerrie58325a42017-02-15 17:42:14 +090097
mkwst62e159c2017-04-11 15:52:21 +090098 explicit Value(DictStorage&& in_dict) noexcept;
99
jdoerrie93a0cf32017-02-01 19:36:56 +0900100 Value& operator=(const Value& that);
jdoerrie74f76802017-03-30 15:40:29 +0900101 Value& operator=(Value&& that) noexcept;
jdoerrie93a0cf32017-02-01 19:36:56 +0900102
jdoerrie1d5b3482017-02-17 22:54:49 +0900103 ~Value();
jdoerrie93a0cf32017-02-01 19:36:56 +0900104
thestige44b9322016-07-19 09:39:30 +0900105 // Returns the name for a given |type|.
106 static const char* GetTypeName(Type type);
107
initial.commit3f4a7322008-07-27 06:49:38 +0900108 // Returns the type of the value stored by the current Value object.
109 // Each type will be implemented by only one subclass of Value, so it's
tfarina@chromium.org254780a2011-08-13 05:59:02 +0900110 // safe to use the Type to determine whether you can cast from
initial.commit3f4a7322008-07-27 06:49:38 +0900111 // Value* to (Implementing Class)*. Also, a Value object never changes
112 // its type after construction.
jdoerrie93a0cf32017-02-01 19:36:56 +0900113 Type GetType() const { return type_; } // DEPRECATED, use type().
114 Type type() const { return type_; }
initial.commit3f4a7322008-07-27 06:49:38 +0900115
116 // Returns true if the current object represents a given type.
tfarina@chromium.org254780a2011-08-13 05:59:02 +0900117 bool IsType(Type type) const { return type == type_; }
jdoerrie93a0cf32017-02-01 19:36:56 +0900118 bool is_bool() const { return type() == Type::BOOLEAN; }
119 bool is_int() const { return type() == Type::INTEGER; }
120 bool is_double() const { return type() == Type::DOUBLE; }
121 bool is_string() const { return type() == Type::STRING; }
122 bool is_blob() const { return type() == Type::BINARY; }
123 bool is_dict() const { return type() == Type::DICTIONARY; }
124 bool is_list() const { return type() == Type::LIST; }
125
126 // These will all fatally assert if the type doesn't match.
127 bool GetBool() const;
128 int GetInt() const;
129 double GetDouble() const; // Implicitly converts from int if necessary.
jdoerrie31299132017-02-01 23:38:32 +0900130 const std::string& GetString() const;
jdoerrie58325a42017-02-15 17:42:14 +0900131 const std::vector<char>& GetBlob() const;
132
133 size_t GetSize() const; // DEPRECATED, use GetBlob().size() instead.
134 const char* GetBuffer() const; // DEPRECATED, use GetBlob().data() instead.
initial.commit3f4a7322008-07-27 06:49:38 +0900135
brettw@chromium.org10374832013-07-17 17:43:33 +0900136 // These methods allow the convenient retrieval of the contents of the Value.
137 // If the current object can be converted into the given type, the value is
138 // returned through the |out_value| parameter and true is returned;
139 // otherwise, false is returned and |out_value| is unchanged.
jdoerrie1d5b3482017-02-17 22:54:49 +0900140 bool GetAsBoolean(bool* out_value) const;
141 bool GetAsInteger(int* out_value) const;
142 bool GetAsDouble(double* out_value) const;
143 bool GetAsString(std::string* out_value) const;
144 bool GetAsString(string16* out_value) const;
jdoerrie0d1295b2017-03-06 20:12:04 +0900145 bool GetAsString(const Value** out_value) const;
jdoerrie1d5b3482017-02-17 22:54:49 +0900146 bool GetAsString(StringPiece* out_value) const;
jdoerriec56cc7f2017-04-11 16:45:50 +0900147 bool GetAsBinary(const Value** out_value) const;
lukaszaf0601bd2016-11-02 06:49:46 +0900148 // ListValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie1d5b3482017-02-17 22:54:49 +0900149 bool GetAsList(ListValue** out_value);
150 bool GetAsList(const ListValue** out_value) const;
lukaszaf0601bd2016-11-02 06:49:46 +0900151 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie1d5b3482017-02-17 22:54:49 +0900152 bool GetAsDictionary(DictionaryValue** out_value);
153 bool GetAsDictionary(const DictionaryValue** out_value) const;
brettw@chromium.org10374832013-07-17 17:43:33 +0900154 // Note: Do not add more types. See the file-level comment above for why.
initial.commit3f4a7322008-07-27 06:49:38 +0900155
156 // This creates a deep copy of the entire Value tree, and returns a pointer
jdoerrie93a0cf32017-02-01 19:36:56 +0900157 // to the copy. The caller gets ownership of the copy, of course.
akalin@chromium.org810f92f2011-01-18 11:16:59 +0900158 // Subclasses return their own type directly in their overrides;
159 // this works because C++ supports covariant return types.
jdoerrie751d7d72017-04-05 15:44:17 +0900160 // DEPRECATED, use Value's copy constructor instead.
161 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie1d5b3482017-02-17 22:54:49 +0900162 Value* DeepCopy() const;
estade77a018f2015-05-07 10:53:08 +0900163 // Preferred version of DeepCopy. TODO(estade): remove the above.
dchengcc8e4d82016-04-05 06:25:51 +0900164 std::unique_ptr<Value> CreateDeepCopy() const;
initial.commit3f4a7322008-07-27 06:49:38 +0900165
jdoerrie046caee2017-03-29 02:52:00 +0900166 // Comparison operators so that Values can easily be used with standard
167 // library algorithms and associative containers.
168 BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
169 BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
170 BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
171 BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
172 BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
173 BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
174
initial.commit3f4a7322008-07-27 06:49:38 +0900175 // Compares if two Value objects have equal contents.
jdoerrie046caee2017-03-29 02:52:00 +0900176 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
177 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie1d5b3482017-02-17 22:54:49 +0900178 bool Equals(const Value* other) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900179
bauerb@chromium.orgfbd55e12010-12-07 03:13:43 +0900180 // Compares if two Value objects have equal contents. Can handle NULLs.
jdoerriefcdbae72017-04-07 15:39:00 +0900181 // NULLs are considered equal but different from Value(Value::Type::NONE).
jdoerrie046caee2017-03-29 02:52:00 +0900182 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
183 // TODO(crbug.com/646113): Delete this and migrate callsites.
bauerb@chromium.orgfbd55e12010-12-07 03:13:43 +0900184 static bool Equals(const Value* a, const Value* b);
185
jdoerrie1d5b3482017-02-17 22:54:49 +0900186 protected:
187 // TODO(crbug.com/646113): Make these private once DictionaryValue and
188 // ListValue are properly inlined.
jdoerrie34ee9f62017-02-02 19:56:03 +0900189 Type type_;
190
initial.commit3f4a7322008-07-27 06:49:38 +0900191 union {
jdoerrie93a0cf32017-02-01 19:36:56 +0900192 bool bool_value_;
193 int int_value_;
arv@chromium.org13413eb2011-02-01 10:02:07 +0900194 double double_value_;
jdoerrie31299132017-02-01 23:38:32 +0900195 ManualConstructor<std::string> string_value_;
jdoerrie58325a42017-02-15 17:42:14 +0900196 ManualConstructor<std::vector<char>> binary_value_;
jdoerrie1d5b3482017-02-17 22:54:49 +0900197 // For current gcc and clang sizeof(DictStorage) = 48, which would result
198 // in sizeof(Value) = 56 if DictStorage was stack allocated. Allocating it
199 // on the heap results in sizeof(Value) = 40 for all of gcc, clang and MSVC.
200 ManualConstructor<std::unique_ptr<DictStorage>> dict_ptr_;
201 ManualConstructor<ListStorage> list_;
initial.commit3f4a7322008-07-27 06:49:38 +0900202 };
jdoerrie1d5b3482017-02-17 22:54:49 +0900203
204 private:
205 void InternalCopyFundamentalValue(const Value& that);
206 void InternalCopyConstructFrom(const Value& that);
207 void InternalMoveConstructFrom(Value&& that);
vabr7eac8352017-03-08 08:34:30 +0900208 void InternalCopyAssignFromSameType(const Value& that);
jdoerrie1d5b3482017-02-17 22:54:49 +0900209 void InternalCleanup();
initial.commit3f4a7322008-07-27 06:49:38 +0900210};
211
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +0900212// DictionaryValue provides a key-value dictionary with (optional) "path"
213// parsing for recursive access; see the comment at the top of the file. Keys
214// are |std::string|s and should be UTF-8 encoded.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900215class BASE_EXPORT DictionaryValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900216 public:
reillygd39049b2015-09-11 09:25:54 +0900217 // Returns |value| if it is a dictionary, nullptr otherwise.
dchengcc8e4d82016-04-05 06:25:51 +0900218 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
reillygd39049b2015-09-11 09:25:54 +0900219
erg@chromium.org493f5f62010-07-16 06:03:54 +0900220 DictionaryValue();
battre@chromium.org29eaa252011-11-26 10:11:44 +0900221
initial.commit3f4a7322008-07-27 06:49:38 +0900222 // Returns true if the current dictionary has a value for the given key.
dchengae1c2d62016-08-26 01:07:11 +0900223 bool HasKey(StringPiece key) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900224
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900225 // Returns the number of Values in this dictionary.
jdoerrie1d5b3482017-02-17 22:54:49 +0900226 size_t size() const { return (*dict_ptr_)->size(); }
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900227
228 // Returns whether the dictionary is empty.
jdoerrie1d5b3482017-02-17 22:54:49 +0900229 bool empty() const { return (*dict_ptr_)->empty(); }
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900230
initial.commit3f4a7322008-07-27 06:49:38 +0900231 // Clears any current contents of this dictionary.
deanm@google.com522c3832008-08-04 22:56:25 +0900232 void Clear();
initial.commit3f4a7322008-07-27 06:49:38 +0900233
234 // Sets the Value associated with the given path starting from this object.
235 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
236 // into the next DictionaryValue down. Obviously, "." can't be used
237 // within a key, but there are no other restrictions on keys.
238 // If the key at any step of the way doesn't exist, or exists but isn't
239 // a DictionaryValue, a new DictionaryValue will be created and attached
estade948395d2015-01-07 05:06:50 +0900240 // to the path in that location. |in_value| must be non-null.
dchengae1c2d62016-08-26 01:07:11 +0900241 void Set(StringPiece path, std::unique_ptr<Value> in_value);
estade948395d2015-01-07 05:06:50 +0900242 // Deprecated version of the above. TODO(estade): remove.
dchengae1c2d62016-08-26 01:07:11 +0900243 void Set(StringPiece path, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900244
245 // Convenience forms of Set(). These methods will replace any existing
246 // value at that path, even if it has a different type.
dchengae1c2d62016-08-26 01:07:11 +0900247 void SetBoolean(StringPiece path, bool in_value);
248 void SetInteger(StringPiece path, int in_value);
249 void SetDouble(StringPiece path, double in_value);
250 void SetString(StringPiece path, StringPiece in_value);
251 void SetString(StringPiece path, const string16& in_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900252
253 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
254 // be used as paths.
dchengae1c2d62016-08-26 01:07:11 +0900255 void SetWithoutPathExpansion(StringPiece key,
dchengcc8e4d82016-04-05 06:25:51 +0900256 std::unique_ptr<Value> in_value);
estade948395d2015-01-07 05:06:50 +0900257 // Deprecated version of the above. TODO(estade): remove.
dchengae1c2d62016-08-26 01:07:11 +0900258 void SetWithoutPathExpansion(StringPiece key, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900259
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900260 // Convenience forms of SetWithoutPathExpansion().
dchengae1c2d62016-08-26 01:07:11 +0900261 void SetBooleanWithoutPathExpansion(StringPiece path, bool in_value);
262 void SetIntegerWithoutPathExpansion(StringPiece path, int in_value);
263 void SetDoubleWithoutPathExpansion(StringPiece path, double in_value);
264 void SetStringWithoutPathExpansion(StringPiece path, StringPiece in_value);
265 void SetStringWithoutPathExpansion(StringPiece path,
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900266 const string16& in_value);
267
initial.commit3f4a7322008-07-27 06:49:38 +0900268 // Gets the Value associated with the given path starting from this object.
269 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
270 // into the next DictionaryValue down. If the path can be resolved
271 // successfully, the value for the last key in the path will be returned
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900272 // through the |out_value| parameter, and the function will return true.
273 // Otherwise, it will return false and |out_value| will be untouched.
initial.commit3f4a7322008-07-27 06:49:38 +0900274 // Note that the dictionary always owns the value that's returned.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900275 // |out_value| is optional and will only be set if non-NULL.
asvitkine22632bf2015-06-24 03:22:52 +0900276 bool Get(StringPiece path, const Value** out_value) const;
277 bool Get(StringPiece path, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900278
279 // These are convenience forms of Get(). The value will be retrieved
280 // and the return value will be true if the path is valid and the value at
281 // the end of the path can be returned in the form specified.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900282 // |out_value| is optional and will only be set if non-NULL.
dchengae1c2d62016-08-26 01:07:11 +0900283 bool GetBoolean(StringPiece path, bool* out_value) const;
284 bool GetInteger(StringPiece path, int* out_value) const;
jdoerrie89ee31a2016-12-08 00:43:28 +0900285 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900286 // doubles.
dchengae1c2d62016-08-26 01:07:11 +0900287 bool GetDouble(StringPiece path, double* out_value) const;
288 bool GetString(StringPiece path, std::string* out_value) const;
289 bool GetString(StringPiece path, string16* out_value) const;
290 bool GetStringASCII(StringPiece path, std::string* out_value) const;
jdoerriec56cc7f2017-04-11 16:45:50 +0900291 bool GetBinary(StringPiece path, const Value** out_value) const;
292 bool GetBinary(StringPiece path, Value** out_value);
asvitkine22632bf2015-06-24 03:22:52 +0900293 bool GetDictionary(StringPiece path,
vabr@chromium.org74562432012-07-28 07:27:11 +0900294 const DictionaryValue** out_value) const;
asvitkine22632bf2015-06-24 03:22:52 +0900295 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
dchengae1c2d62016-08-26 01:07:11 +0900296 bool GetList(StringPiece path, const ListValue** out_value) const;
297 bool GetList(StringPiece path, ListValue** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900298
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900299 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
300 // be used as paths.
dchengae1c2d62016-08-26 01:07:11 +0900301 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
302 bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
303 bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
304 bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
305 bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
306 bool GetStringWithoutPathExpansion(StringPiece key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900307 std::string* out_value) const;
dchengae1c2d62016-08-26 01:07:11 +0900308 bool GetStringWithoutPathExpansion(StringPiece key,
viettrungluu@chromium.org58767ef2010-08-05 04:35:33 +0900309 string16* out_value) const;
vabr@chromium.org74562432012-07-28 07:27:11 +0900310 bool GetDictionaryWithoutPathExpansion(
dchengae1c2d62016-08-26 01:07:11 +0900311 StringPiece key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900312 const DictionaryValue** out_value) const;
dchengae1c2d62016-08-26 01:07:11 +0900313 bool GetDictionaryWithoutPathExpansion(StringPiece key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900314 DictionaryValue** out_value);
dchengae1c2d62016-08-26 01:07:11 +0900315 bool GetListWithoutPathExpansion(StringPiece key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900316 const ListValue** out_value) const;
dchengae1c2d62016-08-26 01:07:11 +0900317 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900318
initial.commit3f4a7322008-07-27 06:49:38 +0900319 // Removes the Value with the specified path from this dictionary (or one
320 // of its child dictionaries, if the path is more than just a local key).
bauerb@chromium.orgae43e0a2013-08-06 22:33:04 +0900321 // If |out_value| is non-NULL, the removed Value will be passed out via
322 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
323 // This method returns true if |path| is a valid path; otherwise it will
324 // return false and the DictionaryValue object will be unchanged.
dchengba055202016-11-29 14:30:31 +0900325 bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900326
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900327 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
328 // to be used as paths.
jdoerrie1d5b3482017-02-17 22:54:49 +0900329 bool RemoveWithoutPathExpansion(StringPiece key,
330 std::unique_ptr<Value>* out_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900331
gab@chromium.org903d5c92013-11-27 10:38:24 +0900332 // Removes a path, clearing out all dictionaries on |path| that remain empty
333 // after removing the value at |path|.
dchengba055202016-11-29 14:30:31 +0900334 bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
gab@chromium.org903d5c92013-11-27 10:38:24 +0900335
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900336 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
337 // the copy. This never returns NULL, even if |this| itself is empty.
dchengcc8e4d82016-04-05 06:25:51 +0900338 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900339
jhawkins@chromium.orgcbb22392012-05-10 06:54:27 +0900340 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
341 // sub-dictionaries will be merged as well. In case of key collisions, the
342 // passed in dictionary takes precedence and data already present will be
343 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
344 // be freed any time after this call.
mnissler@chromium.org17cee0e2010-05-14 22:17:40 +0900345 void MergeDictionary(const DictionaryValue* dictionary);
346
akalin@chromium.org71cd1302011-05-10 18:23:39 +0900347 // Swaps contents with the |other| dictionary.
jdoerrie1d5b3482017-02-17 22:54:49 +0900348 void Swap(DictionaryValue* other);
akalin@chromium.org71cd1302011-05-10 18:23:39 +0900349
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900350 // This class provides an iterator over both keys and values in the
351 // dictionary. It can't be used to modify the dictionary.
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900352 class BASE_EXPORT Iterator {
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900353 public:
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900354 explicit Iterator(const DictionaryValue& target);
vmpstrc8651bf2016-02-25 09:50:31 +0900355 Iterator(const Iterator& other);
phajdan.jr@chromium.org8e2b2082013-12-11 03:52:22 +0900356 ~Iterator();
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900357
jdoerrie1d5b3482017-02-17 22:54:49 +0900358 bool IsAtEnd() const { return it_ == (*target_.dict_ptr_)->end(); }
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900359 void Advance() { ++it_; }
360
361 const std::string& key() const { return it_->first; }
362 const Value& value() const { return *it_->second; }
363
364 private:
365 const DictionaryValue& target_;
jdoerrie1d5b3482017-02-17 22:54:49 +0900366 DictStorage::const_iterator it_;
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900367 };
368
jdoerrie751d7d72017-04-05 15:44:17 +0900369 // DEPRECATED, use DictionaryValue's copy constructor instead.
370 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie1d5b3482017-02-17 22:54:49 +0900371 DictionaryValue* DeepCopy() const;
estade77a018f2015-05-07 10:53:08 +0900372 // Preferred version of DeepCopy. TODO(estade): remove the above.
dchengcc8e4d82016-04-05 06:25:51 +0900373 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
initial.commit3f4a7322008-07-27 06:49:38 +0900374};
375
376// This type of Value represents a list of other Value values.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900377class BASE_EXPORT ListValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900378 public:
jdoerrie1d5b3482017-02-17 22:54:49 +0900379 using const_iterator = ListStorage::const_iterator;
380 using iterator = ListStorage::iterator;
erg@google.comd5fffd42011-01-08 03:06:45 +0900381
reillygd39049b2015-09-11 09:25:54 +0900382 // Returns |value| if it is a list, nullptr otherwise.
dchengcc8e4d82016-04-05 06:25:51 +0900383 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
reillygd39049b2015-09-11 09:25:54 +0900384
erg@chromium.org493f5f62010-07-16 06:03:54 +0900385 ListValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900386
initial.commit3f4a7322008-07-27 06:49:38 +0900387 // Clears the contents of this ListValue
388 void Clear();
389
390 // Returns the number of Values in this list.
jdoerrie1d5b3482017-02-17 22:54:49 +0900391 size_t GetSize() const { return list_->size(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900392
jdoerrie45184002017-04-12 03:09:14 +0900393 // Returns the capacity of storage for Values in this list.
394 size_t capacity() const { return list_->capacity(); }
395
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900396 // Returns whether the list is empty.
jdoerrie1d5b3482017-02-17 22:54:49 +0900397 bool empty() const { return list_->empty(); }
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900398
jdoerrie45184002017-04-12 03:09:14 +0900399 // Reserves storage for at least |n| values.
400 void Reserve(size_t n);
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.
dchengcc8e4d82016-04-05 06:25:51 +0900409 bool Set(size_t index, std::unique_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;
jdoerrie89ee31a2016-12-08 00:43:28 +0900424 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900425 // 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;
jdoerriec56cc7f2017-04-11 16:45:50 +0900429 bool GetBinary(size_t index, const Value** out_value) const;
430 bool GetBinary(size_t index, Value** out_value);
vabr@chromium.orga8ca4fd2012-08-03 17:43:37 +0900431 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.
jdoerrie1d5b3482017-02-17 22:54:49 +0900441 bool Remove(size_t index, std::unique_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.
dchengcc8e4d82016-04-05 06:25:51 +0900452 iterator Erase(iterator iter, std::unique_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.
dchengcc8e4d82016-04-05 06:25:51 +0900455 void Append(std::unique_ptr<Value> in_value);
dchengd66aba82016-10-17 11:19:00 +0900456#if !defined(OS_LINUX)
estade77a018f2015-05-07 10:53:08 +0900457 // Deprecated version of the above. TODO(estade): remove.
initial.commit3f4a7322008-07-27 06:49:38 +0900458 void Append(Value* in_value);
dcheng17f21de2016-09-17 10:30:15 +0900459#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900460
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900461 // Convenience forms of Append.
462 void AppendBoolean(bool in_value);
463 void AppendInteger(int in_value);
464 void AppendDouble(double in_value);
dchengae1c2d62016-08-26 01:07:11 +0900465 void AppendString(StringPiece in_value);
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900466 void AppendString(const string16& in_value);
467 void AppendStrings(const std::vector<std::string>& in_values);
468 void AppendStrings(const std::vector<string16>& in_values);
469
dcheng46cc6ec2016-09-14 14:49:58 +0900470 // Appends a Value if it's not already present. Returns true if successful,
471 // or false if the value was already
472 bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
zork@chromium.orgb5f742b2010-04-13 06:48:10 +0900473
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900474 // Insert a Value at index.
475 // Returns true if successful, or false if the index was out of range.
dcheng46cc6ec2016-09-14 14:49:58 +0900476 bool Insert(size_t index, std::unique_ptr<Value> in_value);
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900477
pastarmovj@chromium.org1602a472011-09-20 00:23:10 +0900478 // Searches for the first instance of |value| in the list using the Equals
479 // method of the Value type.
480 // Returns a const_iterator to the found item or to end() if none exists.
481 const_iterator Find(const Value& value) const;
482
brettw@chromium.org2031cd82010-08-20 03:05:56 +0900483 // Swaps contents with the |other| list.
jdoerrie1d5b3482017-02-17 22:54:49 +0900484 void Swap(ListValue* other);
brettw@chromium.org2031cd82010-08-20 03:05:56 +0900485
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900486 // Iteration.
jdoerrie1d5b3482017-02-17 22:54:49 +0900487 iterator begin() { return list_->begin(); }
488 iterator end() { return list_->end(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900489
jdoerrie1d5b3482017-02-17 22:54:49 +0900490 const_iterator begin() const { return list_->begin(); }
491 const_iterator end() const { return list_->end(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900492
jdoerrie751d7d72017-04-05 15:44:17 +0900493 // DEPRECATED, use ListValue's copy constructor instead.
494 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie1d5b3482017-02-17 22:54:49 +0900495 ListValue* DeepCopy() const;
estade033e61e2015-05-13 03:11:50 +0900496 // Preferred version of DeepCopy. TODO(estade): remove DeepCopy.
dchengcc8e4d82016-04-05 06:25:51 +0900497 std::unique_ptr<ListValue> CreateDeepCopy() const;
initial.commit3f4a7322008-07-27 06:49:38 +0900498};
499
prashhir58322f42015-03-05 18:30:57 +0900500// This interface is implemented by classes that know how to serialize
501// Value objects.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900502class BASE_EXPORT ValueSerializer {
initial.commit3f4a7322008-07-27 06:49:38 +0900503 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900504 virtual ~ValueSerializer();
mmentovai@google.com4a5b6272008-08-07 00:46:59 +0900505
initial.commit3f4a7322008-07-27 06:49:38 +0900506 virtual bool Serialize(const Value& root) = 0;
prashhir58322f42015-03-05 18:30:57 +0900507};
508
509// This interface is implemented by classes that know how to deserialize Value
510// objects.
511class BASE_EXPORT ValueDeserializer {
512 public:
513 virtual ~ValueDeserializer();
initial.commit3f4a7322008-07-27 06:49:38 +0900514
515 // This method deserializes the subclass-specific format into a Value object.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900516 // If the return value is non-NULL, the caller takes ownership of returned
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +0900517 // Value. If the return value is NULL, and if error_code is non-NULL,
518 // error_code will be set with the underlying error.
519 // If |error_message| is non-null, it will be filled in with a formatted
520 // error message including the location of the error if appropriate.
dchengcc8e4d82016-04-05 06:25:51 +0900521 virtual std::unique_ptr<Value> Deserialize(int* error_code,
522 std::string* error_str) = 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900523};
524
pneubeck@google.com6671d3f2013-04-16 18:04:02 +0900525// Stream operator so Values can be used in assertion statements. In order that
526// gtest uses this operator to print readable output on test failures, we must
527// override each specific type. Otherwise, the default template implementation
528// is preferred over an upcast.
kalman@chromium.org829cf752012-09-12 12:39:35 +0900529BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
530
pneubeck@google.com6671d3f2013-04-16 18:04:02 +0900531BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
pneubeck@google.com6671d3f2013-04-16 18:04:02 +0900532 const DictionaryValue& value) {
533 return out << static_cast<const Value&>(value);
534}
535
536BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
537 const ListValue& value) {
538 return out << static_cast<const Value&>(value);
539}
540
jdoerrie89ee31a2016-12-08 00:43:28 +0900541// Stream operator so that enum class Types can be used in log statements.
542BASE_EXPORT std::ostream& operator<<(std::ostream& out,
543 const Value::Type& type);
544
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +0900545} // namespace base
546
darin@chromium.org01f10702008-09-27 05:22:42 +0900547#endif // BASE_VALUES_H_