blob: caa664d52cbeea2337d2d06fde4f732a5a8eaaa1 [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.
Daniel Cheng5fd76632017-09-26 09:52:43 +090016//
17// NOTE: A Value parameter that is always a Value::STRING should just be passed
18// as a std::string. Similarly for Values that are always Value::DICTIONARY
19// (should be flat_map), Value::LIST (should be std::vector), et cetera.
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
mostynb@opera.comf29dd612013-09-04 08:29:12 +090024#include <stddef.h>
avia6a6a682015-12-27 07:15:14 +090025#include <stdint.h>
mostynb@opera.comf29dd612013-09-04 08:29:12 +090026
27#include <iosfwd>
initial.commit3f4a7322008-07-27 06:49:38 +090028#include <map>
dchengcc8e4d82016-04-05 06:25:51 +090029#include <memory>
nsylvain@chromium.org12426672009-03-04 07:59:43 +090030#include <string>
mostynb@opera.comf29dd612013-09-04 08:29:12 +090031#include <utility>
initial.commit3f4a7322008-07-27 06:49:38 +090032#include <vector>
33
darin@chromium.orge585bed2011-08-06 00:34:00 +090034#include "base/base_export.h"
mkwst62e159c2017-04-11 15:52:21 +090035#include "base/containers/flat_map.h"
jdoerrie859d2fd2017-08-23 17:38:27 +090036#include "base/containers/span.h"
avia6a6a682015-12-27 07:15:14 +090037#include "base/macros.h"
avi@chromium.org68a745c2013-06-11 05:11:14 +090038#include "base/strings/string16.h"
asvitkine22632bf2015-06-24 03:22:52 +090039#include "base/strings/string_piece.h"
jdoerrie673fa772017-07-14 23:47:20 +090040#include "base/value_iterators.h"
initial.commit3f4a7322008-07-27 06:49:38 +090041
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +090042namespace base {
43
initial.commit3f4a7322008-07-27 06:49:38 +090044class DictionaryValue;
45class ListValue;
bauerb@chromium.org6fca9582011-02-22 01:35:04 +090046class Value;
initial.commit3f4a7322008-07-27 06:49:38 +090047
tfarina@chromium.org2e2a9022011-08-06 03:20:05 +090048// The Value class is the base class for Values. A Value can be instantiated
jdoerriebc7c44e2017-08-25 05:44:36 +090049// via passing the appropriate type or backing storage to the constructor.
brettw@chromium.org10374832013-07-17 17:43:33 +090050//
51// See the file-level comment above for more information.
Brett Wilsonfe1b2612017-09-02 05:11:49 +090052//
53// base::Value is currently in the process of being refactored. Design doc:
54// https://docs.google.com/document/d/1uDLu5uTRlCWePxQUEHc8yNQdEoE1BDISYdpggWEABnw
55//
56// Previously (which is how most code that currently exists is written), Value
57// used derived types to implement the individual data types, and base::Value
58// was just a base class to refer to them. This required everything be heap
59// allocated.
60//
61// OLD WAY:
62//
63// std::unique_ptr<base::Value> GetFoo() {
64// std::unique_ptr<DictionaryValue> dict;
65// dict->SetString("mykey", foo);
66// return dict;
67// }
68//
69// The new design makes base::Value a variant type that holds everything in
70// a union. It is now recommended to pass by value with std::move rather than
71// use heap allocated values. The DictionaryValue and ListValue subclasses
72// exist only as a compatibility shim that we're in the process of removing.
73//
74// NEW WAY:
75//
76// base::Value GetFoo() {
77// base::Value dict(base::Value::Type::DICTIONARY);
78// dict.SetKey("mykey", base::Value(foo));
79// return dict;
80// }
darin@chromium.orge585bed2011-08-06 00:34:00 +090081class BASE_EXPORT Value {
initial.commit3f4a7322008-07-27 06:49:38 +090082 public:
jdoerrie940a3e52017-04-18 19:22:41 +090083 using BlobStorage = std::vector<char>;
Lei Zhang08499a72017-10-27 09:27:23 +090084 using DictStorage = flat_map<std::string, std::unique_ptr<Value>>;
jdoerrie45184002017-04-12 03:09:14 +090085 using ListStorage = std::vector<Value>;
jdoerrie1d5b3482017-02-17 22:54:49 +090086
jdoerrie89ee31a2016-12-08 00:43:28 +090087 enum class Type {
88 NONE = 0,
89 BOOLEAN,
90 INTEGER,
91 DOUBLE,
92 STRING,
93 BINARY,
94 DICTIONARY,
95 LIST
brettw@chromium.org10374832013-07-17 17:43:33 +090096 // Note: Do not add more types. See the file-level comment above for why.
erg@google.comd5fffd42011-01-08 03:06:45 +090097 };
98
jdoerrie58325a42017-02-15 17:42:14 +090099 // For situations where you want to keep ownership of your buffer, this
100 // factory method creates a new BinaryValue by copying the contents of the
101 // buffer that's passed in.
Jeremy Romancd0c4672017-08-17 08:27:24 +0900102 // DEPRECATED, use std::make_unique<Value>(const BlobStorage&) instead.
jdoerrie58325a42017-02-15 17:42:14 +0900103 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerriec56cc7f2017-04-11 16:45:50 +0900104 static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer,
105 size_t size);
jdoerrie58325a42017-02-15 17:42:14 +0900106
Lei Zhang7792c552017-10-24 04:14:46 +0900107 // Adaptors for converting from the old way to the new way and vice versa.
108 static Value FromUniquePtrValue(std::unique_ptr<Value> val);
109 static std::unique_ptr<Value> ToUniquePtrValue(Value val);
110
brettwce38c192017-03-25 01:36:42 +0900111 Value(Value&& that) noexcept;
jdoerrie74f76802017-03-30 15:40:29 +0900112 Value() noexcept; // A null value.
jdoerrie062d0fc2017-08-23 23:12:30 +0900113
114 // Value's copy constructor and copy assignment operator are deleted. Use this
115 // to obtain a deep copy explicitly.
116 Value Clone() const;
117
jdoerrie93a0cf32017-02-01 19:36:56 +0900118 explicit Value(Type type);
119 explicit Value(bool in_bool);
120 explicit Value(int in_int);
121 explicit Value(double in_double);
122
jdoerrie31299132017-02-01 23:38:32 +0900123 // Value(const char*) and Value(const char16*) are required despite
jdoerrie0a1d0af2017-09-12 02:23:26 +0900124 // Value(StringPiece) and Value(StringPiece16) because otherwise the
jdoerrie31299132017-02-01 23:38:32 +0900125 // compiler will choose the Value(bool) constructor for these arguments.
126 // Value(std::string&&) allow for efficient move construction.
jdoerrie31299132017-02-01 23:38:32 +0900127 explicit Value(const char* in_string);
jdoerrie31299132017-02-01 23:38:32 +0900128 explicit Value(StringPiece in_string);
jdoerrie0a1d0af2017-09-12 02:23:26 +0900129 explicit Value(std::string&& in_string) noexcept;
130 explicit Value(const char16* in_string16);
131 explicit Value(StringPiece16 in_string16);
jdoerrie31299132017-02-01 23:38:32 +0900132
jdoerrie940a3e52017-04-18 19:22:41 +0900133 explicit Value(const BlobStorage& in_blob);
134 explicit Value(BlobStorage&& in_blob) noexcept;
jdoerrie58325a42017-02-15 17:42:14 +0900135
jdoerrie062d0fc2017-08-23 23:12:30 +0900136 explicit Value(const DictStorage& in_dict);
mkwst62e159c2017-04-11 15:52:21 +0900137 explicit Value(DictStorage&& in_dict) noexcept;
138
jdoerrief72e0cb2017-04-19 16:15:38 +0900139 explicit Value(const ListStorage& in_list);
140 explicit Value(ListStorage&& in_list) noexcept;
141
jdoerrie74f76802017-03-30 15:40:29 +0900142 Value& operator=(Value&& that) noexcept;
jdoerrie93a0cf32017-02-01 19:36:56 +0900143
jdoerrie1d5b3482017-02-17 22:54:49 +0900144 ~Value();
jdoerrie93a0cf32017-02-01 19:36:56 +0900145
thestige44b9322016-07-19 09:39:30 +0900146 // Returns the name for a given |type|.
147 static const char* GetTypeName(Type type);
148
initial.commit3f4a7322008-07-27 06:49:38 +0900149 // Returns the type of the value stored by the current Value object.
jdoerrie93a0cf32017-02-01 19:36:56 +0900150 Type type() const { return type_; }
initial.commit3f4a7322008-07-27 06:49:38 +0900151
152 // Returns true if the current object represents a given type.
jdoerrie062d0fc2017-08-23 23:12:30 +0900153 bool is_none() const { return type() == Type::NONE; }
jdoerrie93a0cf32017-02-01 19:36:56 +0900154 bool is_bool() const { return type() == Type::BOOLEAN; }
155 bool is_int() const { return type() == Type::INTEGER; }
156 bool is_double() const { return type() == Type::DOUBLE; }
157 bool is_string() const { return type() == Type::STRING; }
158 bool is_blob() const { return type() == Type::BINARY; }
159 bool is_dict() const { return type() == Type::DICTIONARY; }
160 bool is_list() const { return type() == Type::LIST; }
161
162 // These will all fatally assert if the type doesn't match.
163 bool GetBool() const;
164 int GetInt() const;
165 double GetDouble() const; // Implicitly converts from int if necessary.
jdoerrie31299132017-02-01 23:38:32 +0900166 const std::string& GetString() const;
jdoerrie940a3e52017-04-18 19:22:41 +0900167 const BlobStorage& GetBlob() const;
jdoerrie58325a42017-02-15 17:42:14 +0900168
jdoerrief72e0cb2017-04-19 16:15:38 +0900169 ListStorage& GetList();
170 const ListStorage& GetList() const;
171
jdoerrie673fa772017-07-14 23:47:20 +0900172 // |FindKey| looks up |key| in the underlying dictionary. If found, it returns
jdoerrieacd36c52017-08-18 04:04:45 +0900173 // a pointer to the element. Otherwise it returns nullptr.
174 // returned. Callers are expected to perform a check against null before using
175 // the pointer.
jdoerrie673fa772017-07-14 23:47:20 +0900176 // Note: This fatally asserts if type() is not Type::DICTIONARY.
jdoerrieacd36c52017-08-18 04:04:45 +0900177 //
178 // Example:
179 // auto* found = FindKey("foo");
180 Value* FindKey(StringPiece key);
181 const Value* FindKey(StringPiece key) const;
jdoerrie673fa772017-07-14 23:47:20 +0900182
183 // |FindKeyOfType| is similar to |FindKey|, but it also requires the found
184 // value to have type |type|. If no type is found, or the found value is of a
jdoerrieacd36c52017-08-18 04:04:45 +0900185 // different type nullptr is returned.
186 // Callers are expected to perform a check against null before using the
187 // pointer.
jdoerrie673fa772017-07-14 23:47:20 +0900188 // Note: This fatally asserts if type() is not Type::DICTIONARY.
jdoerrieacd36c52017-08-18 04:04:45 +0900189 //
190 // Example:
191 // auto* found = FindKey("foo", Type::DOUBLE);
192 Value* FindKeyOfType(StringPiece key, Type type);
193 const Value* FindKeyOfType(StringPiece key, Type type) const;
jdoerrie673fa772017-07-14 23:47:20 +0900194
195 // |SetKey| looks up |key| in the underlying dictionary and sets the mapped
196 // value to |value|. If |key| could not be found, a new element is inserted.
jdoerrieacd36c52017-08-18 04:04:45 +0900197 // A pointer to the modified item is returned.
jdoerrie673fa772017-07-14 23:47:20 +0900198 // Note: This fatally asserts if type() is not Type::DICTIONARY.
jdoerrieacd36c52017-08-18 04:04:45 +0900199 //
200 // Example:
201 // SetKey("foo", std::move(myvalue));
202 Value* SetKey(StringPiece key, Value value);
203 // This overload results in a performance improvement for std::string&&.
204 Value* SetKey(std::string&& key, Value value);
jdoerriec5755142017-08-02 11:20:32 +0900205 // This overload is necessary to avoid ambiguity for const char* arguments.
jdoerrieacd36c52017-08-18 04:04:45 +0900206 Value* SetKey(const char* key, Value value);
jdoerrie673fa772017-07-14 23:47:20 +0900207
jdoerrie374e45e2017-09-05 01:33:32 +0900208 // This attemps to remove the value associated with |key|. In case of failure,
209 // e.g. the key does not exist, |false| is returned and the underlying
210 // dictionary is not changed. In case of success, |key| is deleted from the
211 // dictionary and the method returns |true|.
212 // Note: This fatally asserts if type() is not Type::DICTIONARY.
213 //
214 // Example:
215 // bool success = RemoveKey("foo");
216 bool RemoveKey(StringPiece key);
217
Brett Wilson8b9be6f2017-08-03 09:08:27 +0900218 // Searches a hierarchy of dictionary values for a given value. If a path
219 // of dictionaries exist, returns the item at that path. If any of the path
220 // components do not exist or if any but the last path components are not
221 // dictionaries, returns nullptr.
222 //
223 // The type of the leaf Value is not checked.
224 //
225 // Implementation note: This can't return an iterator because the iterator
226 // will actually be into another Value, so it can't be compared to iterators
jdoerrieacd36c52017-08-18 04:04:45 +0900227 // from this one (in particular, the DictItems().end() iterator).
Brett Wilson8b9be6f2017-08-03 09:08:27 +0900228 //
229 // Example:
jdoerrieacd36c52017-08-18 04:04:45 +0900230 // auto* found = FindPath({"foo", "bar"});
jdoerrie859d2fd2017-08-23 17:38:27 +0900231 //
232 // std::vector<StringPiece> components = ...
233 // auto* found = FindPath(components);
Lei Zhang08499a72017-10-27 09:27:23 +0900234 //
235 // Note: If there is only one component in the path, use FindKey() instead.
jdoerrie859d2fd2017-08-23 17:38:27 +0900236 Value* FindPath(std::initializer_list<StringPiece> path);
237 Value* FindPath(span<const StringPiece> path);
238 const Value* FindPath(std::initializer_list<StringPiece> path) const;
239 const Value* FindPath(span<const StringPiece> path) const;
Brett Wilson8b9be6f2017-08-03 09:08:27 +0900240
Lei Zhang08499a72017-10-27 09:27:23 +0900241 // Like FindPath() but will only return the value if the leaf Value type
Brett Wilson8b9be6f2017-08-03 09:08:27 +0900242 // matches the given type. Will return nullptr otherwise.
Lei Zhang08499a72017-10-27 09:27:23 +0900243 //
244 // Note: If there is only one component in the path, use FindKeyOfType()
245 // instead.
jdoerrie859d2fd2017-08-23 17:38:27 +0900246 Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type);
247 Value* FindPathOfType(span<const StringPiece> path, Type type);
248 const Value* FindPathOfType(std::initializer_list<StringPiece> path,
Brett Wilson8b9be6f2017-08-03 09:08:27 +0900249 Type type) const;
jdoerrie859d2fd2017-08-23 17:38:27 +0900250 const Value* FindPathOfType(span<const StringPiece> path, Type type) const;
Brett Wilson8b9be6f2017-08-03 09:08:27 +0900251
252 // Sets the given path, expanding and creating dictionary keys as necessary.
253 //
jdoerrie374e45e2017-09-05 01:33:32 +0900254 // If the current value is not a dictionary, the function returns nullptr. If
255 // path components do not exist, they will be created. If any but the last
256 // components matches a value that is not a dictionary, the function will fail
257 // (it will not overwrite the value) and return nullptr. The last path
258 // component will be unconditionally overwritten if it exists, and created if
259 // it doesn't.
Brett Wilson8b9be6f2017-08-03 09:08:27 +0900260 //
261 // Example:
262 // value.SetPath({"foo", "bar"}, std::move(myvalue));
jdoerrie859d2fd2017-08-23 17:38:27 +0900263 //
264 // std::vector<StringPiece> components = ...
265 // value.SetPath(components, std::move(myvalue));
Lei Zhang08499a72017-10-27 09:27:23 +0900266 //
267 // Note: If there is only one component in the path, use SetKey() instead.
jdoerrie859d2fd2017-08-23 17:38:27 +0900268 Value* SetPath(std::initializer_list<StringPiece> path, Value value);
269 Value* SetPath(span<const StringPiece> path, Value value);
Brett Wilson8b9be6f2017-08-03 09:08:27 +0900270
jdoerrie374e45e2017-09-05 01:33:32 +0900271 // Tries to remove a Value at the given path.
272 //
273 // If the current value is not a dictionary or any path components does not
274 // exist, this operation fails, leaves underlying Values untouched and returns
275 // |false|. In case intermediate dictionaries become empty as a result of this
276 // path removal, they will be removed as well.
277 //
278 // Example:
279 // bool success = value.RemovePath({"foo", "bar"});
280 //
281 // std::vector<StringPiece> components = ...
282 // bool success = value.RemovePath(components);
Lei Zhang08499a72017-10-27 09:27:23 +0900283 //
284 // Note: If there is only one component in the path, use RemoveKey() instead.
jdoerrie374e45e2017-09-05 01:33:32 +0900285 bool RemovePath(std::initializer_list<StringPiece> path);
286 bool RemovePath(span<const StringPiece> path);
287
jdoerrieacd36c52017-08-18 04:04:45 +0900288 using dict_iterator_proxy = detail::dict_iterator_proxy;
289 using const_dict_iterator_proxy = detail::const_dict_iterator_proxy;
jdoerrie673fa772017-07-14 23:47:20 +0900290
291 // |DictItems| returns a proxy object that exposes iterators to the underlying
292 // dictionary. These are intended for iteration over all items in the
293 // dictionary and are compatible with for-each loops and standard library
294 // algorithms.
295 // Note: This fatally asserts if type() is not Type::DICTIONARY.
296 dict_iterator_proxy DictItems();
297 const_dict_iterator_proxy DictItems() const;
298
brettw@chromium.org10374832013-07-17 17:43:33 +0900299 // These methods allow the convenient retrieval of the contents of the Value.
300 // If the current object can be converted into the given type, the value is
301 // returned through the |out_value| parameter and true is returned;
302 // otherwise, false is returned and |out_value| is unchanged.
jdoerrie849fea52017-06-06 20:48:43 +0900303 // DEPRECATED, use GetBool() instead.
jdoerrie1d5b3482017-02-17 22:54:49 +0900304 bool GetAsBoolean(bool* out_value) const;
jdoerrie849fea52017-06-06 20:48:43 +0900305 // DEPRECATED, use GetInt() instead.
jdoerrie1d5b3482017-02-17 22:54:49 +0900306 bool GetAsInteger(int* out_value) const;
jdoerrie849fea52017-06-06 20:48:43 +0900307 // DEPRECATED, use GetDouble() instead.
jdoerrie1d5b3482017-02-17 22:54:49 +0900308 bool GetAsDouble(double* out_value) const;
jdoerrie849fea52017-06-06 20:48:43 +0900309 // DEPRECATED, use GetString() instead.
jdoerrie1d5b3482017-02-17 22:54:49 +0900310 bool GetAsString(std::string* out_value) const;
311 bool GetAsString(string16* out_value) const;
jdoerrie0d1295b2017-03-06 20:12:04 +0900312 bool GetAsString(const Value** out_value) const;
jdoerrie1d5b3482017-02-17 22:54:49 +0900313 bool GetAsString(StringPiece* out_value) const;
lukaszaf0601bd2016-11-02 06:49:46 +0900314 // ListValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie849fea52017-06-06 20:48:43 +0900315 // DEPRECATED, use GetList() instead.
jdoerrie1d5b3482017-02-17 22:54:49 +0900316 bool GetAsList(ListValue** out_value);
317 bool GetAsList(const ListValue** out_value) const;
lukaszaf0601bd2016-11-02 06:49:46 +0900318 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie1d5b3482017-02-17 22:54:49 +0900319 bool GetAsDictionary(DictionaryValue** out_value);
320 bool GetAsDictionary(const DictionaryValue** out_value) const;
brettw@chromium.org10374832013-07-17 17:43:33 +0900321 // Note: Do not add more types. See the file-level comment above for why.
initial.commit3f4a7322008-07-27 06:49:38 +0900322
323 // This creates a deep copy of the entire Value tree, and returns a pointer
jdoerrie93a0cf32017-02-01 19:36:56 +0900324 // to the copy. The caller gets ownership of the copy, of course.
akalin@chromium.org810f92f2011-01-18 11:16:59 +0900325 // Subclasses return their own type directly in their overrides;
326 // this works because C++ supports covariant return types.
jdoerrie062d0fc2017-08-23 23:12:30 +0900327 // DEPRECATED, use Value::Clone() instead.
jdoerrie751d7d72017-04-05 15:44:17 +0900328 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie1d5b3482017-02-17 22:54:49 +0900329 Value* DeepCopy() const;
jdoerrie062d0fc2017-08-23 23:12:30 +0900330 // DEPRECATED, use Value::Clone() instead.
jdoerrie849fea52017-06-06 20:48:43 +0900331 // TODO(crbug.com/646113): Delete this and migrate callsites.
dchengcc8e4d82016-04-05 06:25:51 +0900332 std::unique_ptr<Value> CreateDeepCopy() const;
initial.commit3f4a7322008-07-27 06:49:38 +0900333
jdoerrie046caee2017-03-29 02:52:00 +0900334 // Comparison operators so that Values can easily be used with standard
335 // library algorithms and associative containers.
336 BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
337 BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
338 BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
339 BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
340 BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
341 BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
342
initial.commit3f4a7322008-07-27 06:49:38 +0900343 // Compares if two Value objects have equal contents.
jdoerrie046caee2017-03-29 02:52:00 +0900344 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
345 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie1d5b3482017-02-17 22:54:49 +0900346 bool Equals(const Value* other) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900347
Alexander Yashkinb5802562017-11-30 20:54:45 +0900348 // Estimates dynamic memory usage.
349 // See base/trace_event/memory_usage_estimator.h for more info.
350 size_t EstimateMemoryUsage() const;
351
jdoerrie1d5b3482017-02-17 22:54:49 +0900352 protected:
353 // TODO(crbug.com/646113): Make these private once DictionaryValue and
354 // ListValue are properly inlined.
jdoerrie34ee9f62017-02-02 19:56:03 +0900355 Type type_;
356
initial.commit3f4a7322008-07-27 06:49:38 +0900357 union {
jdoerrie93a0cf32017-02-01 19:36:56 +0900358 bool bool_value_;
359 int int_value_;
arv@chromium.org13413eb2011-02-01 10:02:07 +0900360 double double_value_;
Daniel Chengd6513e12017-10-12 11:31:07 +0900361 std::string string_value_;
362 BlobStorage binary_value_;
363 DictStorage dict_;
364 ListStorage list_;
initial.commit3f4a7322008-07-27 06:49:38 +0900365 };
jdoerrie1d5b3482017-02-17 22:54:49 +0900366
367 private:
jdoerrie1d5b3482017-02-17 22:54:49 +0900368 void InternalMoveConstructFrom(Value&& that);
jdoerrie1d5b3482017-02-17 22:54:49 +0900369 void InternalCleanup();
jdoerrie062d0fc2017-08-23 23:12:30 +0900370
371 DISALLOW_COPY_AND_ASSIGN(Value);
initial.commit3f4a7322008-07-27 06:49:38 +0900372};
373
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +0900374// DictionaryValue provides a key-value dictionary with (optional) "path"
375// parsing for recursive access; see the comment at the top of the file. Keys
376// are |std::string|s and should be UTF-8 encoded.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900377class BASE_EXPORT DictionaryValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900378 public:
Johan Tibellb6751c82017-05-17 14:21:12 +0900379 using const_iterator = DictStorage::const_iterator;
380 using iterator = DictStorage::iterator;
381
reillygd39049b2015-09-11 09:25:54 +0900382 // Returns |value| if it is a dictionary, nullptr otherwise.
dchengcc8e4d82016-04-05 06:25:51 +0900383 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
reillygd39049b2015-09-11 09:25:54 +0900384
erg@chromium.org493f5f62010-07-16 06:03:54 +0900385 DictionaryValue();
jdoerrie062d0fc2017-08-23 23:12:30 +0900386 explicit DictionaryValue(const DictStorage& in_dict);
387 explicit DictionaryValue(DictStorage&& in_dict) noexcept;
battre@chromium.org29eaa252011-11-26 10:11:44 +0900388
initial.commit3f4a7322008-07-27 06:49:38 +0900389 // Returns true if the current dictionary has a value for the given key.
jdoerriebc7c44e2017-08-25 05:44:36 +0900390 // DEPRECATED, use Value::FindKey(key) instead.
dchengae1c2d62016-08-26 01:07:11 +0900391 bool HasKey(StringPiece key) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900392
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900393 // Returns the number of Values in this dictionary.
Daniel Chengd6513e12017-10-12 11:31:07 +0900394 size_t size() const { return dict_.size(); }
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900395
396 // Returns whether the dictionary is empty.
Daniel Chengd6513e12017-10-12 11:31:07 +0900397 bool empty() const { return dict_.empty(); }
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900398
initial.commit3f4a7322008-07-27 06:49:38 +0900399 // Clears any current contents of this dictionary.
deanm@google.com522c3832008-08-04 22:56:25 +0900400 void Clear();
initial.commit3f4a7322008-07-27 06:49:38 +0900401
402 // Sets the Value associated with the given path starting from this object.
403 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
404 // into the next DictionaryValue down. Obviously, "." can't be used
405 // within a key, but there are no other restrictions on keys.
406 // If the key at any step of the way doesn't exist, or exists but isn't
407 // a DictionaryValue, a new DictionaryValue will be created and attached
estade948395d2015-01-07 05:06:50 +0900408 // to the path in that location. |in_value| must be non-null.
jdoerrie9ac28c42017-04-29 06:52:58 +0900409 // Returns a pointer to the inserted value.
jdoerriebc7c44e2017-08-25 05:44:36 +0900410 // DEPRECATED, use Value::SetPath(path, value) instead.
jdoerrie9ac28c42017-04-29 06:52:58 +0900411 Value* Set(StringPiece path, std::unique_ptr<Value> in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900412
413 // Convenience forms of Set(). These methods will replace any existing
414 // value at that path, even if it has a different type.
jdoerriebc7c44e2017-08-25 05:44:36 +0900415 // DEPRECATED, use Value::SetPath(path, Value(bool)) instead.
jdoerrie9ac28c42017-04-29 06:52:58 +0900416 Value* SetBoolean(StringPiece path, bool in_value);
jdoerriebc7c44e2017-08-25 05:44:36 +0900417 // DEPRECATED, use Value::SetPath(path, Value(int)) instead.
jdoerrie9ac28c42017-04-29 06:52:58 +0900418 Value* SetInteger(StringPiece path, int in_value);
jdoerriebc7c44e2017-08-25 05:44:36 +0900419 // DEPRECATED, use Value::SetPath(path, Value(double)) instead.
jdoerrie9ac28c42017-04-29 06:52:58 +0900420 Value* SetDouble(StringPiece path, double in_value);
jdoerriebc7c44e2017-08-25 05:44:36 +0900421 // DEPRECATED, use Value::SetPath(path, Value(StringPiece)) instead.
jdoerrie9ac28c42017-04-29 06:52:58 +0900422 Value* SetString(StringPiece path, StringPiece in_value);
jdoerriebc7c44e2017-08-25 05:44:36 +0900423 // DEPRECATED, use Value::SetPath(path, Value(const string& 16)) instead.
jdoerrie9ac28c42017-04-29 06:52:58 +0900424 Value* SetString(StringPiece path, const string16& in_value);
jdoerriebc7c44e2017-08-25 05:44:36 +0900425 // DEPRECATED, use Value::SetPath(path, Value(Type::DICTIONARY)) instead.
jdoerrie9ac28c42017-04-29 06:52:58 +0900426 DictionaryValue* SetDictionary(StringPiece path,
427 std::unique_ptr<DictionaryValue> in_value);
jdoerriebc7c44e2017-08-25 05:44:36 +0900428 // DEPRECATED, use Value::SetPath(path, Value(Type::LIST)) instead.
jdoerrie9ac28c42017-04-29 06:52:58 +0900429 ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900430
431 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
432 // be used as paths.
jdoerriebc7c44e2017-08-25 05:44:36 +0900433 // DEPRECATED, use Value::SetKey(key, value) instead.
jdoerrie9ac28c42017-04-29 06:52:58 +0900434 Value* SetWithoutPathExpansion(StringPiece key,
435 std::unique_ptr<Value> in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900436
437 // Gets the Value associated with the given path starting from this object.
438 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
439 // into the next DictionaryValue down. If the path can be resolved
440 // successfully, the value for the last key in the path will be returned
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900441 // through the |out_value| parameter, and the function will return true.
442 // Otherwise, it will return false and |out_value| will be untouched.
initial.commit3f4a7322008-07-27 06:49:38 +0900443 // Note that the dictionary always owns the value that's returned.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900444 // |out_value| is optional and will only be set if non-NULL.
jdoerriebc7c44e2017-08-25 05:44:36 +0900445 // DEPRECATED, use Value::FindPath(path) instead.
asvitkine22632bf2015-06-24 03:22:52 +0900446 bool Get(StringPiece path, const Value** out_value) const;
jdoerriebc7c44e2017-08-25 05:44:36 +0900447 // DEPRECATED, use Value::FindPath(path) instead.
asvitkine22632bf2015-06-24 03:22:52 +0900448 bool Get(StringPiece path, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900449
450 // These are convenience forms of Get(). The value will be retrieved
451 // and the return value will be true if the path is valid and the value at
452 // the end of the path can be returned in the form specified.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900453 // |out_value| is optional and will only be set if non-NULL.
jdoerriebc7c44e2017-08-25 05:44:36 +0900454 // DEPRECATED, use Value::FindPath(path) and Value::GetBool() instead.
dchengae1c2d62016-08-26 01:07:11 +0900455 bool GetBoolean(StringPiece path, bool* out_value) const;
jdoerriebc7c44e2017-08-25 05:44:36 +0900456 // DEPRECATED, use Value::FindPath(path) and Value::GetInt() instead.
dchengae1c2d62016-08-26 01:07:11 +0900457 bool GetInteger(StringPiece path, int* out_value) const;
jdoerrie89ee31a2016-12-08 00:43:28 +0900458 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900459 // doubles.
jdoerriebc7c44e2017-08-25 05:44:36 +0900460 // DEPRECATED, use Value::FindPath(path) and Value::GetDouble() instead.
dchengae1c2d62016-08-26 01:07:11 +0900461 bool GetDouble(StringPiece path, double* out_value) const;
jdoerriebc7c44e2017-08-25 05:44:36 +0900462 // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
dchengae1c2d62016-08-26 01:07:11 +0900463 bool GetString(StringPiece path, std::string* out_value) const;
jdoerriebc7c44e2017-08-25 05:44:36 +0900464 // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
dchengae1c2d62016-08-26 01:07:11 +0900465 bool GetString(StringPiece path, string16* out_value) const;
jdoerriebc7c44e2017-08-25 05:44:36 +0900466 // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
dchengae1c2d62016-08-26 01:07:11 +0900467 bool GetStringASCII(StringPiece path, std::string* out_value) const;
jdoerriebc7c44e2017-08-25 05:44:36 +0900468 // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead.
jdoerriec56cc7f2017-04-11 16:45:50 +0900469 bool GetBinary(StringPiece path, const Value** out_value) const;
jdoerriebc7c44e2017-08-25 05:44:36 +0900470 // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead.
jdoerriec56cc7f2017-04-11 16:45:50 +0900471 bool GetBinary(StringPiece path, Value** out_value);
jdoerriebc7c44e2017-08-25 05:44:36 +0900472 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
asvitkine22632bf2015-06-24 03:22:52 +0900473 bool GetDictionary(StringPiece path,
vabr@chromium.org74562432012-07-28 07:27:11 +0900474 const DictionaryValue** out_value) const;
jdoerriebc7c44e2017-08-25 05:44:36 +0900475 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
asvitkine22632bf2015-06-24 03:22:52 +0900476 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
jdoerriebc7c44e2017-08-25 05:44:36 +0900477 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
dchengae1c2d62016-08-26 01:07:11 +0900478 bool GetList(StringPiece path, const ListValue** out_value) const;
jdoerriebc7c44e2017-08-25 05:44:36 +0900479 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
dchengae1c2d62016-08-26 01:07:11 +0900480 bool GetList(StringPiece path, ListValue** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900481
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900482 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
483 // be used as paths.
jdoerrie45c13a72017-08-03 08:25:52 +0900484 // DEPRECATED, use Value::FindKey(key) instead.
dchengae1c2d62016-08-26 01:07:11 +0900485 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
jdoerrie45c13a72017-08-03 08:25:52 +0900486 // DEPRECATED, use Value::FindKey(key) instead.
dchengae1c2d62016-08-26 01:07:11 +0900487 bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
jdoerrie45c13a72017-08-03 08:25:52 +0900488 // DEPRECATED, use Value::FindKey(key) and Value::GetBool() instead.
dchengae1c2d62016-08-26 01:07:11 +0900489 bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
jdoerrie45c13a72017-08-03 08:25:52 +0900490 // DEPRECATED, use Value::FindKey(key) and Value::GetInt() instead.
dchengae1c2d62016-08-26 01:07:11 +0900491 bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
jdoerrie45c13a72017-08-03 08:25:52 +0900492 // DEPRECATED, use Value::FindKey(key) and Value::GetDouble() instead.
dchengae1c2d62016-08-26 01:07:11 +0900493 bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
jdoerrie45c13a72017-08-03 08:25:52 +0900494 // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead.
dchengae1c2d62016-08-26 01:07:11 +0900495 bool GetStringWithoutPathExpansion(StringPiece key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900496 std::string* out_value) const;
jdoerrie45c13a72017-08-03 08:25:52 +0900497 // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead.
dchengae1c2d62016-08-26 01:07:11 +0900498 bool GetStringWithoutPathExpansion(StringPiece key,
viettrungluu@chromium.org58767ef2010-08-05 04:35:33 +0900499 string16* out_value) const;
jdoerrie45c13a72017-08-03 08:25:52 +0900500 // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead.
vabr@chromium.org74562432012-07-28 07:27:11 +0900501 bool GetDictionaryWithoutPathExpansion(
dchengae1c2d62016-08-26 01:07:11 +0900502 StringPiece key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900503 const DictionaryValue** out_value) const;
jdoerrie45c13a72017-08-03 08:25:52 +0900504 // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead.
dchengae1c2d62016-08-26 01:07:11 +0900505 bool GetDictionaryWithoutPathExpansion(StringPiece key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900506 DictionaryValue** out_value);
jdoerrie45c13a72017-08-03 08:25:52 +0900507 // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead.
dchengae1c2d62016-08-26 01:07:11 +0900508 bool GetListWithoutPathExpansion(StringPiece key,
vabr@chromium.org74562432012-07-28 07:27:11 +0900509 const ListValue** out_value) const;
jdoerrie45c13a72017-08-03 08:25:52 +0900510 // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead.
dchengae1c2d62016-08-26 01:07:11 +0900511 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900512
initial.commit3f4a7322008-07-27 06:49:38 +0900513 // Removes the Value with the specified path from this dictionary (or one
514 // of its child dictionaries, if the path is more than just a local key).
bauerb@chromium.orgae43e0a2013-08-06 22:33:04 +0900515 // If |out_value| is non-NULL, the removed Value will be passed out via
516 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
517 // This method returns true if |path| is a valid path; otherwise it will
518 // return false and the DictionaryValue object will be unchanged.
jdoerrie374e45e2017-09-05 01:33:32 +0900519 // DEPRECATED, use Value::RemovePath(path) instead.
dchengba055202016-11-29 14:30:31 +0900520 bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900521
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900522 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
523 // to be used as paths.
jdoerrie374e45e2017-09-05 01:33:32 +0900524 // DEPRECATED, use Value::RemoveKey(key) instead.
jdoerrie1d5b3482017-02-17 22:54:49 +0900525 bool RemoveWithoutPathExpansion(StringPiece key,
526 std::unique_ptr<Value>* out_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900527
gab@chromium.org903d5c92013-11-27 10:38:24 +0900528 // Removes a path, clearing out all dictionaries on |path| that remain empty
529 // after removing the value at |path|.
jdoerrie374e45e2017-09-05 01:33:32 +0900530 // DEPRECATED, use Value::RemovePath(path) instead.
dchengba055202016-11-29 14:30:31 +0900531 bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
gab@chromium.org903d5c92013-11-27 10:38:24 +0900532
jdoerrie374e45e2017-09-05 01:33:32 +0900533 using Value::RemovePath; // DictionaryValue::RemovePath shadows otherwise.
534
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900535 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
536 // the copy. This never returns NULL, even if |this| itself is empty.
dchengcc8e4d82016-04-05 06:25:51 +0900537 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900538
jhawkins@chromium.orgcbb22392012-05-10 06:54:27 +0900539 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
540 // sub-dictionaries will be merged as well. In case of key collisions, the
541 // passed in dictionary takes precedence and data already present will be
542 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
543 // be freed any time after this call.
mnissler@chromium.org17cee0e2010-05-14 22:17:40 +0900544 void MergeDictionary(const DictionaryValue* dictionary);
545
akalin@chromium.org71cd1302011-05-10 18:23:39 +0900546 // Swaps contents with the |other| dictionary.
jdoerrie1d5b3482017-02-17 22:54:49 +0900547 void Swap(DictionaryValue* other);
akalin@chromium.org71cd1302011-05-10 18:23:39 +0900548
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900549 // This class provides an iterator over both keys and values in the
550 // dictionary. It can't be used to modify the dictionary.
jdoerriebc7c44e2017-08-25 05:44:36 +0900551 // DEPRECATED, use Value::DictItems() instead.
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900552 class BASE_EXPORT Iterator {
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900553 public:
hans@chromium.orgd5d38e32012-08-10 21:45:35 +0900554 explicit Iterator(const DictionaryValue& target);
vmpstrc8651bf2016-02-25 09:50:31 +0900555 Iterator(const Iterator& other);
phajdan.jr@chromium.org8e2b2082013-12-11 03:52:22 +0900556 ~Iterator();
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900557
Daniel Chengd6513e12017-10-12 11:31:07 +0900558 bool IsAtEnd() const { return it_ == target_.dict_.end(); }
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900559 void Advance() { ++it_; }
560
561 const std::string& key() const { return it_->first; }
562 const Value& value() const { return *it_->second; }
563
564 private:
565 const DictionaryValue& target_;
jdoerrie1d5b3482017-02-17 22:54:49 +0900566 DictStorage::const_iterator it_;
kalman@chromium.org1604ad62011-11-09 06:26:41 +0900567 };
568
Johan Tibellb6751c82017-05-17 14:21:12 +0900569 // Iteration.
jdoerriebc7c44e2017-08-25 05:44:36 +0900570 // DEPRECATED, use Value::DictItems() instead.
Daniel Chengd6513e12017-10-12 11:31:07 +0900571 iterator begin() { return dict_.begin(); }
572 iterator end() { return dict_.end(); }
Johan Tibellb6751c82017-05-17 14:21:12 +0900573
jdoerriebc7c44e2017-08-25 05:44:36 +0900574 // DEPRECATED, use Value::DictItems() instead.
Daniel Chengd6513e12017-10-12 11:31:07 +0900575 const_iterator begin() const { return dict_.begin(); }
576 const_iterator end() const { return dict_.end(); }
Johan Tibellb6751c82017-05-17 14:21:12 +0900577
jdoerrie062d0fc2017-08-23 23:12:30 +0900578 // DEPRECATED, use Value::Clone() instead.
jdoerrie751d7d72017-04-05 15:44:17 +0900579 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie1d5b3482017-02-17 22:54:49 +0900580 DictionaryValue* DeepCopy() const;
jdoerrie062d0fc2017-08-23 23:12:30 +0900581 // DEPRECATED, use Value::Clone() instead.
jdoerrie849fea52017-06-06 20:48:43 +0900582 // TODO(crbug.com/646113): Delete this and migrate callsites.
dchengcc8e4d82016-04-05 06:25:51 +0900583 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
initial.commit3f4a7322008-07-27 06:49:38 +0900584};
585
586// This type of Value represents a list of other Value values.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900587class BASE_EXPORT ListValue : public Value {
initial.commit3f4a7322008-07-27 06:49:38 +0900588 public:
jdoerrie1d5b3482017-02-17 22:54:49 +0900589 using const_iterator = ListStorage::const_iterator;
590 using iterator = ListStorage::iterator;
erg@google.comd5fffd42011-01-08 03:06:45 +0900591
reillygd39049b2015-09-11 09:25:54 +0900592 // Returns |value| if it is a list, nullptr otherwise.
dchengcc8e4d82016-04-05 06:25:51 +0900593 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
reillygd39049b2015-09-11 09:25:54 +0900594
erg@chromium.org493f5f62010-07-16 06:03:54 +0900595 ListValue();
jdoerrie41be5e82017-04-27 03:19:42 +0900596 explicit ListValue(const ListStorage& in_list);
597 explicit ListValue(ListStorage&& in_list) noexcept;
initial.commit3f4a7322008-07-27 06:49:38 +0900598
initial.commit3f4a7322008-07-27 06:49:38 +0900599 // Clears the contents of this ListValue
jdoerrie849fea52017-06-06 20:48:43 +0900600 // DEPRECATED, use GetList()::clear() instead.
initial.commit3f4a7322008-07-27 06:49:38 +0900601 void Clear();
602
603 // Returns the number of Values in this list.
jdoerrie849fea52017-06-06 20:48:43 +0900604 // DEPRECATED, use GetList()::size() instead.
Daniel Chengd6513e12017-10-12 11:31:07 +0900605 size_t GetSize() const { return list_.size(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900606
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900607 // Returns whether the list is empty.
jdoerrie849fea52017-06-06 20:48:43 +0900608 // DEPRECATED, use GetList()::empty() instead.
Daniel Chengd6513e12017-10-12 11:31:07 +0900609 bool empty() const { return list_.empty(); }
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900610
jdoerrie45184002017-04-12 03:09:14 +0900611 // Reserves storage for at least |n| values.
jdoerrie849fea52017-06-06 20:48:43 +0900612 // DEPRECATED, use GetList()::reserve() instead.
jdoerrie45184002017-04-12 03:09:14 +0900613 void Reserve(size_t n);
614
initial.commit3f4a7322008-07-27 06:49:38 +0900615 // Sets the list item at the given index to be the Value specified by
616 // the value given. If the index beyond the current end of the list, null
617 // Values will be used to pad out the list.
618 // Returns true if successful, or false if the index was negative or
619 // the value is a null pointer.
jdoerrie849fea52017-06-06 20:48:43 +0900620 // DEPRECATED, use GetList()::operator[] instead.
dchengcc8e4d82016-04-05 06:25:51 +0900621 bool Set(size_t index, std::unique_ptr<Value> in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900622
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900623 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commit3f4a7322008-07-27 06:49:38 +0900624 // only if the index falls within the current list range.
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900625 // Note that the list always owns the Value passed out via |out_value|.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900626 // |out_value| is optional and will only be set if non-NULL.
jdoerrie849fea52017-06-06 20:48:43 +0900627 // DEPRECATED, use GetList()::operator[] instead.
vabr@chromium.orga8ca4fd2012-08-03 17:43:37 +0900628 bool Get(size_t index, const Value** out_value) const;
629 bool Get(size_t index, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900630
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900631 // Convenience forms of Get(). Modifies |out_value| (and returns true)
632 // only if the index is valid and the Value at that index can be returned
633 // in the specified form.
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900634 // |out_value| is optional and will only be set if non-NULL.
jdoerrie849fea52017-06-06 20:48:43 +0900635 // DEPRECATED, use GetList()::operator[]::GetBool() instead.
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900636 bool GetBoolean(size_t index, bool* out_value) const;
jdoerrie849fea52017-06-06 20:48:43 +0900637 // DEPRECATED, use GetList()::operator[]::GetInt() instead.
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900638 bool GetInteger(size_t index, int* out_value) const;
jdoerrie89ee31a2016-12-08 00:43:28 +0900639 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
gab@chromium.orgb3fa8142014-03-09 16:13:23 +0900640 // doubles.
jdoerrie849fea52017-06-06 20:48:43 +0900641 // DEPRECATED, use GetList()::operator[]::GetDouble() instead.
arv@chromium.org13413eb2011-02-01 10:02:07 +0900642 bool GetDouble(size_t index, double* out_value) const;
jdoerrie849fea52017-06-06 20:48:43 +0900643 // DEPRECATED, use GetList()::operator[]::GetString() instead.
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900644 bool GetString(size_t index, std::string* out_value) const;
viettrungluu@chromium.org58767ef2010-08-05 04:35:33 +0900645 bool GetString(size_t index, string16* out_value) const;
jdoerrie849fea52017-06-06 20:48:43 +0900646
vabr@chromium.orga8ca4fd2012-08-03 17:43:37 +0900647 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
648 bool GetDictionary(size_t index, DictionaryValue** out_value);
jdoerrie41be5e82017-04-27 03:19:42 +0900649
650 using Value::GetList;
jdoerrie849fea52017-06-06 20:48:43 +0900651 // DEPRECATED, use GetList()::operator[]::GetList() instead.
vabr@chromium.orga8ca4fd2012-08-03 17:43:37 +0900652 bool GetList(size_t index, const ListValue** out_value) const;
653 bool GetList(size_t index, ListValue** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900654
655 // Removes the Value with the specified index from this list.
656 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
ericroman@google.com3d19ee52009-01-06 03:37:51 +0900657 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commit3f4a7322008-07-27 06:49:38 +0900658 // be deleted. This method returns true if |index| is valid; otherwise
659 // it will return false and the ListValue object will be unchanged.
jdoerrie849fea52017-06-06 20:48:43 +0900660 // DEPRECATED, use GetList()::erase() instead.
jdoerrie1d5b3482017-02-17 22:54:49 +0900661 bool Remove(size_t index, std::unique_ptr<Value>* out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900662
pkasting@chromium.orgdfe6a692009-12-01 04:59:11 +0900663 // Removes the first instance of |value| found in the list, if any, and
tfarina@chromium.org09cf4342011-08-14 02:34:31 +0900664 // deletes it. |index| is the location where |value| was found. Returns false
665 // if not found.
jdoerrie849fea52017-06-06 20:48:43 +0900666 // DEPRECATED, use GetList()::erase() instead.
tfarina@chromium.org09cf4342011-08-14 02:34:31 +0900667 bool Remove(const Value& value, size_t* index);
pkasting@chromium.org727139c2009-05-09 09:33:04 +0900668
estade@chromium.org52852d52012-07-03 11:51:43 +0900669 // Removes the element at |iter|. If |out_value| is NULL, the value will be
670 // deleted, otherwise ownership of the value is passed back to the caller.
dubroy@chromium.orgaf2a3962013-02-19 01:31:45 +0900671 // Returns an iterator pointing to the location of the element that
672 // followed the erased element.
jdoerrie849fea52017-06-06 20:48:43 +0900673 // DEPRECATED, use GetList()::erase() instead.
dchengcc8e4d82016-04-05 06:25:51 +0900674 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
estade@chromium.org52852d52012-07-03 11:51:43 +0900675
initial.commit3f4a7322008-07-27 06:49:38 +0900676 // Appends a Value to the end of the list.
jdoerrie849fea52017-06-06 20:48:43 +0900677 // DEPRECATED, use GetList()::push_back() instead.
dchengcc8e4d82016-04-05 06:25:51 +0900678 void Append(std::unique_ptr<Value> in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900679
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900680 // Convenience forms of Append.
jdoerrie849fea52017-06-06 20:48:43 +0900681 // DEPRECATED, use GetList()::emplace_back() instead.
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900682 void AppendBoolean(bool in_value);
683 void AppendInteger(int in_value);
684 void AppendDouble(double in_value);
dchengae1c2d62016-08-26 01:07:11 +0900685 void AppendString(StringPiece in_value);
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900686 void AppendString(const string16& in_value);
jdoerrie849fea52017-06-06 20:48:43 +0900687 // DEPRECATED, use GetList()::emplace_back() in a loop instead.
kalman@chromium.org5081d8e2012-09-14 11:14:01 +0900688 void AppendStrings(const std::vector<std::string>& in_values);
689 void AppendStrings(const std::vector<string16>& in_values);
690
dcheng46cc6ec2016-09-14 14:49:58 +0900691 // Appends a Value if it's not already present. Returns true if successful,
692 // or false if the value was already
jdoerrie849fea52017-06-06 20:48:43 +0900693 // DEPRECATED, use std::find() with GetList()::push_back() instead.
dcheng46cc6ec2016-09-14 14:49:58 +0900694 bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
zork@chromium.orgb5f742b2010-04-13 06:48:10 +0900695
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900696 // Insert a Value at index.
697 // Returns true if successful, or false if the index was out of range.
jdoerrie849fea52017-06-06 20:48:43 +0900698 // DEPRECATED, use GetList()::insert() instead.
dcheng46cc6ec2016-09-14 14:49:58 +0900699 bool Insert(size_t index, std::unique_ptr<Value> in_value);
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900700
pastarmovj@chromium.org1602a472011-09-20 00:23:10 +0900701 // Searches for the first instance of |value| in the list using the Equals
702 // method of the Value type.
703 // Returns a const_iterator to the found item or to end() if none exists.
jdoerrie849fea52017-06-06 20:48:43 +0900704 // DEPRECATED, use std::find() instead.
pastarmovj@chromium.org1602a472011-09-20 00:23:10 +0900705 const_iterator Find(const Value& value) const;
706
brettw@chromium.org2031cd82010-08-20 03:05:56 +0900707 // Swaps contents with the |other| list.
jdoerrie849fea52017-06-06 20:48:43 +0900708 // DEPRECATED, use GetList()::swap() instead.
jdoerrie1d5b3482017-02-17 22:54:49 +0900709 void Swap(ListValue* other);
brettw@chromium.org2031cd82010-08-20 03:05:56 +0900710
tfarina@chromium.orgcc177402011-08-12 05:56:32 +0900711 // Iteration.
jdoerrie849fea52017-06-06 20:48:43 +0900712 // DEPRECATED, use GetList()::begin() instead.
Daniel Chengd6513e12017-10-12 11:31:07 +0900713 iterator begin() { return list_.begin(); }
jdoerrie849fea52017-06-06 20:48:43 +0900714 // DEPRECATED, use GetList()::end() instead.
Daniel Chengd6513e12017-10-12 11:31:07 +0900715 iterator end() { return list_.end(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900716
jdoerrie849fea52017-06-06 20:48:43 +0900717 // DEPRECATED, use GetList()::begin() instead.
Daniel Chengd6513e12017-10-12 11:31:07 +0900718 const_iterator begin() const { return list_.begin(); }
jdoerrie849fea52017-06-06 20:48:43 +0900719 // DEPRECATED, use GetList()::end() instead.
Daniel Chengd6513e12017-10-12 11:31:07 +0900720 const_iterator end() const { return list_.end(); }
initial.commit3f4a7322008-07-27 06:49:38 +0900721
jdoerrie062d0fc2017-08-23 23:12:30 +0900722 // DEPRECATED, use Value::Clone() instead.
jdoerrie751d7d72017-04-05 15:44:17 +0900723 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie1d5b3482017-02-17 22:54:49 +0900724 ListValue* DeepCopy() const;
jdoerrie062d0fc2017-08-23 23:12:30 +0900725 // DEPRECATED, use Value::Clone() instead.
jdoerrie849fea52017-06-06 20:48:43 +0900726 // TODO(crbug.com/646113): Delete this and migrate callsites.
dchengcc8e4d82016-04-05 06:25:51 +0900727 std::unique_ptr<ListValue> CreateDeepCopy() const;
initial.commit3f4a7322008-07-27 06:49:38 +0900728};
729
prashhir58322f42015-03-05 18:30:57 +0900730// This interface is implemented by classes that know how to serialize
731// Value objects.
darin@chromium.orge585bed2011-08-06 00:34:00 +0900732class BASE_EXPORT ValueSerializer {
initial.commit3f4a7322008-07-27 06:49:38 +0900733 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900734 virtual ~ValueSerializer();
mmentovai@google.com4a5b6272008-08-07 00:46:59 +0900735
initial.commit3f4a7322008-07-27 06:49:38 +0900736 virtual bool Serialize(const Value& root) = 0;
prashhir58322f42015-03-05 18:30:57 +0900737};
738
739// This interface is implemented by classes that know how to deserialize Value
740// objects.
741class BASE_EXPORT ValueDeserializer {
742 public:
743 virtual ~ValueDeserializer();
initial.commit3f4a7322008-07-27 06:49:38 +0900744
745 // This method deserializes the subclass-specific format into a Value object.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900746 // If the return value is non-NULL, the caller takes ownership of returned
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +0900747 // Value. If the return value is NULL, and if error_code is non-NULL,
748 // error_code will be set with the underlying error.
749 // If |error_message| is non-null, it will be filled in with a formatted
750 // error message including the location of the error if appropriate.
dchengcc8e4d82016-04-05 06:25:51 +0900751 virtual std::unique_ptr<Value> Deserialize(int* error_code,
752 std::string* error_str) = 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900753};
754
pneubeck@google.com6671d3f2013-04-16 18:04:02 +0900755// Stream operator so Values can be used in assertion statements. In order that
756// gtest uses this operator to print readable output on test failures, we must
757// override each specific type. Otherwise, the default template implementation
758// is preferred over an upcast.
kalman@chromium.org829cf752012-09-12 12:39:35 +0900759BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
760
pneubeck@google.com6671d3f2013-04-16 18:04:02 +0900761BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
pneubeck@google.com6671d3f2013-04-16 18:04:02 +0900762 const DictionaryValue& value) {
763 return out << static_cast<const Value&>(value);
764}
765
766BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
767 const ListValue& value) {
768 return out << static_cast<const Value&>(value);
769}
770
jdoerrie89ee31a2016-12-08 00:43:28 +0900771// Stream operator so that enum class Types can be used in log statements.
772BASE_EXPORT std::ostream& operator<<(std::ostream& out,
773 const Value::Type& type);
774
dmazzoni@chromium.org13e53652011-07-13 04:15:03 +0900775} // namespace base
776
darin@chromium.org01f10702008-09-27 05:22:42 +0900777#endif // BASE_VALUES_H_